HDU 1312 DFS

算法基础 DFS 深度搜索hdu1312 Red and Black下面是题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1312
这道题的题意大致是你在@点处,’.’是能走的,’#’是不能走的,然后问你能走过的最多的’.’的步数。典型的DFS题目。

```
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std ;
int sx, sy;
char maze[100][100];
int n, m; //迷宫的长和宽
int ans; // 最终走的步数
int dirt[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void dfs(int x, int y)
{
    if(x < 0 || x >= n || y < 0 || y >= m) return ;
    if(maze[x][y] == '#') return ;
    ans++;
    for(int i = 0 ; i < 4 ; i++){
        int tx = x + dirt[i][0];
        int ty = y + dirt[i][1];
        maze[x][y] = '#';//每走过一个'.'就吧它标记为不能走的'#'然后ans就会加一
        dfs(tx, ty);
    }
}

int main()
{

    while(cin >> m >> n && (n||m)){
        ans = 0 ;
        for(int i = 0 ; i < n ; i++)
            cin >> maze[i];
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < m ; j++){
            //scanf("%s", &maze[i][j]);
                if(maze[i][j] == '@')
                    sx = i , sy = j ;
            }
        }
        dfs(sx, sy);
        cout << ans << endl;
    }
    system("pause");
    return 0 ;
}

“`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值