Walk Out

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

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

 
Sample Output
111
101
 
Author
XJZX
 
Source
/**
题意:给一个n*m的01矩阵 然后要求从(0,0) 走到(n-1,m-1)
问走到的最小的串
做法:bfs + 贪心先找离(n-1,m-1),最近的1的位置,就是找所有的
前缀0,然后从最近的1开始搜,只需要搜索当前位置的左和下
然后直至(n-1,m-1)
**/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define maxn 1100
int vis[maxn][maxn];
char ch[maxn][maxn];
int n, m;
int dx[][] = {, , , , -, , , -};
int sx, sy;
struct Node
{
int x;
int y;
Node() {}
};
int check(int x, int y)
{
if(x >= && x < n && y >= && y < m) {
return ;
}
return ;
}
void bfs(int x, int y)
{
Node tmp, now, temp;
queue<Node>que;
vis[x][y] = ;
temp.x = x;
temp.y = y;
que.push(temp);
while(!que.empty())
{
now = que.front();
que.pop();
for(int i = ; i < ; i++)
{
tmp.x = now.x + dx[i][];
tmp.y = now.y + dx[i][];
if(check(tmp.x, tmp.y) && vis[tmp.x][tmp.y] == )
{
vis[tmp.x][tmp.y] = ;
if(ch[tmp.x][tmp.y] == '') {
que.push(tmp);
}
if(tmp.x + tmp.y > sx + sy)
{
sx = tmp.x;
sy = tmp.y;
}
}
}
}
}
void bfs1()
{
printf("");
bool isok = false;
bool isok1 = false;
for(int i = sx + sy; i < n + m - ; i++)
{
isok = false;
for(int j = ; j <= i; j++)
{
int x = j;
int y = i - j;
if(check(x, y) == || vis[x][y] == ) {
continue;
}
if(isok1 && ch[x][y] == '') {
continue;
}
for(int p = ; p < ; p++)
{
int tx = x + dx[p][];
int ty = y + dx[p][];
if(check(tx, ty) == ) {
continue;
}
vis[tx][ty] = ;
if(ch[tx][ty] == '') {
isok = true;
}
}
}
isok1 = isok;
if(isok) {
printf("");
}
else {
printf("");
}
}
printf("\n");
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &m);
memset(vis, , sizeof(vis));
for(int i = ; i < n; i++)
{
scanf("%s", ch[i]);
}
sx = sy = ;
vis[][] = ;
if(ch[][] == '') {
bfs(, );
}
//cout << sx << " " << sy << endl;
if(ch[sx][sy] == '') {
printf("0\n");
}
else {
bfs1();
}
}
return ;
}
 

HDU-5335的更多相关文章

  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 (BFS,技巧)

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

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

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

  5. HDU 5335 Walk Out

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

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

  7. 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(多校)

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

  9. 2015 多校赛 第四场 1009 (hdu 5335)

    Problem Description In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit) ...

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

    Walk Out                                                                         Time Limit: 2000/10 ...

随机推荐

  1. BZOJ 1010: [HNOI2008]玩具装箱toy | 单调队列优化DP

    原题: http://www.lydsy.com/JudgeOnline/problem.php?id=1010 题解: #include<cstdio> #include<algo ...

  2. 【区间DP】【lgP3146】248

    传送门 Description 给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-40),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. In ...

  3. @Autowired @Resource @Qualifier的区别

    参考博文: http://www.cnblogs.com/happyyang/articles/3553687.html http://blog.csdn.net/revent/article/det ...

  4. java RSA加密解密实现(含分段加密)

    该工具类中用到了BASE64,需要借助第三方类库:javabase64-1.3.1.jar 下载地址:http://download.csdn.net/detail/centralperk/50255 ...

  5. Android如何在初始化的时候获取加载的布局的宽高

    在自定义ListView中,需要将下拉刷新的View在初始化的时候设置padding隐藏起来,这时就要在初始化的时候获得要加载的布局View的高度. private View headView; he ...

  6. Win7 安装配置 nexus3.7.1

    安装准备: nexus3.7.1 环境准备: maven.jdk 解压nexus目录结构为: E:\nexus-3.7.1-02 配置环境变量: 启动: nexus.exe /run

  7. salt总结

    安装jdk jdk: file.managed: - source: salt://service/zabbix/files/jdk1.8.0_121.tar.gz - name: /usr/loca ...

  8. Mayor's posters(线段树+离散化+区间染色)

    题目链接:http://poj.org/problem?id=2528 题目: 题意:将n个区间进行染色(对于同一个区间,后一次染色会覆盖上一次的染色),问最后可见的颜色有多少种. 思路:由于区间长度 ...

  9. ie8下a标签中的图片出现边框

    1.ie8下a标签中的图片出现边框 <a href="#"><img src="horse.jpg"></a> 效果如图所示 ...

  10. Object的公用方法们

    如图所示,Object一共有10种方法: 下面详细描述: 1.public Object() 方法,默认构造函数方法,当新建一个Object对象的时候,调用这个方法向堆区申请一片内存: 2.priva ...