NBUT [1464] Biggest Number dfs

  • [1464] Biggest Number

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • You have a maze with obstacles and non-zero digits in it:






    You can start from any square, walk in the maze, and finally stop at some square. Each step, you may only walk into one of thefour neighbouring squares (up, down, left, right) and you cannot walk into obstacles or walk into a square more than once. When you finish, you can get a number by writing down the digits you encounter in the same order as you meet them. For example, you can get numbers 9784, 4832145 etc. The biggest number you can get is 791452384, shown in the picture above.



    Your task is to find the biggest number you can get.




  • 输入
  • There will be at most 25 test cases. Each test begins with two integers R and C (2<=R,C<=15, R*C<=30), the number of rows and columns of the maze. The next R rows represent the maze. Each line contains exactly C characters (without leading or trailing spaces), each of them will be either '#' or one of the nine non-zero digits. There will be at least one non-obstacle squares (i.e. squares with a non-zero digit in it) in the maze. The input is terminated by a test case with R=C=0, you should not process it.
  • 输出
  • For each test case, print the biggest number you can find, on a single line.
  • 样例输入
  • 3 7
  • ##9784#
  • ##123##
  • ##45###
  • 0 0
  • 样例输出
  • 791452384
  • 提示
  • 来源
  • 湖南省第六届大学生计算机程序设计竞赛

dfs+剪枝

对后面能搜到的数进行预处理,如果小于当前已经搜到的最大的数,回溯。

#include <cstdio>
#include <cstring>
using namespace std;

int r, c;
char a[20][20];
int used[20][20]; //判断是否走过改路径
int ans[100]; //存放当前最大数数组 由于该数《=30位 所以用数组存
int ansl; //记录当前最大数位数
int aa[100]; //当前数
int zhan[100][2]; //呵呵 顾名思义 栈
int used1[20][20]; //这个也是判断是否走过路径 下面代码中只用在一处地方
int neigh[4][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; //查找上下左右位置用的数组
int z;

void Search(int x, int y, int l) //搜索函数
{
    int i, j, top, bottom, xx, yy;
    if ((l > ansl) || ((l == ansl) && (z == 1))) //如果该数大于最大数  z用来判断大小的 注意后面用法
    {
        memcpy(ans, aa, sizeof(ans)); //aa数组值复制到ans中去
        ansl = l;
        z = 0;
    }
    memset(used1, 0, sizeof(used1)); //初始化use1为0  即都没使用过
    used1[x][y] = 1;
    top = 0;
    bottom = 1; //下面会用栈来存放后继结点的坐标 为的是记录当前数之后能构成的最长的数
    zhan[0][0] = x;
    zhan[0][1] = y;
    while (top < bottom) //把后继结点都放入栈中  这里是提高速度的一核心
    {
        for (i = 0; i < 4; i++) //上下左右
        {
            xx = zhan[top][0] + neigh[i][0];    //得到下一个位置的坐标
            yy = zhan[top][1] + neigh[i][1];
            if ((xx >= 0) && (xx < r) && (yy >= 0) && (yy < c)  //判断坐标的合法性
                    && (a[xx][yy] != '#') && (used[xx][yy] == 0)
                    && (used1[xx][yy] == 0))
            {
                zhan[bottom][0] = xx;       //合法,加入栈
                zhan[bottom][1] = yy;
                used1[xx][yy] = 1;
                bottom++;
            }
        }
        top++;
    }
    if (l + top - 1 < ansl)
        return; //如果当前长度+后继结点构成最长数长度<最大数长度 直接返回
    if ((l + top - 1 == ansl) && (z == -1))
        return; //长度相等但是比最长数小
    for (i = 0; i < 4; i++) //上下左右递归寻找下一个结点
    {
        xx = x + neigh[i][0];
        yy = y + neigh[i][1];
        if ((xx >= 0) && (xx < r) && (yy >= 0) && (yy < c) && (a[xx][yy] != '#')
                && (used[xx][yy] == 0)) //下面都是Search 注意z的值及其含义
        {
            aa[l] = a[xx][yy] - '0';
            used[xx][yy] = 1;
            if (z != 0)
                Search(xx, yy, l + 1);
            else if (l >= ansl)
            {
                z = 1;
                Search(xx, yy, l + 1);
                z = 0;
            }
            else
            {
                if (aa[l] > ans[l])
                {
                    z = 1;
                    Search(xx, yy, l + 1);
                    z = 0;
                }
                else if (aa[l] == ans[l])
                {
                    z = 0;
                    Search(xx, yy, l + 1);
                    z = 0;
                }
                else
                {
                    z = -1;
                    Search(xx, yy, l + 1);
                    z = 0;
                }
            }
            used[xx][yy] = 0;
        }
    }
}

int main() //main里面和Search里面大同小异 就是把第一个结点单独拿出来而已 接下来的结点都是遍历过程
{
    int i, j;
    while (1)
    {
        scanf("%d%d", &r, &c);
        if ((r == 0) && (c == 0))
            break;
        for (i = 0; i < r; i++)
            scanf("%s", a[i]);
        memset(ans, 0, sizeof(ans));
        ans[0] = -1;
        ansl = 1;
        memset(aa, 0, sizeof(aa));
        for (i = 0; i < r; i++)
            for (j = 0; j < c; j++)
                if (a[i][j] != '#')
                {
                    used[i][j] = 1;
                    aa[0] = a[i][j] - '0';
                    if (a[i][j] - '0' > ans[0])
                    {
                        z = 1;
                        Search(i, j, 1);
                    }
                    else if (a[i][j] - '0' == ans[0])
                    {
                        z = 0;
                        Search(i, j, 1);
                    }
                    else
                    {
                        z = -1;
                        Search(i, j, 1);
                    }
                    used[i][j] = 0;
                }
        for (i = 0; i < ansl; i++)
            printf("%d", ans[i]);
        printf("\n");
    }
    return 0;
}

//其实函数使用的就是回溯法 但是需要小技巧来改进

//如果你直接深度遍历的话 大于25个数的时候就能看出明显时间差距 30个数运行时间在10秒以上

//但是经上面程序改进后 30个数也能几乎瞬间完成


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值