3 5 6 3 XXD... ....E. ....X. ....S. ...... 5 6 3 XDX... ....E. ...... ....S. ...... 5 6 8 XXDX.. .XEX.. ...... ....S. ......Sample Output
Case 1: -1 Case 2: 3 Case 3: -1
有篇博客写挺好的:https://blog.csdn.net/xiangAccepted/article/details/79664891
#include
using namespace std;
int n,m,k,a,b,a1,a2,b1,b2;
int inf=0x3f3f3f3f;
char mapp[110][110];
int dd[110][110];
int de[110][110];
int v[110][110];
struct node
{
int x,y,time,a,b;
};
int go(int x,int y)
{
if(0>x||x>=n||0>y||y>=m)
return 0;
if(mapp[x][y]=='X'||mapp[x][y]=='D'||mapp[x][y]=='E')
return 0;
return 1;
}
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int bfs()
{
memset(v,0,sizeof(v));
memset(dd,inf,sizeof(dd));
memset(de,inf,sizeof(de));
queueq;
node st,ed;
st.x=a;
st.y=b;
st.a=st.b=0;
if(mapp[st.x][st.y]=='s')
return 0;
if(mapp[st.x][st.y]=='d')
st.a=1,dd[st.x][st.y]=0;
if(mapp[st.x][st.y]=='e')
st.b=1,de[st.x][st.y]=0;
st.time=0;
q.push(st);
v[a][b]=1;
while(!q.empty())
{
st=q.front();
q.pop();
// printf("%d %d %d %d %d \n",st.x,st.y,st.time,st.a,st.b);
if(st.time>k)
return -1;
for(int i=0;i<4;i++)
{
ed=st;
ed.x=st.x+dir[i][0];
ed.y=st.y+dir[i][1];
ed.time=st.time+1;
if(ed.time>k)
continue;
if(!go(ed.x,ed.y))
continue;
if(v[ed.x][ed.y]&&ed.a==0&&ed.b==0)
continue;
if(ed.time>=dd[ed.x][ed.y]&&ed.a==1&&ed.b==0)
continue;
if(ed.time>=de[ed.x][ed.y]&&ed.a==0&&ed.b==1)
continue;
if(mapp[ed.x][ed.y]=='s')
return ed.time;
if(mapp[ed.x][ed.y]=='d')
ed.a=1;
if(mapp[ed.x][ed.y]=='e')
ed.b=1;
if(ed.a==1&&ed.b==1)
return ed.time;
if(ed.a) dd[ed.x][ed.y]=ed.time;
if(ed.b) de[ed.x][ed.y]=ed.time;
q.push(ed);
v[ed.x][ed.y]=1;
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
int oo=0;
while(t--)
{
oo++;
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i=0;i--)
{
if(mapp[i][b1]=='S'||mapp[i][b1]=='.')
mapp[i][b1]='d';
else
break;
}
for(int i=a1+1;i=0;i--)
{
if(mapp[a1][i]=='S'||mapp[a1][i]=='.')
mapp[a1][i]='d';
else
break;
}
for(int i=b1+1;i=0;i--)
{
if(mapp[i][b2]=='d')
mapp[i][b2]='s';
else if(mapp[i][b2]=='S'||mapp[i][b2]=='.')
mapp[i][b2]='e';
else
break;
}
for(int i=a2+1;i=0;i--)
{
if(mapp[a2][i]=='d')
mapp[a2][i]='s';
else if(mapp[a2][i]=='S'||mapp[a2][i]=='.')
mapp[a2][i]='e';
else
break;
}
for(int i=b2+1;i