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. linux中make的用法

    一.linux中make的用法 目的:       基本掌握了make 的用法,能在Linux系统上编程.环境:       Linux系统准备:       准备三个文件:file1.c, file ...

  2. Android Studio 学习(二) UI

    TextView android:gravity="center"居中对齐 //文字对齐方式 top bottom left right center android:textSi ...

  3. angular 获取ng-repeat完成状态 $last

    $index $first $middle $last $odd $even html <ul> <li ng-repeat="item in data" rep ...

  4. gulp简介

    简介 gulp是基于Nodejs的自动任务运行器, 她能自动化地完成 javascript/coffee/sass/less/html/image/css 等文件的的测试.检查.合并.压缩.格式化.浏 ...

  5. JMeter 关于JMeter 正则表达式提取器的一点研究

    关于JMeter 正则表达式提取器的一点研究   by:授客 QQ:1033553122 1.   实验环境: JMeter 2.13 2.   添加正则表达式提取器 右键线程组->添加-> ...

  6. Native SBS for Android

    Native SBS for Android是一款非常棒的软件,支持安卓在2D界面下左右分屏显示,并可以设置缩放比例及左右间距,横屏自动切换为左右分屏显示模式,竖屏则为正常显示.启动左右分屏模式后,将 ...

  7. 虚拟现实的头戴式设备的视野(FOV)原理

    本文原址https://www.cnblogs.com/zhangmiao14/p/5836664.html. 对于VR,它做得最好的就是它对生活的变化,有一些关键因素需要调整的恰如其分.如果做得正确 ...

  8. python3接收、解析邮件

    邮件接收 python3可以使用poplib.POP3进行邮件接收,具体如下: import poplib from email.parser import Parser def get_email( ...

  9. java导出数据到excel里:直接导出和导出数据库数据

    一.直接导出 package com.ij34.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; ...

  10. Winform调用webapi

    /// <summary> /// 调用api返回json /// </summary> /// <param name="url">api地址 ...