Problem Solving report HDU5335 Walk out

Source: Internet
Author: User

Problem Solving report HDU5335 Walk out

Description

In an Maze, the Right-bottom corner is the exit (position is the exit). In every position of this maze, there is either a or a Written on it.

An explorer gets the lost in this grid. His position are , and he wants to go to the exit. Since to arrive at the exit was easy for him, he wants to does something more difficult. At first, he'll write down the number on position . Every time, he could make a move to one adjacent position (both positions is adjacent if and only if they share an edge). While walking, he would write down the number on the position he's on to the end of his number. When finished, he'll get a binary number. Determine the minimum value of this number in binary system.

Input

The first line of the input was a single integer , indicating the number of testcases.

For each testcase, the first line contains the integers and . The -th Line of the next Lines contains one, string of length , which represents -th row of the maze.

Output

For each testcase, print the answer in binary system. Eliminate all the preceding Unless the answer itself is (in the case, print instead).

Sample Input

Sample Output


The main idea : give you a n*mgrid, each of which is either 1 or 0. beginning at (a), the end point in (N,m). Let you find a way to make the numbers that pass by in sequence the smallest number of binary numbers in order.
Analysis: It is easy to think, if the starting point is 1, then must only go down to the right, or more than the number is the first deadly, and then consider each time to take priority to go 0, no 0 go 1. So this time the direct wide search to the end is the shortest answer. So what if the starting point is not 1? Then we can let the front some 0, until we find a 1 and then repeat the first case. So what we're looking for is x+y maximum position 1, because after finding 1 we have to start thinking about the minimum number of digits, can only go down to the right, so find X+y the largest 1 is guaranteed the smallest number of bits. So we need to do a second BFS, to find the points of all (X+y) Max, and start from these points in turn to repeat the first case.
on the code:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < Queue> #include <vector>using namespace std;const int maxn = n + 10;struct node{int x, y; Node () {}node (int x, int y) {this->x = X;this->y = y;}}; int VIS[MAXN][MAXN]; int ZERO[MAXN * 4]; Whether the position of all x+y exists as a point of 0 char Map[maxn][maxn];int MOVE[MAXN][MAXN]; Record road warp, convenient output vector<node> BG; All x+y the largest point int n, m;int dir[4][2] = {{0,1}, {1,0}, { -1,0}, {0,-1}};  Moving direction, note that 1 and 4 are pairs, 2 and 3 are a pair of void Ini () {memset (Vis, 0, sizeof Vis), memset (zero, 0, sizeof zero), memset (move,-1, sizeof move );} BOOL inline Check (int x, int y) {if (x <= 0 | | x > N | | y <= 0 | | y > m) return False;return true;} void BFs ()//Find X+y farthest point {bg.clear (); Bg.push_back (Node (1, 1));//At this time (0), first assume the farthest point queue<node> q;q.push (node (1, 1 )); vis[1][1] = 1;int dis = 1;  Minimum distance int x, Y;while (!q.empty ()) {Node tem = Q.front (); Q.pop (); x = Tem.x;y = tem.y;for (int i = 0; i < 4; i++) {int temx = X + dir[I][0];int temy = y + dir[i][1];if (!check (temx, temy) | | map[temx][temy] = = ' 1 ' | | vis[temx][temy])//If it's out of bounds or 1, or if you've already searched, you're not looking. Continue;vis[temx][temy] = 1;q.push (Node (Temx, Temy)), if (temx + temy > Dis)//discover farther points {dis = temx + temy;bg.clear ();} if (temx + temy = = dis)//found the farthest point {bg.push_back (Node (Temx, Temy));}}}} void BFS2 ()//Find a small number from X {queue<node> q;if (map[1][1] = = ' 1 ')///If the starting point is 1, also give priority to the shortest digit {Q.push (Node (1, 1));} else//If the starting point is 0, find the farthest point of the first 1 {BFS (); for (int i = 0; i < bg.size (); i++) Q.push (Bg[i]);} while (!q.empty ())//starting from the first 1, only right down {Node tem = Q.front (); Q.pop (); int x = Tem.x;int y = tem.y;if (map[x][y] = = ' 1 ' &&ze Ro[x + y])//If the current grid is 1, but the point with the same x+y is 0 {continue;//skips}for (int i = 0; i < 2; i++) {int temx = x + dir[i][0];int Temy = y + dir[i][1];if (!check (Temx, temy)) continue; Move[temx][temy] = 3-i; Record the reverse direction, convenient output if (map[temx][temy] = = ' 0 ')//x+y Point has 0 zero[temx + temy] = 1;if (!vis[temx][temy]) {Vis[temx][temy] = 1;q.push (Node (Temx, Temy));}}} int main () {int kase;scanf ("%d", &kase), while (kase--) {ini (); scanf ("%d%d ", &n, &m); for (int i = 1; I <= N; i++) {scanf ("%s", Map[i] + 1);} BFS2 (); int x = n, y = M;int mtem, cnt = 0;char ANS[MAXN * 4];while (TRUE)//direction Walk, construction result {ans[cnt++] = Map[x][y];mtem = move[x][ Y];if (Mtem = =-1) break;x = x + dir[mtem][0];y = y + dir[mtem][1];} while (cnt&&ans[cnt-1] = = ' 0 ')//delete leading 0cnt--;if (!cnt) {printf ("0\n"); continue;} for (int i = cnt-1; I >= 0; i--) {printf ("%c", Ans[i]);} printf ("\ n");} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Problem Solving report HDU5335 Walk out

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.