博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 5335 Walk Out 搜索+贪心
阅读量:5810 次
发布时间:2019-06-18

本文共 4283 字,大约阅读时间需要 14 分钟。

Walk Out

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 141    Accepted Submission(s): 17


Problem Description
In an 
nm maze, the right-bottom corner is the exit (position 
(n,m) is the exit). In every position of this maze, there is either a 
0 or a 
1 written on it.
An explorer gets lost in this grid. His position now is 
(1,1), and he wants to go to the exit. Since to arrive at the exit is easy for him, he wants to do something more difficult. At first, he'll write down the number on position 
(1,1). Every time, he could make a move to one adjacent position (two positions are adjacent if and only if they share an edge). While walking, he will write down the number on the position he's on to the end of his number. When finished, he will get a binary number. Please determine the minimum value of this number in binary system.
 

Input
The first line of the input is a single integer 
T (T=10), indicating the number of testcases. 
For each testcase, the first line contains two integers 
n and 
m (1n,m1000). The 
i-th line of the next 
n lines contains one 01 string of length 
m, which represents 
i-th row of the maze.
 

Output
For each testcase, print the answer in binary system. Please eliminate all the preceding 
0 unless the answer itself is 
0 (in this case, print 
0 instead).
 

Sample Input
 
2 2 2 11 11 3 3 001 111 101
 

Sample Output
 
111 101
 

Source

求从1,1到n。n的路径中。得到的二进制数最小是什么

方法:

假设1,1是0那么搜索出全部可达的0号点,前缀0为0,所以能够从这些位置出发。

然后有了一个1,。

那么仅仅要路径最短的,同一时候得到的二进制数最小的。

从全部可出发的点出发,做bfs,计算到n,n的最短路。

然后从n,n做bfs看,那些位置是在最短路上的。

然后从全部可出发的点出发,bfs。

假设存在能到的0点。

则从这些点出发。否则从到的1点出发。

这样贪心做就可以。直到到n。n点

#pragma comment(linker, "/STACK:102400000,102400000")#include
#include
#include
#include
#include
#include
using namespace std;#define maxn 1001char ma[maxn][maxn];int check[maxn][maxn];int dep[maxn][maxn];struct Node{ int x,y; Node(int _x=0,int _y=0):x(_x),y(_y){}};queue
que;vector
ans;vector
one;vector
zero;int n,m;void dfs(int x,int y){ if(check[x][y] || x == n || y == m || x < 0 || y < 0 || ma[x][y] == '1') return; que.push(Node(x,y)); check[x][y] = 1; dfs(x+1,y); dfs(x,y+1); dfs(x-1,y); dfs(x,y-1);}void work(){ while(que.size() > 0) que.pop(); if(ma[0][0] == '1'){ check[0][0] = 1; que.push(Node(0,0)); } else { dfs(0,0); }}void bfs(){ Node a; while(que.size() > 0){ a = que.front(); que.pop(); if(a.x + 1 < n && a.y < m && check[a.x+1][a.y] == 0){ que.push(Node(a.x+1,a.y)); check[a.x+1][a.y] = 1; dep[a.x+1][a.y] = dep[a.x][a.y] + 1; } if(a.x < n && a.y + 1 < m && check[a.x][a.y+1] == 0){ que.push(Node(a.x,a.y+1)); check[a.x][a.y+1] = 1; dep[a.x][a.y+1] = dep[a.x][a.y] + 1; } }}int mark[maxn][maxn];void nidfs(int x,int y){ mark[x][y] = 1; if(x-1>=0 && y >= 0 && mark[x-1][y] == 0 && dep[x-1][y] == dep[x][y] - 1 && dep[x-1][y] > 0) nidfs(x-1,y); if(x >= 0 && y-1>=0 && mark[x][y-1] == 0 && dep[x][y-1] == dep[x][y] - 1 && dep[x][y-1] > 0) nidfs(x,y-1);}void getans(){ ans.clear(); Node a; while(1){ one.clear(); zero.clear(); while(que.size() > 0){ a = que.front(); que.pop(); if(a.x == n -1 && a.y == m -1 ) return ; if(a.x + 1 < n && a.y < m && check[a.x+1][a.y] == 0&& dep[a.x+1][a.y] == dep[a.x][a.y] + 1 && mark[a.x+1][a.y]){ if(ma[a.x+1][a.y] == '1') one.push_back(Node(a.x+1,a.y)); else zero.push_back(Node(a.x+1,a.y)); check[a.x+1][a.y] = 1; } if(a.x < n && a.y + 1< m && check[a.x][a.y+1] == 0 && dep[a.x][a.y+1] == dep[a.x][a.y] + 1 && mark[a.x][a.y+1]){ if(ma[a.x][a.y+1] == '1') one.push_back(Node(a.x,a.y+1)); else zero.push_back(Node(a.x,a.y+1)); check[a.x][a.y+1] = 1; } } if(zero.size() > 0){ ans.push_back(0); for(int i = 0;i < zero.size();i++) que.push(zero[i]); } else { ans.push_back(1); for(int i = 0;i < one.size();i++) que.push(one[i]); } }}int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(int i = 0;i < n; i++){ scanf("%s",ma[i]); } memset(check,0,sizeof(check)); work(); memset(dep,0,sizeof(dep)); bfs();// for(int i = 0;i < n; i++){// for(int j = 0;j < m; j++){// printf("%d ",dep[i][j]);// }cout<
< n; i++){// for(int j = 0;j < m; j++){// printf("%d ",dep[i][j]);// }cout<
< ans.size(); i++) printf("%d",ans[i]); printf("\n"); } } return 0;}/*202 211113 30011111013 31111111113 30000000003 3001011111*/

转载地址:http://trcbx.baihongyu.com/

你可能感兴趣的文章
win10 bash 记录
查看>>
【转载】 HTTP 中 GET 与 POST 的区别
查看>>
linux统配符
查看>>
格式化输出%、基本运算符
查看>>
wp7 listbox底部显示加载更多
查看>>
echarts 图标绘制之折线图
查看>>
步步为营 .NET 代码重构学习笔记 一、为何要代码重构
查看>>
Linux篇---ftp服务器的搭建
查看>>
文本框样式!
查看>>
get请求和post请求
查看>>
Mac 安装phpmyadmin提示无法登陆 mysql服务器解决办法
查看>>
dubbo简单用法
查看>>
debian webmin 安装
查看>>
对于Servlet、Servlet容器以及一个Servlet容器-Tomcat
查看>>
在Qt中调用Mupdf库进行pdf显示
查看>>
Android Studio - 安卓开发工具 打开后报错集合、修复指南
查看>>
[iOS]ARC和MRC下混编
查看>>
UVA 11582 巨大的斐波那契额数列! 快速幂取模
查看>>
夏日里的清新——南锣鼓巷的北京女孩儿们[原创街拍]
查看>>
only女装2011春款 only2011新款春装 only女装官网专卖
查看>>