[HDU] [1312] Red and Black

Problem Description

Loading...

Problem Analysis

I feel like I’ve done this problem so many times, but I never posted it -.-

The simplest DFS maze search problem.

Use Recursion to find walkable paths.

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
29
30
31
32
#include<stdio.h>
char map[22][22];
int move[6]= {1,-1,0,0};
int W,H,cnt;
void dfs(int n,int m) {
    cnt++;
    map[n][m]='#';
    for(int i=0; i<4; i++) {
        int tn=n+move[i],tm=m+move[(i+2)%4];
        if(tn>=0&&tn<H&&tm>=0&&tm<W&&map[tn][tm]!='#')
            dfs(tn,tm);
    }
    return ;
}
int main() {
    while(scanf("%d %d",&W,&H),W&&H) {
        getchar();
        int mH,mW;
        for(int i=0; i<H; i++) {
            for(int j=0; j<W; j++) {
                map[i][j]=getchar();
                if(map[i][j]=='@')
                    mH=i,mW=j;
            }
            getchar();
        }
        cnt=0;
        dfs(mH,mW);
        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...