hdu_1254_推箱子(双BFS)
2016-05-13 00:57
405 查看
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1254
题解:以箱子为主体,第一层BFS,然后用第二层BFS来判断人是否可以到达,这里细节比较多,要注意
题解:以箱子为主体,第一层BFS,然后用第二层BFS来判断人是否可以到达,这里细节比较多,要注意
#include<cstdio> #include<queue> #include<cstring> using namespace std; #define FFC(i,a,b) for(int i=a;i<=b;i++) struct dtm{int x,y,t;}; struct dtb{int x,y,t,mx,my;}; int g[10][10],gg[10][10],t,m,n,x1,y1,x2,y2,d[][2]={1,0,-1,0,0,1,0,-1}; bool v[10][10][4],f[10][10]; bool checkmen(int x,int y){ if(f[x][y]||x<1||x>n||y<1||y>m||gg[x][y]!=0)return 0; return 1; } bool checkbox(int x,int y,int i){ if(v[x][y][i]||x<1||x>n||y<1||y>m||g[x][y]==1)return 0; return 1; } int bfs(int sx,int sy,int ex,int ey){ if(ex<1||ex>n||ey<1||ey>m)return -1; memset(f,0,sizeof(f)); dtm s,o;s.x=sx,s.y=sy,s.t=0,f[sx][sy]=1; queue<dtm>Q;Q.push(s); while(!Q.empty()){ o=Q.front();Q.pop(); if(o.x==ex&&o.y==ey)return o.t; for(int i=0;i<4;i++){ int xx=o.x+d[i][0],yy=o.y+d[i][1]; if(!checkmen(xx,yy))continue; s.x=xx,s.y=yy,s.t=t+1,f[xx][yy]=1; Q.push(s); } } return -1; } int fuck(){ memset(v,0,sizeof(v)); dtb s,o;s.x=x1,s.y=y1,s.mx=x2,s.my=y2,s.t=0; queue<dtb>Q;Q.push(s); while(!Q.empty()){ o=Q.front();Q.pop(); if(g[o.x][o.y]==3)return o.t; for(int i=0;i<4;i++){ int xx=o.x+d[i][0],yy=o.y+d[i][1],ok; if(!checkbox(xx,yy,i))continue; gg[o.x][o.y]=2; ok=bfs(o.mx,o.my,o.x-d[i][0],o.y-d[i][1]),gg[o.x][o.y]=0; if(ok==-1)continue; s.x=xx,s.y=yy,s.mx=o.x,s.my=o.y,s.t=o.t+1,v[xx][yy][i]=1; Q.push(s); } } return -1; } int main(){ scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); FFC(i,1,n)FFC(j,1,m){ scanf("%d",&g[i][j]); gg[i][j]=(g[i][j]==1?1:0); if(g[i][j]==2)x1=i,y1=j; if(g[i][j]==4)x2=i,y2=j; } printf("%d\n",fuck()); } return 0; }
相关文章推荐
- 利用sqoop完成数据导入导出
- Objective-C基础笔记二
- PHP注册页面实现
- leetcode_c++:Next Permutation (031)
- 20款最受欢迎的HTML5游戏引擎收集
- Big-Endian和Little-Endian
- StringBuilder和StringBuffer源码浅析
- RCTF2015 pwn试题分析
- EZ-USB FX2 LP CY7C68013A 开发基本概念
- ubuntu为Python添加默认搜索路径
- 345.[LeetCode]Reverse Vowels of a String
- 5分钟破解wpa2密码(转)
- 安全卫士第三课 设置中心界面——自定义View学习
- ubuntu 15.10 gnome sublime packagecontrol 安装以使用
- Java中线程同步锁和互斥锁-多线程编程
- 10+年程序员总结的20+条经验教训
- Unity3D【教材Unity3D游戏开发:2.7】
- 产品思维学习(四)--增长黑客(Growth Hacker)
- Spring 系列:AOP原理
- Storm Topology及分组原理