第六届湖南省省赛 Biggest Number(DFS+BFS,巧妙剪枝)
来源:互联网 发布:mac快捷键删除 编辑:程序博客网 时间:2024/05/20 01:10
Biggest NumberTime Limit: 2000ms, Special Time Limit:5000ms, Memory Limit:65536KBTotal submit users: 31, Accepted users: 15Problem 10935 : No special judgementProblem description You have a maze with obstacles and non-zero digits in it:
Output For each test case, print the biggest number you can find, on a single line.
Sample Input
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.
Output For each test case, print the biggest number you can find, on a single line.
Sample Input
3 7##9784###123####45###0 0Sample Output
791452384题意:从任意一个非#格子出发,可以往上下左右走,一个格子只能走一次,求字典序最大的路径(先保证长度最大,再保证字典序)
思路:容易想到直接DFS爆搜,可是那样当然会TLE
那么怎么办呢?我们就需要用BFS去判断当前这个点所能走到的其他所有点的个数,如果当前已经走过的点的个数+理想状况下可以走到的点的个数<已经得到的最优解的话就不用往下搜了。
具体可以看代码
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <queue>using namespace std;const int N = 35;int n,m,len;char ma[N][N];bool flag[N][N];bool vis[N][N];int dir[][2] = {{1,0},{-1,0},{0,-1},{0,1}};struct Node{ int x,y;};char last[N],ans[N],ans1[N];bool check(int x,int y){ if(x<1||x>n||y<1||y>m||ma[x][y]=='#') return false; return true;}Node que[400];int bfs(int r,int c){ int num=0; memset(flag,0,sizeof(flag)); Node a,next; a.x=r,a.y=c; int head=0,tail=0; que[tail++]=a; flag[a.x][a.y]=1; while(head!=tail) { Node now=que[head++]; for(int i=0; i<4; i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; if(!check(next.x,next.y)||flag[next.x][next.y]||vis[next.x][next.y]) continue; flag[next.x][next.y]=1; num++; que[tail++]=next; } } return num;}void dfs(int x,int y,int step){ if(step>len||(step==len&&strcmp(ans,last)>0)) { len=step; strcpy(last,ans); } int num=bfs(x,y); strcpy(ans1,ans); ans1[step]='9'; if(step+num<len||(step+num==len&&strcmp(ans1,last)<0)) return; for(int i=0; i<4; i++) { int r=x+dir[i][0],c=y+dir[i][1]; if(!check(r,c)||vis[r][c]) continue; vis[r][c]=1; ans[step]=ma[r][c]; dfs(r,c,step+1); vis[r][c]=0; }}int main(){ // freopen("f.in","r",stdin); // freopen("f.txt","w",stdout); while(~scanf("%d %d",&n,&m)&&(n+m)) { int cnt=0; for(int i=1; i<=n; i++) scanf("%s",ma[i]+1); for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) if(ma[i][j]!='#') cnt++; len=-1; memset(last,0,sizeof(last)); memset(ans,0,sizeof(ans)); for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { if(ma[i][j]=='#'||(len==cnt&&ma[i][j]<last[0])) continue; memset(vis,0,sizeof(vis)); vis[i][j]=1; ans[0]=ma[i][j]; dfs(i,j,1); } printf("%s\n",last); } return 0;}
0 0
- 第六届湖南省省赛 Biggest Number(DFS+BFS,巧妙剪枝)
- UVA 11882 Biggest Number (搜索+剪枝(dfs+bfs))
- UVA 11882 Biggest Number (dfs搜索+bfs 剪枝)
- 湖南省第六届程序设计竞赛 Biggest Number
- 11882 - Biggest Number(DFS + 剪枝)
- UVa 11882:Biggest Number(DFS+剪枝)
- Uva11882 Biggest Number 【dfs回溯+bfs剪枝】【习题7-15】
- 湖南省第六届省赛 Biggest Number (搜索)
- 湖南省第六届大学生程序设计大赛原题 F Biggest Number (UVA1182)
- Biggest Number(刘汝佳,搜索+剪枝)
- 湖南省第6届程序设计大赛 Biggest Number (UVA1182)
- 第六届湖南省省赛 射击游戏(概率博弈,dfs)
- nyoj 667 Biggest Number 搜索 减枝 dfs bfs
- UVA11882 Biggest Number 强剪枝
- csu 1508 地图的四着色(BFS,DFS,巧妙剪枝)
- POJ 生日蛋糕(DFS 巧妙剪枝)
- NBUT [1464] Biggest Number dfs
- 第六届湖南省省赛 台球碰撞(模拟,数学)
- 排序算法
- Maven学习总结(九)——使用Nexus搭建Maven私服
- Snmp最基本概念
- 五大常用算法之三:贪心算法
- I/O多路转接之epoll
- 第六届湖南省省赛 Biggest Number(DFS+BFS,巧妙剪枝)
- BZOJ 1042题解(C++)
- maven 配置tomcat插件启动
- Android 网络请求框架 Retrofit2.0实践使用总结
- 二次封装selenium,简易框架Jzps
- Oracle数据库EXP/IMP导出导入
- Unix下C程序内存泄漏检测工具Valgrind安装与使用
- mongodb用户与角色
- 插画效果