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.
// Problem#: 1172 // Submission#: 3224536 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <string.h> #include <vector> #include <iomanip> #include <map> #include <stack> #include <functional> #include <list> #include <cmath> using namespace std; #define MAX 1005 char board[MAX][MAX]; int w, h; int qn ,kn, pn; short qi[105], qj[105]; short ki[105], kj[105]; short pi[105], pj[105]; short ka[8][2] = {-1, -2, -2, -1, -2, 1, -1, 2, 1, 2, 2, 1, 2, -1, 1, -2}; short qa[8][2] = {-1, 0, -1, 1, 0, 1, 1, 1, 1, 0, 1, -1, 0, -1, -1, -1}; int main() { //std::ios::sync_with_stdio(false); int counter = 1; while (1) { scanf("%d%d", &h, &w); if (h == 0 && w == 0) break; int i, j, k; for (i = 1; i <= h; i++) { for (j = 1; j <= w; j++) { board[i][j] = '\0'; } } for (scanf("%d", &qn), i = 0; i < qn; i++) scanf("%d%d", &qi[i], &qj[i]); for (scanf("%d", &kn), i = 0; i < kn; i++) scanf("%d%d", &ki[i], &kj[i]); for (scanf("%d", &pn), i = 0; i < pn; i++) scanf("%d%d", &pi[i], &pj[i]); for (i = 0; i < kn; i++) { board[ki[i]][kj[i]] = 'K'; for (j = 0; j < 8; j++) { int tempi = ki[i] + ka[j][0], tempj = kj[i] + ka[j][1]; if (1 <= tempi && tempi <= h && 1 <= tempj && tempj <= w && board[tempi][tempj] == '\0') board[tempi][tempj] = 'X'; } } for (i = 0; i < pn; i++) board[pi[i]][pj[i]] = 'P'; for (i = 0; i < qn; i++) { board[qi[i]][qj[i]] = 'Q'; } for (i = 0; i < qn; i++) { for (j = 0; j < 8; j++) { int tempi = qi[i] + qa[j][0], tempj = qj[i] + qa[j][1]; while (1 <= tempi && tempi <= h && 1 <= tempj && tempj <= w && (board[tempi][tempj] == '\0' || board[tempi][tempj] == 'X')) { board[tempi][tempj] = 'X'; tempi += qa[j][0]; tempj += qa[j][1]; } } } int ans = 0; for (i = 1; i <= h; i++) { for (j = 1; j <= w; j++) { if (board[i][j] == '\0') ans++; board[i][j] = '\0'; } } printf("Board %d has %d safe squares.\n", counter, ans); counter++; } //getchar(); //getchar(); return 0; }