题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次。

题目分析:DFS。剪枝方案:在当前的处境下,找出所有还能到达的点的个数,若当前数字的长度加上个数仍小于目前最优答案的长度,则剪去;若长度相等,则将所有还能到达的数字按从大到小排序后连到当前数字上,如果还比目前最优解小,则减去。找出所有还能到达的点的过程用BFS实现。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 16
#define M 36
#define inf 1e12
int n,m;
char mp[N][N];
int vis[N][N];
int flag[N][N];
int can[N<<];
int dirx[]={,,-,};
int diry[]={-,,,};
string ans; bool smaller(string a,string b){
if(a.size()<b.size()) {
return true;
}
if(a.size()==b.size()){
if(a<b){
return true;
}
}
return false;
} bool ok(int x,int y){
if(x< || x>=n || y< || y>=m || mp[x][y]=='#') return false;
return true;
}
int bfs(int x,int y){
queue<int> q;
q.push(x*M+y);
memset(flag,,sizeof(flag));
flag[x][y]=;
int step=;
while(!q.empty()){
int t=q.front();
q.pop(); for(int i=;i<;i++){
int tx=t/M; int ty=t%M;
tx+=dirx[i]; ty+=diry[i];
if(vis[tx][ty] || flag[tx][ty] || !ok(tx,ty)) continue;
can[step++]=mp[tx][ty]-'';
flag[tx][ty]=;
q.push(tx*M+ty);
}
}
return step;
} void dfs(int x,int y,string tmp){
if(smaller(ans,tmp)){
ans=tmp;
}
int get_step=bfs(x,y);
if(get_step+tmp.size()<ans.size()){
return;
}
if(get_step+tmp.size()==ans.size()){
sort(can,can+get_step);
string now=tmp;
for(int i=get_step-;i>=;i--){
now+=char(can[i]+'');
}
if(smaller(now,ans)){
return;
}
}
for(int i=;i<;i++){
int tx=x+dirx[i];
int ty=y+diry[i];
if(vis[tx][ty] || !ok(tx,ty)) continue; vis[tx][ty]=;
dfs(tx,ty,tmp+mp[tx][ty]);
vis[tx][ty]=;
}
}
int main()
{
while(scanf("%d%d",&n,&m)==){
if(n== && m==){
break;
}
for(int i=;i<n;i++){
scanf("%s",mp[i]);
}
ans.clear();
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(mp[i][j]!='#'){
vis[i][j]=;
string tmp; tmp.clear();
tmp+=mp[i][j];
dfs(i,j,tmp);
vis[i][j]=;
}
}
}
cout<<ans<<endl; }
return ;
}

UVA - 11882 Biggest Number(dfs+bfs+强剪枝)的更多相关文章

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

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

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

  3. 【UVa】11882 Biggest Number(dfs+剪枝)

    题目 题目     分析 典型搜索,考虑剪枝. 统计一下联通分量. 1.本位置能够达到所有的点的数量加上本已有的点,还没有之前的结果长,直接返回. 2.当本位置能够达到所有的点的数量加上本已有的点与之 ...

  4. POJ2308连连看dfs+bfs+优化

    DFS+BFS+MAP+剪枝 题意:       就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路:      首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...

  5. poj 3134 Power Calculus(迭代加深dfs+强剪枝)

    Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...

  6. UVA11882-Biggest Number(DFS+最优化剪枝)

    Problem UVA11882-Biggest Number Accept: 177    Submit: 3117Time Limit: 1000 mSec    Memory Limit : 1 ...

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

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

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

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

  9. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

随机推荐

  1. HOWTO Use Python in the web — Python v3.0.1 documentation

    HOWTO Use Python in the web - Python v3.0.1 documentation mod_python¶ People coming from PHP often f ...

  2. 今日成长笔记2016-11-18 - 关于java开发

    好久没有写今日成长笔记了,要记得上一次写笔记还是2016-09-05,今天心血来潮,写一写最近发生在自己身上的事情,以后我要坚持每天写日记.我承认自己身上的确或多或少的存在不足,现在把它们抛出来,并记 ...

  3. laravel5.3 笔记一

    laravel5.3 笔记 安装环境 laravel环境,laravel中文学习论坛上面有相关的教程 创建应用 laravel new blog 其中blog就是你的应用的名字 数据迁移 php ar ...

  4. 让你的javascript函数拥有记忆功能,降低全局变量的使用

    考虑例如以下场景:假如我们须要在界面上画一个圆,初始的时候界面是空白的.当鼠标移动的时候,圆须要尾随鼠标移动.鼠标的当前位置就是圆心.我们的实现方案是:假设界面上还没有画圆,那么就新创建一个:假设已经 ...

  5. poj 3436 (最大流)

    题意:每台电脑共有p种零件,现在有n台机器,给出n台机器每台需要的一些种类零件当原料(0代表不需要,1代表必须要,2代表可有可无)和输出的产品零件.问怎么安排生产线使生产出来零件可以组装的电脑最多. ...

  6. Redis 命令参考

    Redis 命令参考 http://redis.readthedocs.org/en/latest/index.html

  7. 4. Linux 系统目录

    一.Linux 系统目录的作用 /home            用户主目录的根节点,所有用户自己独有的文件一般放在这个目录下的用户目录下 /bin                二进制可执行命令 / ...

  8. (ZZ)WPF经典编程模式-MVVM示例讲解

    http://www.cnblogs.com/xjxz/archive/2012/11/14/WPF.html 本篇从两个方面来讨论MVVM模式: MVVM理论知识 MVVM示例讲解 一,MVVM理论 ...

  9. 查询DB中每个表占用的空间大小

    使用如下sql script可以获得每个数据库表所占用的空间大小,单位是KB create table #Data(name varchar(100),row varchar(100),reserve ...

  10. Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree

    1.Lowest Common Ancestor of a Binary Search Tree Total Accepted: 42225 Total Submissions: 111243 Dif ...