Uva 1589 Day 3 - D題:象棋
題目大意:
給一個有黑將、紅帥和若干車跑馬的棋盤,判斷黑棋是否已經被將死了。
解題思路:
- 很明顯的模擬題
- 注意事項:
- 紅帥和黑將初始化的時候對將,黑沒有被將死,應該輸出NO
- 黑將的一下步可以喫子
- 馬的撇腳規則
解題過程:
這道題順着思路寫完還是比較順的,但我debug了五個小時左右。在不少細節的地方犯了錯誤,有些是自己沒有考慮到的細節,如黑將喫子的情況;有些則是猴子纔會犯的錯誤,將賦值的 = 寫成了 ==。
- 學會了對拍的方法去debug,這可以說是寫這道題的一個大收穫。在對拍的過程裏,最後我找的標程和我自己寫的代碼都AC了,但對於極少部分隨機的數據,它們兩者的輸出還是有差別,這說明AC和WA是相對的,hhhhhh~
AC代碼:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#define rep(i,l,p) for(int i=l; i<=p; i++)
#define drep(i,p,l) for(int i=p; i>=l; i--)
using namespace std;
const int dx[5] = {0,-1,0,1,0};
const int dy[5] = {0,0,1,0,-1};
const int hx[9] = {0,-2,-2,-1,1,2,2,1,-1};
const int hy[9] = {0,-1,1,2,2,1,-1,-2,-2};
struct node{
int x,y;
int t;
}a[25];
int n,x,y;
int maps[12][12];
bool xHave(int x1,int x2,int y){
int b = min(x1,x2),e = max(x1,x2);
rep(i,b+1,e - 1){
if ( maps[i][y] == 1 ) return true;
}
return false;
}
bool yHave(int y1,int y2,int x){
int b = min(y1,y2),e = max(y1,y2);
rep(i,b+1,e - 1){
if ( maps[x][i] == 1 ) return true;
}
return false;
}
bool xHaveOne(int x1,int x2,int y){
int res = 0;
int b = min(x1,x2),e = max(x1,x2);
rep(i,b+1,e - 1){
if ( maps[i][y] == 1 ) res++;
}
if ( res == 1) return true;
else return false;
}
bool yHaveOne(int y1,int y2,int x){
int res = 0;
int b = min(y1,y2),e = max(y1,y2);
rep(i,b+1,e - 1){
if ( maps[x][i] == 1 ) res++;
}
if (res == 1) return true;
else return false;
}
void ini(){
rep(i,0,10)
rep(j,0,10)
maps[i][j] = 0;
n = 0; x = 0; y = 0;
}
int main(){
int p = 0;
while ( true ){
ini();
cin >> n >> x >> y;
if ( n == 0) break;
char c;
int flyx,flyy;
rep(i,1,n){
cin >> c >> a[i].x >> a[i].y;
maps[a[i].x][a[i].y] = 1;
if (c == 'G') {
a[i].t = 10;
flyx = a[i].x;
flyy = a[i].y;
}else if ( c == 'R' ){
a[i].t = 1;
}else if ( c == 'H' ){
a[i].t = 2;
}else if ( c == 'C' ){
a[i].t = 3;
}
}
if ( y == flyy && !xHave(x,flyx,y) ){
printf("NO\n");
continue;
}
int nx,ny;
int flag[5] = {0,1,1,1,1};
rep(i,1,4){
nx = x + dx[i],ny = y + dy[i];
if ( nx <1 || nx >3 || ny <4 || ny >6 ) {
flag[i] = 0;
continue;
}
rep(j,1,n){
if ( a[j].x == nx && a[j].y == ny) continue;
if ( a[j].t == 10){
if ( ny == a[j].y && !xHave(nx,a[j].x,ny)){
flag[i] = 0;
break;
}
}else if ( a[j].t ==1 ){
if ( nx == a[j].x && !yHave(ny,a[j].y,nx) || ny == a[j].y && !xHave(nx,a[j].x,ny)){
flag[i] = 0;
break;
}
}else if ( a[j].t == 3){
if ( a[j].x == nx && yHaveOne(ny,a[j].y,nx) ){
flag[i] = 0;
break;
}else if ( a[j].y == ny && xHaveOne(nx,a[j].x,ny)){
flag[i] = 0;
break;
}
} else if ( a[j].t == 2){
rep(k,1,2){
if (a[j].x - 1 < 1 ) break;
if ( maps[a[j].x - 1][a[j].y] == 1 ) break;
int tx = a[j].x + hx[k];
int ty = a[j].y + hy[k];
if ( tx == nx && ty == ny ){
flag[i] = 0;
break;
}
}
if ( flag[i] == 0 ) break;
rep(k,3,4){
if ( maps[a[j].x][a[j].y + 1] == 1 ) break;
int tx = a[j].x + hx[k];
int ty = a[j].y + hy[k];
if ( tx == nx && ty == ny ){
flag[i] = 0;
break;
}
}
if ( flag[i] == 0 ) break;
rep(k,5,6){
if ( maps[a[j].x + 1][a[j].y] == 1 ) break;
int tx = a[j].x + hx[k];
int ty = a[j].y + hy[k];
if ( tx == nx && ty == ny ){
flag[i] = 0;
break;
}
}
if ( flag[i] == 0 ) break;
rep(k,7,8){
if (a[j].y - 1 < 1) break;
if ( maps[a[j].x][a[j].y - 1] == 1 ) break;
int tx = a[j].x + hx[k];
int ty = a[j].y + hy[k];
if ( tx == nx && ty == ny ){
flag[i] = 0;
break;
}
}
if ( flag[i] == 0 ) break;
}
}
}
int res = 0;
rep(j,1,4) {
res += flag[j];
}
if ( res > 0) {
printf("NO\n");
}
else printf("YES\n");
}
return 0;
}