思路:用嵌套BFS,外层BFS是找从箱子到终点找一条箱子移动的路径,用内层BFS来判断人能否从上一个点走到这一个点。细节比较多,代码量很多,写起来也很麻烦,要多注意。注意箱子是可以经过一个点多次的,但是同一个点移动往同一个方向只能一次(有可能先往下推,然后绕过去再往上推)
代码:
#include
#include
#include
#include
#include
#include
using namespace std;
#define N 22
int n,m;
int ma[N][N];
int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int vis[N][N][4],flag[N][N];
string temp;
struct Node
{
int x,y;
int px,py;
int ans;
};
int check(int x,int y)
{
if(x<1||x>n||y<1||y>m||ma[x][y]==1) return 0;
return 1;
}
queueq;
queueque;
int bfs_person(Node a,Node c)
{
memset(flag,0,sizeof(flag));
Node next,b;
b.x=a.px,b.y=a.py;
while(!q.empty())
q.pop();
q.push(b);
flag[b.x][b.y]=1;
while(!q.empty())
{
Node now=q.front();
q.pop();
if(now.x==a.x&&now.y==a.y)
return 1;
for(int i=0; i<4; i++)
{
next=now;
next.x+=dir[i][0];
next.y+=dir[i][1];
if(!check(next.x,next.y)||(next.x==c.x&&next.y==c.y)) continue;
if(flag[next.x][next.y]) continue;
flag[next.x][next.y]=1;
q.push(next);
}
}
return 0;
}
int bfs_box()
{
Node next,pre;
memset(vis,0,sizeof(vis));
Node st;
st.ans=0;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
if(ma[i][j]==4)
st.px=i,st.py=j;
else if(ma[i][j]==2)
st.x=i,st.y=j;
}
while(!que.empty())
que.pop();
que.push(st);
while(!que.empty())
{
Node now=que.front();
que.pop();
if(ma[now.x][now.y]==3)
return now.ans;
for(int i=0; i<4; i++)
{
next=now;
next.x+=dir[i][0];
next.y+=dir[i][1];
if(!check(next.x,next.y)||vis[next.x][next.y][i]) continue;
pre=now;
if(i==0) pre.x=pre.x+1;
else if(i==1) pre.x=pre.x-1;
else if(i==2) pre.y=pre.y+1;
else if(i==3) pre.y=pre.y-1;
if(!check(pre.px,pre.py)||!bfs_person(pre,now)) continue;
vis[next.x][next.y][i]=1;
next.px=now.x,next.py=now.y;
next.ans=now.ans+1;
que.push(next);
}
}
return -1;
}
int main()
{
int T,tot=1;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++)
for(int j=1;j<=m;j++)
scanf("%d",&ma[i][j]);
printf("%d\n",bfs_box());
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 131072K | |||
Total Submissions: 5302 | Accepted: 1833 | Special Judge |
Description
Input
Output
Sample Input
1 7 SB....T 1 7 SB..#.T 7 11 ########### #T##......# #.#.#..#### #....B....# #.######..# #.....S...# ########### 8 4 .... .##. .#.. .#.. .#.B .##S .... ###T 0 0
Sample Output
Maze #1 EEEEE Maze #2 Impossible. Maze #3 eennwwWWWWeeeeeesswwwwwwwnNN Maze #4 swwwnnnnnneeesssSSS
代码:
#include
#include
#include
#include
#include
#include
using namespace std;
#define N 22
int n,m;
char ma[N][N];
int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int vis[N][N][4],flag[N][N];
char P[4]= {'N','S','W','E'};
char M[4]= {'n','s','w','e'};
string temp;
struct Node
{
int x,y;
int px,py;
string ans;
};
int check(int x,int y)
{
if(x<1||x>n||y<1||y>m||ma[x][y]=='#') return 0;
return 1;
}
queueq;
queueque;
int bfs_person(Node a,Node c)
{
memset(flag,0,sizeof(flag));
Node next,b;
b.x=a.px,b.y=a.py;
b.ans="";
while(!q.empty())
q.pop();
q.push(b);
flag[b.x][b.y]=1;
while(!q.empty())
{
Node now=q.front();
q.pop();
if(now.x==a.x&&now.y==a.y)
{
temp=now.ans;
return 1;
}
for(int i=0; i<4; i++)
{
next=now;
next.x+=dir[i][0];
next.y+=dir[i][1];
if(!check(next.x,next.y)||(next.x==c.x&&next.y==c.y)) continue;
if(flag[next.x][next.y]) continue;
flag[next.x][next.y]=1;
next.ans=now.ans+M[i];
q.push(next);
}
}
return 0;
}
string bfs_box()
{
Node next,pre;
memset(vis,0,sizeof(vis));
Node st;
st.ans="";
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
if(ma[i][j]=='S')
st.px=i,st.py=j;
else if(ma[i][j]=='B')
st.x=i,st.y=j;
}
while(!que.empty())
que.pop();
que.push(st);
while(!que.empty())
{
Node now=que.front();
que.pop();
if(ma[now.x][now.y]=='T')
return now.ans;
for(int i=0; i<4; i++)
{
next=now;
next.x+=dir[i][0];
next.y+=dir[i][1];
if(!check(next.x,next.y)||vis[next.x][next.y][i]) continue;
pre=now;
if(i==0) pre.x=pre.x+1;
else if(i==1) pre.x=pre.x-1;
else if(i==2) pre.y=pre.y+1;
else if(i==3) pre.y=pre.y-1;
if(!check(pre.px,pre.py)||!bfs_person(pre,now)) continue;
vis[next.x][next.y][i]=1;
next.ans=now.ans+temp;
next.ans=next.ans+P[i];
next.px=now.x,next.py=now.y;
que.push(next);
}
}
return "Impossible.";
}
int main()
{
int tot=1;
while(~scanf("%d %d",&n,&m)&&(n+m))
{
for(int i=1; i<=n; i++)
scanf("%s",ma[i]+1);
printf("Maze #%d\n",tot++);
cout<