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 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.
An explorer gets lost in this grid. His position now is
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 integersn 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.
For each testcase, the first line contains two integers
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
嗯 大致的思路是做两次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
- hdu 5335 Walk Out (搜索 + 路径输出)
- hdu 5335 Walk Out【搜索】
- hdu 5335 Walk Out 搜索+贪心
- HDU 5335 Walk Out(搜索+贪心)
- hdu 5335 Walk Out
- HDU 5335 Walk Out
- HDU 5335 Walk Out
- hdu 5335 Walk Out
- hdu 5335 Walk Out
- hdu 5335 Walk Out
- HDU 5335 Walk Out
- HDU 5335 Walk Out
- HDU 5335 Walk Out
- HDU 5335 Walk Out (搜索+贪心,超详解)经典
- HDU 5335 Walk Out(Bfs搜索字典序最小的最短路)
- HDU 5335 Walk Out 状压DP乱搞
- hdu 5335 Walk Out(dfs+bfs)
- 【HDU 5335】Walk Out(BFS)
- Linux下Tomcat的启动、关闭、杀死进程
- 单双向链表
- PHP中cURL的curl_getinfo函数返回的CURLINFO_HTTP_CODE是0
- 使用docker搭建弹性hadoop集群
- Android删除应用缓存的实现
- hdu 5335 Walk Out (搜索 + 路径输出)
- 8月1日解题报告
- ie6 javascript:void(0)、IE7 input透明、IE8 jquery动态加载css
- Ubuntu login incorrect问题解决
- 为什么我们需要TOGAF企业架构?——上海信息化培训中心
- git使用详细介绍
- Lucene
- jira找到字段的ID
- 修改linux打开文件最大数与最大线程数