Walk Out

                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit:
65536/65536 K (Java/Others)

                                                                                               Total Submission(s): 194    Accepted Submission(s): 32

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
 


题目大意:
       从(1,1)到(n,m),路径形成的二进制数最大。

解题思路:
      BFS+贪心,先是bfs找到离目标近期的距离。后用贪心让最前面的尽可能为0。策略就是每往前走一步,推断是否
能够是0,方法就是找它前面离它近期的能够取0的那一位,推断能否够从那个位置走到当前的位置。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=1000+100;
char s[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
struct node
{
int x;
int y;
};
int ans;
int n,m;
queue<node> q;
vector<node> son[maxn*2];
void BFS1()
{
memset(vis,false,sizeof(vis));
node p;
p.x=1;
p.y=1;
ans=0;
vis[1][1]=true;
while(!q.empty())
q.pop();
if(s[1][1]=='0')
{
ans=2;
q.push(p);
}
while(!q.empty())
{
node p2;
p=q.front();
q.pop();
if(p.x==n&&p.y==m)
{
if(s[n][m]=='0')
{
ans=n+m;
vis[n][m]=true;
}
break;
}
for(int i=0; i<4; i++)
{
p2.x=p.x+dir[i][0];
p2.y=p.y+dir[i][1];
if(p2.x>0&&p2.x<=n&&p2.y>0&&p2.y<=m&&!vis[p2.x][p2.y]&&s[p2.x][p2.y]=='0')
{
vis[p2.x][p2.y]=true;
if(p2.x+p2.y>ans)
{
ans=p2.x+p2.y;
}
q.push(p2);
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%s",s[i]+1);
}
BFS1();
if(ans==n+m)
printf("0\n");
else
{
for(int i=0; i<=n+m; i++)
son[i].clear();
int cur=0;//记录前一位0出现的位置
if(ans==0)//起点为1
ans=1;
else
{
for(int i=1; i<=n; i++)//找到全部的离(n,m)近期的点。
{
int j=ans-i;
if(j>=1&&j<=m&&vis[i][j]&&s[i][j]=='0')
{
node v;
v.x=i;
v.y=j;
son[ans].push_back(v);
}
}
cur=ans;
}
for(int i=ans+1; i<=n+m; i++)//枚举每一步
{
if(cur==0)//前面不存在0
{
for(int j=1; j<=n; j++)
{
int k=i-j;
node v;
if(k>=1&&k<=m&&s[j][k]=='0')
{
v.x=j;
v.y=k;
son[i].push_back(v);
cur=i;
}
}
}
else
{
for(int j=1; j<=n; j++)
{
int k=i-j;
node v;
if(k>=1&&k<=m&&s[j][k]=='0')
{
for(int l=0; l<son[cur].size(); l++)
{
v=son[cur][l];
if(v.x<=j&&v.y<=k&&i-cur>=j+k-v.x-v.y)//推断前面的0是否可达
{
node w;
w.x=j;
w.y=k;
son[i].push_back(w);
break;
}
}
}
}
if(son[i].size()>0)
cur=i;
} }
for(int i=ans+1; i<=n+m; i++)
{
if(son[i].size()>0)
printf("0");
else
printf("1");
}
printf("\n");
}
}
return 0;
}


     

hdu 5335 Walk Out (2015 Multi-University Training Contest 4)的更多相关文章

  1. hdu 5335 Walk Out (搜索)

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

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

  3. HDU 5335 Walk Out(多校)

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

  4. hdu 5335 Walk Out 搜索+贪心

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

  5. hdu 5335 Walk Out(bfs+寻找路径)

    Problem Description In an n∗m maze, the right-bottom corner or a written on it. An explorer gets los ...

  6. HDU 5335 Walk Out BFS 比较坑

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

  7. HDU 6091 - Rikka with Match | 2017 Multi-University Training Contest 5

    思路来自 某FXXL 不过复杂度咋算的.. /* HDU 6091 - Rikka with Match [ 树形DP ] | 2017 Multi-University Training Conte ...

  8. HDU 6125 - Free from square | 2017 Multi-University Training Contest 7

    思路来自这里 - - /* HDU 6125 - Free from square [ 分组,状压,DP ] | 2017 Multi-University Training Contest 7 题意 ...

  9. HDU 6129 - Just do it | 2017 Multi-University Training Contest 7

    比赛时脑子一直想着按位卷积... 按题解的思路: /* HDU 6129 - Just do it [ 规律,组合数 ] | 2017 Multi-University Training Contes ...

随机推荐

  1. php获取前天的昨天的日期

    在PHP里得到前天和昨天的日期的代码前天去面试的时候也是这样,不过我当时记不起来了.就记得MYSQL里面的date_sub(now(),'interval 1 day');date('Y/m/d h: ...

  2. Nginx 配置埋点js日志采集

    页面埋点&nginx日志采集 页面(web容器:httpd/nginx负载均衡 + apache server)<===> 日志采集服务器(nginx服务器) 通过某个页面跳转到我 ...

  3. BZOJ 2002 LCT板子题

    思路: LCT啊... (分块也行) 不过YOUSIKI出了一道“弹飞大爷” 就不能用分块水过去了 //By SiriusRen #include <cstdio> #include &l ...

  4. iterator和iterable的区别

    相关博客:  http://blog.csdn.net/lipengcn/article/details/51700153         Java中Iterable和Iterator的辨析 http ...

  5. 微信小程序开发之animation动画实现

    1. 创建动画实例 wx.createAnimation(OBJECT) 创建一个动画实例animation.调用实例的方法来描述动画.最后通过动画实例的export方法导出动画数据传递给组件的ani ...

  6. 最简单的一致性Hash算法实现

    import java.util.Collection;import java.util.SortedMap;import java.util.TreeMap; public class Consis ...

  7. 逐步理解Java中的线程安全问题

    什么是Java的线程安全问题? 线程安全就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读/写完,其他线程才可使用.不会出现数据不一致或者数据 ...

  8. 图表库 - Highchart / Echart

    当前主要使用HighChart和Echart图表库,都基于Jquery,需要先引用Jquery. 实际问题:引入Jquery需在图表库前,否则报错. HighChart官网:https://www.h ...

  9. python调用函数实现数据的增删改查(1)

    实现一个小功能,当输入相应的序号,会执行相关操作, 比如当输入序号1,会执行添加功能 #coding:utf-8print '''1 添加数据2 删除数据3 修改数据4 查看数据5 退出程序'''de ...

  10. 初级模拟电路:1-2 PN结与二极管

    回到目录 1.   掺杂半导体 上面我们分析了本征半导体的导电情况,但由于本征半导体的导电能力很低,没什么太大用处.所以,一般我们会对本征半导体材料进行掺杂,即使只添加了千分之一的杂质,也足以改变半导 ...