Xiangqi UVa1589(多次WA)

  1 #include<stdio.h>
  2 #include<string.h>
  3 #define maxn 8
  4 //#define LOCAL
  5 #include<iostream>
  6 using namespace std;
  7 int test();
  8 int count(int,int);
  9 int horse_judge(int);
 10 int hobble(int,int);
 11
 12 int G1, G2;   //(G1,G2) of black general.
 13 int N;        //the number of pieces of red.
 14 char a[maxn];  //store the pieces name in red.         # i  to (2*i,2*i+1)
 15 int pos[2*maxn]; //record the point.
 16
 17 //judge YES(1) or NO(0)
 18 int judgement()
 19 {   //if the black and red general in the same line and do not have pieces between them.
 20     //black win.
 21     int index = strchr(a,‘G‘)-a;
 22     if( pos[2*index+1] == G2 && count(2,index) == 0) {return 0;}
 23
 24     //move up, down, left, right to try if the black general can save itself.
 25     //if this move is a life way, the program will return 0;
 26     //else the program will go back to the original position and then try different directions.
 27     if( G1-1 >= 1 ){ G1 -= 1; if(test()) return 0; G1 += 1; }
 28
 29     if( G1+1 <= 3 ){ G1 += 1; if(test()) return 0; G1 -= 1; }
 30
 31     if( G2-1 >= 4 ){ G2 -= 1; if(test()) return 0; G2 += 1; }
 32
 33     if( G2+1 <= 6 ){ G2 += 1; if(test()) return 0; G2 -= 1; }
 34     return 1;
 35 }
 36
 37 //test if the black general will die(4 possibilities).
 38 //when the black general do not die, the program will return 1
 39 //when die, return 0
 40 int test()
 41 {
 42     int n = strlen(a);
 43     for(int i = 0; i < n; i++)
 44     {
 45         //if two generals in the same line, I will count the number of pieces between them.
 46         //if the number is zero, I‘m sorry to say that the black general die.
 47         if( a[i] == ‘G‘ && G2 == pos[2*i+1] )
 48         {
 49             if( count(2,i) == 0 )  return 0;
 50         }
 51
 52         if( a[i] == ‘R‘ && (G1 == pos[2*i] || G2 == pos[2*i+1]) )
 53         {
 54             int flag = 0; //used to judge if they are in the same row or col.
 55             if(G1 == pos[2*i] && G2 == pos[2*i+1])continue;//eat the piece.
 56             if(G1 == pos[2*i]) flag = 1;
 57             if(G2 == pos[2*i+1]) flag = 2;
 58             if( count(flag,i) == 0 ) { return 0;}
 59         }
 60         if( a[i] == ‘C‘ && (G1 == pos[2*i] || G2 == pos[2*i+1]) )
 61         {
 62             int flag = 0;
 63             if(G1 == pos[2*i] && G2 == pos[2*i+1]) continue;
 64             if(G1 == pos[2*i]) flag = 1;
 65             if(G2 == pos[2*i+1]) flag = 2;
 66             if( count(flag,i) == 1 ) { return 0;}
 67
 68         }
 69         if( a[i] == ‘H‘ )
 70         {
 71             if(G1 == pos[2*i] && G2 == pos[2*i+1]) continue;
 72             if(horse_judge(i)){ return 0; } //judge by other functions.
 73         }
 74     }
 75     return 1;//if the black survive after all the test, return 1;
 76 }
 77
 78 //count the number of other pieces between these two pieces.
 79 //1 is row; 2 is col
 80 int count(int flag, int index)
 81 {
 82     int n = strlen(a);
 83     int num = 0;
 84     int t(0);
 85     //count the row.
 86     if(flag == 1)
 87     {
 88         for(int i = 0; i < n; i++ )
 89         {
 90             if(pos[2*i] == G1 )
 91             {
 92                 //on the left.
 93                 if(pos[2*i+1]<G2 && (pos[2*i+1] > pos[2*index+1]))
 94                     num++;
 95                 //on the right.
 96                 if(pos[2*i+1]>G2 && (pos[2*i+1] < pos[2*index+1]))
 97                     num++;
 98             }
 99         }
100         return num;
101     }
102     //count the column.
103     if(flag == 2)
104     {
105         for(int i = 0; i < n; i++)
106         {
107             if(pos[2*i+1] == G2)
108             {
109                 //above the  of general
110                 if(pos[2*i] < G1 && pos[2*i] > pos[2*index])
111                     num++;
112                 //under the general
113                 if(pos[2*i] > G1 && pos[2*i] < pos[2*index])
114                     num++;
115             }
116         }
117         return num;
118     }
119 }
120
121 //judge if the black general will be killed by horse.
122 //return 0 live.
123 //return 1 die.
124 int horse_judge(int index)
125 {
126     //cx,cy are uesed to know the horse‘s direction to the black general.
127     int cx, cy;
128     int x(pos[index*2]), y(pos[index*2+1]); //the point of the horse.
129     if( G1 > x ) cx = -1;
130     else if( G1 < x ) cx = 1;
131     else return 0;
132     if( G2 > y ) cy = -1;
133     else if( G2 < y ) cy = 1;
134     else return 0;
135     if( (G1+ cx*1 == x && G2 + cy*2 == y)|| (G1 + cx*2 == x && G2 + cy*1 == y) )
136     {
137         int point1 = G1+1*cx;
138         int point2 = G2+1*cy;
139         if(hobble(point1, point2) ) return 0;
140         else return 1;
141     }
142     else return 0;
143 }
144
145 int hobble(int x, int y)
146 {
147     for(int i = 0; i < strlen(a); i++)
148     {
149         if( pos[2*i]==x && pos[2*i+1] == y) return 1;
150     }
151     return 0;
152 }
153
154 int main()
155 {
156     #ifdef LOCAL
157     freopen("data.in", "r", stdin);
158     freopen("data.out", "w", stdout);
159     #endif
160
161     while( cin>>N>>G1>>G2 && N != 0)     //!!!!!!!!!!!!!!!!
162     {
163         //initialize two arraies.
164         memset(a,0,sizeof(a));
165         memset(pos,0,sizeof(pos));
166         for(int i = 0; i < N; i++)
167         {
168             cin>>a[i];
169             cin>>pos[2*i]>>pos[2*i+1];
170         }
171         if(judgement())
172             printf("YES\n");
173         else
174             printf("NO\n");
175     }
176     return 0;
177 }

思路分析:

  1. 用数字构建一个虚拟的棋盘,将红方的每一个棋子会攻击到的点都用1表示,而攻击不到的点用0表示,这样黑色的将军在移动的时候就可以通过虚拟棋盘的所对应的位置的值来判断会不会死亡。当然这个方案需要构建多个数组,用的内存比较大。
  2. 每一次移动黑色的将军之后,判断它的生死,这样的话更加方便一些

问题:

  1. 使用scanf("%c",&a[i])的时候会读取回车,如果要使用的话需要用getchar()吃掉回车,或者干脆使用iostream,那个输入输出都不要控制,很方便。
  2. 判断马在将的哪个方位的时候讲cx,cy弄反了,这个问题一方面是疏忽,另一方面是注释写的不清楚。清晰的使用注释,不仅能够让代码更容易阅读,而且便于自己在写代码的时候弄清楚变量的关系。
  3. 几个连续的if,千万要注意会不会有互相干扰的问题。应该在一开始就养成分类的良好习惯,如果应该分类,应该毫不犹豫的分类,而不是想当然的认为没有影响。这样的错误十分十分的难查出来,这让调试得难度极大。

改进:

  希望有一天能够写出令自己惊叹的美丽的程序,只要前进就会离目标越来越近!

原文地址:https://www.cnblogs.com/dreamworldclark/p/9471644.html

时间: 2024-10-27 12:57:47

Xiangqi UVa1589(多次WA)的相关文章

紫书第四章训练 UVA1589 Xiangqi by 15 周泽玺

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

UVA1589 Xiangqi

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

UVa1589 Xiangqi 坑点总结+参考代码

题目大意: 给出一个合乎规则的象棋残局,棋盘上黑方只有将,红方已经“delivered a check”.要求判断红方能否把黑方“checkmate”. 思路: 1. 维护一个矩阵表示棋盘上红方棋子的布局: 2. 维护一个vector存储红方的棋子的位置,以便于后续访问: 3. 模拟黑将走一步的过程,对于每个黑将可能走的位置,判断黑将是否会被吃掉.如果所有能走的位置上黑将都会被吃掉,那么输出YES.如果存在一个位置使黑将逃脱,输出NO. 坑点: 1. 黑将走到下一步时也可以吃掉红方. 2. 虽然

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

UVA 1589 Xiangqi

Z1589 - Xiangqi Time limit: 3.000 seconds 做这题的时候WA了很多次. 解决思路是,枚举黑方将军可以移动的位置,接着判断这些位置是否被红方将军,如果所有位置会被红方吃掉,那么就是checkmate了. 要注意的情况可能就是双炮将军. 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 char board[12][12]; 6 int dr[4] = { 1,

HDU 4121 Xiangqi

模拟吧,算是... 被这个题wa到哭,真是什么都不想说了...上代码 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 struct node { 6 int x,y; 7 char c; 8 }q[10]; 9 10 int main (){ 11 int n,x,y; 12 int kase=0; 13 int map[30][30]; 14 while (cin>>n>

HDU-4461-The Power of Xiangqi

The Power of Xiangqi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1420    Accepted Submission(s): 775 Problem Description Xiangqi is one of the most popular two-player board games in China.

poj 4001 Xiangqi(模拟)

Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1357   Accepted: 347 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 e

hdu 4891---水题 但是WA了很多次

这道题的坑在----过程中即使使用long  long 也会超出范围 自己看了很久读了很多遍题还是没想到 记得当时的想法是----反正我用了long long 已经是最大的范围了,肯定没法用更大的类型--即使unsigned也只是 比long long 大一倍,现在回头看,这种想法太唯心,不是理性分析的结果啊,, 代码: //1005 #include <cstdio> #include <cstring> #include <algorithm> #include &