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 ...
随机推荐
- 【bzoj2588】Spoj 10628. Count on a tree 离散化+主席树
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
- Codeforces Round#516 Div.1 翻车记
A:开场懵逼.然后发现有人1min过,于是就sort了一下,于是就过了.正经证明的话,考虑回文串两端点一定是相同的,所以最多有Σcnti*(cnti+1)/2个,cnti为第i种字母出现次数.而sor ...
- 判断form表单每个input字段是否有内容
//---------------------------------------------------input失去焦点时判断是否有值 btn_click: function () { //inp ...
- [洛谷P5173]传球
题目大意:有$n(n\leqslant3500)$个人坐成一个环,$0$号手上有个球,每秒钟可以向左或向右传球,问$m$秒后球在$0$号手上的方案数. 题解:一个$O(nm)$的$DP$,$f_{i, ...
- 重拾C#教程:变量
一个变量只不过是一个供程序操作的存储区的名字.在 C# 中,每个变量都有一个特定的类型,类型决定了变量的内存大小和布局.范围内的值可以存储在内存中,可以对变量进行一系列操作. 我们已经讨论了各种数据类 ...
- 四连测Day3
题目链接:https://pan.baidu.com/s/1_vsHfMI_qO-9IDxmFLkHfg 密码: uza8 T1: 小奥的一笔画,判连通性,查奇偶点即可 #include<ios ...
- 拼接sql语句参数绑定
/** * 事务封装方法 * @access public * @param array $sqls 要执行的sql数组或语句 * @return boolean */ public function ...
- WizTools.org RESTClient 启动方法
关于 WizTools.org RESTClient的使用 今天分享一个很好用的测试service的工具,很好用 提供两种方法使用这个东东. 第一种方法 通过cmd命令窗口. (1)cd C:\Use ...
- HDU 1715 大数java
大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 使用自己的数据集训练和测试"caffenet"
主要步骤可参考: http://blog.csdn.net/u010194274/article/details/50575284 补充几点: 1. convert函数是ImageMagick包里面的 ...