uva1589(象棋)。

这个题昨天晚上做到一点半还是WA,早上起来后认真思考了一下终于AC....

细节比较多,我第一次AC的代码写了350多行,我考虑了黑将能吃子的情况,

我把那种情况去掉后也可以AC,应该没有很BT的数据。

思路就是弄两个二维数组,一个模拟红棋可以杀的范围,另一个模拟黑将到达的位置,再判断是否处于红棋击杀范围之内就好了。

第一次AC的代码太长,下面是第二次AC的代码(把黑将能吃子的情况去掉了)。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <algorithm>
  5 #include <cstring>
  6 
  7 using namespace std;
  8 int a[11][10];
  9 int Map[11][10];//如果不考虑黑将吃子的话这个用不到。
 10 int book[11][10];
 11 int p,q;
 12 struct chess
 13 {
 14     char ch;
 15     int r,c;
 16 };
 17 struct chess che[101];
 18 
 19 int judge(int r,int c)
 20 {
 21     int flag = 0,i,j;
 22     int next[4][2] = { {1,0},{-1,0},{0,-1},{0,1} };
 23     int nextx,nexty;
 24     for(i=0; i<4; i++)
 25     {
 26         nextx = r+next[i][0];
 27         nexty = c+next[i][1];
 28         if(nextx>3||nextx<1||nexty>6||nexty<4)
 29             continue;
 30         if(a[nextx][nexty]==0)
 31             flag = 1;
 32     }
 33     if(flag)
 34         return 0;
 35     else
 36         return 1;
 37 }
 38 void kill(char ch,int r, int c)
 39 {
 40     int i,j;
 41     if(ch == 'R')
 42     {
 43         for(i=r-1; i>0; i--)
 44         {
 45             if(book[i][c])
 46             {
 47                 a[i][c]++;
 48                 break;
 49             }
 50             else
 51                 a[i][c]++;
 52         }
 53         for(i=r+1; i<=10; i++)
 54         {
 55             if(book[i][c])
 56             {
 57                 a[i][c]++;
 58                 break;
 59             }
 60             else
 61                 a[i][c]++;
 62         }
 63         for(i=c+1; i<=10; i++)
 64         {
 65             if(book[r][i])
 66             {
 67                 a[r][i]++;
 68                 break;
 69             }
 70             else
 71                 a[r][i]++;
 72         }
 73         for(i=c-1; i>0; i--)
 74         {
 75             if(book[r][i])
 76             {
 77                 a[r][i]++;
 78                 break;
 79             }
 80             else
 81                 a[r][i]++;
 82         }
 83     }
 84     else if(ch == 'H')
 85     {
 86         int next[4][2] = { {1,0},{-1,0},{0,-1},{0,1} };
 87         for(i=0; i<4; i++)
 88         {
 89             if(i<=1)
 90             {
 91                 if(r+2*next[i][0]<1||r+2*next[i][0]>10||r+next[i][0]<1||r+next[i][0]>10
 92                         ||c+next[i][1]+1<1||c+next[i][1]+1>9||c+next[i][1]<1||c+next[i][1]>9)
 93                     continue;
 94                 if(!book[r+next[i][0]][c+next[i][1]])
 95                 {
 96                     a[r+2*next[i][0]][c+next[i][1]+1]++;
 97                     a[r+2*next[i][0]][c+next[i][1]-1]++;
 98                 }
 99             }
100             else
101             {
102                 if(r+next[i][0]<1||r+next[i][0]>10||r+next[i][0]+1<1||r+next[i][0]+1>10
103                         ||c+next[i][1]<1||c+next[i][1]>9||c+2*next[i][1]<1||c+2*next[i][1]>9)
104                     continue;
105                 if(!book[r+next[i][0]][c+next[i][1]])
106                 {
107                     a[r+next[i][0]+1][c+2*next[i][1]]++;
108                     a[r+next[i][0]-1][c+2*next[i][1]]++;
109                 }
110             }
111         }
112     }
113     else if(ch == 'C')
114     {
115         int flag = 0;
116         for(i=r-1; i>=1; i--)
117         {
118             if(flag==2)break;
119             if(flag ==1)
120             {
121                 a[i][c]++;
122             }
123             if(book[i][c])
124                 flag++;
125         }
126         flag = 0;
127         for(i=r+1; i<=10; i++)
128         {
129             if(flag==2)break;
130             if(flag ==1)
131             {
132                 a[i][c]++;
133             }
134             if(book[i][c])
135                 flag++;
136         }
137         flag = 0;
138         for(i=c+1; i<=10; i++)
139         {
140             if(flag==2)break;
141             if(flag ==1)
142             {
143                 a[r][i]++;
144             }
145             if(book[r][i])
146                 flag++;
147         }
148         flag = 0;
149         for(i=c-1; i>0; i--)
150         {
151             if(flag==2)break;
152             if(flag ==1)
153             {
154                 a[r][i]++;
155             }
156             if(book[r][i])
157                 flag++;
158         }
159     }
160     else if(ch == 'G')
161     {
162         int flag = 0;
163         for(i=r-1; i>p; i--)
164         {
165             if(book[i][c])
166                 flag = 1;
167         }
168         if(!flag)
169         {
170             for(i=p; i<=3; i++)
171                 a[i][c]++;
172         }
173     }
174 }
175 int main()
176 {
177     int n,i,j,k;
178     while(cin >> n >> p >> q && n && p && q)
179     {
180         for(i=0; i<n; i++)
181         {
182             cin >> che[i].ch >> che[i].r >> che[i].c;
183             book[che[i].r][che[i].c] = 1;
184         }
185         for(i=0; i<n; i++)
186             kill(che[i].ch,che[i].r,che[i].c);
187         if(judge(p,q))
188             cout << "YES" << endl;
189         else
190             cout << "NO" << endl;
191         memset(a,0,sizeof(a));
192         memset(book,0,sizeof(book));
193     }
194     return 0;
195 }

 

posted @ 2017-02-01 13:08  萧萧Hsiao  阅读(1083)  评论(0编辑  收藏  举报