1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cstdlib> 6 #define repu(i,a,b) for(int i=a;i<b;i++) 7 #define mem(a) memset(a,0,sizeof(a)) 8 using namespace std; 9 char graph[12][12],ch[3]; 10 int nx,ny; 11 int go[8][2] = {{1,0},{0,1},{-1,0},{0,-1}}; 12 int inBlackPalace(int x,int y) 13 { 14 if(x>=1&&x<=3&&y>=4&&y<=6) 15 return 1; 16 return 0; 17 } 18 int goG(int x,int y) 19 { 20 int a,b; 21 if(y==ny) 22 { 23 if(x>nx) 24 a=nx,b=x; 25 else 26 a=x,b=nx; 27 repu(i,a+1,b) 28 if(graph[i][y])///帅与将之间有间隔 29 return 0; 30 return 1;///被帅将军 31 } 32 return 0; 33 } 34 int goH(int x,int y)///蹩马腿 35 { 36 ///与马能构成“日”字 37 if(abs(nx-x)==2&&abs(ny-y)==1) 38 { 39 if(graph[(nx+x)/2][y]) 40 return 0; 41 return 2;///被马将军 42 } 43 if(abs(ny-y)==2&&abs(nx-x)==1) 44 { 45 if(graph[x][(ny+y)/2]) 46 return 0; 47 return 2;///被马将军 48 } 49 return 0; 50 } 51 int goR(int x,int y) 52 { 53 int a,b; 54 if(x==nx) 55 { 56 if(y>ny) 57 a=ny,b=y; 58 else 59 a=y,b=ny; 60 repu(i,a+1,b) 61 if(graph[x][i]) 62 { 63 //printf("%c\n",graph[x][i]); 64 return 0; 65 } 66 return 3;///被车将军 67 } 68 if(y==ny) 69 { 70 if(x>nx) 71 a=nx,b=x; 72 else 73 a=x,b=nx; 74 repu(i,a+1,b) 75 if(graph[i][y]) 76 return 0; 77 return 3;///被车将军 78 } 79 return 0; 80 } 81 int goC(int x,int y) 82 { 83 int sum = 0,a,b; 84 if(x==nx) 85 { 86 if(y>ny) 87 a=ny,b=y; 88 else 89 a=y,b=ny; 90 repu(i,a+1,b) 91 { 92 if(graph[x][i]) 93 sum++; 94 } 95 if(sum==1) 96 return 4; 97 } 98 if(y==ny) 99 { 100 if(x>nx) 101 a=nx,b=x; 102 else 103 a=x,b=nx; 104 repu(i,a+1,b) 105 { 106 if(graph[i][y]) 107 sum++; 108 } 109 if(sum==1) 110 return 4; 111 } 112 return 0; 113 } 114 int goo(int x, int y) 115 { 116 switch(graph[x][y]) 117 { 118 case 'G': 119 return goG(x,y); 120 case 'R': 121 return goR(x,y); 122 case 'H': 123 return goH(x,y); 124 case 'C': 125 return goC(x,y); 126 } 127 return false; 128 } 129 int main() 130 { 131 int n; 132 while (scanf("%d", &n) != EOF) 133 { 134 int bx, by,temp = false,o = 0; 135 scanf("%d%d", &bx, &by); 136 if (n == 0 && bx == 0 && by == 0) break; 137 138 memset(graph, 0, sizeof(graph)); 139 140 for (int i = 0; i < n; i++) 141 { 142 int x, y; 143 scanf("%s%d%d", ch, &x, &y); 144 graph[x][y] = ch[0]; 145 } 146 for (int way = 0; way < 4; way++) 147 { 148 nx = bx + go[way][0]; 149 ny = by + go[way][1]; 150 if (!inBlackPalace(nx, ny)) 151 continue; 152 char tep = graph[nx][ny]; 153 graph[nx][ny] = 0; 154 int ok = 1; 155 for (int i = 1; i <= 10; i++) 156 { 157 for (int j = 1; j <= 9; j++) 158 { 159 if(graph[i][j]) 160 { 161 if(goo(i,j))///将军成功了,再换条路 162 { 163 ok =0; 164 break; 165 } 166 } 167 } 168 if(ok==0) 169 break; 170 } 171 graph[nx][ny] = tep; 172 if(ok)///此路可以不被将军 173 { 174 temp = true; 175 break; 176 } 177 } 178 if(temp) 179 puts("NO"); 180 else 181 puts("YES"); 182 } 183 return 0; 184 }
自己改了10多遍才交过的题目啊。。。。。。。。