http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1249
题目的大致意思是,给定一个R*C 的maze,其中分别有一个人,一个箱子和一个箱子要推到的目标点,其他的格子分成可走和不可走两种,现在要求在给定的情形之下求出人推箱子到目标点最小需要的步数。
思路:BFS+优先队列。将人和箱子的位置作为一个状态,这里可以将一个点的坐标用20进制压缩成一个数,然后用一个二维数组进行判重。优先队列每次取出最少的步数。
代码:
/*
BFS+优先队列
*/
#include<stdio.h>
#include<string.h>
#include<queue>
#define Base 20
using namespace std ;
int R,C;
struct point{
int r,c ;
point(){}
point(int rr ,int cc)
: r(rr) , c(cc){}
bool operator =(point a){
r = a.r ; c = a.c ;
}
int Zip(){
int res = 0 ;
res = r * Base + c ;
return res ;
}
void Unzip(int num){
c = num % Base ;
r = num / Base ;
}
}S,Box,Target,you_pos,box_pos,n_p1,n_p2;
struct State{
int p_num,t_num ;
int num1 , num2 ;
State(){}
State(int a,int b ,int c , int d)
:num1(a) , num2(b) , p_num(c) , t_num(d) {}
friend bool operator < (State s1 , State s2){
if(s1.p_num == s2.p_num) return s1.t_num > s2.t_num ;
else return s1.p_num > s2.p_num ;
}
};
char map[21][21] ;
bool vis[400][400] ;
int way[400][400] ;
int dr[4] = {0,0,1,-1} ; //右 ,左 , 下,上
int dc[4] = {1,-1,0,0} ;
int re_dr[4] = {0,0,-1,1} ;
int re_dc[4] = {-1,1,0,0} ;
priority_queue<State> que ;
char dir[4] = {'e','w','s','n'} ;
void PRINT(int zip1,int zip2){
point p1, p2 ,n_p1,n_p2;
p1.Unzip(zip1); p2.Unzip(zip2);
int w = way[zip1][zip2] ,z1,z2;
if(w == -1) return ;
n_p1.r = p1.r + re_dr[w%4] ;
n_p1.c = p1.c + re_dc[w%4] ;
if(w >= 4){
n_p2.r = p2.r + re_dr[w%4] ;
n_p2.c = p2.c + re_dc[w%4] ;
}
else
n_p2 = p2 ;
z1 = n_p1.Zip() ; z2 = n_p2.Zip() ;
PRINT(z1,z2);
if(w >= 4){
printf("%c",dir[w%4]-32);
}
else
printf("%c",dir[w%4]);
}
bool bfs(){
State s1 ;
point p1, p2 ;
int zip1 , zip2 ,push , total ,z1,z2;
memset(vis,0,sizeof(vis));
memset(way,-1,sizeof(way));
while(!que.empty()) que.pop() ;
zip1 = S.Zip() ; zip2 = Box.Zip() ;
que.push( State(zip1,zip2,0,0) );
vis[zip1][zip2] = 1 ;
while(!que.empty()){
s1 = que.top(); que.pop() ;
zip1 = s1.num1 ; zip2 = s1.num2 ;
push = s1.p_num ; total = s1.t_num ;
you_pos.Unzip(zip1); box_pos.Unzip(zip2);
if(box_pos.r==Target.r && box_pos.c==Target.c){
PRINT(zip1,zip2);
printf("\n");
return true ;
}
for(int i=0;i<4;i++){
int nr = you_pos.r + dr[i] ;
int nc = you_pos.c + dc[i] ;
if(nr<0 || nc<0 || nr>=R || nc>=C || map[nr][nc]=='#') continue ;
if(box_pos.r==nr && box_pos.c==nc){
if(nr+dr[i]<0 || nr+dr[i]>=R || nc+dc[i]<0 || nc+dc[i]>=C || map[nr+dr[i]][nc+dc[i]]=='#') continue ;
p1.r = nr ; p1.c = nc ;
p2.r = nr + dr[i] ; p2.c = nc + dc[i] ;
z1 = p1.Zip() ; z2 = p2.Zip() ;
if(vis[z1][z2]) continue ;
vis[z1][z2] = 1 ;
que.push( State(z1,z2,push+1,total+1)) ;
way[z1][z2] = i + 4 ;
}
else{
p1.r = nr ; p1.c = nc ;
p2 = box_pos ;
z1 = p1.Zip() ; z2 = p2.Zip() ;
if(vis[z1][z2]) continue ;
vis[z1][z2] = 1 ;
que.push( State(z1,z2,push,total+1));
way[z1][z2] = i ;
}
}
}
return false ;
}
int main(){
int cas = 1 ;
while(scanf("%d %d",&R,&C) && (R+C)){
for(int i=0;i<R;i++){
scanf("%s",map[i]);
for(int j=0;j<C;j++){
if(map[i][j] == 'S'){
S.r = i ; S.c = j ;
map[i][j] = '.' ;
}
else if(map[i][j] == 'B'){
Box.r = i ; Box.c = j ;
map[i][j] = '.' ;
}
else if(map[i][j] == 'T'){
Target.r = i ; Target.c = j ;
map[i][j] = '.' ;
}
}
}
printf("Maze #%d\n",cas++);
if(!bfs()){
printf("Impossible.\n");
}
printf("\n");
}
return 0 ;
}
本文介绍了一种使用BFS算法结合优先队列解决迷宫问题的方法,详细解释了如何通过状态压缩和二维数组判断重复状态,以及如何优化搜索过程以找到人推箱子到目标点的最短路径。
1009

被折叠的 条评论
为什么被折叠?



