1172. Queens, Knights and Pawns
Constraints
Time Limit: 1 secs, Memory Limit: 64 MB
Description
You all are familiar with the famous 8-queens problem which asks you to place 8 queens on a chess board so no two attack each other. In this problem, you will be given locations of queens and knights and pawns and asked to find how many of the unoccupied squares on the board are not under attack from either a queen or a knight (or both). We'll call such squares "safe" squares. Here, pawns will only serve as blockers and have no capturing ability. The board below has 6 safe squares. (The shaded squares are safe.)
Recall that a knight moves to any unoccupied square that is on the opposite corner of a 2x3 rectangle from its current position; a queen moves to any square that is visible in any of the eight horizontal, vertical, and diagonal directions from the current position. Note that the movement of a queen can be blocked by another piece, while a knight's movement can not.
Input
There will be multiple test cases. Each test case will consist of 4 lines. The first line will contain two integers n and m, indicating the dimensions of the board, giving rows and columns, respectively. Neither integer will exceed 1000. The next three lines will each be of the form
k r1 c1 r2 c2 ... rk ck
indicating the location of the queens, knights and pawns, respectively. The numbering of the rows and columns will start at one. There will be no more than 100 of any one piece. Values of n = m = 0 indicate end of input.
Output
Each test case should generate one line of the form
Board b has s safe squares.
where b is the number of the board (starting at one) and you supply the correct value for s.
Sample Input
4 4 2 1 4 2 4 1 1 2 1 2 3 2 3 1 1 2 1 1 1 0 1000 1000 1 3 3 0 0 0 0
Sample Output
Board 1 has 6 safe squares. Board 2 has 0 safe squares. Board 3 has 996998 safe squares.#include<iostream> #include<string h=""> using namespace std; struct stru { int x; int y; }; int map[1005][1005]; // 分别为knight, queue, pawn数量 int nk, nq, np; int n, m; // 记录knight, queue的位置 stru kni[101]; stru qu[101]; // 设置knifht 和 queue 的移动方向 int kdir[8][2] = { { 1, 2 }, { 2, 1 }, { -1, 2 }, { -2, 1 }, { 1, -2 }, { 2, -1 }, { -1, -2 }, {-2,-1} }; int qdir[8][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 }, { 1, -1 }, { -1, 1 }, { 1, 1 }, {-1,-1} }; void find_safe(int t) { // queue for (int i = 0; i < nq; i++) { stru exp = qu[i]; for (int j = 0; j < 8; j++) { int xx = qu[i].x + qdir[j][0]; int yy = qu[i].y + qdir[j][1]; while (xx <= n && yy <= m && xx > 0 && yy > 0) { // 如果这个位置有其他元素,则不能继续走 if (map[xx][yy] == 2 || map[xx][yy] == 3 || map[xx][yy] == 4) { break; } // 没有,置1,并继续往这个方向走 else { map[xx][yy] = 1; xx = xx + qdir[j][0]; yy = yy + qdir[j][1]; } } } } // knight for (int i = 0; i < nk; i++) { stru exp = kni[i]; for (int j = 0; j < 8; j++) { int xx = kni[i].x + kdir[j][0]; int yy = kni[i].y + kdir[j][1]; if (xx <= n && yy <= m && xx > 0 && yy > 0 && map[xx][yy] == 0) { map[xx][yy] = 1; } } } int num = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (map[i][j] == 0) { num++; } } } cout << "Board " << t << " has " << num << " safe squares." << endl; } int main() { int t = 0; cin >> n >> m; while (m != 0 && n != 0) { t++; memset(map, 0, sizeof(map)); cin >> nq; for (int i = 0; i < nq; i++) { cin >> qu[i].x >> qu[i].y; map[qu[i].x][qu[i].y] = 2; } cin >> nk; for (int i = 0; i < nk; i++) { cin >> kni[i].x >> kni[i].y; map[kni[i].x][kni[i].y] = 3; } cin >> np; for (int i = 0; i < np; i++) { int x, y; cin >> x >> y; map[x][y] = 4; } find_safe(t); cin >> n >> m; } return 0; } </string></iostream>