Walk Out

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

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
 
Author
XJZX
 
Source
 
Recommend
wange2014   |   We have carefully selected several
similar problems for you:  5338 5337 5336 5334 5333 
题目意思:
给定一个n*m的图,图上的每个点上的值为0或1,让你找到一条路,所经过的01序列最小。
从当前点来看,如果能走0肯定要走0,如果没有0才会走1,我们可以先找到能到的最远的0,然后从最远的0处,向下和向右两个方向搜索了,
(斜行递推,从第一个行开始标记下一行要走的点,然后在下一行的时候检查那些点被标记了,说明这些点可以走,然后再标记下一行的,
直到倒数第二行,由于下一行的点要么是0,要么是1,所以可以直接输出,即使下一行有多个点可以走,但是它都是0或者1)
这样处理可以排除特殊情况
,(直接从两个方向搜,可能会漏掉往左走最终到达的这个路径上全是0的这种情况)。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
#include <queue>
#define SIZE 1005
#define maxn 2010
using namespace std;
int n,m;
char Map[SIZE][SIZE];
int visit[SIZE][SIZE];
int d[SIZE][SIZE];
int VIS[maxn][maxn];
int tot;
int dir[][]={,-,,,-,,,};
struct node
{
int x,y,cnt;
};
queue <node> que;
void init()
{
tot=;
memset(visit,,sizeof(visit));
memset(d,,sizeof(d));
memset(VIS,,sizeof(VIS));
}
void dfs(int x,int y)
{
if(visit[x][y]==)
return ;
visit[x][y]=;
if(Map[x][y]=='')
return ;
d[x][y]=;
if(x+y>tot)
tot=x+y;
if(x>)
dfs(x-,y);
if(x<n)
dfs(x+,y);
if(y>)
dfs(x,y-);
if(y<m)
dfs(x,y+);
}
void bfs()
{
if(Map[][]=='')
{
node a,b,c;
a.x=; a.y=; a.cnt=;
que.push(a);
VIS[][]=;
d[][]=;
while(!que.empty())
{
b=que.front();
que.pop();
if(b.cnt > tot)
tot=b.cnt;
for(int i=;i<;i++)
{
int next_x=b.x+dir[i][];
int next_y=b.y+dir[i][];
if( (<=next_x) &&(next_x<=n) && (<=next_y) && (next_y<=m) && VIS[next_x][next_y]== && Map[next_x][next_y]=='')
{
c.x=next_x; c.y=next_y;
c.cnt=next_x+next_y;
que.push(c);
VIS[next_x][next_y]=;
d[next_x][next_y]=;
}
} }
}
else
return ; }
void solve()
{
if(tot==m+n)
{
printf("0\n");
return ;
}
if(tot==)
{
tot=; //代表起始点为(1,1)
d[][]=;
printf("");
}
for(int p=tot;p<n+m;p++)
{
int flag=;
for(int q=max(p-m,);q<=min(p-,n);q++) //q代表行号
{
if(d[q][p-q])
{
int x=(Map[q][p-q+]-'')?:;
int y=(Map[q+][p-q]-'')?:;
flag=min(flag,x);
flag=min(flag,y);
}
}
for(int q=max(p-m,);q<=min(p-,n);q++)
{
if(d[q][p-q])
{
int x=(Map[q][p-q+]-'')?:;
int y=(Map[q+][p-q]-'')?:;
if(x==flag)
d[q][p-q+]=;
if(y==flag)
d[q+][p-q]=;
}
}
printf("%d",flag);
}
printf("\n");
}
int main()
{
//freopen("test.txt","r",stdin);
int t;
scanf("%d",&t);
while(t --)
{
init();
scanf("%d%d%*c",&n,&m);
// printf("%d %d %d\n",n,m,tot);
for(int i = ; i <= n ; i ++)
{
scanf("%s",&Map[i][]);
}
// getchar();
//dfs(1,1);
bfs();
// printf("tot: %d\n",tot);
solve();
}
return ;
}
 

hdu5335(搜索)的更多相关文章

  1. SQLSERVER走起微信公众帐号已经开通搜狗微信搜索

    SQLSERVER走起微信公众帐号已经开通搜狗微信搜索 请打开下面链接 http://weixin.sogou.com/gzh?openid=oIWsFt-hiIb_oYqQHaBMoNwRB2wM ...

  2. solr_架构案例【京东站内搜索】(附程序源代码)

    注意事项:首先要保证部署solr服务的Tomcat容器和检索solr服务中数据的Tomcat容器,它们的端口号不能发生冲突,否则web程序是不可能运行起来的. 一:solr服务的端口号.我这里的sol ...

  3. SQLServer地址搜索性能优化例子

    这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...

  4. HTML5轻松实现搜索框提示文字点击消失---及placeholder颜色的设置

    在做搜索框的时候无意间发现html5的input里有个placeholder属性能轻松实现提示文字点击消失功能,之前还傻傻的在用js来实现类似功能... 示例 <form action=&quo ...

  5. bzoj1079--记忆化搜索

    题目大意:有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得 ...

  6. bzoj3208--记忆化搜索

    题目大意: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目.    我们可以把风景区看作一个n*n的地图,每个点有它的初始高度,滑雪只能从高处往低处滑[严格大于] ...

  7. Android中通过ActionBar为标题栏添加搜索以及分享视窗

    在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...

  8. 一步步开发自己的博客 .NET版(5、Lucenne.Net 和 必应站内搜索)

    前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做 ...

  9. Go语言实战 - 我需要站内搜索

    山坡网的用户抱怨"为什么搜索'二鬼子李富贵'找不到'二鬼子汉奸李富贵'?我用百度搜都能找到." 当时我就滴汗了,用户说的有道理,应该要能搜索到. 之前的方案很简单,用户输入的字串会 ...

随机推荐

  1. Java并发编程:自己动手写一把可重入锁

    关于线程安全的例子,我前面的文章Java并发编程:线程安全和ThreadLocal里面提到了,简而言之就是多个线程在同时访问或修改公共资源的时候,由于不同线程抢占公共资源而导致的结果不确定性,就是在并 ...

  2. [luoguP1835] 素数密度_NOI导刊2011提高(04)(素数筛)

    传送门 数据辣么大,怎么搞?(L≤R≤2147483647) 注意到R-L≤1000000 所以可以直接筛R-L区间内的数, 但是需要用已知的小的素数筛, R-L区间内的大部分数肯定能用较小的素数筛去 ...

  3. [luoguP3606] [USACO17JAN]Building a Tall Barn建谷仓(贪心 + 线段树)

    传送门 把线段都读进来然后排序,先按右端点为第一关键字从小到大排序,后按左端点为第二关键字从小到大排序. 注意不能先按左端点后按右端点排序,否则会出现大包小的情况,如下: —————— ———  — ...

  4. MT6755 使用R63350 IC 出现唤醒概率性闪白,并导致ESD FAIL

    现象描述. 手机自动灭屏后按power键或home 键点亮屏幕,概率性上方有白色的一道,还会闪两三下屏.使用的LCM IC是:r63350, (FHD VDO)屏,附件为mtklog看看是什么原因? ...

  5. 45个android实例源码分享

    分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html andriod闹钟源代码 http://www.apkbus.c ...

  6. ACM-ICPC 2018 徐州赛区网络预赛 D 杜教筛 前缀和

    链接 https://nanti.jisuanke.com/t/31456 参考题解  https://blog.csdn.net/ftx456789/article/details/82590044 ...

  7. Java实验--统计字母出现频率及其单词个数

    本周的实验要求在之前实现统计单词的基础之上(可以见之前博客的统计单词的那个实验),对其进行修改成所需要的格式,统计字母出现频率的功能,并按照一定的格式把最终结果的用特定的格式在文本中显示出来 统计过程 ...

  8. Java 函数式接口 lambda

    import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class Demo1 { ...

  9. 图解TCP/IP第五版 -- 文件夹

    非常多年前买过<TCP/IP具体解释>3卷,当时可能根本没看,也可能是看了又忘了,没有留下什么印象,当时的书也当做废品卖了. 卖书时的感觉貌似是.买了太多的书,基本都没看,搬家搬来搬去的麻 ...

  10. python绘图入门

    python绘图入门 学习了:https://zhuanlan.zhihu.com/p/34200452 API:https://matplotlib.org/api/pyplot_api.html ...