알고리즘 문제/BOJ
1600번 말이 되고픈 원숭이
parkit
2018. 11. 15. 23:15
728x90
반응형
https://www.acmicpc.net/problem/1600
그럭저럭 풀만한 문제였다.
if조건문 쓸 때, 실수를 조심해야겠다.
벽 부수고 이동하기 문제류와 비슷하다.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> using namespace std; int Map[201][201] = { 0, }; int dy[4] = { -1, 0, 1, 0 }; int dx[4] = { 0, -1, 0, 1 }; int hy[8] = { -2, -1, 1, 2, 2, 1, -1, -2 }; int hx[8] = { -1, -2, -2, -1, 1, 2 , 2, 1 }; int K = 0, W = 0, H = 0; bool visit[201][201][31] = { false, }; int BFS() { queue<pair<pair<int, int>, int > > q; visit[0][0][0] = true; q.push({ {0, 0}, 0 }); // 행, 열, K, 나이트 이동인지 아닌지 여부(0, 1) int ret = 0; while (!q.empty()) { int qSize = q.size(); while (qSize--) { int r = q.front().first.first; int c = q.front().first.second; int present = q.front().second; q.pop(); if (r == H - 1 && c == W - 1) return ret; if (present < K) { // 나이트 for (int i = 0; i < 8; i++) { int y = r + hy[i]; int x = c + hx[i]; if (y < 0 || y >= H || x < 0 || x >= W || Map[y][x] == 1 || visit[y][x][present + 1]) continue; q.push({ {y, x}, present + 1 }); visit[y][x][present + 1] = true; } } for (int i = 0; i < 4; i++) { int y = r + dy[i]; int x = c + dx[i]; if (y < 0 || y >= H || x < 0 || x >= W || Map[y][x] == 1 || visit[y][x][present]) continue; q.push({ { y, x }, present }); visit[y][x][present] = true; } } ++ret; } return -1; } int main(void) { scanf("%d %d %d", &K, &W, &H); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { scanf("%d", &Map[i][j]); } } printf("%d\n", BFS()); return 0; } | cs |
728x90
반응형