LRJ-Example-06-12-Uva572

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#include <string>
#include <istream>
#include <iostream>
using namespace std;

const int maxm = 100;
char grid[maxm + 5][maxm + 5];
int visited[maxm + 5][maxm + 5];
int m, n;
int ans;

bool count(int i, int j)
{
    if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '*' || visited[i][j])
        return false;
        
    visited[i][j] = 1;
    count(i + 1, j);
    count(i - 1, j);
    count(i, j + 1);
    count(i, j - 1);
    count(i + 1, j + 1);
    count(i + 1, j - 1);
    count(i - 1, j + 1);
    count(i - 1, j - 1);

    return true;
}

int main()
{

    while (cin >> m >> n && m > 0) {
        memset(grid, 0, sizeof(grid));
        memset(visited, 0, sizeof(visited));
        ans = 0;
        string line;
        getline(cin, line); // consume the newline
        for (int i = 0; i < m; i++) {
            getline(cin, line);
            for (int j = 0; j < n; j++) {
                grid[i][j] = line[j];
            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (count(i, j))
                    ans++;
            }
        }

        cout << ans << endl;

    }
    
    return 0;
}

 

posted @ 2017-08-04 18:57  PatrickZhou  阅读(118)  评论(0编辑  收藏  举报