POJ - 1979(深搜)

题目链接:点击打开链接


解析:

深搜无疑,搜的时候记得把当前点置为不可行,之后在搜周边点。另一个坑点是,要特别考虑@周边都是墙这种情况,此时输出0(只有他当前的那一个点)。


完整代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
const int maxn = 31;
char g[maxn][maxn];
int d[4][2] = {0 , 1 , 1 , 0 , 0 , -1 , -1 ,0};
void dfs(int si , int sj , int n , int m , int& cnt)
{
    for(int i = 0 ; i < 4 ; i ++){
        int x = si + d[i][0];
        int y = sj + d[i][1];
        if(x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '#') continue;
        g[x][y] = '#';
        cnt += 1;
        dfs(x , y , n , m , cnt);
    }
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt" , "r" , stdin);
    #endif // DoubleQ
    int n , m;
    while(cin >> n >> m){
        if(n == 0 && m == 0) break;

        string str;
        int si , sj;
        for(int i = 0 ; i < m ; i ++){
            cin >> str;
            for(int j = 0 ; j < n ; j ++){
                g[i][j] = str[j];
                if(str[j] == '@'){
                    si = i;
                    sj = j;
                }
            }
        }
        int cnt = 0;
        /*
        for(int i = 0 ; i < n ;i ++){
            for(int j = 0 ; j < m ; j ++){
                cout << g[i][j] << " ";
            }
            cout << endl;
        }
        */
        dfs(si , sj , m , n , cnt);
        if(cnt == 0) cout << "1" << endl;
        else cout << cnt << endl;

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值