Problem UVA11882-Biggest Number

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

3 7
##9784#
##123##
##45###
0 0
 

 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+最优化剪枝)的更多相关文章

  1. UVA-11882 Biggest Number (DFS+剪枝)

    题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...

  2. UVA - 11882 Biggest Number(dfs+bfs+强剪枝)

    题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...

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

    Biggest Number 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero di ...

  4. 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 ...

  5. UVa11882,Biggest Number

    搜索+剪枝 如此水的一个题,居然搞了一上午 出错在bfs与dfs时共用了一个vis数组,导致bfs完后返回dfs应该能访问到的点访问不到 自己想怎么剪枝,想了几个剪枝方法,又证明,又推翻,再想,再证明 ...

  6. 湖南省第六届大学生程序设计大赛原题 F Biggest Number (UVA1182)

    Biggest Number http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30851#problem/F 解题思路:DFS(检索)+BF ...

  7. 湖南省第6届程序大赛第6题 Biggest Number

    Problem F Biggest Number You have a maze with obstacles and non-zero digits in it: You can start fro ...

  8. 【NOI1999、LOJ#10019】生日蛋糕(搜索、最优化剪枝、可行性剪枝)

    主要是剪枝的问题,见代码,讲的很详细 #include<iostream> #include<cstdio> #include<cmath> #include< ...

  9. UVA10624 - Super Number(dfs)

    题目:UVA10624 - Super Number(dfs) 题目大意:给你n和m要求找出这种m位数,从第n位到第m位都满足前i位是能够被i整除,假设没有这种数,输出-1.有多个就输出字典序最小的那 ...

随机推荐

  1. 为什么需要把页面放在WEB-INF文件夹下面?

    1.基于不同的功能 JSP 被放置在不同的目录下 这种方法的问题是这些页面文件容易被偷看到源代码,或被直接调用.某些场合下这可能不是个大问题,可是在特定情形中却可能构成安全隐患.用户可以绕过Strut ...

  2. CDRAF之Service mesh

    最近翻看一些网上的文章,偶然发现我们的CDRAF其实就是Service mesh的C++版本.不管从架构的理念上,或者功能的支持上面,基本完全符合.发几个简单的文章链接,等有时间的时候,再来详细描述. ...

  3. linux服务器重启指令

    一.Linux 的五个重启命令 1.shutdown 2.poweroff 3.init 4.reboot 5.halt 二.五个重启命令的具体说明 shutdown reboot 在linux下一些 ...

  4. POJ1201 Intervals(差分约束)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 10966 Description You ...

  5. Javascript 对象 - 字符串对象

    字符串对象 字符串对象是JavaScript中比较常见的一种基本数据类型,他封装了一个字符串,并且提供了相应的方法.例如连接字符串.取字符串.分割字符串等.JavaScript中字符串是不可变的,原始 ...

  6. tsung HTTP协议统计报告分析

    tsung HTTP协议统计报告分析 by:授客 QQ:1033553122 1.   Main Static l  higest 10sec mean: 基于每10s的统计,最大耗时 l  lowe ...

  7. Java:字节流和字符流(输入流和输出流)

    本文内容: 什么是流 字节流 字符流 首发日期:2018-07-24 什么是流 流是个抽象的概念,是对输入输出设备的抽象,输入流可以看作一个输入通道,输出流可以看作一个输出通道. 输入流是相对程序而言 ...

  8. go 利用orm简单实现接口分布式锁

    在开发中有些敏感接口,例如用户余额提现接口,需要考虑在并发情况下接口是否会发生问题.如果用户将自己的多条提现请求同时发送到服务器,代码能否扛得住呢?一旦没做锁,那么就真的会给用户多次提现,给公司带来损 ...

  9. SQL server 导出平面文件时出错: The code page on Destination - 3_txt.Inputs[Flat File Destination Input].Columns[UserId] is 936 and is required to be 1252.

    我在导出平面文件时:Error 0xc00470d4: Data Flow Task 1: The code page on Destination - 3_txt.Inputs[Flat File ...

  10. zabbix监控tomcat(使用jmx监控,但不使用系统自带模版)

    一,zabbx使用jmx监控tomcat的原理分析 1.Zabbix-Server找Zabbix-Java-Gateway获取Java数据 2.Zabbix-Java-Gateway找Java程序(j ...