toj 4610 Biggest Number

时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte
总提交: 25 测试通过:5

描述

You have a maze with obstacles and non-zero digits in it:

You can start from any square, walk in the maze, and finally stop at some square. Each step, you may only walk into one of the four neighbouring squares (up, down, left, right) and you cannot walk into obstacles or walk into a square more than once. When you finish, you can get a number by writing down the digits you encounter in the same order as you meet them. For example, you can get numbers 9784, 4832145 etc. The biggest number you can get is 791452384, shown in the picture above.

Your task is to find the biggest number you can get.

输入

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.

输出

For each test case, print the biggest number you can find, on a single line.

样例输入

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

样例输出

791452384

#include <iostream>
#include <cstdio>
#include <cstring>using namespace std;const int MAX_NUM = 30 + 5;// 位置节点,用于广搜中的队列
struct node {int x, y;
} queue[1000];
// 行、列
int n, m;
// 当前最大递归深度
int Max;
// 标记当前深搜深度与当前最优解该位置的关系
// 0:相等 1:大于 -1:小于
int flag;
// 数字总数
int total;
// 图
char map[MAX_NUM][MAX_NUM];
// 最优值和当前值
char ans[MAX_NUM], stack[MAX_NUM];
// 方向
int dir[4][2] = {{-1, 0},{0,  -1},{0,  1},{1,  0}};bool ok(int x, int y) {return x >= 0 && x < n && y >= 0 && y < m;
}// 广搜找到(x,y)点开始的,剩下可访问数字的数量
int bfs(int x, int y) {node t;char g[20][20];for (int i = 0; i < n; i++) {strcpy(g[i], map[i]);}int head, tail;head = tail = 0;t.x = x, t.y = y;queue[tail++] = t;while (head < tail) {x = queue[head].x;y = queue[head++].y;for (int i = 0; i < 4; i++) {int xx = x + dir[i][0];int yy = y + dir[i][1];if (!ok(xx, yy) || g[xx][yy] == '#') {continue;}g[xx][yy] = '#';t.x = xx;t.y = yy;queue[tail++] = t;}}return head;
}void dfs(int x, int y, int cnt) {// 深度更深或者深度相等但是当前值更大if (Max < cnt || (Max == cnt && flag == 1)) {// 0表示字符串结尾stack[cnt] = 0;// 更新当前值为当前最优解strcpy(ans, stack);// 当前最大递归深度为当前深度Max = cnt;// 位置关系更新为等于flag = 0;}// 剩下可访问数字的数量int res = bfs(x, y);// 最优性剪枝:剩下可访问数字数量+当前深度小于当前最优深度// 或者以上相同但是当前值小一些if (res + cnt - 1 < Max || (res + cnt - 1 == Max && flag == -1)) {return;}for (int i = 0; i < 4; i++) {int xx = x + dir[i][0];int yy = y + dir[i][1];// 可行性剪枝:出界或者不为数字if (!ok(xx, yy) || map[xx][yy] == '#') {continue;}// 最优性剪枝:当前深搜深度或值不大于// 且当前最优的第cnt位大于扩展节点值// 且已经找到了一条覆盖了所有数字的路径if (flag != 1 && ans[cnt] > map[xx][yy] && total == Max) {continue;}// 赋值并标记stack[cnt] = map[xx][yy];map[xx][yy] = '#';// 更新flag表示的位置关系if (flag == 0) {if (cnt >= Max) {flag = 1;} else if (ans[cnt] == stack[cnt]) {flag = 0;} else if (ans[cnt] < stack[cnt]) {flag = 1;} else {flag = -1;}// 深搜dfs(xx, yy, cnt + 1);// 回溯flag = 0;} else {// 深搜dfs(xx, yy, cnt + 1);}// 回溯map[xx][yy] = stack[cnt];}
}int main() {while (scanf("%d%d", &n, &m) && n + m) {// 输入并获取数字总数total = 0;for (int i = 0; i < n; i++) {scanf("%s", map[i]);for (int j = 0; j < m; j++) {if (map[i][j] != '#') {total++;}}}// 初始化Max = 1;memset(ans, 0, sizeof(ans));// 遍历深搜for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {// 可行性剪枝:必须是数字if (map[i][j] == '#') {continue;}// 最优性剪枝:搜索到的路径已覆盖所有数字// 如果这时的最优值最高位>当前深搜的第一位,那么就可以剪枝if (Max == total && ans[0] > map[i][j]) {continue;}stack[0] = map[i][j];// 标记map[i][j] = '#';// 获取当前深搜首位与当前最优解首位的关系if (ans[0] == stack[0]) {flag = 0;} else if (ans[0] < stack[0]) {flag = 1;} else {flag = -1;}// 深搜dfs(i, j, 1);// 还原map[i][j] = stack[0];}}printf("%s\n", ans);}return 0;
}

toj 4610 Biggest Number相关推荐

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

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

  2. toj 3616 Add number (没想到啊~~)

    Add number 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 总提交: 60 测试通过: 21 描述 Employees of Baidu like ...

  3. UVa11882,Biggest Number

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

  4. 2010年湖南省第六届大学生程序设计大赛 F题 “Biggest Number” CSG - 1051 // UVA 11882 (dfs+bfs+剪枝)

    题目链接 一.题目内容 一个r行c列的矩阵,里面只有1-9的数字和'#',最开始可以随意挑选一个数字然后可以上下左右移动,问所走的路径所含数字最大为多少. 样例解释 input 3 7 ##9784# ...

  5. [Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  6. HDOJ_ACM_数塔

    Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大 ...

  7. 《Two Dozen Short Lessons in Haskell》学习(十三)迭代及重复的常规模式

    <Two Dozen Short Lessons in Haskell>(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学 ...

  8. javascript库函数大全

    Global(全局对象/属性) Global 全局对象 Infinity 表示无穷大的数字属性 NaN 非数字属性 undefined 未定义值 Global 全局对象     可用性 JavaScr ...

  9. Javascript 笔记(1)----函数

    1.parseInt: >>>parseInt('123dsfsd') 123 >>>parseInt('abc1.33') NaN >>>par ...

最新文章

  1. Spring @Lazy
  2. h5怎么加入php代码,HTML5主要新增标签的使用代码分享
  3. 【Python】一种超简单的变量交换方法
  4. PAT (Advanced Level) 1140~1143:1140模拟 1141模拟 1142暴力 1143 BST+LCA
  5. Oracle On Linux
  6. mqtt实例 php_php搭建MQtt协议服务
  7. 苹果macfcpx视频剪辑软件:Final Cut Pro X
  8. Bug: tf.contrib.checkpoint.NoDependency object
  9. MT4 API 跟单交易接口更新
  10. 渗透之——网站入侵思路
  11. chrome浏览器上传文件fakepath问题
  12. nginx搭建mp4、flv流媒体服务器
  13. 将文件目录生成文档目录或者excel目录
  14. 【Flink】Flink 消费 kafka retries和retry.backoff.ms 引起问题
  15. 一种很厉害的AI学习方式
  16. 想要运营公众号?公众号形象定位有哪些?
  17. 关于心理咨询师与咨询者的讨论
  18. 12月小报|读小报,涨知识
  19. swift 判断是否设置了代理
  20. 有4个圆塔,圆心分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),圆半径为1,这4个塔的高度为10m,塔以外无建筑物。今输入任一点的坐标,求该点的建筑高度(塔外的建筑高度为零)

热门文章

  1. php 将前端网页输出成unicdoe编码
  2. mui switch 实现方案 让你的html 设计更贴近原生
  3. C#LeetCode刷题-线段树
  4. node环境变量_实际使用Node环境变量的方法如下
  5. json 插入数据_Power BI数据回写SQL Server(2)——存储过程一步到位
  6. golang.org/x/lint安装失败
  7. 解决 idea Method threw ‘java.lang.NoClassDefFoundError‘ exception. Cannot evaluate xxx toString 问题
  8. python 基本类型,运算,循环
  9. Python实现红黑树的删除操作
  10. 漫步线性代数十一—— 四个基本子空间