hdu 1254 推箱子
来源:互联网 发布:mac系统usb转网口设置 编辑:程序博客网 时间:2024/06/11 03:28
推箱子游戏:0代表空的地板,1代表墙,2代表箱子的起始位置,3代表箱子要被推去的位置,4代表搬运工的起始位置.
问你箱子最少移动多少次能到目的位置,如无法到达则输出-1.
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<queue>using namespace std;int n, m, a[10][10], move[4][2]={0,1,0,-1,1,0,-1,0}, vis[10][10][10][10], flag[10][10];struct node{int x, y, px, py, step;}st, end; //箱子位置x,y,人的位置px,py。bool dfs(int sx, int sy, int tx, int ty){ if(sx==tx&&sy==ty)return true; for(int i=0; i<4; i++){ int x1=sx+move[i][0]; int y1=sy+move[i][1]; if(x1==0||y1==0||x1>n||y1>m||flag[x1][y1]||a[x1][y1]==1)continue; flag[x1][y1]=1; if(dfs(x1, y1, tx, ty))return true; } return false;}int bfs(){ queue<node> q; st.step=0; q.push(st); int i, sx, sy; memset(vis, 0, sizeof(vis)); vis[st.x][st.y][st.px][st.py]=1; node head, next; while(!q.empty()){ head=q.front(); q.pop(); for(i=0; i<4; i++){ next.x=head.x+move[i][0]; next.y=head.y+move[i][1]; next.px=head.x; next.py=head.y; next.step=head.step+1; if(next.x==0||next.y==0||next.x>n||next.y>m|| vis[next.x][next.y][head.x][head.y]||a[next.x][next.y]==1)continue; sx=head.x-move[i][0];sy=head.y-move[i][1]; if(sx==0||sy==0||sx>n||sy>m||a[sx][sy]==1)continue; a[head.x][head.y]=1; memset(flag, 0, sizeof(flag)); flag[head.px][head.py]=1; if(dfs(head.px, head.py, sx, sy)){ //判断搬运工能否到达要推的位置 vis[next.x][next.y][head.x][head.y]=1; if(next.x==end.x&&next.y==end.y){ return next.step; } q.push(next); } a[head.x][head.y]=0; } } return -1;}int main(){ //freopen("1.txt", "r", stdin); int T, i, j; scanf("%d", &T); while(T--){ scanf("%d %d", &n, &m); for(i=1; i<=n; i++) for(j=1; j<=m; j++){ scanf("%d", &a[i][j]); if(a[i][j]==2){ st.x=i;st.y=j; } else if(a[i][j]==3){ end.x=i;end.y=j; } else if(a[i][j]==4){ st.px=i;st.py=j; } } printf("%d\n", bfs()); } return 0;}
- HDU 1254 推箱子
- HDU 1254 推箱子
- HDU 1254 推箱子
- hdu 1254 推箱子
- hdu 1254 推箱子
- hdu 1254 推箱子
- hdu 1254 推箱子
- hdu 1254 推箱子
- hdu 1254 推箱子
- HDU 1254 推箱子
- HDU 1254 推箱子
- HDU 1254 推箱子
- hdu 1254 推箱子
- hdu 1254 推箱子
- HDU 1254推箱子
- hdu 1254 推箱子
- hdu 1254 推箱子
- HDU 1254 推箱子
- 怎样看linux的源代码
- 写字符串到文本【可设定格式】
- 2.17-比较有头节点的单链表与无头节点插入元素的区别;
- 杭电2006 求奇数的乘积
- 帮忙看一个大型oltp数据库"穷"存储规划~
- hdu 1254 推箱子
- c/c++源代码50名优秀网站和网页清单
- 10905 - Children's Game
- ubuntu建立开发板环境
- 堆排序
- 堆排序(heap sort)
- Qt信号与槽
- C++ 引用及其与指针的区别
- 解决 JqueryMobile backbtn 默认出现的问题