ECNU 3260 袋鼠妈妈找孩子(dfs)

链接:http://acm.ecnu.edu.cn/problem/3260/

题意:

给出一个x,y,k。求从左上角到(x,y)最短路径不少于k而且最快到达(x,y)的迷宫。(迷宫有多个 输出其中一个就行)

分析:

因为数据量很少,而且限时很宽,可以考虑dfs。限制是每个要走的格四个方向只能有一个走过的格,其实就是上一个走到这个格子的格,因为如果有多于一个相邻的格子,那么就会从另一个格子走到这格而不是从另一个格子走到上一个格子再走到这格,所以限制条件是成立的。

#include <bits/stdc++.h>
using namespace std;
int G[10][10];
int n,m,fx,fy,k ,maxx = 9999;
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int ans[10][10];
void dfs(int x,int y,int step)
{

    int cnt = 0,tx, ty;
    for(int i = 0; i < 4; i++)
    {
        tx = x, ty = y;
        tx += dir[i][0];
        ty += dir[i][1];
        if(tx < 1 || tx >n || ty<1 || ty > m)
            continue;
        if(G[tx][ty]) cnt++;
    }
    if(cnt > 1) return;

    if(x == fx && y == fy && step >= k)
    {
        if(step < maxx)
        {
//            printf("%d\n", step);
            maxx = step;
            for(int i = 1; i <= n; i++)
            {
                for(int j = 1; j <= m; j++)
                {
                    if(G[i][j]) ans[i][j] = '.';
                    else ans[i][j] = '*';
                }
            }
        }
        else return;
    }

    for(int i = 0; i < 4; i++)
    {
        tx = x, ty = y;
        tx += dir[i][0];
        ty += dir[i][1];
        if(tx < 1 || tx >n || ty<1 || ty > m || G[tx][ty])
            continue;
        G[tx][ty] = 1;
        dfs(tx,ty,step+1);
        G[tx][ty] = 0;
    }
}
int main()
{
    memset(G,0,sizeof(G));
    scanf("%d %d", &n, &m);
    scanf("%d %d %d", &fx, &fy, &k);
    G[1][1] = 1;
    dfs(1,1,0);
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            printf("%c",ans[i][j]);
        }
        printf("\n");
    }
}

  

转载于:https://www.cnblogs.com/Jadon97/p/6849175.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值