传送门:UVA-1589
黑棋可能直接吃掉红棋
这题早该AC的,一个bug卡了好久。。。一直以为if后面如果只有一句(循环也算?)就不用加花括号,但是我代码里写的judge函数的if else里都是循环,不加花括号就一直wa,加了就过。。。有大佬能解释下吗
AC代码:
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
vector<pair<int,int> > g,r,h,c;//g表示将,r表示车,h表示马,c表示炮
int table[20][20],vis[20][20];//table表示该位置是否有棋子,vis表示该位置棋子是否可用
bool judge(int fix,int p,int q,int dire){//dire为0时表示同一行,1时表示同一列,中间没有棋子返回真
if(dire) {for(int i=min(p,q)+1;i<max(p,q);++i) if(table[i][fix]) return false;}
else {for(int i=min(p,q)+1;i<max(p,q);++i) if(table[fix][i]) return false;}
return true;
}
bool test(int posx,int posy)//返回真时表示红方暂时不能胜利
{
if(posy==g[0].second&&judge(posy,posx,g[0].first,1)) return false;
for(int i=0;i<r.size();++i){
if(!vis[r[i].first][r[i].second]) continue;
if(r[i].first==posx&&judge(posx,posy,r[i].second,0)) return false;
else if(r[i].second==posy&&judge(posy,posx,r[i].first,1)) return false;
}
for(int i=0;i<h.size();++i){
if(!vis[h[i].first][h[i].second]) continue;
int hx[]={-1,1,-2,2,-2,2,-1,1},hy[]={-2,-2,-1,-1,1,1,2,2};//表示马可能的走向
int jx[]={0,0,-1,1,-1,1,0,0},jy[]={-1,-1,0,0,0,0,1,1};//表示相应的马腿的位置
for(int j=0;j<8;++j){
int tmpx=h[i].first+hx[j],tmpy=h[i].second+hy[j];
int tmpx1=h[i].first+jx[j],tmpy1=h[i].second+jy[j];
if(tmpx==posx&&tmpy==posy&&!table[tmpx1][tmpy1]) return false;
}
}
for(int i=0;i<c.size();++i){
if(!vis[c[i].first][c[i].second]) continue;
if(c[i].first==posx&&!judge(posx,posy,c[i].second,0)){
int cnt=0;
for(int j=min(c[i].second,posy)+1;j<max(c[i].second,posy);++j)
if(table[posx][j]) ++cnt;
if(cnt==1) return false;//中间只有一个棋子时才能攻击
}
else if(c[i].second==posy&&!judge(posy,posx,c[i].first,1)){
int cnt=0;
for(int j=min(c[i].first,posx)+1;j<max(c[i].first,posx);++j)
if(table[j][posy]) ++cnt;
if(cnt==1) return false;
}
}
return true;
}
int main()
{
int n,x,y;
while(cin>>n>>x>>y){
if(!n) break;
g.clear();r.clear();h.clear();c.clear();memset(table,0,sizeof(table));memset(vis,0,sizeof(vis));
char tmp;
for(int i=0;i<n;++i){
int a,b;cin>>tmp>>a>>b;table[a][b]=1;vis[a][b]=1;
pair<int,int> yu(a,b);
if(tmp=='G') g.push_back(yu);else if(tmp=='R') r.push_back(yu);
else if(tmp=='H') h.push_back(yu);else if(tmp=='C') c.push_back(yu);
}
int flag=0,mx[]={-1,0,0,1},my[]={0,-1,1,0};//表示黑将可能的走向
for(int i=0;i<4;++i){
if(y==g[0].second&&judge(y,x,g[0].first,1)){flag=1;break;}
int tmpx=x+mx[i],tmpy=y+my[i];
if(tmpx>=1&&tmpx<=3&&tmpy>=4&&tmpy<=6){
if(table[tmpx][tmpy]) vis[tmpx][tmpy]=0;//处理黑棋吃掉红棋的情况
if(test(tmpx,tmpy)){flag=1;break;}
if(table[tmpx][tmpy]) vis[tmpx][tmpy]=1;
}
}
if(flag) cout<<"NO\n";
else cout<<"YES\n";
}
return 0;
}