UVA 11882 Biggest Number(搜索+剪枝)
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.
Input
There will be at most 25 test cases. Each test begins with two integers R and C (2R,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 Ccharacters (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.
Output
For each test case, print the biggest number you can find, on a single line.
题目大意:有一个R*C的矩阵,矩阵里面有1~9的数字(太好了不用处理前导0),或者是#(代表不能通过),先要从矩阵任意一点出发(之前英语抓鸡看成了边界,英语差的孩纸伤不起啊>_<),只能往上下左右四个方向移动,每个格子不能重复走,到达矩阵内任意一点。把这条路径的数字连起来变成一个很大的数字,求这个数字最大是什么。
思路:DP?记忆化搜索?30个点噢,时间吃得消内存都吃不消啦。所以呢?搜索。还是超时啊?剪枝啊。
剪枝1:假设当前答案为ans,那么当我们走到一个点(x, y)的时候,作一个小小的搜索预判。假设现在能从(x, y)走到的点,我们都能到达,这是最好的情况。设从(x, y)能走到的点数为maxlen,那么如果从出发点走到(x, y)经过的格子,加上maxlen,都没有ans的长度大,那么不管从(x, y)怎么搜,我们都不能取代我们现在的ans(长才是王道懂不懂),那么直接回溯,不要这个点了。这个剪枝效力还是不错的,但是还是TLE,我试过了QAQ。最近有点脑残,明明可以做一个数据测试一下非要交上去试一下……
剪枝2:同剪枝1,假设当前答案为ans,那么当我们走到一个点(x, y)的时候,搜到maxlen(同剪枝1),如果从出发点走到(x, y)经过的格子,加上maxlen,大于ans的长度,我们就只能继续搜了……如果等于呢?那么就再作一个最好预期的答案。把从(x, y)能走到的所有格子,从大到小排好序(我的代码是从小到大排序然后从后面开始取的……),都接在当前走到(x, y)的后面,这是从(x, y)可能搜到的最好的答案,如果这个都比ans要小,那么我们也就没有必要往下搜了,果断回溯。
PS:弄个结构体存答案很好写妥妥的。
代码(866MS):
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- #include <queue>
- using namespace std;
- typedef long long LL;
- const int MAXN = ;
- struct Node {
- int a[MAXN], len;
- void clear() {
- len = ;
- }
- void print() {
- //cout<<len<<endl;
- for(int i = ; i < len; ++i) printf("%d", a[i]);
- printf("\n");
- }
- bool operator < (const Node &rhs) const {
- if(len != rhs.len) return len < rhs.len;
- for(int i = ; i < len; ++i)
- if(a[i] != rhs.a[i]) return a[i] < rhs.a[i];
- return false;
- }
- };
- int fx[] = {, , -, };
- int fy[] = {, , , -};
- Node ans, now, tmp;
- char mat[MAXN][MAXN];
- bool vis[MAXN][MAXN];
- bool vis2[MAXN][MAXN];
- int n, m;
- int can[MAXN];
- int maxlen(int x, int y) {
- queue<int> que; que.push(x * MAXN + y);
- int ret = ;
- can[] = mat[x][y] - '';
- memset(vis2, , sizeof(vis2));
- vis2[x][y] = ;
- while(!que.empty()) {
- int tmp = que.front(); que.pop();
- int nx = tmp / MAXN, ny = tmp % MAXN;
- for(int i = ; i < ; ++i) {
- int px = nx + fx[i], py = ny + fy[i];
- if(!isdigit(mat[px][py]) || vis[px][py] || vis2[px][py]) continue;
- vis2[px][py] = true;
- can[ret++] = mat[px][py] - '';
- que.push(px * MAXN + py);
- }
- }
- return ret;
- }
- void dfs(int x, int y) {
- now.a[now.len++] = mat[x][y] - '';
- vis[x][y] = true;
- for(int i = ; i < ; ++i) {
- int px = x + fx[i], py = y + fy[i];
- if(!isdigit(mat[px][py]) || vis[px][py]) continue;
- int wantlen = maxlen(px, py);
- if(now.len + wantlen < ans.len) continue;
- if(now.len + wantlen == ans.len) {
- sort(can, can + wantlen);
- tmp = now;
- for(int i = wantlen - ; i >= ; --i) tmp.a[tmp.len++] = can[i];
- if(tmp < ans) continue;
- }
- dfs(px, py);
- }
- if(ans < now) ans = now;
- --now.len;
- vis[x][y] = false;
- }
- int main() {
- while(scanf("%d%d", &n, &m) != EOF) {
- if(n + m == ) break;
- memset(mat, , sizeof(mat));
- for(int i = ; i <= n; ++i) scanf("%s", &mat[i][]);
- ans.clear(); now.clear();
- for(int i = ; i <= n; ++i) {
- for(int j = ; j <= m; ++j)
- if(isdigit(mat[i][j])) dfs(i, j);
- }
- ans.print();
- }
- }
UVA 11882 Biggest Number(搜索+剪枝)的更多相关文章
- UVA - 11882 Biggest Number(dfs+bfs+强剪枝)
题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...
- 【UVa】11882 Biggest Number(dfs+剪枝)
题目 题目 分析 典型搜索,考虑剪枝. 统计一下联通分量. 1.本位置能够达到所有的点的数量加上本已有的点,还没有之前的结果长,直接返回. 2.当本位置能够达到所有的点的数量加上本已有的点与之 ...
- 湖南省第六届大学生程序设计大赛原题 F Biggest Number (UVA1182)
Biggest Number http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30851#problem/F 解题思路:DFS(检索)+BF ...
- 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)
Biggest Number 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...
- 湖南省第6届程序大赛第6题 Biggest Number
Problem F Biggest Number You have a maze with obstacles and non-zero digits in it: You can start fro ...
- hdu 5887 搜索+剪枝
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move
poj 1568:Find the Winning Move [迭代博弈+搜索+剪枝] 题面省略... Input The input contains one or more test cas ...
- NOIP2015 斗地主(搜索+剪枝)
4325: NOIP2015 斗地主 Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 270 Solved: 192[Submit][Status] ...
- hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)
题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...
随机推荐
- Mac 使用问题
Mac 使用 Mac改变系统截图存储位置 鼠标在屏幕中间上下滚动时,有时反应不灵敏: 看看偏好设置-> 鼠标 -> 跟踪速度或者手势部分是否有哪里设置不当: 考虑重新开启鼠标鼠标底部有一个 ...
- oracle聚簇表的理解 (转自:https://blog.csdn.net/gumengkai/article/details/51009345 )
Oracle支持两种类型的聚簇:索引聚簇和哈希聚簇 一.索引聚簇表的原理 聚簇:如果一些表有一些共同的列,则将这样一组表存储在相同的数据块中 聚簇还表示把相关的数据存储在同一个块上.利用聚簇,一个块可 ...
- JavaScript 基础(六) 数组方法 闭包
在一个对象中绑定函数,称为这个对象的方法.在JavaScript 中,对象的定义是这样的: var guagua = { name:'瓜瓜', birth:1990 }; 但是,如果我们给瓜瓜绑定一个 ...
- SP1716 GSS3 - Can you answer these queries III(单点修改,区间最大子段和)
题意翻译 nnn 个数, qqq 次操作 操作0 x y把 AxA_xAx 修改为 yyy 操作1 l r询问区间 [l,r][l, r][l,r] 的最大子段和 题目描述 You are give ...
- docker搭建基于percona-xtradb-cluster方案的mysql集群
一.部署环境 序号 hostname ip 备注 1 manager107 10.0.3.107 centos7;3.10.0-957.1.3.el7.x86_64 2 worker68 10.0.3 ...
- Percona-Tookit工具包之pt-index-usage
Preface There're many ways relevent with performance tuning.For example,using indexes proper ...
- 关于python的GIL
转自依云在知乎上的回答,链接为https://www.zhihu.com/question/27245271/answer/462975593 侵删. python的多线程,其实不是真的多线程,它会通 ...
- ASP.NET安全验证
一.为什么要用安全验证,使用安全验证有什么好处. 构造特殊的链接地址,导致文件内的数据泄露 数据库泄露 安全防范的首要策略:所有的HTTP访问都要经过IIS,所以限制IIS的安全性是关键 二.安全验证 ...
- jQuery的封装
封装,最简单的效果就是一个效果你可以重复的去调用 本来前端之路时间不是很长,但是对封装还是有一点点自己个人的理解,曾经踩过的坑也不在少数,最后总结出我个人风格的封装,听一位大神指点过,每个人都有属 ...
- Centos7安装FastDFS
离线安装包准备: 将相关的安装包上传到 /usr/local 目录,安装包下载 并解压到当前目录 1.安装 gcc yum install -y gcc gcc-c++ 2.安装 perl yum i ...