HDU-4121 Xiangqi 模拟

深度解析信息技术领域关键知识点
本文深入探讨了信息技术领域的核心内容,包括前端开发、后端开发、移动开发、游戏开发等细分技术领域的关键知识点,从大数据开发到嵌入式硬件,从音视频基础到AI音视频处理,全面覆盖了开发工具、测试、基础运维、DevOps等多个方面。

原题链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4121


Xiangqi

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4073    Accepted Submission(s): 981


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
Hint
In the first situation, the black general is checked by chariot and “flying general”. In the second situation, the black general can move to (1, 4) or (1, 6) to stop check. See the figure above.
 

Source
 


题意: 给出此时的棋盘,已知黑方只剩将,红方有将G,以及车R,炮C,马H,又已知此时红方已对黑方将军,判断黑方是否有可行方案摆脱死局。

思路:一次判断黑将可以走的格子,再判断每一个红棋是否可以吃掉,都不能吃掉说明方案可行。

几点注意:

①输入要用cin,用scanf容易WA。

②将只能走上下左右,不要被田字格斜线迷惑。

③黑将移动时可能将对方的旗子吃掉。

④双方将不可以直接对面。

被WA哭的一道题,从前一天晚上WA到第二天,太不仔细了。。。

代码:

#include "stdio.h"
#include "string.h"
#include "iostream"
using namespace std;
char map[11][10],r;
int n,bi,bj,ri[8],rj[8],ch1[4]={1,0,0,-1},ch2[4]={0,1,-1,0},a,flag,b;

int jude_G(int ii,int jj)
{
	if(jj!=b)
		return 1;
	for(int i=ii-1;i>a;i--)
	{
		if(map[i][jj]!='0')
			return 1;
	}
	return 0;
}

int jude_R(int ii,int jj)
{
	if(ii==a)
	{
		int xmin=min(jj,b),xmax=max(jj,b);
		for(int i=xmin+1;i<xmax;i++)
		{
			if(map[ii][i]!='0')
				return 1;
		}
		return 0;
	}
	else if(jj==b)
	{
		int xmin=min(ii,a),xmax=max(ii,a);
		for(int i=xmin+1;i<xmax;i++)
		{
			if(map[i][jj]!='0')
				return 1;
		}
		return 0;
	}
	else
		return 1;
}

int jude_C(int ii,int jj)
{
	int nmid=0;
	if(ii==a)
	{
		int xmin=min(jj,b),xmax=max(jj,b);
		for(int i=xmin+1;i<xmax;i++)
		{
			if(map[ii][i]!='0')
				nmid++;
		}
		if(nmid!=1)
			return 1;
		return 0;
	}
	else if(jj==b)
	{
		int xmin=min(ii,a),xmax=max(ii,a);
		for(int i=xmin+1;i<xmax;i++)
		{
			if(map[i][jj]!='0')
				nmid++;
		}
		if(nmid!=1)
			return 1;
		return 0;
	}
	else
		return 1;
}

int jude_H(int ii,int jj)
{
	int ch3[8]={2,2,1,-1,-2,-2,-1,1},ch4[8]={-1,1,2,2,1,-1,-2,-2},ch5[8]={1,1,0,0,-1,-1,0,0},ch6[8]={0,0,1,1,0,0,-1,-1};
	for(int i=0;i<8;i++)
	{
		if(a==ii+ch3[i]&&b==jj+ch4[i]&&map[ii+ch5[i]][jj+ch6[i]]=='0')
			return 0;
	}
	return 1;
}

int jude()
{
	for(int i=1;i<=n;i++)
	{
		if(ri[i]==a&&rj[i]==b)
			continue;
		if(map[ri[i]][rj[i]]=='G'&&jude_G(ri[i],rj[i])==0)
		{
			return 0;
		}
		else if(map[ri[i]][rj[i]]=='R'&&jude_R(ri[i],rj[i])==0)
		{
			return 0;
		}
		else if(map[ri[i]][rj[i]]=='H'&&jude_H(ri[i],rj[i])==0)
		{
			return 0;
		}
		else if(map[ri[i]][rj[i]]=='C'&&jude_C(ri[i],rj[i])==0)
		{
			return 0;
		}
	}
	return 1;
}

int main()
{
	while(cin>>n>>bi>>bj&&n)
	{
		flag=0;
		for(int i=0;i<11;i++)
            for(int j=0;j<10;j++)
                map[i][j]='0' ;
		for(int i=1;i<=n;i++)
		{
			cin>>r>>ri[i]>>rj[i];
			map[ri[i]][rj[i]]=r;
		}
		a=bi;
		b=bj;
		for(int i=1;i<=n;i++)
		{
			if(map[ri[i]][rj[i]]=='G'&&jude_G(ri[i],rj[i])==0)
			{
				flag=1;
				break;
			}
		}
		if(flag==0)
		{
			for(int i=0;i<4;i++)
			{
				if(bi+ch1[i]>0&&bi+ch1[i]<4&&bj+ch2[i]>3&&bj+ch2[i]<7)
				{
					a=bi+ch1[i];
					b=bj+ch2[i];
					if(jude())
					{
						flag=1;
						break;
					}
				}
			}
		}
		if(flag==0)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}

### 关于HDU - 6609 的题目解析 由于当前未提供具体关于 HDU - 6609 题目的详细描述,以下是基于一般算法竞赛题型可能涉及的内容进行推测和解答。 #### 可能的题目背景 假设该题目属于动态规划类问题(类似于多重背包问题),其核心在于优化资源分配或路径选择。此类问题通常会给出一组物品及其属性(如重量、价值等)以及约束条件(如容量限制)。目标是最优地选取某些物品使得满足特定的目标函数[^2]。 #### 动态转移方程设计 如果此题确实是一个变种的背包问题,则可以采用如下状态定义方法: 设 `dp[i][j]` 表示前 i 种物品,在某种条件下达到 j 值时的最大收益或者最小代价。对于每一种新加入考虑范围内的物体 k ,更新规则可能是这样的形式: ```python for i in range(n): for s in range(V, w[k]-1, -1): dp[s] = max(dp[s], dp[s-w[k]] + v[k]) ``` 这里需要注意边界情况处理以及初始化设置合理值来保证计算准确性。 另外还有一种可能性就是它涉及到组合数学方面知识或者是图论最短路等相关知识点。如果是后者的话那么就需要构建相应的邻接表表示图形结构并通过Dijkstra/Bellman-Ford/Floyd-Warshall等经典算法求解两点间距离等问题了[^4]。 最后按照输出格式要求打印结果字符串"Case #X: Y"[^3]。 #### 示例代码片段 下面展示了一个简单的伪代码框架用于解决上述提到类型的DP问题: ```python def solve(): t=int(input()) res=[] cas=1 while(t>0): n,k=list(map(int,input().split())) # Initialize your data structures here ans=find_min_unhappiness() # Implement function find_min_unhappiness() res.append(f'Case #{cas}: {round(ans)}') cas+=1 t-=1 print("\n".join(res)) solve() ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值