[HDU] [1241] Oil Deposits

Problem Description

Loading...

Problem Analysis

Ask how many oil regions
Note that as long as there is oil in any of the eight directions
then this region is considered connected
so just use dfs to flood-fill

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
int move[2][8]= {{1,1,1,0,0,-1,-1,-1},{0,1,-1,1,-1,0,1,-1}};
char map[120][120];
int H,W;
void dfs(int n,int m) {
    map[n][m]='*';
    for(int i=0; i<8; i++) {
        int tn=n+move[0][i],tm=m+move[1][i];
        if(tn>=0&&tn<H&&tm>=0&&tm<W&&map[tn][tm]=='@')
            dfs(tn,tm);
    }
}
int main() {
    while(scanf("%d %d",&H,&W),H||W) {
        for(int i=0; i<H; i++)
            scanf("%s",map[i]);
        int cnt=0;
        for(int i=0; i<H; i++) {
            for(int j=0; j<W; j++)
                if(map[i][j]=='@') {
                    cnt++;
                    dfs(i,j);
                }
        }
        printf("%d\n",cnt);
    }
    return 0;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...