題目:?http://acm.hdu.edu.cn/showproblem.php?pid=4121
首先對標題贊一個,非要叫 “Xiangqi” 而不是 ”中國象棋“ 或者 ”Chinese chess“ 。。
然后是題意:黑棋只剩下一個”將“了,紅棋各種 ”車” “馬” “炮“,判斷黑棋是不是死棋了。。。
對于一個會下中國象棋的選手簡直簡單啊,第一次手賤加腦殘WA一次,稍微一改就0ms AC了,模擬題沒什么可以講的,代碼中有詳細注釋。


1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 5 char palace[11][10]; //棋盤 6 7 //判斷(x, y)點有沒有棋子 8 char has_chess(int x, int y) 9 { 10 if(x < 1 || x > 10 || y < 1 || y > 9) 11 return 0; 12 return palace[x][y]; 13 } 14 15 //判斷(x, y)點是不是棋子c 16 bool is_chess(int x, int y, char c) 17 { 18 return has_chess(x, y) == c; 19 } 20 21 //判斷(x, y)點的左邊直線上有沒有棋子c,有的話返回橫坐標 22 int has_left(int x, int y, char c) 23 { 24 for(int i = 1; i < y; i++) 25 { 26 if(palace[x][i] == c) 27 return i; 28 } 29 return 0; 30 } 31 32 //判斷(x, y)點的右邊直線上有沒有棋子c,有的話返回橫坐標 33 int has_right(int x, int y, char c) 34 { 35 for(int i = 9; i > y; i--) 36 { 37 if(palace[x][i] == c) 38 return i; 39 } 40 return 0; 41 } 42 43 //判斷(x, y)點的上邊直線上有沒有棋子c,有的話返回縱坐標 44 int has_up(int x, int y, char c) 45 { 46 for(int i = 1; i < x; i++) 47 { 48 if(palace[i][y] == c) 49 return i; 50 } 51 return 0; 52 } 53 54 //判斷(x, y)點的下邊直線上有沒有棋子c,有的話返回縱坐標 55 int has_down(int x, int y, char c) 56 { 57 for(int i = 10; i > x; i--) 58 { 59 if(palace[i][y] == c) 60 return i; 61 } 62 return 0; 63 } 64 65 //判斷(x1, y)到(x2, y)之間棋子的數量 66 int cnt_chess_x(int x1, int x2, int y) 67 { 68 if(x1 > x2) 69 std::swap(x1, x2); 70 int cnt = 0; 71 for(x1++; x1 < x2; x1++) 72 { 73 if(palace[x1][y]) 74 cnt++; 75 } 76 return cnt; 77 } 78 79 //判斷(x, y1)到(x, y2)之間棋子的數量 80 int cnt_chess_y(int y1, int y2, int x) 81 { 82 if(y1 > y2) 83 std::swap(y1, y2); 84 int cnt = 0; 85 for(y1++; y1 < y2; y1++) 86 { 87 if(palace[x][y1]) 88 cnt++; 89 } 90 return cnt; 91 } 92 93 //判斷能否被“帥”吃掉 94 bool general(int x, int y) 95 { 96 int gx, gy; 97 for(int i = 8; i <= 10; i++) 98 for(int j = 4; j <= 6; j++) 99 if(palace[i][j] == 'G') 100 { 101 gx = i; 102 gy = j; 103 } 104 if(gy != y)return 0; 105 return cnt_chess_x(gx, x, y) == 0; 106 } 107 108 //判斷能否被“車”吃掉 109 bool chariot(int x, int y) 110 { 111 int tmp = has_left(x, y, 'R'); 112 if(tmp != 0 && cnt_chess_y(tmp, y, x) == 0) 113 return 1; 114 tmp = has_right(x, y, 'R'); 115 if(tmp != 0 && cnt_chess_y(tmp, y, x) == 0) 116 return 1; 117 tmp = has_up(x, y, 'R'); 118 if(tmp != 0 && cnt_chess_x(tmp, x, y) == 0) 119 return 1; 120 tmp = has_down(x, y, 'R'); 121 if(tmp != 0 && cnt_chess_x(tmp, x, y) == 0) 122 return 1; 123 return 0; 124 } 125 126 //判斷能否被“馬”吃掉 127 bool horse(int x, int y) 128 { 129 return(is_chess(x-2, y-1, 'H') && !has_chess(x-1, y-1) 130 || is_chess(x-2, y+1, 'H') && !has_chess(x-1, y+1) 131 || is_chess(x+2, y-1, 'H') && !has_chess(x+1, y-1) 132 || is_chess(x+2, y+1, 'H') && !has_chess(x+1, y+1) 133 || is_chess(x-1, y-2, 'H') && !has_chess(x-1, y-1) 134 || is_chess(x-1, y+2, 'H') && !has_chess(x-1, y+1) 135 || is_chess(x+1, y-2, 'H') && !has_chess(x+1, y-1) 136 || is_chess(x+1, y+2, 'H') && !has_chess(x-1, y+1)); 137 } 138 139 //判斷能否被“炮”吃掉 140 bool cannon(int x, int y) 141 { 142 int tmp = has_left(x, y, 'C'); 143 if(tmp != 0 && cnt_chess_y(tmp, y, x) == 1) 144 return 1; 145 tmp = has_right(x, y, 'C'); 146 if(tmp != 0 && cnt_chess_y(tmp, y, x) == 1) 147 return 1; 148 tmp = has_up(x, y, 'C'); 149 if(tmp != 0 && cnt_chess_x(tmp, x, y) == 1) 150 return 1; 151 tmp = has_down(x, y, 'C'); 152 if(tmp != 0 && cnt_chess_x(tmp, x, y) == 1) 153 return 1; 154 return 0; 155 } 156 157 //判斷是不是死棋 158 bool is_die(int x, int y) 159 { 160 if(x < 1 || x > 3 || y < 4 || y > 6) 161 return 1; 162 return (general(x, y) || chariot(x, y) || horse(x, y) || cannon(x, y)); 163 } 164 165 int main() 166 { 167 int n, xt, yt; 168 int x, y; 169 char s[2]; 170 while(scanf("%d %d %d", &n, &xt, &yt) != EOF && (n || xt || yt)) 171 { 172 memset(palace, 0, sizeof(palace)); 173 for(int i = 0; i < n; i++) 174 { 175 scanf("%s %d %d", s, &x, &y); 176 palace[x][y] = s[0]; 177 } 178 179 //如果“將”向周圍走一步不死或者在原地不死,就說明不是死棋 180 //(話說不知道該不該判斷原地,因為我沒讀懂題,不知道輪到誰走棋了。。。) 181 if(!is_die(xt, yt) || !is_die(xt+1, yt) || !is_die(xt-1, yt) || 182 !is_die(xt, yt+1) || !is_die(xt, yt-1)) 183 printf("NO\n"); 184 else 185 printf("YES\n"); 186 } 187 return 0; 188 }
?