Codeforces Round #212 (Div. 2) A题
来源:互联网 发布:香港2047知乎 编辑:程序博客网 时间:2024/05/01 11:34
这道题目其实蛮简单的,我比赛的时候一看到这个题目就想到用广搜,可是错在第三组数据上了,后面就没怎么想了,原来题目有两个条件很重要,我却没看,导致CF只刷了一题,今天晚上重做这题时,看到了这两个条件,就把它给AC 了!!这两个条件是:'#'这个不能加入判断,因为题目中说可以穿过‘#’,还有就是两个人同时动,所以利用广搜的思路,一个人不动,另一个人去搜他,然后判断步数是不是偶数,如果是奇数的话就直接输出NO,否则直接输出YES代码如下:#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;int t,ans;char s[10][10];int hash[10][10];int dir[4][2]={{2,-2},{2,2},{-2,2},{-2,-2}};int px[100],py[100];struct node{ int x,y,step;};queue<node>q;int bfs(int x,int y){ while(!q.empty())q.pop(); node cur={x,y,0}; hash[x][y]=1; q.push(cur); while(!q.empty()) { cur=q.front(); q.pop(); int x,y,dis,nx,ny; x=cur.x;y=cur.y;dis=cur.step; for(int i=0; i<4; i++) { nx=x+dir[i][0]; ny=y+dir[i][1]; if(nx<0||nx>=8||ny<0||ny>=8||hash[nx][ny])continue; hash[nx][ny]=1; node next={nx,ny,dis+1}; q.push(next); if(next.x==px[1]&&next.y==py[1])return next.step; } } return -1;}int main(){ while(scanf("%d",&t)!=EOF) { while(t--) { for(int i=0; i<8; i++)scanf("%s",&s[i]); ans=0; for(int i=0; i<8; i++) { for(int j=0; j<8; j++) { if(s[i][j]=='K') { px[ans]=i; py[ans]=j; ans++; } } } memset(hash,0,sizeof(hash)); int num; num=bfs(px[0],py[0]); if(num<0||num%2) { printf("NO\n"); } else printf("YES\n"); } } return 0;}