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

猜你喜欢

转载自blog.csdn.net/weixin_34414196/article/details/93871308