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
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAARABIDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAMFBwT/xAAlEAACAQQCAQMFAAAAAAAAAAABAgMABAURBiESIjFBMjZxdbP/xAAYAQACAwAAAAAAAAAAAAAAAAAAAwEEBf/EABsRAQEAAgMBAAAAAAAAAAAAAAEAAgMEEyFh/9oADAMBAAIRAxEAPwDQeRW+SyVnctBIkiiScOk87qm0ciP0aZWA8dkEDZA2fcGPCWPI+PXkUt3GIcQjkyQxTGdtMrAhUVQO5CraVd/UB1pa7cnHmbaW5hjxEktoZJJGulnjChWYsT4lvLoHvr3B1vommvuQYaSe/jGSxrW9yXEiCWIiTe9eWohvs/LH8n5ocDh9jlnsER+zt+9wDE9G0uKWO4hSaGRJIpFDI6MCrKewQR7ilVfFPs7B/r4P5rStB8ZJW9KUqIlKUoi//9k=" alt="" /> Copy sample input to clipboard 
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.
这题可能有理解错误的地方就是对queen,它在八个方向的移动是不限的,除非遇到一个其他对象。
#include <iostream>
#include <vector> using namespace std; char location[][]; int king[] = {-, -, -, , -, , , , , , , -, , -, -, -};
int queueL[] = {-, -, -, , -, , , -, , , , -, , , , }; int main(int argc, char const *argv[])
{
int m, n, num, x, y;
int testCase = ;
while (cin >> m >> n && m != && n != ) {
++testCase;
memset(location, '', sizeof(location));
vector<int> queue; // queen
cin >> num;
queue.resize(num * + );
int i = , j = ;
while (i++ < num) {
cin >> x >> y;
location[x - ][y - ] = '';
queue[j] = x - ;
queue[j + ] = y - ;
j += ;
} // knight
i = ;
cin >> num;
while (i++ < num) {
cin >> x >> y;
location[x - ][y - ] = '';
for (int k = ; k < ; k += ) { // 处理8个方向,也即八个“日”字形路线
int xT = x - + king[k];
int yT = y - + king[k + ];
if (xT >= && xT < m && yT >= && yT < n && location[xT][yT] == '')
location[xT][yT] = '';
}
} // pawn
i = ;
cin >> num;
while (i++ < num) {
cin >> x >> y;
location[x - ][y - ] = '';
} // Queen 的处理
for (i = ; i < j; i += ) {
for (int p = ; p < ; p += ) {
int xT = queue[i];
int yT = queue[i + ];
for (int q = ; ; ++q) {
xT += queueL[p];
yT += queueL[p + ];
if (xT >= && xT < m && yT >= && yT < n && location[xT][yT] == '') {
location[xT][yT] = '';
} else if (xT >= && xT < m && yT >= && yT < n && location[xT][yT] == '') { // 在该直线上遇到了其他对象,所以不需要再走了
break;
} else if(xT < || xT >= m || yT < || yT >= n) { // 跑出了棋盘,那么说明在这条直线上已经不需要再走了
break;
}
}
}
} int result = ;
for (i = ; i != m; ++i) {
for (j = ; j != n; ++j)
if (location[i][j] == '')
++result;
} cout << "Board " << testCase << " has " << result << " safe squares." << endl;
}
return ;
}

sicily 1172. Queens, Knights and Pawns的更多相关文章

  1. Codeforces Gym 100650D Queens, Knights and Pawns 暴力

    Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...

  2. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  3. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

  4. sicily 中缀表达式转后缀表达式

    题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...

  5. POJ 2942 Knights of the Round Table

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 10911   Acce ...

  6. LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)

    题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...

  7. 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS

    [Usaco2005 Dec]Knights of Ni 骑士 Description  贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...

  8. sicily 1934. 移动小球

    Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...

  9. Knights of the Round Table-POJ2942(双连通分量+交叉染色)

    Knights of the Round Table Description Being a knight is a very attractive career: searching for the ...

随机推荐

  1. iOS-系统 图片、视频 管理控制器UIImagePickerController

        UIImagePickerController 是一个管理系统多媒体文件库(相册)中的图片.视频文件的视图控制器,诞生于iOS4之前,虽然功能不是很完善,我们仍可以用这个视图控制器做一些有创造 ...

  2. bzoj3168-钙铁锌硒维生素

    题目 这道题的题意理解很重要,直接写原题了. 小林把人体需要的营养分成了\(n\)种,他准备了2套厨师机器人,一套厨师机器人有\(n\)个,每个厨师机器人只会做一道菜,这道菜一斤能提供第\(i\)种营 ...

  3. BZOJ 1211 树的计数(purfer序列)

    首先考虑无解的情况, 根据purfer序列,当dee[i]=0并且n!=1的时候,必然无解.否则为1. 且sum(dee[i]-1)!=n-2也必然无解. 剩下的使用排列组合即可推出公式.需要注意的是 ...

  4. 【bzoj2834】回家的路 分层图最短路

    题目描述 输入 输出 样例输入 2 1 1 2 1 1 2 2 样例输出 5 题解 分层图最短路 dis[i][0]表示到i为横向时起点到i的最短路,dis[i][1]表示到i为纵向时起点到i的最短路 ...

  5. 【bzoj1708】[USACO2007 Oct]Money奶牛的硬币 背包dp

    题目描述 在创立了她们自己的政权之后,奶牛们决定推广新的货币系统.在强烈的叛逆心理的驱使下,她们准备使用奇怪的面值.在传统的货币系统中,硬币的面值通常是1,5,10,20或25,50,以及100单位的 ...

  6. Devc++编译系统分配给int多少字节

    我看的是<C语言程序设计>..谭浩强的PDF版 里面只讲了VC和TC 的,没有Devc++的..(我的是5.10版) 还有这是什么意思? 经过查阅我进行了这样的测试: 得到了这样的结果: ...

  7. CF961D Pair Of Lines

    题目描述 You are given n n n points on Cartesian plane. Every point is a lattice point (i. e. both of it ...

  8. Android View 绘制刷新流程分析

    Android中对View的更新有很多种方式,使用时要区分不同的应用场合.1.不使用多线程和双缓冲      这种情况最简单,一般只是希望在View发生改变时对UI进行重绘.你只需显式地调用View对 ...

  9. 【BZOJ1486】最小圈(分数规划)

    [BZOJ1486]最小圈(分数规划) 题面 BZOJ 洛谷 求图中边权和除以点数最小的环 题解 分数规划 二分答案之后将边权修改为边权减去二分值 检查有无负环即可 #include<iostr ...

  10. BZOJ4596:[SHOI2016]黑暗前的幻想乡——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4596 https://www.luogu.org/problemnew/show/P4336#su ...