程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> hdu1254 推箱子(bfs+bfs)

hdu1254 推箱子(bfs+bfs)

編輯:關於C++

推箱子

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



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

Author Ignatius.L & weigang Lee
分析:bfs嵌套bfs,一個搜人的軌跡,一個搜箱子的軌跡。
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))

int n,m;
int mat[11][11],dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
bool vis[11][11][4],vm[11][11];
int man_x,man_y,box_x,box_y,en_x,en_y;

struct A
{
    int x,y;
};
struct node
{
    int step;
    struct A box,man;
};
bool bb(int b_x, int b_y, int m_x, int m_y, int k)//搜人是否能到達要推的位置
{
    CL(vm, false);
    queue qq;
    node ss,tt;
    ss.man.x=m_x;
    ss.man.y=m_y;
    vm[ss.man.x][ss.man.y]=true;
    vm[b_x][b_y]=true;//當前箱子的位置標記不可訪問
    qq.push(ss);
    while(!qq.empty())
    {
        ss=qq.front();
        qq.pop();

        if(ss.man.x+dir[k][0]==b_x&&ss.man.y+dir[k][1]==b_y) return true;
        for(int i=0; i<4; i++)
        {
            tt.man.x=ss.man.x+dir[i][0];
            tt.man.y=ss.man.y+dir[i][1];
            if(mat[tt.man.x][tt.man.y]!=0) continue;
            if(!vm[tt.man.x][tt.man.y])
            {
                vm[tt.man.x][tt.man.y]=true;
                qq.push(tt);
            }
        }
    }
    return false;
}
void bfs()//搜箱子的軌跡
{
    queue q;
    node now,next;
    now.box.x=box_x;
    now.box.y=box_y;
    now.man.x=man_x;
    now.man.y=man_y;
    now.step=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.box.x==en_x&&now.box.y==en_y)
        {
            cout<>T;
    while(T--)
    {
        CL(mat, -1);//地圖初始化,邊界全為-1
        cin>>n>>m;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                cin>>mat[i][j];
                if(mat[i][j]==2)//人、箱子和終點的位置存儲好,再把地圖中相應位置變為可走,即0
                {
                    box_x=i;
                    box_y=j;
                    mat[i][j]=0;
                }
                if(mat[i][j]==3)
                {
                    en_x=i;
                    en_y=j;
                    mat[i][j]=0;
                }
                if(mat[i][j]==4)
                {
                    man_x=i;
                    man_y=j;
                    mat[i][j]=0;
                }
            }
        }
        CL(vis, false);
        bfs();
    }
    return 0;
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved