sicily 1172. Queens, Knights and Pawns
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.
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.
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.
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
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的更多相关文章
- Codeforces Gym 100650D Queens, Knights and Pawns 暴力
Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- sicily 中缀表达式转后缀表达式
题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...
- POJ 2942 Knights of the Round Table
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10911 Acce ...
- LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- sicily 1934. 移动小球
Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...
- Knights of the Round Table-POJ2942(双连通分量+交叉染色)
Knights of the Round Table Description Being a knight is a very attractive career: searching for the ...
随机推荐
- connectedSignal 简单使用
import java.util.concurrent.CountDownLatch; public class CountDown { private static CountDownLatch c ...
- hdu 1086 You can Solve a Geometry Problem too (几何)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- BZOJ4873 [Shoi2017]寿司餐厅 【最大权闭合子图】
题目链接 BZOJ4873 题解 题意很鬼畜,就可以考虑网络流[雾] 然后就会发现这是一个裸的最大权闭合子图 就是注意要离散化一下代号 #include<algorithm> #inclu ...
- adb 进入 recovery adb 进入 bootloader
重启到Recovery界面 adb reboot recovery重启到bootloader界面 adb reboot bootloader adb wait-for-device #等待设备 adb ...
- Codeforces Round #307 (Div. 2) D 矩阵快速幂+快速幂
D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...
- bzoj 3252 攻略 长链剖分思想+贪心
攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 889 Solved: 423[Submit][Status][Discuss] Descrip ...
- C#泛型实例详解
本文以实例形式讲述了C#泛型的用法,有助于读者深入理解C#泛型的原理,具体分析如下: 首先需要明白什么时候使用泛型: 当针对不同的数据类型,采用相似的逻辑算法,为了避免重复,可以考虑使用泛型. 一.针 ...
- 南阳ACM 题目275:队花的烦恼一 Java版
队花的烦恼一 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 ACM队的队花C小+经常抱怨:"C语言中的格式输出中有十六.十.八进制输出,然而却没有二进制输出, ...
- 数据结构:Treap
关于重量平衡树的相关概念可以参考姊妹文章:重量平衡树之替罪羊树 Treap是依靠旋转来维护平衡的重量平衡树中最为好写的一中,因为它的旋转不是LL就是RR 对于每一个新的节点,它给这个节点分配了一个随机 ...
- javascript「篱式」条件判断
我们已经知道,null 没有任何的属性值,并且无法获取其实体(existence)值.所以 null.property 返回的是错误(error)而不是 undefined . 考虑下面的代码 if ...