[Search]hdu5335

Source: Internet
Author: User

Test instructions
Given the 01 matrix, ask the minimum number of digits represented by all 01 binary strings from (N,M) through the road.

Analysis:
First, the prefix 0 is to be removed, but also found that only constantly down and to the right to achieve the shortest path. So first DFS finds all the 0 positions that can go, minus the prefix 0. Then BFS to the lower right corner, 0 of the place to go 0, do not walk 1, Without any 0 of the cases to go 1. If there are more than 0 go, see who is behind 0. If there is no 0, all 1 will go, and see who may be behind 0. It's Jiangzi.
In addition, the problem uses the nature of the diagonal, when the X+y==tot, all the points to the end of the condition of the distance are equal. In fact, this is also a backslash, equivalent to a layer of anti-slash search.
In addition, judging tot==0 and tot==n+m when the order is different, an AC one WA. Self-study to think about is a special situation,
1
1 1
1
At this time both are suitable, so should first Judge Tot==n+m.

#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <vector>#include <map>#include <algorithm>#define Read Freopen ("Q.in", "R", stdin)#define LL Long LongConst intMAXN =1003;using namespace STD;intN,m;CharMP[MAXN][MAXN];intdx[]={0,0,-1,1};intdy[]={1,-1,0,0};intVIS[MAXN][MAXN];intTotvoidDfsintXintY) {if(Vis[x][y])return;if(mp[x][y]==' 1 ')return; vis[x][y]=1;//vis represents all of the 0 locations visited    if(X+y>tot) Tot=x+y;//tot represents the coordinates of the currently traversed road and    if(x+1<=n) DFS (x+1, y);if(y+1&LT;=M) DFS (x,y+1);if(X-1>=1) DFS (X-1, y);if(Y1>=1) DFS (x,y-1);}voidBFS () {intI,j; for(i=tot;i<n+m;i++) {intflag=1;//To determine if there are 0 can walk if there are more than 0 can go?          for(J=max (1, i-m); J<=min (n,i-1); J + +)if(Vis[j][i-j]) {inty=i-j+1<=m?mp[j][i-j+1]-' 0 ':1;intx=j+1<=n?mp[j+1][i-j]-' 0 ':1;if(x==0|| y==0) flag=0;//cout<<endl<< "x=" <<x<< "y=" <<y<<endl;} for(J=max (1, i-m); J<=min (n,i-1); J + +)if(Vis[j][i-j]) {intx=i-j+1<=m?mp[j][i-j+1]-' 0 ':1;inty=j+1<=n?mp[j+1][i-j]-' 0 ':1;if(X==flag) vis[j][i-j+1]=1;if(Y==flag) vis[j+1][i-j]=1;//If there are more than 0, two-pronged}printf("%d", flag); }}intMain () {//Read;    intCAsscanf("%d", &cas); while(cas--) {intI,j;memset(Vis,0,sizeof(VIS));scanf("%d%d", &n,&m); for(i=1; i<=n;i++)scanf('%s ', mp[i]+1); tot=0; Dfs1,1);if(tot==n+m) {printf("0\n");Continue; }if(tot==0)        {printf("1"); vis[1][1]=1; tot=2; } BFS ();printf("\ n"); }}

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

[Search]hdu5335

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.