HDU-5335

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

2015 Multi-University Training Contest 4

/**
    题意:给一个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[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
int sx, sy;
struct Node
{
    int x;
    int y;
    Node() {}
};
int check(int x, int y)
{
    if(x >= 0 && x < n && y >= 0 && y < m) {
        return 1;
    }
    return 0;
}
void bfs(int x, int y)
{
    Node tmp, now, temp;
    queue<Node>que;
    vis[x][y] = 1;
    temp.x = x;
    temp.y = y;
    que.push(temp);
    while(!que.empty())
    {
        now = que.front();
        que.pop();
        for(int i = 0; i < 4; i++)
        {
            tmp.x = now.x + dx[i][0];
            tmp.y = now.y + dx[i][1];
            if(check(tmp.x, tmp.y) && vis[tmp.x][tmp.y] == 0)
            {
                vis[tmp.x][tmp.y] = 1;
                if(ch[tmp.x][tmp.y] == ‘0‘) {
                    que.push(tmp);
                }
                if(tmp.x + tmp.y > sx + sy)
                {
                    sx = tmp.x;
                    sy = tmp.y;
                }
            }
        }
    }
}
void bfs1()
{
    printf("1");
    bool isok = false;
    bool isok1 = false;
    for(int i = sx + sy; i < n + m - 2; i++)
    {
        isok = false;
        for(int j = 0; j <= i; j++)
        {
            int x = j;
            int y = i - j;
            if(check(x, y) == 0 || vis[x][y] == 0) {
                continue;
            }
            if(isok1 && ch[x][y] == ‘1‘) {
                continue;
            }
            for(int p = 0; p < 2; p++)
            {
                int tx = x + dx[p][0];
                int ty = y + dx[p][1];
                if(check(tx, ty) == 0) {
                    continue;
                }
                vis[tx][ty] = 1;
                if(ch[tx][ty] == ‘0‘) {
                    isok = true;
                }
            }
        }
        isok1 = isok;
        if(isok) {
            printf("0");
        }
        else {
            printf("1");
        }
    }
    printf("\n");
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &n, &m);
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i++)
        {
            scanf("%s", ch[i]);
        }
        sx = sy = 0;
        vis[0][0] = 1;
        if(ch[0][0] == ‘0‘) {
            bfs(0, 0);
        }
        //cout << sx << " " << sy << endl;
        if(ch[sx][sy] == ‘0‘) {
            printf("0\n");
        }
        else {
            bfs1();
        }
    }
    return 0;
}
时间: 2024-10-13 21:29:33

HDU-5335的相关文章

hdu 5335 Walk Out (搜索)

题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走的路线上的0/1组成的二进数最小,现在要为矫情无比的探险家找最优路径咯. 解题思路: 对于二进制数,前导零是对数字大小没有任何影响的.当到不得不走1的时候就只能向下,或者向右走了.所以先搜索出来一直走零,能走到的最靠近终点的位置,然后在类似搜索,找出最优路径. 1 #include <queue>

HDU 5335 多校第4场 1009 Walk Out

Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1794    Accepted Submission(s): 340 Problem Description In an n?m maze, the right-bottom corner is the exit (position (n,m) is the exit)

HDU 5335 Walk Out (搜索+贪心,超详解)经典

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5335 题面: Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2355    Accepted Submission(s): 459 Problem Description In an n?m maze, the righ

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

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 want

HDU 5335 Walk Out(Bfs搜索字典序最小的最短路)

 题意:nXm的地图, 问通过四个方向从(1,1)走到(1000,1000)所经过的最小二进制序列是多少,忽略前缀0. 思路:首先如果起点为0,那么我们bfs搜索和起点0联通的为0的连通块,这样我们第一步肯定是从与这个连通块相邻的且与重点最近的地方出发. 将所有可能起点加入队列,在bfs一遍找到字典序最小的那条路就是答案, 在这里可以用两个vector类型容器,一个是q2存储所有节点值存为0的结点, 另一个q3存储节点值为1的结点. 那么如果q2不为空那么也就是有可以走零,那么就从这里面选,

HDU 5335 Walk Out(多校)

Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2912    Accepted Submission(s): 599 Problem Description In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit).

【HDU 5335】Walk Out(BFS)

这道题卡时间卡的比较紧. 一开始直接BFS 毫无疑问的超时,之后想到根据BFS的常规优化思想,去选择起始点进行遍历. 这样我们一开始先BFS一次,这次的BFS是选择出这一点为1并且从起点到这一个点,中间路径的点全为0的点. 这样选择出这个点之后,这个点到终点的路径长度就可以断定了. 之后我们把所有到终点距离最小的点放在一个容器里进行BFS. 这道题没有做出来的原因很大一部分就是对BFS的理解不够深以及该有的贪心思路没有,导致了这道题没有AC. 嗯,有句话说的对:综合是第一生产力. #includ

HDU 5335(2015 ACM多校训练第四场1009)

Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2469    Accepted Submission(s): 485 Problem Description In an n?m maze, the right-bottom corner is the exit (position (n,m) is the exit)

HDU 5335——Walk Out——————【贪心】

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

HDU 5335 Walk Out

题意:在一个只有0和1的矩阵里,从左上角走到右下角, 每次可以向四个方向走,每个路径都是一个二进制数,求所有路径中最小的二进制数. 解法:先bfs求从起点能走到离终点最近的0,那么从这个点起只向下或向右走就可以获得位数最少的二进制数,然后贪心的想,如果后或下有0就一定走0,没0就把1都看一遍,以之前搜到的0做起点,一层一层遍历可行路径,直到终点. 代码: #include<stdio.h> #include<iostream> #include<algorithm> #