在dfs值返回時兩個NO的返回值不同寫錯了一個-100,一個-10,肯定不對了!!嘿嘿····,找到了,改了,提交了!對了!!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include<stdio.h> #include<string.h> #include<queue> using namespace std; struct node { int x,y,k; int time; friend bool operator<(node a,node b) { return a.time>b.time; } }; int visit[20][20][2]; int n,m,endx,endy,endk,ti,startx,starty,startk; char a[20][20],b[20][20]; int dir[4][2]={0,1,1,0,-1,0,0,-1}; int judge(int x,int y,int k) { if(x>=0&&x<n&&y>=0&&y<m) { if(k==0&&a[x][y]!='*') return 1; if(k==1&&b[x][y]!='*') return 1; } return 0; } int dfs(int startx,int starty,int startk) { priority_queue<node>q; node cur,next; int i,x,y,k; cur.x=startx;cur.y=starty; cur.k=startk;cur.time=0; visit[startx][starty][startk]=1; q.push(cur); while(!q.empty()) { next=q.top(); q.pop(); if(next.x==endx&&next.y==endy&&next.k==endk) return next.time; if(next.time>=ti) return -100; for(i=0;i<4;i++) { x=next.x+dir[i][0]; y=next.y+dir[i][1]; k=next.k; if(judge(x,y,k)&&visit[x][y][k]==0) { if(k==0&&a[x][y]=='#') { cur.time=next.time+1; cur.k=1; cur.x=x;cur.y=y; q.push(cur); visit[x][y][1]=1; continue; } if(k==1&&b[x][y]=='#') { cur.time=next.time+1; cur.k=0; cur.x=x;cur.y=y; q.push(cur); visit[x][y][0]=1; continue; } cur.time=next.time+1; cur.k=next.k; cur.x=x;cur.y=y; visit[x][y][k]=1; q.push(cur); } } } return -100; } int main() { int T,i,j; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&ti); memset(visit,0,sizeof(visit)); for(i=0;i<n;i++) scanf("%s",a[i]); for(i=0;i<n;i++) scanf("%s",b[i]); for(i=0;i<n;i++) for(j=0;j<m;j++) { if(a[i][j]=='#'&&b[i][j]=='#') {a[i][j]='*';b[i][j]='*';} if(a[i][j]=='#'&&b[i][j]=='*') a[i][j]='*'; if(a[i][j]=='*'&&b[i][j]=='#') b[i][j]='*'; } for(i=0;i<n;i++) for(j=0;j<m;j++) { if(a[i][j]=='P') { endx=i;endy=j;endk=0; } if(b[i][j]=='P') { endx=i;endy=j;endk=1; } if(a[i][j]=='S') { startx=i;starty=j;startk=0; } if(b[i][j]=='S') { startx=i;starty=j;startk=1; } } int ans; ans=dfs(startx,starty,startk); if(ans<=ti&&ans!=-100) printf("YES\n"); else printf("NO\n"); } return 0;
}