hdu 1254 推箱子/poj 1475 Pushing Boxes(推箱子经典问题,BFS嵌套BFS)

推箱子

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7477    Accepted Submission(s): 2143


Problem Description
推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙上,那么箱子只能沿着墙移动.

现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.


 

Input
输入数据的第一行是一个整数T(1<=T<=20),代表测试数据的数量.然后是T组测试数据,每组测试数据的第一行是两个正整数M,N(2<=M,N<=7),代表房间的大小,然后是一个M行N列的矩阵,代表房间的布局,其中0代表空的地板,1代表墙,2代表箱子的起始位置,3代表箱子要被推去的位置,4代表搬运工的起始位置.
 

Output
对于每组测试数据,输出搬运工最少需要推动箱子多少格才能帮箱子推到指定位置,如果不能推到指定位置则输出-1.
 

Sample Input
 
   
1 5 5 0 3 0 0 0 1 0 1 4 0 0 0 1 0 0 1 0 2 0 0 0 0 0 0 0
 

Sample Output
 
   
4
 
题意:中文题不解释

思路:用嵌套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;
}
Pushing Boxes
Time Limit: 2000MS   Memory Limit: 131072K
Total Submissions: 5302   Accepted: 1833   Special Judge

Description

Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not be filled with rock. You can move north, south, east or west one cell at a step. These moves are called walks. 
One of the empty cells contains a box which can be moved to an adjacent free cell by standing next to the box and then moving in the direction of the box. Such a move is called a push. The box cannot be moved in any other way than by pushing, which means that if you push it into a corner you can never get it out of the corner again. 

One of the empty cells is marked as the target cell. Your job is to bring the box to the target cell by a sequence of walks and pushes. As the box is very heavy, you would like to minimize the number of pushes. Can you write a program that will work out the best such sequence? 
hdu 1254 推箱子/poj 1475 Pushing Boxes(推箱子经典问题,BFS嵌套BFS)_第1张图片

Input

The input contains the descriptions of several mazes. Each maze description starts with a line containing two integers r and c (both <= 20) representing the number of rows and columns of the maze. 

Following this are r lines each containing c characters. Each character describes one cell of the maze. A cell full of rock is indicated by a `#' and an empty cell is represented by a `.'. Your starting position is symbolized by `S', the starting position of the box by `B' and the target cell by `T'. 

Input is terminated by two zeroes for r and c. 

Output

For each maze in the input, first print the number of the maze, as shown in the sample output. Then, if it is impossible to bring the box to the target cell, print ``Impossible.''. 

Otherwise, output a sequence that minimizes the number of pushes. If there is more than one such sequence, choose the one that minimizes the number of total moves (walks and pushes). If there is still more than one such sequence, any one is acceptable. 

Print the sequence as a string of the characters N, S, E, W, n, s, e and w where uppercase letters stand for pushes, lowercase letters stand for walks and the different letters stand for the directions north, south, east and west. 

Output a single blank line after each test case. 

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

题意:跟推箱子游戏的规则一模一样,求推的步数最少的路径,注意不是总步数。此题跟上个题的区别是要输出路径,但是不用怕,我们在BFS的过程中维护一个路径字符串即可

代码:

#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<









你可能感兴趣的:(搜索)