hdu 1254 推箱子(两次广搜)
2014-02-08 19:55
253 查看
/*
这题一个下午了,现在终于过了;
用了两次广搜,一次搜箱子到目标地,第二次搜人是可以到推动箱子的地方,要注意的是箱子可能会阻挡人,也可以来回的推。
给几组数据就知道了(这题数据都是后面讨论版中提供的)
10
4 3
0 0 0
0 0 1
0 2 3
1 4 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
4 4
1 2 1 1
0 0 0 0
0 4 3 0
0 0 0 0
4 4
0 3 0 0
0 0 2 0
0 0 4 0
0 1 0 1
3 3
0 3 0
1 0 2
0 4 0
5 5
0 1 4 0 0
0 1 1 1 1
0 2 3 0 0
0 0 0 0 0
0 0 0 0 0
4 4
0 1 0 1
0 2 0 4
0 0 0 0
0 3 0 1
*/
这题一个下午了,现在终于过了;
用了两次广搜,一次搜箱子到目标地,第二次搜人是可以到推动箱子的地方,要注意的是箱子可能会阻挡人,也可以来回的推。
给几组数据就知道了(这题数据都是后面讨论版中提供的)
10
4 3
0 0 0
0 0 1
0 2 3
1 4 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
4 4
1 2 1 1
0 0 0 0
0 4 3 0
0 0 0 0
4 4
0 3 0 0
0 0 2 0
0 0 4 0
0 1 0 1
3 3
0 3 0
1 0 2
0 4 0
5 5
0 1 4 0 0
0 1 1 1 1
0 2 3 0 0
0 0 0 0 0
0 0 0 0 0
4 4
0 1 0 1
0 2 0 4
0 0 0 0
0 3 0 1
*/
#include<iostream> #include<queue> #define N 8 using namespace std; int n, m, str , mark , pmark , dir[4][2]={1,0, -1,0, 0,1, 0,-1}; int s_x, s_y, e_x, e_y, p_x, p_y; struct node { int x, y, step, dx, dy; }; int judge(int x, int y) { if(x>=0 && x< n && y>=0 && y<m && str[x][y] != 1) return 1; else return 0; } int work(int box_x, int box_y, int end_x, int end_y, int sta_x, int sta_y) { int i, x, y; node cur, next; queue<node>q2; cur.x = sta_x; cur.y = sta_y; cur.step=0; q2.push(cur); while(!q2.empty()) { cur = q2.front(); q2.pop(); if(cur.x==end_x && cur.y==end_y) return cur.step; for( i=0; i < 4; i++ ) { next.x = x = cur.x + dir[i][0]; next.y = y = cur.y + dir[i][1]; next.step = cur.step + 1; if(judge(x, y) && pmark[x][y]==0 && (x!=box_x || y!=box_y)) { pmark[x][y] = 1; q2.push(next); } } } return -1; } int bfs() { int i; node cur, next; queue<node>q1; cur.x=s_x; cur.y=s_y; cur.step=0; next.dx=cur.dx=p_x; next.dy=cur.dy=p_y; q1.push(cur); while(!q1.empty()) { cur = q1.front(); q1.pop(); if(cur.x==e_x && cur.y==e_y) return cur.step; for( i=0; i < 4; i++ ) { next.x = cur.x+dir[i][0]; next.y = cur.y+dir[i][1]; next.step = cur.step+1; //next.dx = cur.x; next.dy = cur.y; memset(pmark, 0, sizeof(pmark)); int ans = work(cur.x, cur.y, cur.x-dir[i][0], cur.y-dir[i][1], cur.dx, cur.dy); if(ans!=-1 && judge(next.x, next.y) && mark[next.x][next.y]<=30) { mark[next.x][next.y] += 1; next.dx = cur.x-dir[i][0]; next.dy = cur.y-dir[i][1]; q1.push(next); } } } return 0; } int main() { int T, i, j; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); for( i=0; i < n; i++ ) { for(j=0; j < m; j++ ) { scanf("%d", &str[i][j]); if(str[i][j] == 2) s_x=i, s_y=j; if(str[i][j] == 3) e_x=i, e_y=j; if(str[i][j] == 4) p_x=i, p_y=j; } } memset(mark, 0, sizeof(mark)); int ans=bfs(); if(ans==0) printf("-1\n"); else printf("%d\n", ans); } return 0; }
相关文章推荐
- HDU_1254——推箱子,两次BFS
- hdu 1254 推箱子(BFS+BFS或BFS+DFS)
- hdu 1254 推箱子
- hdu 1254 推箱子
- HDU 1254推箱子
- hdu1254 推箱子
- HDU 1254 推箱子游戏(搞了一下午。。。)
- HDU 1254 推箱子 C++优先队列+bfs
- HDU 1254 推箱子 (搜索中套搜索,BFS + DFS 这感觉,酸爽!)
- HDU 1254 推箱子(BFS)
- hdu 1254-推箱子
- HDU 1254 推箱子
- HDU 1254 推箱子(双重bfs)
- HDU- 1254 推箱子(BFS)
- HDU 1254:推箱子
- hdu 1254 推箱子(bfs判断路径可达+bfs搜索最小)
- HDU 1254 推箱子 (BFS + DFS)
- hdu 1254 推箱子(嵌套搜索)
- hdu_1254_推箱子(双BFS)
- HDU 1254 推箱子