POJ 1475 Pushing Boxes

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1475 Pushing Boxes相关的知识,希望对你有一定的参考价值。

【题目链接】

            http://poj.org/problem?id=1475

【算法】

          双重BFS

【代码】

           

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 30

int n,m,i,j,px,py,bx,by,ex,ey,TC;
string t;
char mp[MAXN][MAXN];
bool visited1[MAXN][MAXN],visited2[MAXN][MAXN];

const int dx[4] = {0,0,-1,1};
const int dy[4] = {-1,1,0,0};
const char box_path[5] = "WENS";
const char people_path[5] = "wens";

struct Box
{
        int bx,by,px,py;
        string path;
};
struct People
{
        int x,y;
        string path;
};
inline bool ok(int x,int y)
{
        return x >= 1 && x <= n && y >= 1 && y <= m;
}
inline bool bfs2(int sx,int sy,int ex,int ey,int nx,int ny)
{
        int i,tx,ty;
        People cur;
        queue< People > q;
        while (!q.empty()) q.pop();
        memset(visited2,false,sizeof(visited2));
        q.push((People){sx,sy,""});
        while (!q.empty())
        {
                cur = q.front();
                q.pop();
                if (cur.x == ex && cur.y == ey)
                {
                        t = cur.path;
                        return true;
                } 
                for (i = 0; i < 4; i++)
                {
                        tx = cur.x + dx[i];
                        ty = cur.y + dy[i];
                        if (ok(tx,ty) && !visited2[tx][ty] && mp[tx][ty] != #)
                        {
                                if (tx == nx && ty == ny) continue;
                                visited2[tx][ty] = true;
                                q.push((People){tx,ty,cur.path+people_path[i]});
                        }
                }
        }    
        return false;    
}
inline void bfs1()
{
        int i,tx,ty;
        queue<Box> q;
        Box cur;
        memset(visited1,false,sizeof(visited1));
        while (!q.empty()) q.pop();
        q.push((Box){bx,by,px,py,""});
        while (!q.empty())
        {    
                cur = q.front();
                q.pop();
                if (cur.bx == ex && cur.by == ey)
                {
                        cout<< cur.path << endl;
                        return;
                }
                for (i = 0; i < 4; i++)
                {
                        tx = cur.bx + dx[i];
                        ty = cur.by + dy[i];
                        if (ok(tx,ty) && !visited1[tx][ty] && mp[tx][ty] != #)
                        {
                                if (bfs2(cur.px,cur.py,cur.bx-dx[i],cur.by-dy[i],cur.bx,cur.by))        
                                {
                                        visited1[tx][ty] = true;
                                        q.push((Box){tx,ty,cur.bx,cur.by,cur.path+t+box_path[i]});
                                } 
                        } 
                }
        }
        printf("Impossible.
");
}

int main() 
{
        
        while (scanf("%d%d",&n,&m) != EOF && n && m)
        {
                getchar();
                for (i = 1; i <= n; i++)
                {
                        for (j = 1; j <= m; j++)
                        {
                                mp[i][j] = getchar();
                                if (mp[i][j] == S)
                                {
                                        px = i;
                                        py = j;
                                }
                                if (mp[i][j] == B)
                                {
                                        bx = i;
                                        by = j;
                                }
                                if (mp[i][j] == T)
                                {
                                        ex = i;
                                        ey = j;
                                }
                        }
                        getchar();
                }
                printf("Maze #%d
",++TC);
                bfs1();
                printf("
");
        }
        
        return 0;
    
}

 

以上是关于POJ 1475 Pushing Boxes的主要内容,如果未能解决你的问题,请参考以下文章

[poj P1475] Pushing Boxes

POJ1475 Pushing Boxes(BFS套BFS)

POJ 1475 Pushing Boxes

poj1475 Pushing Boxes(BFS)

POJ - 1475 Pushing Boxes

POJ1475(Pushing Boxes)--bbffss