HDU - 1312 Red and Black 搜索DFS

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ACMer_QJ/article/details/79965608

找到@,搜索四个方向

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define ll long long

const int maxn = 21;
int n, m;
char map_[21][21];

int dfs(int i, int j) {
        if (i < 0 || j < 0 || i >= m || j >= n) {
                return 0;
        }
        if (map_[i][j] != '#') {
                map_[i][j] = '#';
                return 1 + dfs(i - 1, j) + dfs(i, j - 1) + dfs(i, j + 1) + dfs(i + 1, j);
        }
        return 0;
}
int main(int argc, char * argv[])
{
        while (~scanf("%d%d", &n, &m), n + m) {
                int x, y;
                for (int i = 0; i < m; i ++) {
                        scanf("%s", map_[i]);
                        for (int j = 0; j < n; j ++) {
                                if (map_[i][j] == '@') {
                                        x = i, y = j;
                                }
                        }
                }
                cout << dfs(x, y) << endl;;
        }
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页