HDU1241 - Oil Deposits (深搜)

来源:互联网 发布:免费收款收据打印软件 编辑:程序博客网 时间:2024/06/03 23:00

题目链接

思路

dfs求联通块。

代码

#include <iostream>#include <cstdio>using namespace std;const int maxn = 100;int n, m;int xx[] = {0, 1, 1, 1, 0, -1, -1, -1};int yy[] = {1, 1, 0, -1, -1, -1, 0, 1};bool maze[maxn][maxn];char sta[maxn+10];void input(){    for(int i=0; i<n; i++)    {        scanf("%s", sta);        for(int j=0; j<m; j++)        {            if(sta[j]=='@') maze[i][j] = false;            else maze[i][j] = true;        }    }}void dfs(int x, int y){    if(x<0||x>=n) return;    if(y<0||y>=m) return;    if(maze[x][y]) return;    maze[x][y] = true;    for(int i=0; i<8; i++)    {        dfs(x+xx[i], y+yy[i]);    }}int solve(){    int ans = 0;    for(int i=0; i<n; i++)    {        for(int j=0; j<m; j++)        {            if(!maze[i][j])            {                dfs(i, j);                ans++;            }        }    }    return ans;}int main(){    while(scanf("%d%d", &n, &m)&&n)    {        input();        printf("%d\n", solve());    }    return 0;}
0 0