zoukankan      html  css  js  c++  java
  • hdu 1732 bfs

    题意:推箱子游戏

    代码写错居然卡内存!!

     搞了两天了

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <queue>
      5 
      6 using namespace std;
      7 char s[9][9];
      8 int d[4][2]={1,0,0,1,-1,0,0,-1};
      9 int n,m,tt;
     10 struct node
     11 {
     12     int x,y;
     13 };
     14 struct all
     15 {
     16     node a[4];
     17     int t;
     18 }st,ed;
     19 bool vis[8][8][8][8][8][8][8][8];
     20 
     21 bool aim[9][8];
     22 bool ok(all e)
     23 {
     24     for(int i=0;i<3;i++)
     25     {
     26         if(!aim[e.a[i].x][e.a[i].y])
     27         {
     28             return false;
     29         }
     30     }
     31     return true;
     32 }
     33 queue<all> q;
     34 void bfs()
     35 {
     36     all now,next;
     37     memset(aim,false,sizeof(aim));
     38     memset(vis,false,sizeof(vis));
     39     while(!q.empty())   q.pop();
     40     q.push(st);
     41     for(int i=0;i<3;i++)
     42         aim[ed.a[i].x][ed.a[i].y]=true;
     43     while(!q.empty())
     44     {
     45         now=q.front();
     46         q.pop();
     47         if(ok(now))
     48         {
     49             printf("%d
    ",now.t);
     50             return;
     51         }
     52         //printf("%d %d %d %d %d %d %d %d %d
    ",now.a[3].x,now.a[3].y,now.a[0].x,now.a[0].y,now.a[1].x,now.a[1].y,now.a[2].x,now.a[2].y,now.t);
     53         if(vis[now.a[0].x][now.a[0].y][now.a[1].x][now.a[1].y][now.a[2].x][now.a[2].y][now.a[3].x][now.a[3].y])
     54             continue;
     55         vis[now.a[0].x][now.a[0].y][now.a[1].x][now.a[1].y][now.a[2].x][now.a[2].y][now.a[3].x][now.a[3].y]=true;
     56         for(int i=0;i<4;i++)
     57         {
     58             int nx=now.a[3].x+d[i][0];  //人的移动
     59             int ny=now.a[3].y+d[i][1];
     60             if(s[nx][ny]=='#'||nx<0||nx>=n||ny<0||ny>=m) continue;
     61             int j;
     62             for(j=0;j<3;j++)    //人撞到箱子
     63             {
     64                 if(nx==now.a[j].x&&ny==now.a[j].y)
     65                 {
     66                     break;
     67                 }
     68             }
     69             if(j>=3)    //并没有推到箱子
     70             {
     71                 next=now;
     72                 next.a[3].x=nx,next.a[3].y=ny;
     73                 next.t++;
     74                 q.push(next);
     75             }
     76             else    //推到箱子
     77             {
     78                 nx+=d[i][0],ny+=d[i][1];  //箱子移动
     79                 if(nx<0||nx>=n||ny<0||ny>=m||s[nx][ny]=='#')  continue;
     80                 int k=0;
     81                 for(k=0;k<3;k++)
     82                 {
     83                     if(nx==now.a[k].x&&ny==now.a[k].y)
     84                     {
     85                         break;
     86                     }
     87                 }
     88                 if(k>=3)    //箱子旁边并没有其他箱子
     89                 {
     90                     next=now;
     91                     next.a[3].x=nx-d[i][0],next.a[3].y=ny-d[i][1];  //人的坐标
     92                     next.a[j].x=nx,next.a[j].y=ny;  //箱子坐标
     93                     next.t++;
     94                     q.push(next);
     95                 }
     96             }
     97         }
     98     }
     99     printf("-1
    ");
    100 }
    101 int main()
    102 {
    103     int i,j,k;
    104     #ifndef ONLINE_JUDGE
    105     freopen("1.in","r",stdin);
    106     #endif
    107     while(scanf("%d%d",&n,&m)!=EOF)
    108     {
    109         int cnt1=0,cnt2=0;
    110         for(i=0;i<n;i++)
    111         {
    112             scanf("%s",s[i]);
    113             for(j=0;j<m;j++)
    114             {
    115                 if(s[i][j]=='X')
    116                 {
    117                     st.a[3].x=i,st.a[3].y=j;
    118                 }
    119                 else if(s[i][j]=='*')
    120                 {
    121                     st.a[cnt1].x=i,st.a[cnt1++].y=j;
    122                 }
    123                 else if(s[i][j]=='@')
    124                 {
    125                     ed.a[cnt2].x=i,ed.a[cnt2++].y=j;
    126                 }
    127                 if(s[i][j]!='#') s[i][j]='.';
    128             }
    129         }
    130         st.t=0;
    131         bfs();
    132     }
    133     return 0;
    134 }
  • 相关阅读:
    20200917-2 词频统计
    20200910-2 博客作业
    20200910-1 每周例行报告
    20200910-3命令行和控制台编程
    使用Requests库实现api接口测试(Python)
    Python Lambda函数的几种使用方法
    文本与向量之间的转换
    Oracle连接出现error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
    一图看懂新一代人工智能知识体系大全
    SqlDeveloper连接MySQL出现The connection property ‘zeroDateTimeBehavior’ acceptable values are: ‘CONVERT_TO_NULL’, ‘EXCEPTION’ or ‘ROUND’. The value ‘convertToNull’ is not acceptable 错误
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4471647.html
Copyright © 2011-2022 走看看