2015 Multi-school game fourth 1009 (Hdu 5335)

Source: Internet
Author: User

Problem DescriptionIn ann∗m Maze, the Right-bottom corner is the exit (position(n,m) is the exit). In every position of this maze, there is either a0or a1Written on it.

An explorer gets the lost in this grid. His position are(1,1) , 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 (1,1). 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.

Inputthe first line of the input was a single integerT (t=ten) , indicating the number of testcases.

For each testcase, the first line contains the integersNandm (1≤n,m≤) . TheI-th Line of the next n lines contains one, string of length m, which represents I-th row of the Maz E.

Outputfor each testcase, print the answer in binary system. Eliminate all the preceding 0 unless the answer itself is 0 (in this case, print 0 instead).

Sample Input22 211113 3001111101

Sample Output111101

Test instructions: Give a picture, starting at the upper left corner at the bottom left corner. The number traveled along the way is connected to the last binary number, trying to make the number minimum and output.

Ideas:

The starting point is 1, so that the length of the binary number is the smallest, each time only to the right or down, and the same number of steps to go 0 priority.

The starting point is 0, then press 0 to the nearest point of 1, then follow the above way.

Wide search questions, but T to kneel. Looked at the standard to make a copy. This BFS method for the first time see, indeed 6, I still too young.

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>using namespacestd;Charg[1005][1005];intN,m,t,head,tail;#defineMAXN 1000005intvis[1005][1005],X[MAXN],Y[MAXN];intdx[]={1,-1,0,0},dy[]={0,0,1,-1};stringBFs () {memset (Vis,0,sizeof(VIS)); Vis[head=0][tail=0]=1; X[head]=0, y[head++]=0;  while(head!=tail) {        if(g[x[tail]][y[tail]]=='0'){             for(intI=0;i<4; i++){                intx=x[tail]+dx[i],y=y[tail]+Dy[i]; if(x>=0&&x<n&&y>=0&&y<m&&!Vis[x][y]) {X[head]=x,y[head++]=x; Vis[x][y]=1; } }} tail++; }    if(vis[n-1][m-1]&&g[n-1][m-1]=='0')return "0"; intsum=0; stringans="1";  for(intI=0; i<n;i++){         for(intj=0; j<m;j++)            if(Vis[i][j]) Sum=max (sum,i+j); }     for(intk=sum;k<n+m-2; k++){        CharC='1';  for(intI=0, j=k-i;i<n;i++,j--)if(j>=0&&j<m&&Vis[i][j]) {            if(i+1<n) C=min (c,g[i+1][j]); if(j+1<m) C=min (c,g[i][j+1]); } ans+=C;  for(intI=0, j=k-i;i<n;i++,j--)if(j>=0&&j<m&&Vis[i][j]) {            if(i+1<n&&g[i+1][J]==C) vis[i+1][j]=1; if(j+1<m&&g[i][j+1]==C) vis[i][j+1]=1; }    }    returnans;}intMain () {//freopen ("In.txt", "R", stdin);scanf"%d",&t);  while(t--) {scanf ("%d%d",&n,&m);  for(intI=0; i<n;i++) scanf ("%s", G[i]); cout&LT;&LT;BFS () <<Endl; }    return 0;}
View Code

2015 Multi-school game fourth 1009 (Hdu 5335)

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.