Uva 1589 Xiangqi 判斷是否將死
阿新 • • 發佈:2019-01-30
UVa 1589 Xiangqi
簡單分析
- 題目是讓我們檢查紅方是不是把黑方將死
- 首先要考慮黑方是不是已經和紅方見面,如果見面了,黑方直接勝利
- 接下來就要考慮每個紅子可以走的地方了,其中要注意黑方將可能會吃掉紅子,所以我用了兩個二維陣列,一個儲存之前的狀態,一個儲存黑方將移動後的狀態。
- 對移動後的棋盤進行標記(為了標記方便,黑方將不會顯示在棋盤,因為只有一個棋子了)將紅方棋子能吃的位置全部標記成特定字元,我的是‘D’
- 另外,帥可以當作在黑方家裡移動的車處理
程式碼
WA了多發終於過了。。。 程式碼有點醜
#include<iostream>
#include<cstring>
using namespace std;
char map0[11][10];
char map[11][10];
int gx, gy, rgx, rgy;
int n, x, y;
char c;
//用於複製之前的狀態到標記棋盤
void copy() {
for (int i = 1; i < 11; i++) {
for (int j = 1; j < 10; j++) {
map[i][j] = map0[i][j];
}
}
}
//標記棋盤
void eat(char c, int x, int y) {
switch (c) {
//將
case'G': {
int endx = 0;
for (int i = x - 1; i > 0; i--) {
if (map[i][y] != 'D' && map[i][y] != 'A') {
endx = i;
break;
}
}
for (int i = 3; i > endx; i--) {
if (map[i][y] == 'A') {
map[i][y] = 'D';
}
}
break;
}
//車
case'R':{
int endx = 0;
int endy = 0;
//up
for (int i = x - 1; i >= 1; i--) {
if (map[i][y] != 'D' && map[i][y] != 'A') {
endx = i;
break;
}
}
for (int i = endx + 1; i < x; i++) {
if (map[i][y] == 'A') {
map[i][y] = 'D';
}
}
//down
endx = 11;
for (int i = x + 1; i < 11; i++) {
if (map[i][y] != 'D' && map[i][y] != 'A') {
endx = i;
break;
}
}
for (int i = x + 1; i < endx; i++) {
if (map[i][y] == 'A') {
map[i][y] = 'D';
}
}
//left
for (int i = y - 1; i > 0; i--) {
if (map[x][i] != 'D' && map[x][i] != 'A') {
endy = i;
break;
}
}
for (int i = endy + 1; i < y; i++) {
if (map[x][i] == 'A') {
map[x][i] = 'D';
}
}
//right
endy = 10;
for (int i = y + 1; i < 10; i++) {
if (map[x][i] != 'D' && map[x][i] != 'A') {
endy = i;
break;
}
}
for (int i = y + 1; i < endy; i++) {
if (map[x][i] == 'A') {
map[x][i] = 'D';
}
}
break;
}
//炮
case'C': {
int cnt = 0;
int startx, starty, endx = 0, endy = 0;
//up
for (int i = x - 1; i > 0; i--) {
if ((map[i][y] != 'A' && map[i][y] != 'D')) {
cnt++;
if (cnt == 1) {
startx = i;
}
if (cnt == 2) {
endx = i;
break;
}
}
}
if (cnt) {
for (int i = endx + 1; i < startx; i++) {
if (map[i][y] == 'A') {
map[i][y] = 'D';
}
}
}
//down
cnt = 0;
endx = 11;
for (int i = x + 1; i < 11; i++) {
if ((map[i][y] != 'A' && map[i][y] != 'D')) {
cnt++;
if (cnt == 1) {
startx = i;
}
if (cnt == 2) {
endx = i;
break;
}
}
}
if (cnt) {
for (int i = startx + 1; i < endx; i++) {
if (map[i][y] == 'A') {
map[i][y] = 'D';
}
}
}
//left
cnt = 0;
for (int i = y - 1; i >= 1; i--) {
if (map[x][i] != 'A' && map[x][i] != 'D') {
cnt++;
if (cnt == 1) {
starty = i;
}
if (cnt == 2) {
endy = i;
break;
}
}
}
if (cnt) {
for (int i = endy + 1; i < starty; i++) {
if (map[x][i] == 'A') {
map[x][i] = 'D';
}
}
}
//right
cnt = 0;
endy = 10;
for (int i = y + 1; i < 10; i++) {
if (map[x][i] != 'A' && map[x][i] != 'D') {
cnt++;
if (cnt == 1) {
starty = i;
}
if (cnt == 2) {
endy = i;
break;
}
}
}
if (cnt) {
for (int i = starty + 1; i < endy; i++) {
if (map[x][i] == 'A') {
map[x][i] = 'D';
}
}
}
break;
}
//馬
case 'H': {
//up
if (x - 2 > 0 && (map[x - 1][y] == 'D' || map[x - 1][y] == 'A')) {
if (y - 1 > 0 && map[x - 2][y - 1] == 'A') {
map[x - 2][y - 1] = 'D';
}
if (y + 1 < 10 && map[x - 2][y + 1] == 'A') {
map[x - 2][y + 1] = 'D';
}
}
//down
if (x + 2 < 11 && (map[x + 1][y] == 'D' || map[x + 1][y] == 'A')) {
if (y - 1 > 0 && map[x + 2][y - 1] == 'A') {
map[x + 2][y - 1] = 'D';
}
if (y + 1 < 10 && map[x + 2][y + 1] == 'A') {
map[x + 2][y + 1] = 'D';
}
}
//left
if (y - 2 > 0 && (map[x][y - 1] == 'D' || map[x][y - 1] == 'A')) {
if (x - 1 > 0 && map[x - 1][y - 2] == 'A') {
map[x - 1][y - 2] = 'D';
}
if (x + 1 < 11 && map[x + 1][y - 2] == 'A') {
map[x + 1][y - 2] = 'D';
}
}
//right
if (y + 2 > 0 && (map[x][y + 1] == 'D' || map[x][y + 1] == 'A')) {
if (x - 1 > 0 && map[x - 1][y + 2] == 'A') {
map[x - 1][y + 2] = 'D';
}
if (x + 1 < 11 && map[x + 1][y + 2] == 'A') {
map[x + 1][y + 2] = 'D';
}
}
break;
}
default:
break;
}
}
void deal() {
for (int i = 1; i < 11; i++) {
for (int j = 1; j < 10; j++) {
eat(map[i][j], i, j);
}
}
}
bool canMove(int x, int y) {
return x >= 1 && x <= 3 && y >= 4 && y <= 6;
}
//將的移動
int dx[] = { 1, -1, 0, 0 };
int dy[] = { 0, 0, 1, -1 };
bool isDead() {
for (int i = 0; i < 4; i++) {
if (canMove(gx + dx[i], gy + dy[i])) {
copy();
//可能吃掉了紅方一個棋子
map[gx + dx[i]][gy + dy[i]] = 'A';
deal();
if (map[gx + dx[i]][gy + dy[i]] != 'D')
return false;
}
}
return true;
}
int main() {
while (cin >> n >> gx >> gy && n) {
memset(map0, 'A', sizeof(map0));
for (int i = 0; i < n; i++) {
cin >> c >> x >> y;
map0[x][y] = c;
if (x == 'G') {
rgx = x;
rgy = y;
}
}
//將帥已經見面
bool isMet = false;
if (rgy == gy) {
for (int i = rgx - 1; i <= gx; i++) {
if (map0[i][rgy] != 'A') {
isMet = map0[i][rgy] == 'G';
}
}
}
if (isMet) {
cout << "NO" << endl;
} else {
deal();
if (isDead()) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}
}