BOJ 1986 :: 체스
문제 링크 : https://www.acmicpc.net/problem/1986
나의 코드
Github : https://github.com/j2wooooo/Daliy_Algorithms/blob/master/Daliy_Algorithms/BOJ_1986/BOJ_1986.cpp
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 | // 백준알고리즘 1986번 :: 체스 #include<iostream> #include<queue> using namespace std; int n, m; int block[1010][1010]; int ans[1010][1010]; int ret = 0; // 상 하 좌 우 대각왼위 대각오위 대각왼아래 대각오아래 int qdx[8] = {-1,1,0,0,-1,-1,1,1}; int qdy[8] = {0,0,-1,1,-1,1,-1,1}; int kdx[8] = {2,2,1,1,-1,-1,-2,-2}; int kdy[8] = {1,-1,2,-2,2,-2,1,-1}; queue<pair<int, int>> knight; queue<pair<int, int>> queen; void knight_simulate() { while (!knight.empty()) { int kx = knight.front().first; int ky = knight.front().second; knight.pop(); for (int i = 0; i < 8; i++) { int mkx = kx + kdx[i]; int mky = ky + kdy[i]; if (mkx < 1 || mkx > n || mky < 1 || mky > m) continue; if (block[mkx][mky]) continue; else { if (!ans[mkx][mky]) { ans[mkx][mky] = 1; ret++; } } } } return; } void queen_simulate() { while (!queen.empty()) { int qx = queen.front().first; int qy = queen.front().second; queen.pop(); for (int i = 0; i < 8; i++) { int mqx = qx + qdx[i]; int mqy = qy + qdy[i]; while (1) { if (mqx < 1 || mqx > n || mqy < 1 || mqy > m) break; if (block[mqx][mqy]) break; else { if (!ans[mqx][mqy]) { ans[mqx][mqy] = 1; ret++; } } mqx += qdx[i]; mqy += qdy[i]; } } } return; } int main(void) { int num = 0; int x, y; cin >> n >> m; cin >> num; for (int i = 0; i < num; i++) { cin >> x >> y; block[x][y] = 1; ans[x][y] = 1; ret++; queen.push(make_pair(x, y)); } cin >> num; for (int i = 0; i < num; i++) { cin >> x >> y; block[x][y] = 1; ans[x][y] = 1; ret++; knight.push(make_pair(x, y)); } cin >> num; for (int i = 0; i < num; i++) { cin >> x >> y; block[x][y] = 1; ret++; ans[x][y] = 1; } queen_simulate(); knight_simulate(); cout << n*m - ret; return 0; } | cs |
'Problem > 시뮬레이션' 카테고리의 다른 글
[C/C++] BOJ 1331 :: 나이트 투어 (0) | 2019.02.08 |
---|---|
[C/C++] BOJ 13901 :: 로봇 (0) | 2019.02.07 |
[C/C++] BOJ 1063 :: 킹 (0) | 2019.01.23 |
[C/C++] BOJ 1347 :: 미로 만들기 (0) | 2019.01.21 |
[C/C++] BOJ 15685 :: 드래곤 커브 (0) | 2019.01.21 |