UVA

来源:互联网 发布:python 图片相似度 编辑:程序博客网 时间:2024/06/06 11:01

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 4G 10 5R 6 43 1 5H 4 5G 10 5C 7 50 0 0
Sample Output
YESNO          
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.


题意:

就是给出一个象棋残局,问是否绝杀,并且黑方只有一个帅(其他规则与象棋走法一致)


分析:

开始存入将能走到的四个点,判断这四个点是否有未被其他棋子将军到的。

一道细节题,不同的人有不同的错误,一些方面没有考虑,或者细节上就是有问题,当然,也有可能是逻辑上的问题。

这份代码是我WA多次后的,做的心烦,导致自己无法静心。下面错误数据就是导致我错N次的原因。
4 1 4
C 3 4
R 5 4
C 7 4
G 10 5

4 1 4
C 3 4
G 10 4
R 10 5
R 7 5
细节决定成败,一个逻辑错误,导致一晚上的心烦。
HDU上数据与本题并不一致,一开始就飞将,本题要输出NO,而HDU上的4121却不用
(这个情况题目没说明,但根据象棋的走法,应该是输出NO的)


uva-1589

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <time.h>#include <iostream>#include <algorithm>#include <string>#include <vector>#include <map>#include <stack>#include <queue>#include <deque>#include <set>#define ll long long#define eps 1e-6#define pi (acos(-1))#define e exp(1.0)#define cei(n) ((int)ceil((n)))#define rou(n) ((int)round((n)))#define qclear(q) while(!q.empty())q.pop();template<class T> T gcd(T a, T b) { return b ? GCD(b, a%b) : a; }template<class T> T lcm(T a, T b) { return a / GCD(a,b) * b;    }using namespace std;char a[22][22];bool panduan(int x,int y)//判断是否超过中间3*3的格子{if(x<1||x>3||y<4||y>6)return false;return true;}int fx[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};//马的路线int matui[8][2]={1,1,1,-1,-1,1,-1,-1,1,1,1,-1,-1,1,-1,-1};//马脚的位置bool ma(int x,int y)//判断是否能打到这个点{for(int i=0;i<8;i++){if(x+fx[i][0]>=1&&y+fx[i][1]>=1&&x+fx[i][0]<=10&&y+fx[i][1]<=10){if(a[x+fx[i][0]][y+fx[i][1]]=='H'&&a[x+matui[i][0]][y+matui[i][1]]=='.')return true;}}return false;}bool kk(int x,int y)//判断车、将、跑是否能打到这个点{int i=x-1,k=0;for(i=x-1;i>=1;i--){if(((a[i][y]=='R'||a[i][y]=='G')&&k==0)||(a[i][y]=='C'&&k==1)){return true;}else if(a[i][y]!='.') {k++;if(k>=2)break;}}i=x+1,k=0;for(i=x+1;i<=10;i++){if(((a[i][y]=='R'||a[i][y]=='G')&&k==0)||(a[i][y]=='C'&&k==1)){return true;}else if(a[i][y]!='.') {k++;if(k>=2)break;}}i=y+1,k=0;for(i=y+1;i<=10;i++){if(((a[x][i]=='R'||a[x][i]=='G')&&k==0)||(a[x][i]=='C'&&k==1)){return true;}else if(a[x][i]!='.') {k++;if(k>=2)break;}}i=y-1,k=0;for(i=y-1;i>=1;i--){if(((a[x][i]=='R'||a[x][i]=='G')&&k==0)||(a[x][i]=='C'&&k==1)){return true;}else if(a[x][i]!='.') {k++;if(k>=2)break;}}return false;}bool fj(int x,int y)//一开始将是否在面对面{for(int i=x+1;i<=10;i++){if(a[i][y]=='G')return 1;if(a[i][y]!='.')return 0;}}int main(){#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);freopen("out.txt","w",stdout);ll _btime=clock();#endifchar ch[2];int T,x,y,x1,y1;queue< pair <int,int> >q; pair <int,int> t;while(~scanf("%d%d%d",&T,&x,&y),T||x||y){//初始化while(!q.empty())q.pop();x1=x;y1=y;memset(a,'.',sizeof(a));a[x][y]='#';//判断将旁边4个位置if(panduan(x-1,y)){q.push(make_pair(x-1,y));}if(panduan(x+1,y)){q.push(make_pair(x+1,y));}if(panduan(x,y+1)){q.push(make_pair(x,y+1));}if(panduan(x,y-1)){q.push(make_pair(x,y-1));}#ifndef ONLINE_JUDGEfor(int i=0;i<kyz.size();i++){cout<<kyz[i].first<<"*"<<kyz[i].second<<endl;}xintengzhiji,c-free moumingdeguangbi; #endifwhile(T--){scanf("%s%d%d",ch,&x,&y);a[x][y]=ch[0];}#ifndef ONLINE_JUDGEfor(int i=1;i<=10;i++){for(int j=1;j<=10;j++){cout<<a[i][j];}puts("");}#endifif(fj(x1,y1))//一开始飞将的情况{puts("NO");continue;}int llll=0; while(!q.empty()){t=q.front();int xx=t.first;int yy=t.second;if(kk(xx,yy)||ma(xx,yy))//判断这个点是否能走{q.pop();}else{llll=1;break;} }//本来不想有llll的,一直错,就改成这样了if(!llll)puts("YES");else puts("NO");}#ifndef ONLINE_JUDGEll _etime=clock();printf("time = %lld ms",_etime-_btime);#endifreturn 0;}/*2 1 4R 2 2G 10 52 1 5H 3 5C 10 52 1 4G 10 5R 6 43 1 5H 4 5G 10 5C 7 52 1 5R 4 4G 10 53 1 5G 10 4R 5 5H 3 74 1 5G 10 4C 6 5H 5 5R 1 15 1 5G 10 4C 6 5H 5 5H 4 5R 1 13 1 5G 10 4C 2 7H 3 73 1 5G 10 4R 5 5R 1 64 1 5G 10 4R 5 5R 1 6H 3 711 4G 10 43 1 4G 10 4R 10 3R 10 50 0 0*/

HDU-4121

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <time.h>#include <iostream>#include <algorithm>#include <string>#include <vector>#include <map>#include <stack>#include <queue>#include <deque>#include <set>#define ll long long#define eps 1e-6#define pi (acos(-1))#define e exp(1.0)#define cei(n) ((int)ceil((n)))#define rou(n) ((int)round((n)))#define qclear(q) while(!q.empty())q.pop();template<class T> T gcd(T a, T b) { return b ? GCD(b, a%b) : a; }template<class T> T lcm(T a, T b) { return a / GCD(a,b) * b;    }using namespace std;char a[22][22];bool panduan(int x,int y){if(x<1||x>3||y<4||y>6)return false;return true;}int fx[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};int matui[8][2]={1,1,1,-1,-1,1,-1,-1,1,1,1,-1,-1,1,-1,-1};bool ma(int x,int y){for(int i=0;i<8;i++){if(x+fx[i][0]>=1&&y+fx[i][1]>=1&&x+fx[i][0]<=10&&y+fx[i][1]<=10){if(a[x+fx[i][0]][y+fx[i][1]]=='H'&&a[x+matui[i][0]][y+matui[i][1]]=='.')return true;}}return false;}bool kk(int x,int y){int k=0,i;for(i=x+1;i<=10;i++){if(k==0&&(a[i][y]=='G'||a[i][y]=='R'))return 1;if(k==1&&a[i][y]=='C')return 1;if(a[i][y]!='.')k++;if(k>=2)break;}k=0;for(i=x-1;i>=1;i--){if(k==0&&(a[i][y]=='G'||a[i][y]=='R'))return 1;if(k==1&&a[i][y]=='C')return 1;if(a[i][y]!='.')k++;if(k>=2)break;}k=0;for(i=y-1;i>=1;i--){if(k==0&&(a[x][i]=='G'||a[x][i]=='R'))return 1;if(k==1&&a[x][i]=='C')return 1;if(a[x][i]!='.')k++;if(k>=2)break;}k=0;for(i=y+1;i<=10;i++){if(k==0&&(a[x][i]=='G'||a[x][i]=='R'))return 1;if(k==1&&a[x][i]=='C')return 1;if(a[x][i]!='.')k++;if(k>=2)break;}return 0; }int main(){char ch[2];int T,x,y,x1,y1;queue< pair <int,int> >q; pair <int,int> t;while(~scanf("%d%d%d",&T,&x,&y),T||x||y){while(!q.empty())q.pop();x1=x;y1=y;memset(a,'.',sizeof(a));if(panduan(x-1,y)){q.push(make_pair(x-1,y));}if(panduan(x+1,y)){q.push(make_pair(x+1,y));}if(panduan(x,y+1)){q.push(make_pair(x,y+1));}if(panduan(x,y-1)){q.push(make_pair(x,y-1));}while(T--){scanf("%s%d%d",ch,&x,&y);a[x][y]=ch[0];}int llll=0; while(!q.empty()){t=q.front();int xx=t.first;int yy=t.second;if(kk(xx,yy)||ma(xx,yy)){q.pop();}else{llll=1;break;} }if(!llll)puts("YES");else puts("NO");}return 0;}



0 0