DFS相关专题练习——POJ 1979, POJ3009, C++ 代码实现

  1. POJ 1979:Red and Black http://poj.org/problem?id=1979
    一道简单的DFS入门题,主要需要注意的地方就是输入输出的方式:输入是 scanf 按行读入而非按字符读入;输出需要针对多个输入时给出结果,使用while循环来实现。
#include <iostream>
#include <cstdio>

using namespace std;
int w, h, ans, sx, sy;
const int maxn = 25;
char tile[maxn][maxn];

void dfs(int m, int n)
{
    int a[4] = {-1, 0, 0, 1}, b[4] = {0, -1, 1, 0};
    for(int i=0; i<4; ++i)
    {
        int x = m + a[i], y = n + b[i];
        if(x>=0 && x<w && y>=0 && y<h && tile[x][y]=='.')
        {
            ++ans;
            tile[x][y]='!';
            dfs(x, y);
        }
    }
}

int main()
{
    while(scanf("%d %d", &h, &w)!=EOF && h && w)
    {

        for(int i=0; i<w; ++i)
        {
            scanf("%s", tile[i]);
        }

        for(int i=0; i<w; ++i)
        {
            for(int j=0; j<h; ++j)
            {
                if(tile[i][j]=='@')
                {
                    sx = i;
                    sy = j;
                    break;
                }
            }
        }
        ans = 1;
        dfs(sx, sy);
        printf("%d\n", ans);
    }
    return 0;
}
  1. POJ 3009:Curling 2.0 http://poj.org/problem?id=3009
    这道题是DFS的变形题,加了一些条件设置,实现代码时按模块编写,每一部分完成相应的功能,这样写出来的代码会更清晰,也容易检查错误。
#include <iostream>
#include <cstdio>
#include <stack>

using namespace std;
const int maxn = 25;

int data[maxn][maxn];
int sx, sy, gx, gy, w, h;
int ans;
void dfs(int x, int y, int sum);
void judge(int x, int y, int dx, int dy, int sum){
    while(x+dx>=0 && x+dx<h && y+dy>=0 && y+dy<w && data[x+dx][y+dy]==0) {
        x += dx, y += dy;
    }
    if(x+dx>=0 && x+dx<h && y+dy>=0 && y+dy<w && data[x+dx][y+dy]==1){
        data[x+dx][y+dy] = 0;
        dfs(x, y, sum+1);
        data[x+dx][y+dy] = 1;
    }
    else if(x+dx==gx && y+dy==gy)
        ans = min(ans, sum);
}

void dfs(int x, int y, int sum){
    if(x<0 || y<0 || x>=h || y>=w || sum>10) return;
    if(x>=0 && x<h && y-1>=0 && y-1<w && data[x][y-1]!=1) judge(x, y, 0, -1, sum);
    if(x>=0 && x<h && y+1>=0 && y+1<w && data[x][y+1]!=1) judge(x, y, 0, 1, sum);
    if(x-1>=0 && x-1<h && y>=0 && y<w && data[x-1][y]!=1) judge(x, y, -1, 0, sum);
    if(x+1>=0 && x+1<h && y>=0 && y<w && data[x+1][y]!=1) judge(x, y, 1, 0, sum);

}

int main()
{
    while(scanf("%d%d", &w, &h)!= EOF && w && h){
        for(int i=0; i<h; ++i){
            for(int j=0; j<w; ++j){
                scanf("%d", &data[i][j]);
                if(data[i][j]==2)   {sx = i; sy = j;}
                if(data[i][j]==3)   {gx = i; gy = j;}
            }
        }
        //cout <<sx << sy << gx << gy<<endl;
        ans = 11;
        data[sx][sy] = 0;
        dfs(sx, sy, 1);
        if(ans>10) printf("-1\n");
        else printf("%d\n", ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值