-
[C++] ํ๋ก๊ทธ๋๋จธ์ค 2020 KAKAO BLIND RECRUITMENT ๋ธ๋ก ์ด๋ํ๊ธฐ์ฝ๋ฉํ ์คํธ/์ฝ๋ฉํ ์คํธ ์ฐ์ต 2020. 10. 28. 09:27728x90
(4์๊ฐ)
๋จ์ํ BFS๋ก ๋ฏธ๋ก ์ฐพ๊ธฐ ๋ฌธ์ ์ ์กฐ๊ฑด๋ค์ด ์ถ๊ฐ๋ ๋๋.
๋จธ๋ฆฌ ์์ ์๊ฐํ ๋ฐฉ๋ฒ์ ๊ทธ๋๋ก ์ฝ๋๋ก ๊ตฌํํ๋ ๋์๊ฐ๋คใ ใ
๋ค์ฐจ์ ๋ฐฐ์ด์ผ๋ก ๋ฐฉ๋ฌธ ์ฒดํฌ๋ ์จ๋ดค๋ค.
๊ทผ๋ฐ ๊ตฌํํ๊ณ ๋ณด๋ ์๊ฐ์ด ๋๋ฌด ์ค๋ ์ง๋๊ฐ ์๋ ๋ฌธ์ ...
#include <string> #include <vector> #include <queue> using namespace std; int N; int visited[100][100][100][100]; int dir[4][2] = { {0,1},{1,0},{0,-1},{-1,0} }; vector<vector<int>> boards; struct robot { int x1; int y1; int x2; int y2; int direction; int count; robot(int _x1, int _y1, int _x2, int _y2, int _direction, int _count) { x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2; direction = _direction; count = _count; } }; bool checkMove(robot bot) { if (bot.x1 < 0 || bot.y1 < 0 || bot.x2 < 0 || bot.y2 < 0) return false; if (bot.x1 >= N || bot.y1 >= N || bot.x2 >= N || bot.y2 >= N) return false; if (boards[bot.x1][bot.y1] == 1 || boards[bot.x2][bot.y2] == 1) return false; if (visited[bot.x1][bot.y1][bot.x2][bot.y2] == 1) return false; if (visited[bot.x2][bot.y2][bot.x1][bot.y1] == 1) return false; return true; } bool checkRotate(int x1, int y1, int x2, int y2) { if (x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0) return false; if (x1 >= N || y1 >= N || x2 >= N || y2 >= N) return false; if (boards[x1][y1] == 1 || boards[x2][y2] == 1) return false; return true; } int solution(vector<vector<int>> board) { N = board.size(); boards = board; queue<robot> q; robot start(0, 0, 0, 1, 0, 0); q.push(start); while (!q.empty()) { robot now = q.front(); q.pop(); // ๋ชฉ์ ์ง ๋์ฐฉ if (now.x1 == N - 1 && now.y1 == N - 1) return now.count; if (now.x2 == N - 1 && now.y2 == N - 1) return now.count; visited[now.x1][now.y1][now.x2][now.y2] = 1; visited[now.x2][now.y2][now.x1][now.y1] = 1; // ์ํ์ข์ฐ ์์ง์ด๋ ๊ฒฝ์ฐ for (int i = 0; i < 4; i++) { robot next(now.x1 + dir[i][0], now.y1 + dir[i][1], now.x2 + dir[i][0], now.y2 + dir[i][1], now.direction, now.count + 1); if (checkMove(next)) { visited[next.x1][next.y1][next.x2][next.y2] = 1; visited[next.x2][next.y2][next.x1][next.y1] = 1; q.push(next); } } // ๊ฐ๋ก ๋ชจ์์ผ๋ if (now.direction == 0) { for (int i = 1; i < 4; i += 2) { // ํ์ ์ด ๊ฐ๋ฅํ๊ฐ if (checkRotate(now.x1 + dir[i][0], now.y1 + dir[i][1], now.x2 + dir[i][0], now.y2 + dir[i][1])) { // ์ผ์ชฝ์ด ์ถ์ผ ๊ฒฝ์ฐ if (!visited[now.x1][now.y1][now.x1 + dir[i][0]][now.y1 + dir[i][1]]) { robot next(now.x1, now.y1, now.x1 + dir[i][0], now.y1 + dir[i][1], 1, now.count + 1); visited[next.x1][next.y1][next.x2][next.y2] = 1; visited[next.x2][next.y2][next.x1][next.y1] = 1; q.push(next); } // ์ค๋ฅธ์ชฝ์ด ์ถ์ผ ๊ฒฝ์ฐ if (!visited[now.x2][now.y2][now.x2 + dir[i][0]][now.y2 + dir[i][1]]) { robot next(now.x2, now.y2, now.x2 + dir[i][0], now.y2 + dir[i][1], 1, now.count + 1); visited[next.x1][next.y1][next.x2][next.y2] = 1; visited[next.x2][next.y2][next.x1][next.y1] = 1; q.push(next); } } } } else if (now.direction == 1) {// ์ธ๋ก ๋ชจ์์ผ๋ for (int i = 0; i < 3; i += 2) { // ํ์ ์ด ๊ฐ๋ฅํ๊ฐ if (checkRotate(now.x1 + dir[i][0], now.y1 + dir[i][1], now.x2 + dir[i][0], now.y2 + dir[i][1])) { // ์๋๊ฐ ์ถ์ผ ๊ฒฝ์ฐ if (!visited[now.x1][now.y1][now.x1 + dir[i][0]][now.y1 + dir[i][1]]) { robot next(now.x1, now.y1, now.x1 + dir[i][0], now.y1 + dir[i][1], 0, now.count + 1); visited[next.x1][next.y1][next.x2][next.y2] = 1; visited[next.x2][next.y2][next.x1][next.y1] = 1; q.push(next); } // ์๊ฐ ์ถ์ผ ๊ฒฝ์ฐ if (!visited[now.x2][now.y2][now.x2 + dir[i][0]][now.y2 + dir[i][1]]) { robot next(now.x2, now.y2, now.x2 + dir[i][0], now.y2 + dir[i][1], 0, now.count + 1); visited[next.x1][next.y1][next.x2][next.y2] = 1; visited[next.x2][next.y2][next.x1][next.y1] = 1; q.push(next); } } } } } return -1; }
728x90'์ฝ๋ฉํ ์คํธ > ์ฝ๋ฉํ ์คํธ ์ฐ์ต' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ