# 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>

• 本文已收录于以下专栏：

## sicily 1172. Queens, Knights and Pawns

1172. Queens, Knights and Pawns Constraints Time Limit: 1 secs, Memory Limit: 64 MB Descriptio...
• huangjq36SYSU
• 2015年10月30日 20:53
• 219

## Sicily 1172. Queens, Knights and Pawns

1172. Queens, Knights and Pawns Constraints Time Limit: 1 secs, Memory Limit: 64 MB Descr...
• u012925008
• 2015年03月21日 08:44
• 409

## 1172. Queens, Knights and Pawns

#include #include using namespace std; int height, width; int board[1001][1001];//0=safe, 1=quee...
• Er_mu
• 2014年10月27日 17:38
• 300

## sicily 1172 Queens, Knights and Pawns

• x_pi_hai
• 2012年10月20日 15:29
• 495

## sicily 1172 Queens, Knights and Pawns

#include #include #include using namespace std; struct node { int vis; int has; ...
• chocolate_22
• 2011年06月02日 11:45
• 670

## Sicily 1172 Queens, Knights and Pawns

• wbchou
• 2013年02月21日 17:05
• 218

## sicily1172 Queens, Knights and Pawns

• guozhengchun
• 2014年12月17日 00:10
• 331

## 1172 Queens & Knights & Pawns

//this low-intermediate level takes three hours to ac including 2.5 hours to inefficiently //find o...
• u013147914
• 2013年12月27日 19:39
• 335

## 1172. Queens, Knights and Pawns(计算安全的格子)

/*1172. Queens, Knights and Pawns(计算安全的格子) 大意：给出三种Queens、Knights、和Pawns。Queen每次走一格，可走八个方向 ...
• xiehaoyun2012
• 2013年01月02日 19:57
• 182

## sicily 1172.Queens,Knights and Pawns

#include #include #include #include using namespace std; struct board { bool have; bool visit...
• zsjhxl
• 2013年11月07日 18:07
• 375

举报原因： 您举报文章：sicily 1172. Queens, Knights and Pawns 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)