sicily 1172. Queens, Knights and Pawns

原创 2015年11月18日 19:01:56


1172. Queens, Knights and Pawns

Constraints

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 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

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<iostream>
#include<string h="">
using namespace std;

struct stru {
	int x;
	int y;
};
int map[1005][1005];
// 分别为knight, queue, pawn数量
int nk, nq, np;
int n, m;
// 记录knight, queue的位置
stru kni[101];
stru qu[101];
// 设置knifht 和 queue 的移动方向
int kdir[8][2] = { { 1, 2 }, { 2, 1 }, { -1, 2 }, { -2, 1 }, 
					{ 1, -2 }, { 2, -1 }, { -1, -2 }, {-2,-1} };
int qdir[8][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
				   { 1, -1 }, { -1, 1 }, { 1, 1 }, {-1,-1} };

void find_safe(int t) {
	// queue
	for (int i = 0; i < nq; i++) {
		stru exp = qu[i];
		for (int j = 0; j < 8; j++) {
			int xx = qu[i].x + qdir[j][0];
			int yy = qu[i].y + qdir[j][1];
			while (xx <= n && yy <= m && xx > 0 && yy > 0) {
				// 如果这个位置有其他元素,则不能继续走
				if (map[xx][yy] == 2 || map[xx][yy] == 3 || map[xx][yy] == 4) {
					break;
				}
				// 没有,置1,并继续往这个方向走
				else {
					map[xx][yy] = 1;
					xx = xx + qdir[j][0];
					yy = yy + qdir[j][1];
				}
			}
		}
	}

	// knight
	for (int i = 0; i < nk; i++) {
		stru exp = kni[i];
		for (int j = 0; j < 8; j++) {
			int xx = kni[i].x + kdir[j][0];
			int yy = kni[i].y + kdir[j][1];
			if (xx <= n && yy <= m && xx > 0 && yy > 0 && map[xx][yy] == 0) {
					map[xx][yy] = 1;
			}
		}
	}

	int num = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (map[i][j] == 0) {
				num++;
			}
		}
	}
	cout << "Board " << t << " has " << num << " safe squares." << endl;
}

int main() {
	int t = 0;
	cin >> n >> m;
	while (m != 0 && n != 0) {
		t++;
		memset(map, 0, sizeof(map));
		cin >> nq;
		for (int i = 0; i < nq; i++) {
			cin >> qu[i].x >> qu[i].y;
			map[qu[i].x][qu[i].y] = 2;
		}
		cin >> nk;
		for (int i = 0; i < nk; i++) {
			cin >> kni[i].x >> kni[i].y;
			map[kni[i].x][kni[i].y] = 3;
		}
		cin >> np;
		for (int i = 0; i < np; i++) {
			int x, y;
			cin >> x >> y;
			map[x][y] = 4;
		}
		find_safe(t);
		cin >> n >> m;
	}
	return 0;
}
</string></iostream>


sicily 1172 Queens, Knights and Pawns

题目意思: 类似于八皇后问题,但不是放置皇后,是计算安全位置。有三种棋子,皇后、骑士和卒(pawn,貌似不是这个意思)。皇后是全方向攻击(八个方向),距离不限,但是会被阻挡;骑士只能走“日”字形,也...

sicily1172 Queens, Knights and Pawns

题目链接:http://soj.sysu.edu.cn/1172 题目大意:在一个棋盘上,放这皇后、骑士和兵三种棋子。其中皇后有八个方向可以占位(即在该方向上的位置不能再放棋子),但是其遇到任何的另一...

Sicily 1172. Queens, Knights and Pawns

1172. Queens, Knights and Pawns Constraints Time Limit: 1 secs, Memory Limit: 64 MB Descr...

Sicily 1172 Queens, Knights and Pawns

没什么好说的,把代码写简洁一些。#include #include using namespace std; enum Status {OCCUPIED, UNSAFE, SAFE}; typ...
  • wbchou
  • wbchou
  • 2013年02月21日 17:05
  • 206

1172. Queens, Knights and Pawns

#include #include using namespace std; int height, width; int board[1001][1001];//0=safe, 1=quee...
  • Er_mu
  • Er_mu
  • 2014年10月27日 17:38
  • 285

1172 Queens & Knights & Pawns

//this low-intermediate level takes three hours to ac including 2.5 hours to inefficiently //find o...

CodeForces 382D Ksenia and Pawns

题意: 有一副棋盘  每个格子上有方向  要求在棋盘上放两个人  两个人按照方向同时走  得分为两人步数之和 规定除了停止格子‘#’以外每个格子只能容下一个人  求最大的得分  如果得分无穷大则输...

URAL 1216 Two Pawns and One King

题意n*n的棋盘中,有三个国际象棋棋子,分别是黑王,黑兵,白兵。 规定: 如果白兵到底线升变,白方胜 如果黑王被吃,白方胜 如果黑兵到达底线,可以升变。 如果白兵无法移动或被吃,黑方胜。 ...

Unreal教学(6)——玩家输入和Pawn(Player Input and Pawns)

Pawn这个单词不会翻译,找不到可以形象描述这个单词的汉语,大概的意思就是可以通过很多的Components合成一个含有多种属性状态的物体。 这里主要是如果获取键盘和鼠标的输入, 在Edit  -...
  • WAN_EXE
  • WAN_EXE
  • 2017年11月22日 14:24
  • 47

sicily 1172之水题

题目在这里1172 最近写的都是水题啊。。。囧
  • xujqiao
  • xujqiao
  • 2014年08月17日 16:02
  • 165
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:sicily 1172. Queens, Knights and Pawns
举报原因:
原因补充:

(最多只允许输入30个字)