삼성모의테스트 탈주범검거입니다.
https://www.swexpertacademy.com/main/learn/course/lectureProblemViewer.do
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | #include <iostream> #include <queue> using namespace std; int dx[] = { 0,0,-1,1 }; int dy[] = { -1,1,0,0 }; bool pos(int dir,int a, int b) { if (dir == 2)//위로 { if (a == 1 && ((b == 1) || (b == 2) || (b == 5) || (b == 6))) return true; else if (a == 2 && ((b == 1) || (b == 2) || (b == 5) || (b == 6))) return true; else if (a == 4 && ((b == 1) || (b == 2) || (b == 5) || (b == 6))) return true; else if (a == 7 && ((b == 1) || (b == 2) || (b == 5) || (b == 6))) return true; else return false; } else if (dir == 3)//아래로 { if (a == 1 && ((b == 1) || (b == 2) || (b == 4) || (b == 7))) return true; else if (a == 2 && ((b == 1) || (b == 2) || (b == 4) || (b == 7))) return true; else if (a == 5 && ((b == 1) || (b == 2) || (b == 4) || (b == 7))) return true; else if (a == 6 && ((b == 1) || (b == 2) || (b == 4) || (b == 7))) return true; else return false; } else if (dir == 0)//왼쪽 { if (a == 1 && ((b == 1) || (b == 3) || (b == 4) || (b == 5))) return true; else if (a == 3 && ((b == 1) || (b == 3) || (b == 4) || (b == 5))) return true; else if (a == 6 && ((b == 1) || (b == 3) || (b == 4) || (b == 5))) return true; else if (a == 7 && ((b == 1) || (b == 3) || (b == 4) || (b == 5))) return true; else return false; } else if (dir == 1)//오른쪽 { if (a == 1 && ((b == 1) || (b == 3) || (b == 6) || (b == 7))) return true; else if (a == 3 && ((b == 1) || (b == 3) || (b == 6) || (b == 7))) return true; else if (a == 4 && ((b == 1) || (b == 3) || (b == 6) || (b == 7))) return true; else if (a == 5 && ((b == 1) || (b == 3) || (b == 6) || (b == 7))) return true; else return false; } } int main() { int T; cin >> T; for (int test_case = 1; test_case <= T; test_case++) { int n, m, r, c, l; cin >> n >> m >> r >> c >> l;//세로 가로 맨홀세로 맨홀가로 탈출후시간 int a[51][51] = { 0, }; int check[51][51] = { 0, }; int ans = 0; bool timeoff = false; queue<pair<int, int>>q; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; //1 상하좌우 2 상하 3 좌우 4 상우 5 하우 6하좌 7 상좌 } } q.push({ r,c }); check[r][c] = 1; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int dir = 0; dir < 4; dir++) { int nx = x + dx[dir]; int ny = y + dy[dir]; if (nx >= 0 && nx < n&&ny >= 0 && ny < m&&a[nx][ny]!=0&&pos(dir,a[x][y],a[nx][ny])) { if (check[nx][ny]==0) { check[nx][ny] = check[x][y] + 1; if (check[nx][ny] > l) { check[nx][ny] = 0; timeoff = true; break; } q.push({ nx,ny }); } } } if (timeoff) break; } cout << '\n'; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (check[i][j]) ans++; cout << check[i][j] << ' '; } cout << '\n'; } cout << '#' << test_case << ' ' << ans << '\n'; } } | cs |
'알고리즘 > BFS 문제풀이' 카테고리의 다른 글
[B7562] - 나이트의 이동 (0) | 2018.08.30 |
---|---|
[B5014] - 스타트링크 (0) | 2018.08.26 |
[S1249] - 보급로 (0) | 2018.08.25 |
[B14442]-벽 부수고 이동하기2 (0) | 2018.08.24 |
[B2206]-벽 부수고 이동하기 (0) | 2018.08.24 |