改了好久,上午来实验室打出来了,运行就是不对,一直找啊找!还是没找到,最后突然停电了,打好的代码还没保存呢!
刚才来的时候又重新打了一遍!!!结果一个小小的错误wrong了好久!!!
在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;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102