TOJ3100 BFS or DFS

3100.   Getting Gold

Time Limit: 5.0 Seconds   Memory Limit: 65536K
Total Runs: 246   Accepted Runs: 142    Multiple test files



We're building an old-school back-to-basics computer game. It's a very simple text based adventure game where you walk around and try to find treasure, avoiding falling into traps. The game is played on a rectangular grid and the player gets very limited information about her surroundings.

The game will consist of the player moving around on the grid for as long as she likes (or until she falls into a trap). The player can move up, down, left and right (but not diagonally). She will pick up gold if she walks into the same square as the gold is. If the player stands next to (i.e., immediately up, down, left, or right of) one or more traps, she will "sense a draft" but will not know from what direction the draft comes, or how many traps she's near. If she tries to walk into a square containing a wall, she will notice that there is a wall in that direction and remain in the position where she was.

For scoring purposes, we want to show the player how much gold she could have gotten safely. That is, how much gold can a player get playing with an optimal strategy and always being sure that the square she walked into was safe. The player does not have access to the map and the maps are randomly generated for each game so she has no previous knowledge of the game.

Input

The first line of input contains two positive integers W and H , neither of them smaller than 3 or larger than 50, giving the width and the height of the map, respectively. The next H lines contain W characters each, giving the map. The symbols that may occur in a map are as follows:

P - the player/'s starting position

G - a piece of gold

T - a trap

# - a wall

. - normal floor

There will be exactly one 'P' in the map, and the border of the map will always contain walls.

Output

Output the number of pieces of gold the player can get without risking falling into a trap.

Sample Input

7 4

#######

#P.GTG#

#..TGG#

#######

Sample Output

1

Sample Input 2

8 6

########

#...GTG#

#..PG.G#

#...G#G#

#..TG.G#

########

Sample Output 2

4

 

 

此题有点灵活 , 可以 DFS 也可以 BFS, 以下是两种解法的代码 :

 

//DFS #include<stdio.h> #include<string.h> int step[55][55],res; char g[55][55]; int w,h,i,j,p,q; void dfs(int x,int y) { if (step[x][y]) return ; step[x][y]=1; if (g[x][y]=='G') {res++;g[x][y]='.';} if ((g[x][y+1]=='T')||(g[x][y-1]=='T')||(g[x+1][y]=='T')||(g[x-1][y]=='T')) return ; else { if ((g[x][y+1]!='#')&&(y<w)) dfs(x,y+1); if ((g[x][y-1]!='#')&&(y>0)) dfs(x,y-1); if ((g[x+1][y]!='#')&&(x<h)) dfs(x+1,y); if ((g[x-1][y]!='#')&&(x>0)) dfs(x-1,y); } } int main() { while (scanf("%d%d",&w,&h)!=EOF) { res=0; memset(step,0,sizeof(step)); gets(g[1]); for(i=1;i<=h;++i) gets(g[i]); for (i=1;i<=h;++i) for (j=1;j<=w;++j) { if (g[i][j]=='P') {p=i;q=j;break;} } dfs(p,q); printf("%d/n",res); } return 0; } //BFS #include<stdio.h> #include<string.h> int step[55][55],res; char g[55][55]; int w,h,i,j,p,q; int qx[10000],qy[10000],h1,h2,t1,t2; void bfs(int x,int y) { h1=h2=t1=t2=0; qx[t1++]=x; qy[t2++]=y; step[x][y]=1; while (h1<t1) { x=qx[h1++];y=qy[h2++]; if (g[x][y]=='G') {res++;g[x][y]='.';} if ((g[x][y+1]=='T')||(g[x][y-1]=='T')||(g[x+1][y]=='T')||(g[x-1][y]=='T')) ; else { if ((g[x][y+1]!='#')&&(y<w)&&!step[x][y+1]) {qx[t1++]=x;qy[t2++]=y+1;step[x][y+1]=1;} if ((g[x][y-1]!='#')&&(y>0)&&!step[x][y-1]) {qx[t1++]=x;qy[t2++]=y-1;step[x][y-1]=1;} if ((g[x+1][y]!='#')&&(x<h)&&!step[x+1][y]) {qx[t1++]=x+1;qy[t2++]=y;step[x+1][y]=1;} if ((g[x-1][y]!='#')&&(x>0)&&!step[x-1][y]) {qx[t1++]=x-1;qy[t2++]=y;step[x-1][y]=1;} } } } int main() { while (scanf("%d%d",&w,&h)!=EOF) { res=0; memset(step,0,sizeof(step)); gets(g[1]); for(i=1;i<=h;++i) gets(g[i]); for (i=1;i<=h;++i) for (j=1;j<=w;++j) { if (g[i][j]=='P') {p=i;q=j;break;} } bfs(p,q); printf("%d/n",res); } return 0; }

你可能感兴趣的:(input,Access,UP,each,border,output)