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;
}