HDOJ 1254 推箱子【bfs && dfs】
2016-03-17 21:54
357 查看
推箱子
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6788 Accepted Submission(s): 1914
[align=left]Problem Description[/align]
推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙上,那么箱子只能沿着墙移动.
现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.
[align=left]Input[/align]
输入数据的第一行是一个整数T(1<=T<=20),代表测试数据的数量.然后是T组测试数据,每组测试数据的第一行是两个正整数M,N(2<=M,N<=7),代表房间的大小,然后是一个M行N列的矩阵,代表房间的布局,其中0代表空的地板,1代表墙,2代表箱子的起始位置,3代表箱子要被推去的位置,4代表搬运工的起始位置.
[align=left]Output[/align]
对于每组测试数据,输出搬运工最少需要推动箱子多少格才能帮箱子推到指定位置,如果不能推到指定位置则输出-1.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
4
测试数据:
7
5 5 3 0 1 0 0 1 0 1 4 0 0 0 1 0 0 1 0 2 0 0 0 0 0 0 0 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 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 2 1 1 4 0 3 0 0
输出:
-1
4
5
#include <iostream> #include<cstdio> #include<cstring> #include<queue> #include<cmath> using namespace std; int m,n,flag; int mazz[10][10]; bool vis[10][10][10][10]; bool fot[10][10]; int dir[4][2]={0,1,0,-1,1,0,-1,0}; struct node { int x,y,rx,ry,t; friend bool operator < (node a,node b) { return a.t > b.t; } }; int judge(int x,int y,int rx,int ry) { if(vis[x][y][rx][ry]) return 0; if(x<0||x>=m||y<0||y>=n) return 0; if(mazz[x][y]!=0) return 0; return 1; } int j_udge(int x,int y) { if(fot[x][y]) return 0; if(x<0||x>=m||y<0||y>=n) return 0; if(mazz[x][y]!=0) return 0; return 1; } void dfs(int sx,int sy,int ex,int ey) { if(sx==ex&&sy==ey) { flag=1; return ; } int nx,ny; for(int i=0;i<4;++i) { nx=sx+dir[i][0]; ny=sy+dir[i][1]; if(j_udge(nx,ny)) { fot[nx][ny]=true; dfs(nx,ny,ex,ey); } } } int solve(int x,int y,int sx,int sy,int ex,int ey) { memset(fot,false,sizeof(fot)); fot[x][y]=true; fot[sx][sy]=true; flag=0; dfs(sx,sy,ex,ey); if(flag) return 1; else return 0; } void bfs(int x,int y,int sx,int sy,int ex,int ey) { node now,ss,next; ss.x=sx; ss.y=sy; ss.rx=x; ss.ry=y; ss.t=0; memset(vis,false,sizeof(vis)); vis[sx][sy][x][y]=true; priority_queue<node>q; q.push(ss); while(!q.empty()) { now=q.top(); q.pop(); //printf("x=%d y=%d\n",now.x,now.y); if(now.x==ex&&now.y==ey) { printf("%d\n",now.t); return ; } for(int i=0;i<4;++i) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; next.rx=now.x-dir[i][0]; next.ry=now.y-dir[i][1]; //printf("%d %d j=%d s=%d\n",dir[i][0],dir[i][1],judge(next.x,next.y),solve(now.x,now.y,now.rx,now.ry,next.rx,next.ry)); if(judge(next.x,next.y,next.rx,next.ry)&&solve(now.x,now.y,now.rx,now.ry,next.rx,next.ry)) { next.t=now.t+1; vis[next.x][next.y][next.rx][next.ry]=true; q.push(next); } } } printf("-1\n"); } int main() { int t,x,y,sx,sy,ex,ey; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); for(int i=0;i<m;++i) { for(int j=0;j<n;++j) { scanf("%d",&mazz[i][j]); if(mazz[i][j]==4) { x=i,y=j; mazz[i][j]=0; } if(mazz[i][j]==2) { sx=i,sy=j; mazz[i][j]=0; } if(mazz[i][j]==3) { ex=i,ey=j; mazz[i][j]=0; } } } bfs(x,y,sx,sy,ex,ey); } return 0; }
相关文章推荐
- MOOC《Linux内核分析》第四课
- linux c/c++网络编程----select模型
- Rust 中包访问策略和module开发策略
- 练习脚本三:日志清除
- IOS APP 上传到AppStore
- uva 414 Machined Surfaces
- Python list、tuple、dict区别
- 【2011集训队出题】Crash的数字表格
- VMWare 12 序列号/注册机
- 第三周项目10-小学生计算题的随机生成
- IMMNotificationClient interface(MSDN理解)
- 20.ELK实时日志分析平台之Elasticsearch 查询简介
- OC中的字典NSDictionary
- 练习脚本二:批量添加用户
- 任务三:三栏式布局
- Python:OS 模块 -- 进程管理
- win10下apache不能正常启动的解决方案之一
- VPCS不能通过dhcp协议在win server下获取ip地址的验证性试验
- iOS 开发基础UIControl事件
- onDraw绘画