UVA 1589:Xiangqi (模拟 Grade D)
题目:
象棋,黑棋只有将,红棋有帅车马炮。问是否死将。
思路:
对方将四个方向走一步,看看会不会被吃。
代码:
很难看……WA了很多发,还越界等等。
#include <cstdio> #include <cstring> #include <cstdlib> char graph[13][13]; int go[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; bool inBlackPalace(int x, int y) { return 4 <= y && y <= 6 && 1 <= x && x <= 3; } bool inChess(int x, int y) { return 1 <= x && x <= 10 && 1 <= y && y <= 9; } bool goG(int x, int y) { for (int i = x-1; i >= 1; i--) { if (graph[i][y]) { return graph[i][y] == 'B'; } } return false; } bool goR(int x, int y) { for (int i = x-1; i >= 1; i--) { if (graph[i][y]) { if (graph[i][y] == 'B') return true; break; } } for (int i = x+1; i <= 10; i++) { if (graph[i][y]) { if (graph[i][y] == 'B') return true; break; } } for (int j = y-1; j >= 1; j--) { if (graph[x][j]) { if (graph[x][j] == 'B') return true; break; } } for (int j = y+1; j <= 9; j++) { if (graph[x][j]) { if (graph[x][j] == 'B') return true; break; } } return false; } int can_move(int x,int y,int w) { if(w==1) if(x<1||x>3||y<4||y>6) return 0; if(w==2) if(x<1||x>10||y<1||y>9) return 0; return 1; } bool goH(int x, int y) { for (int i = 0; i < 4; i++) { int nx = x + go[i][0]; int ny = y + go[i][1]; if (1 <= nx && nx <= 10 && 1 <= ny && ny <= 9) { if (graph[nx][ny] == 0) { if (inChess(nx+go[i][0]+go[i][1],ny+go[i][1]+go[i][0])) if (graph[nx+go[i][0]+go[i][1]][ny+go[i][1]+go[i][0]] == 'B') return true; if (inChess(nx+go[i][0]-go[i][1],ny+go[i][1]-go[i][0])) if (graph[nx+go[i][0]-go[i][1]][ny+go[i][1]-go[i][0]] == 'B') return true; } } } return false; } bool goC(int x, int y) { bool first = true; for (int i = x-1; i >= 1; i--) { if (graph[i][y]) { if (first) { first = false; } else { if (graph[i][y] == 'B') return true; break; } } } first = true; for (int i = x+1; i <= 10; i++) { if (graph[i][y]) { if (first) { first = false; } else { if (graph[i][y] == 'B') return true; break; } } } first = true; for (int j = y-1; j >= 1; j--) { if (graph[x][j]) { if (first) { first = false; } else { if (graph[x][j] == 'B') return true; break; } } } first = true; for (int j = y+1; j <= 9; j++) { if (graph[x][j]) { if (first) { first = false; } else { if (graph[x][j] == 'B') return true; break; } } } return false; } bool goo(int x, int y) { switch(graph[x][y]) { case 'G': return goG(x,y); case 'R': return goR(x,y); case 'H': return goH(x,y); case 'C': return goC(x,y); } return false; } char ch[200]; int main() { int n; while (scanf("%d", &n) != EOF) { int bx, by; scanf("%d%d", &bx, &by); if (n == 0 && bx == 0 && by == 0) break; memset(graph, 0, sizeof(graph)); for (int i = 0; i < n; i++) { int x, y; scanf("%s%d%d", ch, &x, &y); graph[x][y] = ch[0]; } bool flag = false; for (int way = 0; way < 4; way++) { int nx = bx + go[way][0]; int ny = by + go[way][1]; if (!inBlackPalace(nx, ny)) continue; char tmp = graph[nx][ny]; graph[nx][ny] = 'B'; bool ok = true; for (int i = 1; i <= 10 && ok; i++) { for (int j = 1; j <= 9 && ok; j++) { if (graph[i][j]) { if (goo(i,j)) ok = false; } } } graph[nx][ny] = tmp; if (ok) { flag = true; break; } } puts(flag?"NO":"YES"); } return 0; }
posted on 2014-10-16 21:45 ShineCheng 阅读(1595) 评论(1) 编辑 收藏 举报