hdu 1732 Push Box

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

推箱子和游戏规则一样。

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 #include <algorithm>
  5 using namespace std;
  6
  7 char g[10][10];
  8 int n,m;
  9 int sx,sy;
 10 bool vis[9][9][9][9][9][9][9][9];
 11 int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
 12 struct node
 13 {
 14     int x[4],y[4],xx,yy,step;
 15 } st1,st2,st;
 16
 17 int deal(node p,int i,int pos)
 18 {
 19     p.xx+=dir[i][0];
 20     p.yy+=dir[i][1];
 21     if(p.xx>=0&&p.xx<n&&p.yy>=0&&p.yy<m)
 22     {
 23         for(int j=0; j<3; j++)
 24         {
 25             if(j!=pos&&p.x[j]==p.xx&&p.y[j]==p.yy)
 26             {
 27                 return 0;
 28             }
 29         }
 30         return 1;
 31     }
 32     return 0;
 33 }
 34
 35 int bfs()
 36 {
 37     queue<node>q;
 38     st.step=0;
 39     st.xx=sx;
 40     st.yy=sy;
 41     q.push(st);
 42     memset(vis,false,sizeof(vis));
 43     vis[sx][sy][st.x[0]][st.y[0]][st.x[1]][st.y[1]][st.x[2]][st.y[2]]=true;
 44     while(!q.empty())
 45     {
 46         st1=q.front();
 47         q.pop();
 48         int cnt=0;
 49         for(int i=0; i<3; i++)
 50         {
 51             if(g[st1.x[i]][st1.y[i]]==‘@‘)
 52             {
 53                 cnt++;
 54             }
 55         }
 56         if(cnt==3)
 57         {
 58             return st1.step;
 59         }
 60         for(int i=0; i<4; i++)
 61         {
 62             st2=st1;
 63             st2.xx=st2.xx+dir[i][0];
 64             st2.yy=st2.yy+dir[i][1];
 65             st2.step++;
 66             if(st2.xx>=0&&st2.xx<n&&st2.yy>=0&&st2.yy<m&&g[st2.xx][st2.yy]!=‘#‘)
 67             {
 68                 int pos;
 69                 for(pos=0; pos<3; pos++)
 70                 {
 71                     if(st2.x[pos]==st2.xx&&st2.y[pos]==st2.yy)
 72                     {
 73                         break;
 74                     }
 75                 }
 76                 if(pos<3)
 77                 {
 78                     if(deal(st2,i,pos))
 79                     {
 80                         st2.x[pos]+=dir[i][0];
 81                         st2.y[pos]+=dir[i][1];
 82                         if(!vis[st2.xx][st2.yy][st2.x[0]][st2.y[0]][st2.x[1]][st2.y[1]][st2.x[2]][st2.y[2]])
 83                         {
 84                             vis[st2.xx][st2.yy][st2.x[0]][st2.y[0]][st2.x[1]][st2.y[1]][st2.x[2]][st2.y[2]]=true;
 85                             q.push(st2);
 86                         }
 87                     }
 88                 }
 89                 else
 90                 {
 91                     if(!vis[st2.xx][st2.yy][st2.x[0]][st2.y[0]][st2.x[1]][st2.y[1]][st2.x[2]][st2.y[2]])
 92                     {
 93                         vis[st2.xx][st2.yy][st2.x[0]][st2.y[0]][st2.x[1]][st2.y[1]][st2.x[2]][st2.y[2]]=true;
 94                         q.push(st2);
 95                     }
 96                 }
 97             }
 98         }
 99     }
100     return -1;
101 }
102
103 int main()
104 {
105     while(scanf("%d%d",&n,&m)!=EOF)
106     {
107         int num=0;
108         for(int i=0; i<n; i++)
109         {
110             scanf("%s",g[i]);
111             for(int j=0; j<m; j++)
112             {
113                 if(g[i][j]==‘X‘)
114                 {
115                     g[i][j]=‘.‘;
116                     sx=i;
117                     sy=j;
118                 }
119                 else if(g[i][j]==‘*‘)
120                 {
121                     g[i][j]=‘.‘;
122                     st.x[num]=i;
123                     st.y[num]=j;
124                     num++;
125                 }
126             }
127         }
128         printf("%d\n",bfs());
129     }
130     return 0;
131 }

hdu 1732 Push Box

时间: 2024-08-11 01:06:12

hdu 1732 Push Box的相关文章

【HDOJ】1732 Push Box

BFS.使用当前结点位置以及三个箱子的位置作为状态. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <queue> 6 using namespace std; 7 8 #define MAXN 8 9 10 typedef struct { 11 int x, y; 12 } Point_t; 13 1

HDU 1110 Equipment Box (判断一个大矩形里面能不能放小矩形)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1110 Equipment Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2989    Accepted Submission(s): 759 Problem Description There is a large room in

杭电 HDU ACM 1326 Box of Bricks

Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5457    Accepted Submission(s): 2358 Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one up

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;

HDU - 2475:Box(splay维护森林)

There are N boxes on the ground, which are labeled by numbers from 1 to N. The boxes are magical, the size of each one can be enlarged or reduced arbitrarily. Jack can perform the “MOVE x y” operation to the boxes: take out box x; if y = 0, put it on

(HDU)1326 -- Box of Bricks (砖块)

题目链接:http://vjudge.net/problem/HDU-1326 一堆砖要摆平,问最少移动多少块.刚开始认为是最少要移动多少次,一个水题被写成了贪心题.mdzz. 注意输出还有一个换行!! 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <algorithm> 6 #include <str

50道hdu基础搜索总结(转)

Dfs: 大部分是直接递归枚举,即求满足约束条件下的解,虽不用剪枝,但也需要代码能力. 练习递归枚举的题目: 1241       Oil Deposits (dfs的连通块个数) 1016       Prime Ring Problem 1584       蜘蛛牌(简单dfs,简单的剪枝,还有人用DP做(???)) 1426       Sudoku Killer(练习递归的好题目 or Dancing links(???)) 2510       符号三角形(打表题,写写打表程序还是不错

HDU 1254——推箱子

这题跟 hdu 1734 push box 是一样的,只不过这题推的是一个箱子,另外求的是箱子被推了多少次,那么只要在箱子被推的时候 次数才加1,然后使用优先队列就ok了 写了1734就会觉得这题很水啦  HDU1734题解 网上主流的都是bfs+bfs,或者是bfs+dfs <span style="font-size:18px;">#include<iostream> #include<cstring> #include<algorithm

搜索题推荐

(转自网络博客): POJ POJ 1376 – Robot(基础) http://acm.pku.edu.cn/JudgeOnline/problem?id=1376 题意:略 解法:bfs,A*-. POJ 2688 – Cleaning Robot(基础) http://acm.pku.edu.cn/JudgeOnline/problem?id=2688 题意:bfs后转换为tsp问题 解法:bfs+dp,bfs+dfs 相关:http://hi.baidu.com/zfy0701/blo