OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

1.链接地址:

http://bailian.openjudge.cn/practice/1979

http://poj.org/problem?id=1979

2.题目:

总时间限制:
1000ms
内存限制:
65536kB
描述
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.
输入
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
输出
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
样例输入
6 9

....#.

.....#

......

......

......

......

......

#@...#

.#..#.

11 9

.#.........

.#.#######.

.#.#.....#.

.#.#.###.#.

.#.#..@#.#.

.#.#####.#.

.#.......#.

.#########.

...........

11 6

..#..#..#..

..#..#..#..

..#..#..###

..#..#..#@.

..#..#..#..

..#..#..#..

7 7

..#.#..

..#.#..

###.###

...@...

###.###

..#.#..

..#.#..

0 0
样例输出
45

59

6

13

来源
Japan 2004 Domestic

3.思路:

4.代码:

 1 #include <iostream>

 2 #include <cstdio>

 3 

 4 using namespace std;

 5 

 6 int f(char **arr,const int i,const int j,int w,int h)

 7 {

 8     int res = 0;

 9     if(arr[i][j] == '.') 

10     {

11         res += 1; 

12         arr[i][j] = '#';

13         if(i > 0) res += f(arr,i - 1,j,w,h);

14         if(i < h - 1) res += f(arr,i + 1,j,w,h);

15         if(j > 0) res += f(arr,i,j - 1,w,h);

16         if(j < w - 1) res += f(arr,i,j + 1,w,h);

17     }

18     return res;

19 }

20 

21 

22 int main()

23 {

24     //freopen("C:\\Users\\wuzhihui\\Desktop\\input.txt","r",stdin);

25 

26     int i,j;

27 

28     int w,h;

29     //char ch;

30     while(cin>>w>>h)

31     {

32         if(w == 0 && h == 0) break;

33         //cin>>ch;

34 

35         char **arr = new char*[h];

36         for(i = 0; i < h; ++i) arr[i] = new char[w];

37 

38         for(i = 0; i < h; ++i)

39         {

40             for(j = 0; j < w; ++j)

41             {

42                 cin>>arr[i][j];

43             }

44             //cin>>ch;

45         }

46 

47         for(i = 0; i < h; ++i)

48         {

49             for(j = 0; j < w; ++j)

50             {

51                 if(arr[i][j] == '@')

52                 {

53                     arr[i][j] = '.';

54                     cout << f(arr,i,j,w,h) << endl;

55                     break;

56                 }

57             }

58             if(j < w) break;

59         }

60 

61         for(i = 0; i < h; ++i) delete [] arr[i];

62         delete [] arr;

63     }

64 

65     return 0;

66 }

 

你可能感兴趣的:(open)