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.有多个就输出字典序最小的那 ...
随机推荐
- Java 数组声明的几种方式
Java数组定义声明的几种方法: 1. 类型名称[] 变量名=new 类型名称[length]; 2.类型名称[] 变量名={?,?,?}; 3.类型名称[] 变量名=new 类型名称[]{?,?,? ...
- PHP全路径无限分类原理
全路径无限分类:以一个字段把他所有的父级id按顺序记录下来以此实现的无限分类叫做全路径无限分类 优点:查询方便 缺点:增加,移动分类时数据维护时稍微复杂.
- 带你使用JS-SDK自定义微信分享效果
前言 想必各位在写wap端时都遇到过这样的场景吧 ----自定义分享标题.图片.描述 接下来小编给大家讲解下分享相关操作 预期效果 原始的分享效果: 使用微信JS-SDK的分享效果: 可以看出缩略图, ...
- mac gulp: command not found
mac下执行gulp的时候报错:gulp: command not found 1.查看npm的安装目录 npm root 2.如果不是/usr/local , 说明未全局安装,执行 sudo npm ...
- [转]原生JS-查找相邻的元素-siblings方法的实现
在针对element的操作里,查找附近的元素是一个不可少的过程,比如在实现tab时,其中的一个div增加了“on”class,其他的去除“on”class.如果用jquery的朋友就肯定不会陌生sib ...
- 数据筛选和API优化
筛选数据 需求:如果数据库中存在OrderNum相同,且IsDefault不同的记录,那么IsDefault值为0的记录将替换值为1的记录(IsDefault值为1的记录不展示). 由于查出来的数据不 ...
- session,cookie,sessionStorage,localStorage的区别
浏览器的缓存机制提供了可以将用户数据存储在客户端上的方式,可以利用cookie,session等跟服务端进行数据交互. 一.cookie和session cookie和session都是用来跟踪浏览器 ...
- abseil初体验[google开源的C++库]
Google公开了其项目内部使用的一系列C++库,具体介绍参考: http://www.infoq.com/cn/news/2017/10/abseil?utm_source=infoq&ut ...
- VirtualBox网络连接方式
VirtualBox图形界面下有四种网络接入方式,它们分别是: 1.NAT 网络地址转换模式(NAT,Network Address Translation) 2.Bridged Adapter 桥接 ...
- Microsoft Teams 集成 (协作, 沟通 和 行为)
Microsoft Teams 集成 (协作, 沟通 和 行为) 概述 Microsoft Teams是在Office 365中以chat为中心的工作空间.软件开发团队可以快速获得在一个专门的团队协作 ...