hdu 5335 Walk Out 搜索+贪心
Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 141 Accepted Submission(s): 17
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.
indicating the number of testcases.
For each testcase, the first line contains two integers n and m (1≤n,m≤1000).
The i-th
line of the next n lines
contains one 01 string of length m,
which represents i-th
row of the maze.
the answer itself is 0 (in
this case, print 0 instead).
2
2 2
11
11
3 3
001
111
101
111
101
求从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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define maxn 1001 char 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<Node> que;
vector<int> ans;
vector<Node> one;
vector<Node> 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<<endl;
// }
// cout<<endl;
memset(mark,0,sizeof(mark));
nidfs(n-1,m-1); memset(check,0,sizeof(check));
work();
getans();
// for(int i = 0;i < n; i++){
// for(int j = 0;j < m; j++){
// printf("%d ",dep[i][j]);
// }cout<<endl;
// }
if(ans.size() == 0 && ma[0][0] =='0'){
printf("0\n");
}
else {
if(ma[0][0] == '1') printf("1");
for(int i = 0;i < ans.size(); i++)
printf("%d",ans[i]);
printf("\n");
} }
return 0; }
/*
20
2 2
11
11
3 3
001
111
101
3 3
111
111
111
3 3
000
000
000
3 3
001
011
111
*/
hdu 5335 Walk Out 搜索+贪心的更多相关文章
- hdu 5335 Walk Out (搜索)
题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...
- HDU 5335——Walk Out——————【贪心】
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5335 Walk Out BFS 比较坑
H - H Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- HDU 6034---Balala Power!(搜索+贪心)
题目链接 Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He ...
- hdu 5335 Walk Out (2015 Multi-University Training Contest 4)
Walk Out Time Limit: 2000/10 ...
- 2015 Multi-University Training Contest 4 hdu 5335 Walk Out
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- hdu 5335 Walk Out(bfs+斜行递推) 2015 Multi-University Training Contest 4
题意—— 一个n*m的地图,从左上角走到右下角. 这个地图是一个01串,要求我们行走的路径形成的01串最小. 注意,串中最左端的0全部可以忽略,除非是一个0串,此时输出0. 例: 3 3 001 11 ...
- HDU 5335 Walk Out
题意:在一个只有0和1的矩阵里,从左上角走到右下角, 每次可以向四个方向走,每个路径都是一个二进制数,求所有路径中最小的二进制数. 解法:先bfs求从起点能走到离终点最近的0,那么从这个点起只向下或向 ...
- HDU 5335 Walk Out(多校)
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
随机推荐
- vijos- P1385盗窃-月之眼 (水题 + python)
P1385盗窃-月之眼 Accepted 标签:怪盗基德 VS OIBH[显示标签] 背景 怪盗基德 VS OIBH 第三话 描写叙述 怪盗基德第三次来到熟悉的OIBH总部.屡屡失败的OIBH这次看守 ...
- comp.lang.javascript FAQ [zz]
comp.lang.javascript FAQ Version 32.2, Updated 2010-10-08, by Garrett Smith FAQ Notes 1 Meta-FAQ met ...
- Android顶部粘至视图具体解释
不知从某某时间開始,这样的效果開始在UI设计中流行起来了.让我们先来看看效果: 大家在支付宝.美团等非常多App中都有使用.要实现这个效果,我们能够来分析下思路: 我们肯定要用2个一样的布局来显示我们 ...
- java.lang.ClassNotFoundException: org.objectweb.asm.ClassWriter
转自:https://www.cnblogs.com/yfceshi/p/6814802.html Caused by: javax.xml.ws.WebServiceException: java. ...
- Reusability1
Reusability 1. 复用的层面 1.1 代码级别复用 顾名思义,代码复用就是把代码都搬过来,这是最主要的复用 1.1.1 白盒复用 白盒的意思就是里面的东西我们都知道是什么,具体实现的方式也 ...
- POJ 1471 模拟?
题意:求最大无坏点三角形 思路: 模拟? (为什么我模拟过了...) 有人用 DP,有人用 搜索... // by SiriusRen #include <cstdio> #include ...
- 搭建简单的Habernate环境(一)
一.开篇 下载Habernate所需要的jar包和mysql驱动. 二.建立java项目并且导入所需要的包 三.建立实体类和配置映射文件 user实体类 package testPackage; pu ...
- <Android Framework 之路>Android5.1 Camera Framework(一)
Android5.0 Camera Framework 简介 CameraService启动 CameraService是在MediaServer启动过程中进行的 main_mediaserver.c ...
- 查看网站域名IP地址
运行:CMD 输入: Nslookup 网站地址 示例: Nslookup httpwww.baidu.com 我们是筑梦团队,我们的座右铭是:当提起逐梦的脚步,也请举起筑梦的双手
- 转载:常用 Git 命令清单
转载:常用 Git 命令清单 原文地址:http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html 作者: 阮一峰 我每天使用 Git , ...