zoukankan      html  css  js  c++  java
  • hdu 1254(搜索题)

    推箱子

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


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

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

     
    Input
    输 入数据的第一行是一个整数T(1<=T<=20),代表测试数据的数量.然后是T组测试数据,每组测试数据的第一行是两个正整数M,N(2& lt;=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
     
    好多坑的一道题。。。很好的一道题。。。参考了别人的代码。。真的做不出来。。。每次结点存个新的图。。get。。。做了才知道。。。
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <queue>
    #include <algorithm>
    using namespace std;
    int n,m;
    int graph[8][8];
    bool flag[8][8][4];
    bool vis[8][8];
    int dir[][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    struct Node
    {
        int x,y;
        int step;
        int g[10][10];
    }s,person,t;
    bool check(int x,int y)
    {
        if(x<0||x>=n||y<0||y>=m) return false;
        return true;
    }
    bool bfs2(Node s,Node target){
        person = s;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(person.g[i][j]==4){
                    person.x = i;
                    person.y = j;
                    person.step = 0;
                }
            }
        }
        queue<Node>q;
        memset(vis,false,sizeof(vis));
        q.push(person);
        vis[person.x][person.y] = true;
        while(!q.empty()){
            Node now = q.front();
            q.pop();
            if(now.x==target.x&&now.y==target.y){
                return true;
            }
            Node next;
            for(int i=0;i<4;i++){
                next.step = now.step+1;
                next.x = now.x +dir[i][0];
                next.y = now.y + dir[i][1];
                if(!check(next.x,next.y)||s.g[next.x][next.y]==2||vis[next.x][next.y]||s.g[next.x][next.y]==1) continue;
                vis[next.x][next.y] = true;
                q.push(next);
            }
        }
        return false;
    }
    int bfs(){
        memset(flag,false,sizeof(flag));
        queue <Node> q;
        q.push(s);
        while(!q.empty()){
            Node now = q.front();
            q.pop();
            if(now.x==t.x&&now.y==t.y) return now.step;
            Node next,target;
            for(int i=0;i<4;i++){
                next = now;
                next.x = now.x+dir[i][0];
                next.y = now.y+dir[i][1];
                next.step = now.step+1;
                if(!check(next.x,next.y)||flag[next.x][next.y][i]||next.g[next.x][next.y]==1) continue; ///该方向已经走过了
                ///人的目标点(人以此点为目标点将箱子从now推向next)
                target.x = now.x - dir[i][0];
                target.y = now.y - dir[i][1];
                if(!check(target.x,target.y)) continue;
                if(bfs2(next,target)){
                    swap(next.g[now.x][now.y],next.g[next.x][next.y]);
                    swap(next.g[target.x][target.y],next.g[person.x][person.y]);
                    flag[next.x][next.y][i] = true;
                    q.push(next);
                }
            }
        }
        return -1;
    }
    int main()
    {
        int tcase;
        scanf("%d",&tcase);
        while(tcase--)
        {
            scanf("%d%d",&n,&m);
            for(int i=0; i<n; i++)
                for(int j=0; j<m; j++){
                    scanf("%d",&s.g[i][j]);
                    if(s.g[i][j]==2){
                        s.x = i;
                        s.y = j;
                        s.step = 0;
                    }
                    if(s.g[i][j]==3){
                        t.x = i;
                        t.y = j;
                    }
                }
           int res = bfs();
           printf("%d
    ",res);
        }
    }
  • 相关阅读:
    STM8S TIM4库函数应用
    几种更新(Update语句)查询的方法
    CentOS 配置httpd使局域网能够正常訪问
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    Numeral.js 是一个用于格式化和数字四则运算的js 库
    SVN高速新手教程
    我的Android开发相关文章
    cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发
    运动检测(前景检测)之(一)ViBe
    linux概念之分区与文件系统
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5585469.html
Copyright © 2011-2022 走看看