티스토리 뷰
백준 소스코드 [C++] 15644 구슬 탈출 3
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n, m;
char arr[10][10];
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1, 0, -1, 0 };
string ds = "DRUL";
int holey, holex;
struct dot {
int redy, redx, bluey, bluex;
string s;
};
dot info;
void bfs() {
queue<dot> q, t;
q.push(info);
int cnt = 0;
while (q.empty() != true) {
while (q.empty() != true) {
t.push(q.front());
q.pop();
}
cnt++;
if (cnt > 10) {
cout << "-1";
return;
}
while (t.empty() != true) {
dot olddot = t.front();
t.pop();
for (int i = 0;i < 4;i++) {
dot newdot = olddot;
int redcnt = 0, bluecnt = 0;
newdot.s += ds[i];
while (true) {
if (arr[newdot.redy + dy[i]][newdot.redx + dx[i]] != '#') {
newdot.redy += dy[i]; newdot.redx += dx[i];
redcnt++;
}
else break;
if (newdot.redy == holey && newdot.redx == holex) break;
}
while (true) {
if (arr[newdot.bluey + dy[i]][newdot.bluex + dx[i]] != '#') {
newdot.bluey += dy[i]; newdot.bluex += dx[i];
bluecnt++;
}
else break;
if (newdot.bluey == holey && newdot.bluex == holex) break;
}
if (newdot.bluey == holey && newdot.bluex == holex)continue;
else if (newdot.redy == holey && newdot.redx == holex) {
cout << cnt<<'\n'<<newdot.s;
return;
}
else if (newdot.bluey == newdot.redy && newdot.bluex == newdot.redx) {
if (redcnt < bluecnt) {
newdot.bluey -= dy[i];
newdot.bluex -= dx[i];
if (!(newdot.bluey == olddot.bluey &&
newdot.bluex == olddot.bluex &&
newdot.redy == olddot.redy &&
newdot.redx == olddot.redx))
q.push(newdot);
}
else {
newdot.redy -= dy[i];
newdot.redx -= dx[i];
if (!(newdot.bluey == olddot.bluey &&
newdot.bluex == olddot.bluex &&
newdot.redy == olddot.redy &&
newdot.redx == olddot.redx))
q.push(newdot);
}
}
else {
if (!(newdot.bluey == olddot.bluey &&
newdot.bluex == olddot.bluex &&
newdot.redy == olddot.redy &&
newdot.redx == olddot.redx))
q.push(newdot);
}
}
}
}
cout << "-1";
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0;i < n;i++)
for (int j = 0;j < m;j++) {
cin >> arr[i][j];
if (arr[i][j] == 'R') { info.redy = i; info.redx = j; }
if (arr[i][j] == 'B') { info.bluey = i; info.bluex = j; }
if (arr[i][j] == 'O') { holey = i; holex = j; }
}
info.s = "";
bfs();
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 14890 경사로 (0) | 2021.02.05 |
---|---|
백준 소스코드 [C++] 15653 구슬 탈출 4 (0) | 2021.02.04 |
백준 소스코드 [C++] 13459 구슬 탈출 (0) | 2021.02.04 |
백준 소스코드 [C++] 13460 구슬 탈출 2 (0) | 2021.02.04 |
백준 소스코드 [C++] 14499 주사위 굴리기 (0) | 2021.02.04 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- MeTal
- 최대 매칭
- CPU와 Memory
- 최단경로 문제
- 다익스트라 시간복잡도
- WWDC21
- test coverage
- WWDC16
- 강한 순환 참조
- IOS
- observeOn
- 네트워크 플로우
- WWDC17
- Testable
- 에드몬드 카프 알고리즘
- 벨만포드 알고리즘
- 컴퓨터 추상화
- 부스트캠프 6기
- 포드 풀커슨 알고리즘
- CompositionalLayout
- WWDC19
- mach-o
- HIG
- 최단경로 알고리즘
- 네트워크 유량
- 벨만포드 시간복잡도
- State Restoration
- 최단경로문제
- rxswift
- 코딩대회
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함