acm algorithm practice Jan 3 BFS

poj 2251

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
L is the number of levels making up the dungeon. 
R and C are the number of rows and columns making up the plan of each level. 
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape. 
If it is not possible to escape, print the line 
Trapped!

-------------------------------

it is a simple BFS problem, but at beginning, I didn't find its BFS solution. (actually, because of the false category, I mistaken it as DFS problem ).

since we want to find the shortest path, obviously BFS satisfies the condition. (BFS move forward one step each round, if we use DFS, it will trace all the routes before we can decide which route is the shortest)

in this case,it is the  first time we use class. actually the class  here has nothing different with struct we used before. it is a good structure to abstract a Point in the graph.(the Point has x, y, z axis and info)

 
  
class Point
{
public :
int x, y, z, count;
Point(){};
Point(
int tx, int ty, int tz, int tcount):x(tx),y(ty),z(tz),count(tcount){};
};

 

then we use BFS, pseudocode:

while(!queue.empty()){

  point <---dequeue

  //move 1 step towards 4 +2 =6 directions.

  //if the block is 'E'

    record and return 

  //if the block is '.'

     put the block in queue and set the block '#'

}

here the key is a count in Point structure, which records the path length. 

代码
 
   
while ( ! q.empty()){
Point point
= q.front();
q.pop();
// in 4 directions left right forward backward
for ( int i = 0 ; i < 4 ; i ++ ){
int tx = point.x + dir_x[i];
int ty = point.y + dir_y[i];
if (maze[point.z][tx][ty] == ' E ' ){ // exit
res = point.count + 1 ;
return ;
}
if (tx > 0 && tx <= R && ty > 0 && ty <= C && maze[point.z][tx][ty] == ' . ' ){ // bfs
maze[point.z][tx][ty] = ' # ' ;
q.push(Point(tx, ty, point.z, point.count
+ 1 ));
}
}
// upside
if (point.z > 1 && maze[point.z - 1 ][point.x][point.y] == ' E ' ){
res
= point.count + 1 ;
return ;
}
if (point.z > 1 && maze[point.z - 1 ][point.x][point.y] == ' . ' ){
maze[point.z
- 1 ][point.x][point.y] = ' # ' ;
q.push(Point(point.x, point.y, point.z
- 1 , point.count + 1 ));
}
// downside
if (point.z < L && maze[point.z + 1 ][point.x][point.y] == ' E ' ){
res
= point.count + 1 ;
return ;
}
if (point.z < L && maze[point.z + 1 ][point.x][point.y] == ' . ' ){
maze[point.z
+ 1 ][point.x][point.y] = ' # ' ;
q.push(Point(point.x, point.y, point.z
+ 1 , point.count + 1 ));
}
}

 

-----------------------

misc 

we use 

  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
to deal with input and output.

 

we use char maze[][][] as the data structure to store the info of the maze

the input of the maze is :

    for (int i = 1; i<=L ; i++){
      for (int j = 1; j<= R; j++) {
	for(int k = 1; k<= C; k++){
	  cin>> maze[i][j][k];
	  if(maze[i][j][k] == 'S'){
	    startz = i;
	    startx = j;
	    starty = k;
	  }
	}
      }
    }
 

 

转载于:https://www.cnblogs.com/ggppwx/archive/2011/01/05/1925969.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值