hdu-1241-Oil Deposits(dfs水)

題目鏈接

dfs水題

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<time.h>
#include<set>
#include<stack>
#include<vector>
#include<map>

#define pi acos(-1)
#define maxn 111111
#define maxm 11111
#define INF 0x3F3F3F3F
#define eps 1e-8

#define pb push_back

#define mem(a) memset(a,0,sizeof a)

using namespace std;

const long long mod = 1000000007;
/**lyc**/
int n, m;
char smp[111][111];
int mp[111][111];
bool vis[111][111];
int stepx[9] = { 0, -1, 0, 1, 0, -1, -1, 1, 1 };
int stepy[9] = { 0, 0, -1, 0, 1, -1, 1, -1, 1 };
bool pan(int x, int y) {
    if (x >= 1 && x <= n && y >= 1 && y <= m) return 1;
    return 0;
}
void dfs(int x, int y) {
    for (int i = 1; i <= 8; i++) {
        int xx = x + stepx[i];
        int yy = y + stepy[i];
        if (pan(xx, yy) && !vis[xx][yy] && !mp[xx][yy] ) {
        //  cout << xx << " " << yy << endl;
            vis[xx][yy] = 1;
            dfs(xx, yy);
        }
    }
}
int main() {
    while (scanf("%d%d", &n, &m) && (n != 0 && m != 0)) {
        for (int i = 0; i < n; i++) {
            scanf("%s", smp[i]);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (smp[i - 1][j - 1] == '*') mp[i][j] = 1;
                else mp[i][j] = 0;
            }
        }
    /*  for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                cout << mp[i][j] << " ";
            }
            cout << endl;
        }*/
        memset(vis, 0, sizeof vis);
        int cnt = 0;
        for(int i = 1; i <= n;i++) 
            for (int j = 1; j <= m; j++) {
                if (!vis[i][j] && !mp[i][j]) {
                    cnt++;
                    vis[i][j] = 1;
                    dfs(i, j);
                }
            }
        printf("%d\n", cnt);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章