Red and Black

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=67590#problem/D
 
Red and Black
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 1312

Description

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. 
 

Input

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) 
 

Output

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). 
 

Sample Input

Red and Black

 

Sample Output

45
59
6
13
 
CODE:
Red and Black
 1 #include<stdio.h>

 2 

 3 char a[25][25];

 4 

 5 int w, h;

 6 

 7 int f(int x, int y)

 8 {

 9     if(x < 0 || x >= h || y < 0 || y >= w)

10         return 0;

11     if(a[x][y] == '#')

12         return 0;

13     else

14     {

15         a[x][y] = '#';

16         return 1 + f(x, y-1) + f(x, y+1) + f(x-1, y) + f(x+1, y);

17     }

18 }

19 

20 int main()

21 {

22     int i, j, x, y;

23 

24     while(scanf("%d%d", &w, &h), w+h)

25     {

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

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

28             {

29                 scanf(" %c", &a[i][j]);

30                 if(a[i][j] == '@')

31                 {

32                     x = i;

33                     y = j;

34                 }

35             }

36 

37         printf("%d\n", f(x, y));

38     }

39     return 0;

40 }
View Code

受不鸟为啥这样不对,先放这好了:

#include<iostream>

#include<cstdio>



using namespace std;



#define N 25



int w, h, cou;

char maps[N][N];

int dir[4][2] = { {-1, 0}, {0, -1}, {0, 1}, {1, 0}};



void DFS(int x, int y)

{

    int i, nx, ny;



    cou++;

    

    maps[x][y] = '#';



    for(i = 0; i < 4; i++)

    {

        nx = x + dir[i][0];

        ny = y + dir[i][1];

        if(maps[nx][ny] == '.' && nx >= 0 && nx < h && ny >= 0 && ny < w)

        {

            DFS(nx, ny);

        }

    }

}

int main()

{

    int i, j;



    while(cin >> w >> h, w+h)

    {

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

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

				scanf(" %c", &maps[i][j]);



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

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

        {

            if(maps[i][j] == '@')

                DFS(i, j);

        }

        cout << cou << endl;

    }

    return 0;

}

  

你可能感兴趣的:(c)