Walk Out

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

Problem Description
In an n∗m 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 (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.

 
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
 
 
题目大意:t组数据。每组n,m分别表示行和列。下面的n行m列用0、1表示地图情况。问你从(1,1)走到(n,m)所经过的点按顺序形成的二级制数最小是多少?
 
 
解题思路:先把所有的跟入口相连的0连通块都标记上。然后找到跟这个0连通块相邻接的1.这些1是可能的出发点。再在这些1中找到离出口最近的点。从这些点只需要往右往下走。因为当你确定离出口最近的点的时候,二进制数的位数已经确定。然后再广搜,贪心0。如果广搜时发现有0,那么将所有的0入队,如果没有发现0,那么将所有的1入队。
 
#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
const int INF=0x3f3f3f3f;
int vis[maxn][maxn];
int Map[maxn][maxn];
int way[2*maxn];
int f[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int n,m,k;
struct NODE{
int x,y;
};
queue<NODE>Q;
stack<NODE>S;
int BFS(){
NODE st,tmp;
while(!Q.empty()){
st=Q.front();
Q.pop();
int xx,yy;
for(int i=0;i<4;i++){
xx=st.x+f[i][0];
yy=st.y+f[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(!vis[xx][yy])&&Map[xx][yy]==0){
if(xx==n&&yy==m)
return -1;
tmp.x=xx,tmp.y=yy;
vis[xx][yy]=1;
Q.push(tmp);
}
}
}
}
bool check(int x,int y){
int xx,yy;
for(int i=0;i<4;i++){
xx=x+f[i][0];
yy=y+f[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy]==1){
return true;
}
}
return false;
}
void Find1(int typ){
while(!S.empty())
S.pop();
while(!Q.empty())
Q.pop();
int maxs=-INF;
NODE st,tmp; if(typ==0){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(Map[i][j]==1){
if(check(i,j)){ if(i+j>maxs){
maxs=i+j;
}
st.x=i,st.y=j;
vis[i][j]=2;
S.push(st);
}
}
}
}
}else{
st.x=1,st.y=1;
vis[1][1]=2;
maxs=2;
S.push(st);
}
if(S.empty()==0)
way[k++]=1;
while(!S.empty()){
st=S.top();
S.pop();
if(st.x+st.y==maxs){
Q.push(st);
}
}
while(1){
int flag=0;
while(!Q.empty()){
st=Q.front();
Q.pop();
if(st.x==n&&st.y==m){
return ;
}
int xx,yy;
for(int i=0;i<2;i++){
xx=st.x+f[i][0];
yy=st.y+f[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(!vis[xx][yy])){
if(Map[xx][yy]==0){
flag=1;
}
tmp.x=xx,tmp.y=yy;
vis[xx][yy]=2;
S.push(tmp);
}
}
}
if(flag==1){
way[k++]=0;
while(!S.empty()){
st=S.top();
S.pop();
if(Map[st.x][st.y]==0){
Q.push(st);
}
}
}else{
way[k++]=1;
while(!S.empty()){
st=S.top();
S.pop();
Q.push(st);
}
}
}
}
int main(){
int t;
char str[1020];
scanf("%d",&t);
while(t--){
k=0;
memset(vis,0,sizeof(vis));
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",str+1);
for(int j=1;j<=m;j++){
Map[i][j]=str[j]-'0';
}
}
if(n==m&&n==1&&Map[1][1]==0){
printf("0\n");continue;
}
if(Map[1][1]==0){
NODE st;
st.x=1,st.y=1;
vis[1][1]=1;
while(!Q.empty())
Q.pop();
Q.push(st);
if(BFS()==-1) {
printf("0\n");
continue;
}
Find1(0);
}
else{
Find1(1);
}
for(int i=0;i<k;i++){
printf("%d",way[i]);
}printf("\n");
}
return 0;
}
/*
50
3 3
001
111
101 55
1 2
01 */

  

 
 

HDU 5335——Walk Out——————【贪心】的更多相关文章

  1. hdu 5335 Walk Out (搜索)

    题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...

  2. HDU 5335 Walk Out BFS 比较坑

    H - H Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status ...

  3. hdu 5335 Walk Out 搜索+贪心

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S ...

  4. hdu 5335 Walk Out (2015 Multi-University Training Contest 4)

    Walk Out                                                                         Time Limit: 2000/10 ...

  5. HDU 5335 Walk Out

    题意:在一个只有0和1的矩阵里,从左上角走到右下角, 每次可以向四个方向走,每个路径都是一个二进制数,求所有路径中最小的二进制数. 解法:先bfs求从起点能走到离终点最近的0,那么从这个点起只向下或向 ...

  6. HDU 5335 Walk Out(多校)

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  7. 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 ...

  8. HDU 5335 Walk Out (BFS,技巧)

    题意:有一个n*m的矩阵,每个格子中有一个数字,或为0,或为1.有个人要从(1,1)到达(n,m),要求所走过的格子中的数字按先后顺序串起来后,用二进制的判断大小方法,让这个数字最小.前缀0不需要输出 ...

  9. hdu 5335 Walk Out(bfs+斜行递推) 2015 Multi-University Training Contest 4

    题意—— 一个n*m的地图,从左上角走到右下角. 这个地图是一个01串,要求我们行走的路径形成的01串最小. 注意,串中最左端的0全部可以忽略,除非是一个0串,此时输出0. 例: 3 3 001 11 ...

随机推荐

  1. winform ComBox绑定数据

    初始化数据: List<KeyValuePair<string, string>> list: ComBox1.ValueMember = "Key";Co ...

  2. C#字符串要点(复习专用)

    一.字符串 通过string定义一个字符串,或者通过String类来创建对象. 通过new String() 创建有一下几种构造函数(从元数据),以此顺序创建string: // // 摘要: // ...

  3. Java开发环境的搭建-JDK的安装

    一.下载 JDK是个免费的东东,所以不要去百度啥破解版了,直接去官网下载最新版本吧,比较安全, 下载地址 如下图所示 - 点击上图中的圈中部分,之后会下图的部分. 根据你的电脑系统是64位,还是32位 ...

  4. docker常用命令行集锦

    对工作中用到的docker命令行进行一个汇总,方便以后的命令行查询,同时也为了加强记忆,会把工作中用到的命令,持续更新上 1.查看私有仓库都有哪些镜像 curl -X GET http://10.27 ...

  5. 开窗函数Over用法

    比如我们有这个表: 销售记录表 日期 姓名 产品 销售额 201601 A 电脑 12560 201601 A 手机 6501 201601 A 平板 8510 201602 A 手机 1560 20 ...

  6. 数据结构13: 括号匹配算法及C语言实现

    在编写代码的时候,经常会用到两种括号:圆括号 “()” 和大括号 “{}” .不管使用哪种括号,程序编译没有问题的其中一个重要因素就是所使用的括号是否能够匹配上. 在编写程序时,括号可以嵌套,即: “ ...

  7. jquery发送请求

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. php 常见递归实例

    //计算数组{1,1,2,3,5,8.......} 第n位值 function Process1($i){ if ($i == 0) return 0; if ($i == 1) return 1; ...

  9. Django 解答 01 (pycharm创建项目)

    pycharm创建项目 1. 2. 3.Tools --->Deployment--->Options 这一条由always 改为 On explicit save action(Ctrl ...

  10. 2015苏州大学ACM-ICPC集训队选拔赛(2)1002

    草爷要的雷 Problem Description 扫雷一直是风靡实验室的重要娱乐游戏,在赛前赛后.刷题疲惫的时候,扫一局雷经常可以让队员们感受到身心的振奋,毕竟,劳逸结合刷题,防猝死才是硬道理.但是 ...