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

来源:互联网 发布:阿里云200m无限流量 编辑:程序博客网 时间:2024/06/04 19:20

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
22 211113 3001111101
 

Sample Output
111101
 

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-8const 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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信钱包锁忘了怎么办 xp系统管理员密码忘了怎么办 沃尔沃menu键没反应怎么办 微信转账受限制怎么办 被米虎网骗了手里还有合同怎么办 学校要求上传论文pdf格式怎么办 备份的Wifi密码查看乱码怎么办 u盘密码忘了怎么办 京东会员号被黑怎么办 淘宝企业店铺三证不合一怎么办 淘宝企业店铺营业执照注销了怎么办 不想开淘宝企业店铺了怎么办 id图片跨页排版怎么办 合约机不想要了怎么办? 移动A3手机老卡怎么办 中国移动手机a3很卡怎么办 移动手机a4好卡怎么办 红米手机卡顿反应慢怎么办 红米3s网速慢怎么办 红米4a内存不足怎么办 红米3s手机发热怎么办 魅蓝s6信号差怎么办 oppo手机媒体音量没声音怎么办 红米note3反应慢怎么办 红米4g信号差怎么办 红米4g网速慢怎么办 红米24g信号不好怎么办 红米54g信号不稳定怎么办 红米4a玩游戏卡怎么办 红米4x卡顿怎么办 红米主板烧了怎么办 红米3按键失灵怎么办 l安卓手机运存不够用怎么办 红米2屏幕失灵怎么办 红米手机电池不耐用怎么办 红米手机没内存怎么办 红米2a卡顿怎么办 红米2a手机卡顿怎么办 红米5a内存不足怎么办 红米note1s内存不够怎么办 红米2手机没内存怎么办