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. BZOJ1911:[Apio2010]特别行动队——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1911 又是一个显然的dp……好吧我懒得讲了. s[i]是战斗力前缀和. 我们仍然设k<j< ...

  2. BZOJ [Ctsc2002] Award 颁奖典礼 解题报告

    [Ctsc2002] Award 颁奖典礼 Description IOI2002的颁奖典礼将在YONG-IN Hall隆重举行.人们在经历了充满梦幻的世界杯之后变得更加富于情趣.为了使颁奖典礼更具魅 ...

  3. Hadoop Yarn-入门篇

    参考并推荐博文:https://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ 重构根本的思想是将 JobTracker 两个主 ...

  4. 图片上传(方法一:jquery.upload.js)

    一.在JSP页面引入jquery.upload.js 文件: <script type="text/javascript" src="${ctx}/script/j ...

  5. CORS解决跨域访问问题

    简言之,CORS就是为了让AJAX可以实现可控的跨域访问而生的. Tomcat下的配置   下载cors-filter-1.7.jar,java-property-utils-1.9.jar  [下载 ...

  6. HDU1164

    //HDU 1164 //输入一个数(1<x<=65535) 转化为素数的乘积() #include "iostream" #include "cstdio& ...

  7. c# delegate知识

    一.引用方法 委托是寻址方法的.NET版本.委托是类型安全的类,它定义了返回类型和参数的类型.委托是对方法的引用,也可以对多个方法进行引用,委托可以理解为指向方法地址的指针. 如:delegate i ...

  8. iOS 时间转换

    #pragma mark - 获取当前时间戳 -(NSString *)getTimeSp{ NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:]; ...

  9. NGINX: 返回大 JSON 数据不完整的问题

    说明: 内容全部来自 [ CSDN 金玮良 ] nginx 返回数据不完整的问题 当nginx 遇到大数据流时,会把数据先放在自己的缓冲区,然后一并发给客户端. 那如果这个结论成立, 那一次请求的数据 ...

  10. Mac 上真机调试cocos2d-x-3.16的test程序

    文章比较长,一个算是新手又不是新手的程序员的解决过程. 一 xcode中打开项目 首先,下载完成cocos2d-x-3.16之后,解压,然后在根目录build目录下双击cocos2d_tests.xc ...