hdu 1254

http://acm.hdu.edu.cn/showproblem.php?pid=1254

 

BFS+hash,廣搜路線,每次小人走都要哈希一下箱子的位置,這樣保證走的時候狀態是不一樣的,然後要注意,這裏要用優先隊列,時間短的要先彈出來,這個地方值得琢磨,WA了n次!

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

using namespace std;

#define MAXN 12

struct node
{
    int x,y;
    int t;
    int key;
    int px,py;
    node(){};
    node(int x,int y,int px,int py,int t,int key):x(x),y(y),px(px),py(py),t(t),key(key){};
    bool friend operator < (const node &a, const node &b)
    {
        return a.t > b.t;
    }//
};

int map[MAXN][MAXN];
bool mark[MAXN][MAXN][MAXN*MAXN+10];
int val[MAXN][MAXN];
int n,m;
int sx,sy;
int px,py;
priority_queue<node> v;
int dir[][2]={{1,0},{-1,0},{0,1},{0,-1}};

/*bool isconer(node p)
{
    if(p.x==0)
}*/
bool wall(node p)
{
    if(p.x<0||p.x>=n||p.y<0||p.y>=m)
        return true;
    if(map[p.x][p.y]==1)
        return true;
    return false;
}

bool isbox(node p)
{
    if((p.x==p.px)&&(p.y==p.py))
        return true;
    return false;
}

bool wall2(node p)
{
    if(p.px<0||p.px>=n||p.py<0||p.py>=m)
        return true;
    if(map[p.px][p.py]==1)
        return true;
    return false;
}

int BFS()
{
    node p(sx,sy,px,py,0,val[px][py]);
    node temp;
    mark[sx][sy][val[px][py]]=true;
    while(!v.empty())
        v.pop();
    v.push(p);
    while(!v.empty())
    {
        p=v.top();
        v.pop();
//        if(isconer(p))//在角落
//            continue;
        for(int i=0;i<4;i++)
        {
            temp=p;
            temp.x+=dir[i][0];
            temp.y+=dir[i][1];
        //    temp.t++;
            if(wall(temp))//如果是牆
                continue;
            if(isbox(temp))//如果這個位置是箱子
            {
                temp.px+=dir[i][0];
                temp.py+=dir[i][1];//經過移動之後的位置
                temp.t++;
                if(wall2(temp))//箱子被移動到的位置是牆
                    continue;
                if(map[temp.px][temp.py]==3)//箱子被移動到目的位置
                    return temp.t;
                if(mark[temp.x][temp.y][val[temp.px][temp.py]])//如果位置走過了
                    continue;
                mark[temp.x][temp.y][val[temp.px][temp.py]]=true;
                v.push(temp);
                continue;
            }
            if(mark[temp.x][temp.y][val[temp.px][temp.py]])//如果位置走過了
                continue;
            mark[temp.x][temp.y][val[temp.px][temp.py]]=true;
            v.push(temp);
        }
    }
    return -1;
}

int main()
{
    int t;
    int value;
    scanf("%d",&t);
    while(t--)
    {
        value=0;
        px=py=9;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%d",&map[i][j]);
                if(map[i][j]==4)
                {
                    sx=i;
                    sy=j;
                }
                if(map[i][j]==2)
                {
                    px=i;
                    py=j;
                }
                val[i][j]=value++;
            }
        }
        memset(mark,0,sizeof(mark));
        printf("%d\n",BFS());
    }
    return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章