题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1254

题解:以箱子为主体,第一层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[][],gg[][],t,m,n,x1,y1,x2,y2,d[][]={,,-,,,,,-};
bool v[][][],f[][];
bool checkmen(int x,int y){
if(f[x][y]||x<||x>n||y<||y>m||gg[x][y]!=)return ;
return ;
}
bool checkbox(int x,int y,int i){
if(v[x][y][i]||x<||x>n||y<||y>m||g[x][y]==)return ;
return ;
}
int bfs(int sx,int sy,int ex,int ey){
if(ex<||ex>n||ey<||ey>m)return -;
memset(f,,sizeof(f));
dtm s,o;s.x=sx,s.y=sy,s.t=,f[sx][sy]=;
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=;i<;i++){
int xx=o.x+d[i][],yy=o.y+d[i][];
if(!checkmen(xx,yy))continue;
s.x=xx,s.y=yy,s.t=t+,f[xx][yy]=;
Q.push(s);
}
}
return -;
}
int fuck(){
memset(v,,sizeof(v));
dtb s,o;s.x=x1,s.y=y1,s.mx=x2,s.my=y2,s.t=;
queue<dtb>Q;Q.push(s);
while(!Q.empty()){
o=Q.front();Q.pop();
if(g[o.x][o.y]==)return o.t;
for(int i=;i<;i++){
int xx=o.x+d[i][],yy=o.y+d[i][],ok;
if(!checkbox(xx,yy,i))continue;
gg[o.x][o.y]=;
ok=bfs(o.mx,o.my,o.x-d[i][],o.y-d[i][]),gg[o.x][o.y]=;
if(ok==-)continue;
s.x=xx,s.y=yy,s.mx=o.x,s.my=o.y,s.t=o.t+,v[xx][yy][i]=;
Q.push(s);
}
}
return -;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
FFC(i,,n)FFC(j,,m){
scanf("%d",&g[i][j]);
gg[i][j]=(g[i][j]==?:);
if(g[i][j]==)x1=i,y1=j;
if(g[i][j]==)x2=i,y2=j;
}
printf("%d\n",fuck());
}
return ;
}

最新文章

  1. ASP.NET Web数据控件
  2. laravel 开启sql查询日志
  3. javascript面向对象思想2
  4. Where is Vasya?
  5. ASP大数据量使用GetRows()提升速度
  6. C#基础:集合
  7. leetcode第一刷_Path Sum II
  8. Python学习入门基础教程(learning Python)--6.3 Python的list切片高级
  9. 同源策略 &amp; 高效调试CORS实现
  10. react-create-app 构建react项目的流程以及需要注意的地方
  11. N维偏序:cdq分治
  12. layui(四)——table组件常见用法总结
  13. Luogu2467 SDOI2010 地精部落 DP
  14. hihoCoder 1515 分数调查(带权并查集)
  15. windows7下安装apache+PHP5.3
  16. C++系统时间及字符串转换参考资料
  17. 获取tomcat源码
  18. (原创)确保JAVA线程安全的4种常用方法
  19. WPF之换肤
  20. 1.8(SQL学习笔记)触发器

热门文章

  1. FireFox的配置文件的引用
  2. Selenium 中文API
  3. React Redux学习笔记
  4. JAVA监听
  5. Html批量读取json
  6. Java中的DateFormatter
  7. html 超链接(a)详细讲解
  8. 【1】Chrome - 更换主题
  9. JavaScript事件响应的基础语法总结
  10. Web程序和应用程序服务器[转]