首页 > 其他 > 详细

light oj 1012 Guilty Prince(dfs )

时间:2015-11-16 22:25:25      阅读:317      评论:0      收藏:0      [点我收藏+]
1012 - Guilty Prince
 
 

Once there was a king named Akbar. He had a son named Shahjahan. For an unforgivable reason the king wanted him to leave the kingdom. Since he loved his son he decided his son would be banished in a new place. The prince became sad, but he followed his father‘s will. In the way he found that the place was a combination of land and water. Since he didn‘t know how to swim, he was only able to move on the land. He didn‘t know how many places might be his destination. So, he asked your help.

For simplicity, you can consider the place as a rectangular grid consisting of some cells. A cell can be a land or can contain water. Each time the prince can move to a new cell from his current position if they share a side.

Now write a program to find the number of cells (unit land) he could reach including the cell he was living.

Input

Input starts with an integer T (≤ 500), denoting the number of test cases.

Each case starts with a line containing two positive integers W and H; W and H are the numbers of cells in the x and y directions, respectively. W and H are not more than 20.

There will be H more lines in the data set, each of which includes W characters. Each character represents the status of a cell as follows.

1) ‘.‘ - land

2) ‘#‘ - water

3) ‘@‘ - initial position of prince (appears exactly once in a dataset)

Output

For each case, print the case number and the number of cells he can reach from the initial position (including it).

Sample Input

Output for Sample Input

4

6 9

....#.

.....#

......

......

......

......

......

#@...#

.#..#.

11 9

.#.........

.#.#######.

.#.#.....#.

.#.#.###.#.

.#.#..@#.#.

.#.#####.#.

.#.......#.

.#########.

...........

11 6

..#..#..#..

..#..#..#..

..#..#..###

..#..#..#@.

..#..#..#..

..#..#..#..

7 7

..#.#..

..#.#..

###.###

...@...

###.###

..#.#..

..#.#..

Case 1: 45

Case 2: 59

Case 3: 6

Case 4: 13

 

 

题目大意: 简单来说 就是与"@"符号连在一起的点有几个,“@”也算一个,就是这个样子,,可以用bfs 也可以用bfs ,模板题

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 bool vis[25][25];               //访问标记,访问了为1 没访问的为0 
 5 char map[25][25];               //地图
 6 int dir[4][2] = {0,1,0,-1,1,0,-1,0};        //四个方向, 分别是下,上,右,左
 7 int w,h,res;
 8 int check(int x,int y)
 9 {
10     if( vis[x][y] == 0 && x >= 0 && x < h && y >= 0 && y < w && map[x][y] == .)   //判断能不能走
11     {
12         return 1;
13     }
14     else 
15     {
16         return 0;
17     }
18 }
19 void dfs(int x,int y)
20 {
21     
22     vis[x][y] = 1;
23     for (int i = 0 ; i < 4 ; i++ )      //四个方向都试一次
24     {
25         if(check(x+dir[i][0],y+dir[i][1]))
26         {
27             res++;                  //如果可走,则结果++
28             dfs(x+dir[i][0],y+dir[i][1]);
29         }
30 
31     }
32     return ;
33 }
34 int main()
35 {
36     int t;
37     scanf("%d",&t);
38     for (int cas = 1; cas <= t ; cas++ )
39     {
40         res = 1;
41         memset(vis,0,sizeof(vis));
42         scanf("%d %d",&w,&h);
43         getchar();
44         int x,y;        //记录"@"符号的坐标
45         for (int  i = 0 ; i < h; i++ )
46         {
47             for (int  j = 0 ; j < w ; j++ )
48             {
49                 scanf("%c",&map[i][j]);
50                 if ( map[i][j] == @)  
51                 {
52                     x = i;
53                     y = j;
54                 }
55             }
56             getchar();
57         }
58         
59         dfs(x,y);
60         printf("Case %d: %d\n",cas,res);
61     }
62     return 0;
63 }

 

light oj 1012 Guilty Prince(dfs )

原文:http://www.cnblogs.com/hkhv/p/4970029.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!