您的位置:首页 > 其它

poj 1475 (bfs+bfs)

2012-10-07 21:36 190 查看
bfs套一个bfs

先bfs box的移动情况,这里注意的是可能一个坐标x,ybox多次经过是可行的。网上的很多AC代码就没注意这个。比如下面这组数据:

8 9
#########
#......T#
#.S.....#
##B######
#.......#
#.......#
#.......#
#########


网上很多AC代码输出“Impossible.”明显错的。。。

然后在每次bfs box的时候bfs person 判断是否能走到相应的点。(即相反的方向点)

最后还注意的是 Output a single blank line after each test case. 如果最后没有空行竟然是报wa的。。。

View Code

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <string>

using namespace std;

#define MAXN 22
char P[4]={'N','S','W','E'};
char M[4]={'n','s','w','e'};
int R,C;
int dir[4][2]={-1,0,1,0,0,-1,0,1};
char map[MAXN][MAXN];
struct point
{
int x,y;
int p_x,p_y;//当前状态person所在的地方
string ans;
};
bool isok(int x,int y)
{
if(x>=0 && x<R && y>=0 && y<C && map[x][y] != '#')
return true;
return false;
}
string tmp;
bool bfs_person(point en,point cu)
{
tmp="";
point st;
st.x=en.p_x;
st.y=en.p_y;
st.ans="";
queue<point>q;
bool vis[MAXN][MAXN];
memset(vis,0,sizeof(vis));
while(!q.empty())
q.pop();
q.push(st);
while(!q.empty())
{
point cur,next;
cur=q.front();
q.pop();
if(cur.x==en.x && cur.y==en.y)
{
tmp=cur.ans;
return true;
}
for(int i=0;i<4;i++)
{
next=cur;
next.x=cur.x+dir[i][0];
next.y=cur.y+dir[i][1];
if(!isok(next.x,next.y)) continue;
if(next.x==cu.x && next.y==cu.y) continue;
if(vis[next.x][next.y]) continue;
vis[next.x][next.y]=1;
next.ans=cur.ans+M[i];
q.push(next);
}
}
return false;
}
string bfs_box()
{
bool vis[MAXN][MAXN][4];//某点四个方向是否访问!!0==N,1==S,2==W,3==E
point st;
st.x=st.y=-1;
st.p_x=st.p_y=-1;
st.ans="";
for(int i=0;i<R && (st.x==-1 || st.p_x==-1);i++)
for(int j=0;j<C && (st.x==-1 || st.p_x==-1);j++)
if(map[i][j]=='B')
{
st.x=i;
st.y=j;
map[i][j]='.';
}
else if(map[i][j]=='S')
{
st.p_x=i;
st.p_y=j;
map[i][j]='.';
}
//----------------------------------------
//cout<<"st.x="<<st.x<<" st.y="<<st.y<<" st.p_x="<<st.p_x<<" st.p_y="<<st.p_y<<endl;
//----------------------------------------
queue<point> q;
while(!q.empty())
q.pop();
q.push(st);
memset(vis,0,sizeof(vis));
while(!q.empty())
{
point cur=q.front();q.pop();
//----------------------------------------
//        cout<<"cur.x="<<cur.x<<" cur.y="<<cur.y<<" cur.p_x="<<cur.p_x<<" cur.p_y="<<cur.p_y<<endl;
//    cout<<"-----------------------------\n";
//----------------------------------------
point next,pre;
if(map[cur.x][cur.y]=='T')
return cur.ans;
for(int i=0;i<4;i++)
{
next=cur;
next.x=cur.x+dir[i][0];
next.y=cur.y+dir[i][1];
if(!isok(next.x,next.y))
continue;
if(vis[next.x][next.y][i])
continue;
pre=cur;
switch(i)
{
case 0: pre.x=cur.x+1;break;
case 1: pre.x=cur.x-1;break;
case 2: pre.y=cur.y+1;break;
case 3: pre.y=cur.y-1;break;
}
if(!bfs_person(pre,cur))//搜寻人是否能走到特定的位置
continue;
vis[next.x][next.y][i]=1;
next.ans=cur.ans+tmp;
next.ans=next.ans+P[i];
next.p_x=cur.x;next.p_y=cur.y;
q.push(next);
}
}
return "Impossible.";
}

int main()
{
int cas=1;
while(scanf("%d%d",&R,&C) && (R+C))
{
getchar();
for(int i=0;i<R;i++)
gets(map[i]);

//---------------------------------------
//    for(int i=0;i<R;i++)
//    cout<<map[i]<<endl;
//----------------------------------------

printf("Maze #%d\n",cas++);
//printf("%s\n",bfs_box());
cout<<bfs_box()<<endl<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: