sicily 1172. Queens, Knights and Pawns
2015-10-30 20:53
525 查看
1172. Queens, Knights and Pawns
Constraints
<4000
p>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 andasked 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. Neitherinteger 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 formBoard 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 <stdio.h>
#include <memory.h>
int dir[8][2] = {-1, -2, -2, -1, 1, -2, 2, -1, 1, 2, 2, 1, -1, 2, -2, 1};
int main()
{
int row, col;
int count = 1;
while (scanf("%d%d", &row, &col)) {
if (row ==0 && col == 0)
break;
int board[row+1][col+1];
memset(board, 0, sizeof(board));
int numQueen, numKnight, numPawn;
scanf("%d", &numQueen);
int posQueen[numQueen][2];
for (int i = 0; i < numQueen; ++i) {
scanf("%d%d", &posQueen[i][0], &posQueen[i][1]);
board[ posQueen[i][0] ][ posQueen[i][1] ] = 2;
}
scanf("%d", &numKnight);
int posKnight[numKnight][2];
for (int i = 0; i < numKnight; ++i) {
scanf("%d%d", &posKnight[i][0], &posKnight[i][1]);
board[ posKnight[i][0] ][ posKnight[i][1] ] = 2;
}
scanf("%d", &numPawn);
int posPawn[numPawn][2];
for (int i = 0; i < numPawn; ++i) {
scanf("%d%d", &posPawn[i][0], &posPawn[i][1]);
board[ posPawn[i][0] ][ posPawn[i][1] ] = 2;
}
int r, c;
for (int i = 0; i < numQueen; ++i) {
//
r = posQueen[i][0];
c = posQueen[i][1] - 1;
for (; c >= 1; --c)
if (board[r][c] == 2)
break;
else
board[r][c] = 1;
c = posQueen[i][1] + 1;
for (; c <= col; ++c)
if (board[r][c] == 2)
break;
else
board[r][c] = 1;
//
c = posQueen[i][1];
r = posQueen[i][0] - 1;
for (; r >= 1; --r)
if (board[r][c] == 2)
break;
else
board[r][c] = 1;
r = posQueen[i][0] + 1;
for (; r <= row; ++r)
if (board[r][c] == 2)
break;
else
board[r][c] = 1;
//
c = posQueen[i][1] - 1;
r = posQueen[i][0] - 1;
while (r >= 1 && c >= 1) {
if (board[r][c] == 2)
break;
else
board[r--][c--] = 1;
}
c = posQueen[i][1] + 1;
r = posQueen[i][0] + 1;
while (r <= row && c <= col) {
if (board[r][c] == 2)
break;
else
board[r++][c++] = 1;
}
//
c = posQueen[i][1] - 1;
r = posQueen[i][0] + 1;
while (r <= row && c >= 1) {
if (board[r][c] == 2)
break;
else
board[r++][c--] = 1;
}
c = posQueen[i][1] + 1;
r = posQueen[i][0] - 1;
while (r >= 1 && c <= col) {
if (board[r][c] == 2)
break;
else
board[r--][c++] = 1;
}
}
for (int i = 0; i < numKnight; ++i) {
for (int j = 0; j < 8; ++j) {
r = posKnight[i][0] + dir[j][0];
c = posKnight[i][1] + dir[j][1];
if (r >= 1 && r <= row && c >= 1 && c <= col && !board[r][c])
board[r][c] = 1;
}
}
int ans = 0;
for (int i = 1; i <= row; ++i)
for (int j = 1; j <= col; ++j)
if (!board[i][j])
++ans;
printf("Board %d has %d safe squares.\n", count++, ans);
}
}
相关文章推荐
- sicily 1443. Printer Queue
- HDU 5504 GT and sequence
- ResultSet rs = stmt.executeQuery(sql); 返回值问题判断
- ResultSet rs = stmt.executeQuery(sql); 返回值问题判断
- sicily 1122. Prerequisites?
- IOS开发—UIScrollView的contentSize、contentInset、contentOffset的理解
- UI --- Xcode7 模拟器运行时崩溃解决方法
- CloudSuite之Web Search Benchmark环境搭建
- UI --- UIView 画圆和矩形
- 2015 CCPC D- Pick The Sticks(UESTC 1218) (01背包变形)
- UITableView的图片自适应高度
- 详细讲解安装HUE
- UITableView的cell自适应高度
- Java 反编译利器 JD-GUI/Jad/JadClipse
- Codeforces Round #327 (Div. 2) D. Chip 'n Dale Rescue Rangers
- puer工具的使用
- RHEL5.8修改yum源为163的源
- STL-单向队列queue
- UIScrollView和UIPageControl 实现让图片无限循环播放和左右滑动
- 解决"mex setup或mbuild setup找不到编译器"问题