UVA11882-Biggest Number(DFS+最优化剪枝)
Accept: 177 Submit: 3117
Time Limit: 1000 mSec Memory Limit : 128MB
Problem Description

Input
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.
Output
For each test case, print the biggest number you can find, on a single line.
Sample Input
Sample Output
791452384
题解:一看题目觉得是个大水题,dfs一下就行,然后果断TLE,这个题对效率要求还是比较高的,两个比较重要的剪枝:
1、如果剩余最长可走长度加上目前已有长度小于答案的长度,直接return.
2、如果剩余最长可走长度加上目前已有长度等于答案的长度,比较字典序,如果答案更优就retrun.
#include <bits/stdc++.h> using namespace std; const int maxn = ; int n, m, cnt;
int head, tail;
pair<int,int> que[maxn*maxn];
int dx[] = { ,-,, };
int dy[] = { ,,-, };
char gra[maxn][maxn];
bool vis[maxn][maxn],vvis[maxn][maxn];
string ans, tmp; int h(int x,int y) {
head = tail = ;
int cnt = ;
que[tail++] = make_pair(x, y);
memcpy(vvis, vis, sizeof(vis)); while (head < tail) {
pair<int, int> temp = que[head++];
for (int i = ; i < ; i++) {
int xx = temp.first + dx[i], yy = temp.second + dy[i];
if ( <= xx && <= yy && xx < n && yy < m && !vvis[xx][yy] && gra[xx][yy] != '#') {
vvis[xx][yy] = true;
cnt++;
que[tail++] = make_pair(xx, yy);
}
}
}
return cnt;
} void update(const string& s) {
int len1 = s.size(), len2 = ans.size();
if (len2 < len1 || (len2 == len1 && ans < s)) ans = s;
} void dfs(int x,int y,string s,int deep) {
int hh = h(x, y);
int l = ans.size();
if (deep + hh < l) return;
if (deep + hh == l) {
if (s + "z" < ans) return;
} update(s); for (int i = ; i < ; i++) {
int xx = x + dx[i], yy = y + dy[i];
if ( <= xx && <= yy && xx < n && yy < m && !vis[xx][yy] && gra[xx][yy] != '#') {
vis[xx][yy] = true;
dfs(xx, yy, s + gra[xx][yy], deep + );
vis[xx][yy] = false;
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d%d", &n, &m) && (n || m)) {
ans = "";
for (int i = ; i < n; i++) {
scanf("%s", gra[i]);
} for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
if (isdigit(gra[i][j])) {
tmp = "";
tmp += gra[i][j];
vis[i][j] = true;
dfs(i, j, tmp, );
vis[i][j] = false;
}
}
}
cout << ans << endl;
}
return ;
}
UVA11882-Biggest Number(DFS+最优化剪枝)的更多相关文章
- UVA-11882 Biggest Number (DFS+剪枝)
题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...
- UVA - 11882 Biggest Number(dfs+bfs+强剪枝)
题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...
- 湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)
Biggest Number 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...
- 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 ...
- UVa11882,Biggest Number
搜索+剪枝 如此水的一个题,居然搞了一上午 出错在bfs与dfs时共用了一个vis数组,导致bfs完后返回dfs应该能访问到的点访问不到 自己想怎么剪枝,想了几个剪枝方法,又证明,又推翻,再想,再证明 ...
- 湖南省第六届大学生程序设计大赛原题 F Biggest Number (UVA1182)
Biggest Number http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30851#problem/F 解题思路:DFS(检索)+BF ...
- 湖南省第6届程序大赛第6题 Biggest Number
Problem F Biggest Number You have a maze with obstacles and non-zero digits in it: You can start fro ...
- 【NOI1999、LOJ#10019】生日蛋糕(搜索、最优化剪枝、可行性剪枝)
主要是剪枝的问题,见代码,讲的很详细 #include<iostream> #include<cstdio> #include<cmath> #include< ...
- UVA10624 - Super Number(dfs)
题目:UVA10624 - Super Number(dfs) 题目大意:给你n和m要求找出这种m位数,从第n位到第m位都满足前i位是能够被i整除,假设没有这种数,输出-1.有多个就输出字典序最小的那 ...
随机推荐
- 推荐 3 篇关于 java8 Lambda表达式的文章
State of the Lambda 这篇主要讲 Labmbda 的背景和用法 译文:深入理解Java 8 Lambda(语言篇--lambda,方法引用,目标类型和默认方法) State of t ...
- sourcetree Authentication failed
sourcetree 的 git 密码存在 mac 的 钥匙串里面, 需要在钥匙串里删除掉对应信息,再次打开就会让你重新输入密码, 问题就解决了。 参看: https://stackoverflow. ...
- JavaAndroid开发部分API
JavaAndroid开发中的部分系统API 四大组件,都需要在清单文件中配置 Activity: 用来提供一个能让用户操作并与之交互的界面 onCreate(): 自动调用的方法, 在其中加载布局显 ...
- angular select2 ng-model 取值 ng-change调用方法
页面: 引入文件 '/select2.css', '/select2-bootstrap.css', '/select2.min.js', '/ui-select2.js' html: <div ...
- react学习笔记2
1.build文件介绍 (1)react.js 是react的核心库 (2)react-dom.js 提供与DOM相关功能 (3)browser.js 是将JSX语法转为javascript语法 ...
- javaScript 设计模式之中介者模式示例
飞机把注册信息放到铁塔里,发送数据到铁塔,报告其它的飞机一些信息. var feiji = function( name ){ this.name = name; } feiji.prototype. ...
- vuejs2.0实现一个简单的分页
用js实现的分页结果如图所示: css .page-bar{ margin:40px; } ul,li{ margin: 0px; padding: 0px; } li{ list-style: no ...
- spring boot 集成 redis lettuce
一.简介 spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端,两种客户端的区别如下 # Jedis和L ...
- Win7怎么录制电脑屏幕视频
我们在看视频的时候,经常会看到自己特别喜爱的视频,想要把其中的某些片段给录制下来,那么Win7怎么录制电脑屏幕视频?其实步骤很简单,下面就来分享下具体的步骤. 使用工具: 电脑 操作方法: 第一步.首 ...
- 如何获取view的大小
很多初学者都会犯一个错误 ,就是在onCreate或者onStart里面去获取view的大小,然而这样获取到的宽高通常都是0,为什么呢?因为view的测量过程和activity的生命周期不是同步的,因 ...