Baekjoon/Simulation
[백준 1986] 체스 (Simulation) (C/C++) (★)
워니-
2020. 3. 25. 11:39
#include <stdio.h> #include <iostream> #include <algorithm> #include <vector> using namespace std; int row, col; int queenNum, knightNum, pawnNum; char map[1010][1010]; int visited[1010][1010]; int safeZone; vector<pair<int, int>> queen; vector<pair<int, int>> knight; int safe(int x, int y) { if(x >= 1 && y >= 1 && x <= row && y <= col) { return 1; } else { return 0; } } void knight_move(int x, int y) { int dx[8] = {-1, -2, -2, -1, 1, 2, 2, 1}; int dy[8] = {-2, -1, 1, 2, 2, 1, -1, -2}; for(int i = 0; i < 8; i++) { int xpos = x+dx[i]; int ypos = y+dy[i]; if(safe(xpos, ypos) == 1 && map[xpos][ypos] == '.') { visited[xpos][ypos] = 1; } } } void queen_move(int x, int y) { int dx[8] = {-1, -1, -1, 1, 1, 1, 0, 0}; int dy[8] = {-1, 0, 1, -1, 0, 1, -1, 1}; for(int i = 0; i < 8; i++) { for(int k = 1; k <= max(row, col); k++) { int xpos = x + (dx[i] * k); int ypos = y + (dy[i] * k); if(safe(xpos, ypos) == 1 && map[xpos][ypos] == '.') { visited[xpos][ypos] = 1; } else { break; } } } } int main(void) { // freopen("B1986_input.txt", "r", stdin); cin >> row >> col; for(int i = 1; i <= row; i++) { for(int j = 1; j <= col; j++) { map[i][j] = '.'; } } cin >> queenNum; for(int i = 1; i <= queenNum; i++) { int x, y; cin >> x >> y; visited[x][y] = 1; map[x][y] = 'Q'; queen.push_back({x, y}); } cin >> knightNum; for(int i = 1; i <= knightNum; i++) { int x, y; cin >> x >> y; visited[x][y] = 1; map[x][y] = 'K'; knight.push_back({x, y}); } cin >> pawnNum; for(int i = 1; i <= pawnNum; i++) { int x, y; cin >> x >> y; visited[x][y] = 1; map[x][y] = 'P'; } for(int i = 0; i < knight.size(); i++) { knight_move(knight[i].first, knight[i].second); } for(int i = 0; i < queen.size(); i++) { queen_move(queen[i].first, queen[i].second); } for(int i = 1; i <= row; i++) { for(int j = 1; j <= col; j++) { if(visited[i][j] == 0) { safeZone++; } } } cout << safeZone; return 0; } | cs |