湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)

Biggest Number

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述

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 the four 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一个位置的时候,处理它能够到达的所有的点,我们假设这些点都是沿着它可以到达的,如果当前点的数量+当前的搜索长度<已经得到的最优解的长度,那么这段必定是可以减掉的,因为无论怎么走都达不到最优解的长度了.还有一种就是能够达到点的数量+当前的搜索长度 = 已经得到的最优解的长度,那么我们只要在当前搜索的解后面添一个最大的数字 '9',如果这样的结果字典序还比已经得到的最优解的字典序小,那么也没有搜索下去的必要了,直接剪枝,这里的BFS按照我的理解就相当于一个估价函数,让结果一直往正确的方向走.很厉害的题目。
还有一个小插曲:就是关于手动队列和STL自带队列,自带队列在某些时候要慎用,比如说这个题,BFS调用的次数比较多,给大家看一下两者的时间。
(手动队列)
(STL自带)
一直超时找不到原因,改成手动队列跑得飞快,还有就是 string 类型也要谨慎使用,这些STL自带功能会消耗时间
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
using namespace std;
const int N = 35;
int n,m,MAX;
char mp[N][N];
bool flag[N][N];
bool vis[N][N];
int dir[][2] = {{1,0},{-1,0},{0,-1},{0,1}};
struct Node{
    int x,y;
};
char res[N],ans[N],ans1[N];
bool check(int x,int y){
    if(x<1||x>n||y<1||y>m||mp[x][y]=='#') return false;
    return true;
}
Node q[N * N];
int bfs(int x,int y){
    memset(flag,0,sizeof(flag));
    int head = 0, tail = 0,ret = 0;;
    q[tail].x = x;  ///手动队列非常快
    q[tail++].y = y;
    flag[x][y] = true;
    while(head!= tail){
        int nowx = q[head].x;
        int nowy = q[head++].y;
        for(int i=0;i<4;i++){
            int nextx = nowx+dir[i][0];
            int nexty = nowy+dir[i][1];
            if(!check(nextx,nexty)||flag[nextx][nexty]||vis[nextx][nexty]) continue;
            ret++;
            flag[nextx][nexty] = true;
            q[tail].x = nextx;
            q[tail++].y = nexty;
        }
    }
    return ret;
}

void dfs(int x,int y,int step){
    if(step>MAX||step==MAX&&strcmp(ans,res)>0){
        MAX = step;
        strcpy(res,ans);
    }
    int GO = bfs(x,y); ///预处理最好的情况,所有点都可达
    strcpy(ans1,ans);
    ans1[step] = '9';
    if(GO+step<MAX||GO+step==MAX&&strcmp(ans1,res)<0) return; ///剪枝,(x,y)能够走的距离 < 答案
    for(int i=0;i<4;i++){
         int nextx = x+dir[i][0];
         int nexty = y+dir[i][1];
         if(!check(nextx,nexty)||vis[nextx][nexty]) continue;
         vis[nextx][nexty] = true;
         ans[step] = mp[nextx][nexty];
         dfs(nextx,nexty,step+1);
         ans[step] = 0;
         vis[nextx][nexty] = false;
    }
}
int main()
{
    freopen("f.in","r",stdin);
   freopen("f.txt","w",stdout);
    while(scanf("%d%d",&n,&m)!=EOF,n+m){
        int tot = 0;
        for(int i=1;i<=n;i++){
            scanf("%s",mp[i]+1);
            for(int j=1;j<=m;j++){
                if(mp[i][j]!='#') tot++;
            }
        }
        memset(res,0,sizeof(res));
        MAX = -1;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mp[i][j]=='#'||MAX==tot&&mp[i][j]<res[0]) continue;
                memset(vis,false,sizeof(vis));
                ans[0] = mp[i][j];
                vis[i][j] = true;
                dfs(i,j,1);
            }
        }
        printf("%s\n",res);
    }
    return 0;
}
/**
5 6
245356
342534
534635
423535
324345
*/

 

转载于:https://www.cnblogs.com/liyinggang/p/5797714.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值