zoukankan      html  css  js  c++  java
  • hdu

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

    题目意思很简单,只要思路对就好。

    首先考虑搬运工能否到达推箱子的那个点,这个可以根据箱子前进方向得出搬运工需要到达的目的地,用另一个bfs判断,然后就类似两个点的bfs那样用一个数组标记状态,

    需要注意箱子在边上的情况。

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<queue>
      4 using namespace std;
      5 
      6 struct point
      7 {
      8     int x,y,nx,ny,step;
      9 };
     10 
     11 int n,m;
     12 int maze[10][10];
     13 int used[10][10][10][10];
     14 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
     15 
     16 bool bfs1(int a,int b,int c,int d,int p,int q)
     17 {
     18     int mark[10][10];
     19     memset(mark,0,sizeof(mark));
     20     queue<point>que;
     21     point s;
     22     s.x=a,s.y=b;
     23     mark[s.x][s.y]=1;
     24     que.push(s);
     25     while(!que.empty())
     26     {
     27         point e=que.front();que.pop();
     28         //printf("%d %d
    ",e.x,e.y);
     29         if(e.x==c&&e.y==d) return true;
     30         for(int i=0;i<4;i++)
     31         {
     32             s.x=e.x+dir[i][0];
     33             s.y=e.y+dir[i][1];
     34             if(s.x==p&&s.y==q) continue;
     35             //printf("%d %d
    ",s.x,s.y);
     36             if(s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&maze[s.x][s.y]!=1&&!mark[s.x][s.y])
     37             {
     38                 mark[s.x][s.y]=1;
     39                 que.push(s);
     40             }
     41         }
     42     }
     43     return false;
     44 }
     45 
     46 void bfs2(int a,int b,int c,int d)
     47 {
     48     memset(used,0,sizeof(used));
     49     queue<point>que;
     50     point s,e;
     51     s.x=a,s.y=b,s.nx=c,s.ny=d,s.step=0;
     52     que.push(s);
     53     used[s.x][s.y][s.nx][s.ny]=1;
     54     while(!que.empty())
     55     {
     56         e=que.front();que.pop();
     57         //printf("%d %d %d %d %d
    ",e.x,e.y,e.nx,e.ny,e.step);
     58         if(maze[e.x][e.y]==3) {printf("%d
    ",e.step);return;}
     59         for(int i=0;i<4;i++)
     60         {
     61             s.x=e.x+dir[i][0];
     62             s.y=e.y+dir[i][1];
     63             s.nx=e.x;
     64             s.ny=e.y;
     65             if(s.x<0||s.x>=n||s.y<0||s.y>=m||maze[s.x][s.y]==1||used[s.x][s.y][s.nx][s.ny]) continue;
     66             if(i==0)
     67             {
     68                 if(e.x+1>=n||maze[e.x+1][e.y]==1||(bfs1(e.nx,e.ny,e.x+1,e.y,e.x,e.y)==false)) continue;
     69             }
     70             else if(i==1)
     71             {
     72                 if(e.x-1<0||maze[e.x-1][e.y]==1||(bfs1(e.nx,e.ny,e.x-1,e.y,e.x,e.y)==false)) continue;
     73             }
     74             else if(i==2)
     75             {
     76                 if(e.y+1>=m||maze[e.x][e.y+1]==1||(bfs1(e.nx,e.ny,e.x,e.y+1,e.x,e.y)==false)) continue;
     77             }
     78             else if(i==3)
     79             {
     80                 if(e.y-1<0||maze[e.x][e.y-1]==1||bfs1(e.nx,e.ny,e.x,e.y-1,e.x,e.y)==false) continue;
     81             }
     82             used[s.x][s.y][s.nx][s.ny]=1;
     83             s.step=e.step+1;
     84             que.push(s);
     85         }
     86     }
     87     printf("-1
    ");
     88 }
     89 
     90 int main()
     91 {
     92     //freopen("a.txt","r",stdin);
     93     int t,a,b,c,d;
     94     scanf("%d",&t);
     95     while(t--)
     96     {
     97         scanf("%d%d",&n,&m);
     98         for(int i=0;i<n;i++)
     99             for(int j=0;j<m;j++)
    100             {
    101                 scanf("%d",&maze[i][j]);
    102                 if(maze[i][j]==2)
    103                 {
    104                     a=i;
    105                     b=j;
    106                 }
    107                 else if(maze[i][j]==4)
    108                 {
    109                     c=i;
    110                     d=j;
    111                 }
    112             }
    113         bfs2(a,b,c,d);
    114     }
    115     return 0;
    116 }
  • 相关阅读:
    VIM
    函数指针
    《BOOST程序库完全开发指南》 第13章 编程语言支持
    《BOOST程序库完全开发指南》 第01章 Boost程序库总论
    gtest
    《BOOST程序库完全开发指南》 第04章 实用工具
    distcc配置
    《BOOST程序库完全开发指南》 第08章 算法
    Makefile
    《BOOST程序库完全开发指南》 第03章 内存管理
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4540942.html
Copyright © 2011-2022 走看看