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 ...
随机推荐
- 转:Simple Introduction to Dirichlet Process
来源:http://hi.baidu.com/vyfrcemnsnbgxyd/item/2f10ecc3fc35597dced4f88b Dirichlet Process(DP)是一个很重要的统计模 ...
- [洛谷P2774]方格取数问题
题目大意:给你一个$n\times m$的方格,要求你从中选择一些数,其中没有相邻两个数,使得最后和最大 题解:网络流,最小割,发现相邻的两个点不可以同时选择,进行黑白染色,原点向黑点连一条容量为点权 ...
- BZOJ1934:[SHOI2007]善意的投票 & BZOJ2768:[JLOI2010]冠军调查——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1934 https://www.lydsy.com/JudgeOnline/problem.php? ...
- BZOJ3534:[SDOI2014]重建——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3534 https://www.luogu.org/problemnew/show/P3317 T国 ...
- mac, xcode 6.1 安装command line tools 支持,autoconf,automake等
以下软件包 都去我的环境库找到 1 先安装 tcl库 2 安装macports /opt/local/bin/port 一般装到这里 安装autoconf时提示: Warning: The Xcode ...
- Install JDK In Ubuntu
安装Linux软件包管理器rpm apt install rpm 查看已安装的软件,如JDK rpm -qa|grep jdk #查询所有 找jdk 卸载已安装的软件 rpm -e nodeps 包名 ...
- phalcon安装
参考网站:https://docs.phalconphp.com/zh/latest/reference/tools.html (中文版)cento6.5环境安装:cd ~mkdir phalconc ...
- JQuery学习六
<JQuery cookie>插件 cookie是保存在浏览器上的内容,用户在这次浏览页面的时候向cookie中保存文本内容.下次再访问页面的时侯就可以取出来上次保存的内容.这样可以得到上 ...
- synchronize 和volatile 实现共享变量在多线程中的可见性
1.什么是线程可见性 可见性:一个线程对共享变量值的修改能够及时被其他线程看到. 共享变量:如果一个变量在多个线程工作内存中都存在副本,那么着给按量就是这几个线程的共享变量. 2.导致共享变量在线程间 ...
- UVA 10766 Organising the Organisation
https://vjudge.net/problem/UVA-10766 题意: n个员工,除总经理外每个人只能有一个直接上级 有m对人不能成为直接的上下级关系 规定k为总经理 问员工分级方案 无向图 ...