POJ 1979 Red and Black

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

方法和Lake Counting 完全一样

 1 #include <iostream>
 2 #include <stdio.h>
 3 
 4 using namespace std;
 5 //思路与Lake counting 完全一样
 6 const int maxsize = 128;
 7 int M,N,cnt;
 8 char room[maxsize][maxsize];
 9 int d[2][4] = {{-1, 0, 1, 0}, {0, 1, 0, -1}};
10 
11 bool check(int x, int y)
12 {
13     if (x < 0 || x >= N || y < 0 || y >= M) return false;
14     if (room[x][y] == '#') return false;
15     return true;
16 }
17 
18 void dfs(int x, int y)
19 {
20     int nx, ny;
21     room[x][y] = '#';//将这个格置为#
22     for (int i = 0; i < 4; i++)
23     {
24         nx = x + d[0][i];
25         ny = y + d[1][i];
26         if (check(nx, ny))
27         {
28             cnt++;
29             dfs(nx, ny);
30         }
31     }
32 }
33 
34 int main()
35 {
36     int x,y;
37     freopen("in.txt", "r", stdin);
38     while(~scanf("%d%d", &M, &N))
39     {
40         if (M == 0 && N == 0) break;
41         for (int i = 0; i < N; i++)
42         {
43             getchar();
44             for (int j = 0; j < M; j++)
45             {
46                 scanf("%c", &room[i][j]);
47                 if (room[i][j] == '@')
48                 {
49                     x = i;
50                     y = j;
51                 }
52             }
53         }
54         cnt = 1;
55         dfs(x, y);
56         printf("%d
", cnt );
57     }
58     return 0;
59 }
原文地址:https://www.cnblogs.com/oscar-cnblogs/p/6291451.html