码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 1589 Xiangqi (模拟)

时间:2015-04-04 09:11:41      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

Xiangqi


Problem Description
Xiangqi is one of the most popular two-player board games in China. The game represents a battle between two armies with the goal of capturing the enemy’s “general” piece. In this problem, you are given a situation of later stage in the game. Besides, the red side has already “delivered a check”. Your work is to check whether the situation is “checkmate”.

Now we introduce some basic rules of Xiangqi. Xiangqi is played on a 10×9 board and the pieces are placed on the intersections (points). The top left point is (1,1) and the bottom right point is (10,9). There are two groups of pieces marked by black or red Chinese characters, belonging to the two players separately. During the game, each player in turn moves one piece from the point it occupies to another point. No two pieces can occupy the same point at the same time. A piece can be moved onto a point occupied by an enemy piece, in which case the enemy piece is "captured" and removed from the board. When the general is in danger of being captured by the enemy player on the enemy player’s next move, the enemy player is said to have "delivered a check". If the general‘s player can make no move to prevent the general‘s capture by next enemy move, the situation is called “checkmate”.
技术分享

We only use 4 kinds of pieces introducing as follows:
技术分享General: the generals can move and capture one point either vertically or horizontally and cannot leave the “palace” unless the situation called “flying general” (see the figure above). “Flying general” means that one general can “fly” across the board to capture the enemy general if they stand on the same line without intervening pieces.
技术分享Chariot: the chariots can move and capture vertically and horizontally by any distance, but may not jump over intervening pieces
技术分享Cannon: the cannons move like the chariots, horizontally and vertically, but capture by jumping exactly one piece (whether it is friendly or enemy) over to its target.
技术分享Horse: the horses have 8 kinds of jumps to move and capture shown in the left figure. However, if there is any pieces lying on a point away from the horse horizontally or vertically it cannot move or capture in that direction (see the figure below), which is called “hobbling the horse’s leg”.

技术分享


Now you are given a situation only containing a black general, a red general and several red chariots, cannons and horses, and the red side has delivered a check. Now it turns to black side’s move. Your job is to determine that whether this situation is “checkmate”.
 

Input
The input contains no more than 40 test cases. For each test case, the first line contains three integers representing the number of red pieces N (2<=N<=7) and the position of the black general. The following n lines contain details of N red pieces. For each line, there are a char and two integers representing the type and position of the piece (type char ‘G’ for general, ‘R’ for chariot, ‘H’ for horse and ‘C’ for cannon). We guarantee that the situation is legal and the red side has delivered the check.
There is a blank line between two test cases. The input ends by 0 0 0.
 

Output
For each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the word ‘NO’.
 

Sample Input
2 1 4 G 10 5 R 6 4 3 1 5 H 4 5 G 10 5 C 7 5 0 0 0
 

Sample Output
YES NO 其实这样子的题做一做还是蛮有意思的,实现一个完整程序中的一小部分,对编程能力还是有锻炼的。我的思路是把红方棋子的攻击范围标出来,当然不能让将帅直接面对,注意下细节就好了。不过作为一道细节题竟然一次直接过了,难道我的强项真的是考细节的水题吗??好吧,我水题是做得好。
#include<iostream>
#include<cstring>
#include<string>
using namespace std;

int map[13][13]; //初始化为0,1表示有棋子
int sign[13][13]; //初始化为0,1表示在红方棋子攻击范围内

struct node
{
	char c;
	int x, y;
};

int main()
{
	int n, bx, by;
	while (cin >> n >> bx >> by&&n)
	{
		memset(map, 0, sizeof(map));           //初始化
		memset(sign, 0, sizeof(sign));
		node qizi[10];
		int rjx, rjy;
		for (int i = 0; i < n; i++)
		{
			cin >> qizi[i].c >> qizi[i].x >> qizi[i].y;
			if (qizi[i].c == 'G')
			{
				rjx = qizi[i].x;
				rjy = qizi[i].y;
			}
			map[qizi[i].x][qizi[i].y] = 1;
		}

		if (rjy == by)                              //对特殊情况,黑将和红帅如果直接相对,直接是NO
		{
			int ok = 0;
			for (int i = bx + 1; i < rjx; i++)
			if (map[i][rjy])
				ok = 1;
			if (!ok)
			{
				cout << "NO" << endl;
				continue;
			}
		}

		for (int i = 0; i < n; i++)                  //处理出红方棋子的攻击范围,棋子本身不在其内(可以被吃)
		{
			if (qizi[i].c == 'G')                     //处理出将的攻击范围
			{
				for (int i = rjx - 1; i >= bx; i--)
				{
					sign[i][rjy] = 1;
					if (map[i][rjy]) break;
				}
			}
			else if (qizi[i].c == 'R')               //处理出车的攻击范围
			{
				int xx = qizi[i].x;
				int yy = qizi[i].y;
				for (int i = xx + 1; i <= 10; i++)
				{
					sign[i][yy] = 1;
					if (map[i][yy]) break;
				}
				for (int i = xx - 1; i >= 1; i--)
				{
					sign[i][yy] = 1;
					if (map[i][yy]) break;
				}
				for (int i = yy + 1; i <= 9; i++)
				{
					sign[xx][i] = 1;
					if (map[xx][i]) break;
				}
				for (int i = yy - 1; i >= 1; i--)
				{
					sign[xx][i] = 1;
					if (map[xx][i]) break;
				}
			}
			if (qizi[i].c == 'C')                    //处理出炮的攻击范围
			{
				int xx = qizi[i].x;
				int yy = qizi[i].y;
				int ok;

				ok = 0;
				for (int i = xx + 1; i <= 10; i++)
				{
					if (ok == 1) sign[i][yy] = 1;
					if (map[i][yy]) ok++;
				}

				ok = 0;
				for (int i = xx - 1; i >= 1; i--)
				{
					if (ok == 1) sign[i][yy] = 1;
					if (map[i][yy]) ok++;
				}

				ok = 0;
				for (int i = yy + 1; i <= 9; i++)
				{
					if (ok == 1) sign[xx][i] = 1;
					if (map[xx][i]) ok++;
				}

				ok = 0;
				for (int i = yy - 1; i >= 1; i--)
				{
					if (ok == 1) sign[xx][i] = 1;
					if (map[xx][i]) ok++;
				}
			}
			if (qizi[i].c == 'H')                    //处理出马的攻击范围
			{
				int xx = qizi[i].x;
				int yy = qizi[i].y;
				if (!map[xx - 1][yy])
				{
					if (xx - 2 >= 1 && yy - 1 >= 1) sign[xx - 2][yy - 1] = 1;
					if (xx - 2 >= 1 && yy + 1 <= 9) sign[xx - 2][yy + 1] = 1;
				}
				if (!map[xx + 1][yy])
				{
					if (xx + 2 <= 10 && yy - 1 >= 1) sign[xx + 2][yy - 1] = 1;
					if (xx + 2 <= 10 && yy + 1 <= 9) sign[xx + 2][yy + 1] = 1;
				}
				if (!map[xx][yy - 1])
				{
					if (yy - 2 >= 1 && xx - 1 >= 1) sign[xx - 1][yy - 2] = 1;
					if (yy - 2 >= 1 && xx + 1 <= 10) sign[xx + 1][yy - 2] = 1;
				}
				if (!map[xx][yy + 1])
				{
					if (yy + 2 <= 9 && xx - 1 >= 1) sign[xx - 1][yy + 2] = 1;
					if (yy + 2 <= 9 && xx + 1 <= 10) sign[xx + 1][yy + 2] = 1;
				}
			}

		}

		
		/*for (int i = 1; i <= 10; i++)  //测试
		{
			for (int j = 1; j <= 9; j++)
				cout << sign[i][j] << " ";
			cout << endl;
		}*/
		

		int cnt = 0;                       //判断黑将四个方向是否可以走
		if (bx - 1 >= 1)
		{
			if (sign[bx - 1][by])
				cnt++;
		}
		else cnt++;
		if (bx + 1 <= 3)
		{
			if (sign[bx + 1][by])
				cnt++;
		}
		else cnt++;
		if (by - 1 >= 4)
		{
			if (sign[bx][by - 1])
				cnt++;
		}
		else cnt++;
		if (by + 1 <= 6)
		{
			if (sign[bx][by + 1])
				cnt++;
		}
		else cnt++;

		//cout << cnt << endl;

		if (cnt == 4) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}



UVA - 1589 Xiangqi (模拟)

标签:

原文地址:http://blog.csdn.net/qq_18738333/article/details/44869823

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!