【杭电】[1312]Red and Black

文章字数:143

问题描述

Red and Black

问题分析

感觉做过好多遍的题都没贴-.-
最简单的DFS迷宫搜索问题
递归查找能走的路径

 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;
}

该内容采用 CC BY-NC-SA 4.0 许可协议。

如果对您有帮助或存在意见建议,欢迎在下方评论交流。

加载中...