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, 以下是两种解法的代码 :

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值