假设当前已经到达(x,y),用bfs判断一下还可以到达的点有maxd个,如果maxd加上当前已经经过的长度小于当前答案的长度就退出,如果相同,就将bfs搜索到的点从大到小排序,如果连最大序列都无法大于当前答案,就直接退出,否则继续搜索。

但是这题时间要求极高,不可以同时搜索同时更新答案,必须直到完成一种搜索(到达边界)才能更新答案,否则必定超时。

AC代码:

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 30 + 5;
char G[maxn][maxn];
int vis[maxn][maxn], d[maxn][maxn];
int n, m;

int ans[maxn], len; //答案以及答案的长度
int a[maxn];

struct node{
	int x, y;
	node(){
	}
	node(int x, int y):x(x), y(y){
	}
};

const int dx[] = {1,-1,0,0};
const int dy[] = {0,0,1,-1};

int bfs(int x, int y, int cur){ //从cur开始保存
	int c = cur;
	memcpy(d, vis, sizeof(vis));
	queue<node>q;
	q.push(node(x, y));
	d[x][y] = 1;
	while(!q.empty()){
		node pos = q.front();
		q.pop();
		x = pos.x, y = pos.y;
		for(int i = 0; i < 4; ++i){
			int px = x + dx[i], py = y + dy[i];
			if(px < 0 || px >= n || py < 0 || py >= m) continue;
			if(G[px][py] == '#' || d[px][py]) continue;
			d[px][py] = 1;
			a[c++] = G[px][py] - '0';
			q.push(node(px, py));
		}
	}
	return c;
}

bool cmp(int a, int b){
	return a > b;
}

void dfs(int x, int y, int cur){
	int h = bfs(x, y, cur);

	if(h == cur) {  //边界
		int flag = 0;
		if(cur < len) return;
		else if(cur > len) flag = 1;
		else for(int i = 0; i < len; ++i){
			if(a[i] < ans[i]) return;
			else if(a[i] > ans[i]) {
				flag = 1;
				break;
			}
		}
		if(flag) {
			len = cur;
			for(int i = 0; i < len; ++i) ans[i] = a[i]; //update
		}
		return;
	}

	if(h < len) return;
	else if(h == len){
		sort(a + cur, a + h, cmp);
		int flag = 0;
		for(int i = 0; i < len; ++i) {
			if(a[i] < ans[i]) return;
			else if(a[i] > ans[i]) {
				flag = 1;
				break;
			}
		}
		if(!flag) return;
	}

	for(int i = 0; i < 4; ++i){
		int px = x + dx[i], py = y + dy[i];
		if(px < 0 || px >= n || py < 0 || py >= m) continue;
		if(G[px][py] == '#' || vis[px][py]) continue;
		a[cur] = G[px][py] - '0';
		vis[px][py] = 1;
		dfs(px, py, cur + 1);
		vis[px][py] = 0;
	}
}

int  main(){
	while(scanf("%d%d", &n, &m) == 2 && n){
		memset(ans, 0, sizeof(ans));
		len = 0;
		for(int i = 0; i < n; ++i) scanf("%s", G[i]);

		for(int i = 0; i < n; ++i)
		for(int j = 0; j < m; ++j) {
			if(G[i][j] == '#') continue;
			memset(vis, 0, sizeof(vis));
			a[0] = G[i][j] - '0';
			vis[i][j] = 1;
			dfs(i, j, 1);
			vis[i][j] = 0;
		}

		for(int i = 0; i < len; ++i){
			printf("%d", ans[i]);
		}
		printf("\n");
	}
	return 0;
}

还有一种dfs代码,思路同上

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 30 + 5;
char G[maxn][maxn];
int vis[maxn][maxn], d[maxn][maxn];
int n, m;

int ans[maxn], len; //答案以及答案的长度
int a[maxn];

struct node{
	int x, y;
	node(){
	}
	node(int x, int y):x(x), y(y){
	}
};

const int dx[] = {1,-1,0,0};
const int dy[] = {0,0,1,-1};

bool cmp(int a, int b){
	return a > b;
}

int bfs(int x, int y, int cur){
	int c = cur;
	memcpy(d, vis, sizeof(vis));
	queue<node>q;
	q.push(node(x, y));
	d[x][y] = 1;
	while(!q.empty()){
		node pos = q.front();
		q.pop();
		x = pos.x, y = pos.y;
		for(int i = 0; i < 4; ++i){
			int px = x + dx[i], py = y + dy[i];
			if(px < 0 || px >= n || py < 0 || py >= m) continue;
			if(G[px][py] == '#' || d[px][py]) continue;
			d[px][py] = 1;
			a[c++] = G[px][py] - '0';
			q.push(node(px, py));
		}
	}
	return c;
}

void dfs(int x, int y, int cur){
	a[cur++] = G[x][y] - '0';
	int h = bfs(x, y, cur);

	if(h == cur) {  //边界
		int flag = 0;
		if(cur < len) return;
		else if(cur > len) flag = 1;
		else for(int i = 0; i < len; ++i){
			if(a[i] < ans[i]) return;
			else if(a[i] > ans[i]) {
				flag = 1;
				break;
			}
		}
		if(flag) {
			len = cur;
			for(int i = 0; i < len; ++i) ans[i] = a[i]; //update
		}
		return;
	}

	if(h < len) return;
	else if(h == len){
		sort(a + cur, a + h, cmp);
		int flag = 0;
		for(int i = 0; i < len; ++i) {
			if(a[i] < ans[i]) return;
			else if(a[i] > ans[i]) {
				flag = 1;
				break;
			}
		}
		if(!flag) return;
	}

	vis[x][y] = 1;
	for(int i = 0; i < 4; ++i){
		int px = x + dx[i], py = y + dy[i];
		if(px < 0 || px >= n || py < 0 || py >= m) continue;
		if(G[px][py] == '#' || vis[px][py]) continue;
		dfs(px, py, cur);
	}
	vis[x][y] = 0;

}

int  main(){
	while(scanf("%d%d", &n, &m) == 2 && n){
		memset(ans, 0, sizeof(ans));
		len = 0;
		for(int i = 0; i < n; ++i) scanf("%s", G[i]);

		for(int i = 0; i < n; ++i)
		for(int j = 0; j < m; ++j) {
			if(G[i][j] == '#') continue;
			memset(vis, 0, sizeof(vis));
			dfs(i, j, 0);
		}
		for(int i = 0; i < len; ++i){
			printf("%d", ans[i]);
		}
		printf("\n");
	}
	return 0;
}

如有不当之处欢迎指出!

UVA-11882 bfs + dfs + 剪枝的更多相关文章

  1. Sticks(UVA - 307)【DFS+剪枝】

    Sticks(UVA - 307) 题目链接 算法 DFS+剪枝 1.这道题题意就是说原本有一些等长的木棍,后来把它们切割,切割成一个个最长为50单位长度的小木棍,现在想让你把它们组合成一个个等长的大 ...

  2. hdu 1044(bfs+dfs+剪枝)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. POJ2688状态压缩(可以+DFS剪枝)

    题意:       给你一个n*m的格子,然后给你一个起点,让你遍历所有的垃圾,就是终点不唯一,问你最小路径是多少? 思路:       水题,方法比较多,最省事的就是直接就一个BFS状态压缩暴搜就行 ...

  4. soj1091 指环王 bfs+hash+剪枝

    原题链接http://acm.scu.edu.cn/soj/problem.action?id=1091 这题的主要解法就是搜索,我用的是bfs,用map将二维数组处理成字符串作为主键,到达当前状态的 ...

  5. Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)

    An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...

  6. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  8. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  9. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

随机推荐

  1. tomcat server.xml context path配置需要注意的事情

    在tomcat下放个war包,假如我是这样配置server.xml的,<Context docBase="eggchina" path="/yanan" ...

  2. Maven 常用配置

    pom.xml基础配置: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEn ...

  3. 【javaweb学习笔记】WEB01_HTML

    案例一:网站信息显示页面1.什么是HTML?(Hyper Text Markup Language:超文本标记语言) 超文本:功能比普通文本更加强大 标记语言:使用一组标签对内容进行描述的一门语言(它 ...

  4. SSMS 2005 连接 SQL SERVER 2008问题

    用本机的 Microsoft SQL Server Management Studio 2005 客户端连接数据库服务器时报错:"This version of Microsoft SQL ...

  5. Spring MVC的DispatcherServlet

    Spring MVC提供了一个名为org.springframework.web.servlet.DispatcherServlet的Selvet充当前端控制器,所有的请求驱动都围绕这个Dispatc ...

  6. 编译安装 apache 2.4.6

    如果配置apr,需要预先安装apr 以下是安装apache 步骤: groupadd webuser useradd -g webuser webuser 下载apache2 下载链接:http:// ...

  7. CentOS7安装WDCP3

    CentOS7安装WDCP3.2面板教程 到此WDCP安装完毕

  8. 51NOD 1222 最小公倍数计数 [莫比乌斯反演 杜教筛]

    1222 最小公倍数计数 题意:求有多少数对\((a,b):a<b\)满足\(lcm(a,b) \in [1, n]\) \(n \le 10^{11}\) 卡内存! 枚举\(gcd, \fra ...

  9. (python功能定制)复杂的xml文件对比,产生HTML展示区别

    功能的设计初衷: 处理复杂的xml对比,屏蔽同节点先后顺序的影响 主要涉及知识点: 1.xml解析 ------- ElementTree库 2.文件比对差别 ------- difflib库 3.获 ...

  10. create-react-app搭建的项目中添加bootstrap

    react-bootstrap是一个非常受欢迎的针对react封装过的bootstrap,它本身不包含css,所以也是需要使用bootstrap原生库. 在create-react-app建的项目目录 ...