hdu 5335 Walk Out (搜索 + 路径输出)

15 篇文章 0 订阅
3 篇文章 0 订阅

Walk Out

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


Problem Description
In an  nm  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 (1n,m1000) . 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
 


题目的意思大概输出是从左上角走到右下角所组成的最小的二进制数 ,并且忽略前导0,如果结果全部某路径上的点全为0也输出0

嗯 大致的思路是做两次BFS 第一次BFS找到开始的位置并且判断是否存在全为0的路径

在第二次BFS之前 从第一次BFS找出的起始的位置中找出距离目标点最近的点(可能多个点,但步数相同) 并且对这些点进行第二次BFS ;

可以看出要求最短路径(在首位为1的情况下,长度越小二进制数值越小) 那么只有两个走法向右和向下 ,所以可以以步数为基础一层一层判断

在走相同步数时 判断是当前步否存在0 如果是 相同步数为1的路全部不选择,而所有0点再继续BFS,并且输出0;

如果不存在0,那么输出1,所有当前步1的点继续BFS ;知道到达右下点为止,BFS结束


嗯 这样写时间有点长... - -

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<bitset>
#define pi acos(-1.0)
#define inf 1<<29
#define INF 0xffffff
#define zero 1e-8

const int li[] = { -1, 0, 1, 0};
const int lj[] = {0, -1, 0, 1};
const int N = 1007;
using namespace std;

char Map[N][N];
bool flag[N][N];
bool ans[N * 2];

struct lo {
    int x, y;
    int len;
    lo() = default;
    lo(int x, int y, int len): x(x), y(y), len(len) {}
    lo(const lo &a): x(a.x), y(a.y), len(a.len) {}
    void show()const
    {

        cout << "x:" << x << " y:" << y << " len:" << len << endl;
    }
    bool operator < (const lo &b)const
    {

        if (Map[x][y] > Map[b.x][b.y]) return true;

        return false;
    }
} node[N * N], Que[N * N];

priority_queue<lo> que, que2;
int r, c;
int head, tail, nodenum;

void init()
{
    nodenum =  head = tail = 0;
    memset(flag, 0, sizeof(flag));
    while (!que.empty()) que.pop();
    while (!que2.empty()) que2.pop();
}


inline void changelo(int n, int &x, int &y)
{
    y = n % r;
    x = n / r;
}

inline int changeid(int x, int y)
{
    return x * r + y;
}

void bfs()
{

    while (head < tail) {

        for (int i = 0; i < 4; ++i) {
            int x = Que[head].x + li[i], y = Que[head].y + lj[i];
            if (x < 0 || y < 0 || x >= r || y >= c || flag[x][y])
                continue;
            if (Map[x][y] == '1') {
                node[nodenum].x = x;
                node[nodenum].y = y;
                node[nodenum++].len = r - 1 - x + c - 1 - y;
                flag[x][y] = true;
                continue;
            }
            Que[tail].x = x;
            Que[tail++].y = y;
            flag[x][y] = true;
        }
        head++;
    }
}

void bfs2(int t)
{
    if (t % 2 == 0) {

        int tip = 0, tk = 0;

        while (!que.empty()) {


            if (tip && Map[que.top().x][que.top().y] == '1') {
                while (!que.empty())
                    que.pop();
            }

            if (que.empty()) break ;

            lo tem(que.top());
            que.pop();

            if (!tk) {
                printf("%c", Map[tem.x][tem.y]);
                tk = 1;
            }

            if (tem.x == r - 1 && tem.y == c - 1)  {
                return;

            }

            if (tem.x + 1 < r && !flag[tem.x + 1][tem.y]) {

                lo tf(tem.x + 1, tem.y, r - 1 + c - 1 - tem.x - 1 - tem.y);
                que2.push(tf);
                flag[tem.x + 1][tem.y] = true;
            }
            if (tem.y + 1 < c && !flag[tem.x][tem.y + 1]) {
                lo tf(tem.x, tem.y + 1, r - 1 + c - 1 - tem.x - 1 - tem.y);
                que2.push(tf);
                flag[tem.x][tem.y + 1] = true;

            }

            if (Map[tem.x][tem.y] == '0') tip = 1;
        }
    } else {


        int tip = 0, tk = 0;

        while (!que2.empty()) {


            if (tip && Map[que2.top().x][que2.top().y] == '1') {
                while (!que2.empty())
                    que2.pop();
            }

            if (que2.empty()) break ;

            lo tem(que2.top());
            que2.pop();

            if (!tk) {
                printf("%c", Map[tem.x][tem.y]);
                tk = 1;
            }

            if (tem.x == r - 1 && tem.y == c - 1)  {
                return;
            }

            if (tem.x + 1 < r && !flag[tem.x + 1][tem.y]) {

                lo tf(tem.x + 1, tem.y, r - 1 + c - 1 - tem.x - 1 - tem.y);
                que.push(tf);
                flag[tem.x + 1][tem.y] = true;

            }
            if (tem.y + 1 < c && !flag[tem.x][tem.y + 1]) {
                lo tf(tem.x, tem.y + 1, r - 1 + c - 1 - tem.x - 1 - tem.y);
                que.push(tf);
                flag[tem.x][tem.y + 1] = true;
            }

            if (Map[tem.x][tem.y] == '0') tip = 1;
        }

    }

    bfs2(t + 1);

}

bool cmp(const lo &a, const lo &b)
{
    return a.len < b.len;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d %d", &r, &c);
        init();
        for (int i = 0; i < r; ++i)
            scanf("%s", Map[i]);

        if (Map[0][0] == '0') {

            flag[0][0] = true;
            Que[tail].x = 0;
            Que[tail].y = 0;
            tail++;
            bfs();

        } else {
            node[0].x = 0;
            node[0].y = 0;
            node[nodenum++].len = r - 1 + c - 1;
        }

        if (flag[r - 1][c - 1]) {
            printf("%c\n", Map[r - 1][c - 1]);
            continue;
        }
        sort(node, node + nodenum, cmp);
        int len = node[0].len;

        int top = 0;
        while (node[top].len == len) {
            que.push(node[top++]);
        }
        bfs2(0);
        puts("");
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值