Bzoj 1698 [Usaco2007 feb]lilypad Pond Lotus leaf Pond bfs+ Shortest

Source: Internet
Author: User

Test Instructions:Link * * Method: **bfs+ Shortest circuit parsing:This problem is still very interesting. The first idea gets stuck, the first one is two cents +bfs, but if so, what can be done? To a point of traffic. So you have to change your mind. You may want to redefine the shortest path. Consider all 0 of the points in the diagram as feasible, and each time you search from that point to 0 points, and then treat this edge as 1. That is, f[i][j] represents the shortest circuit to i,j point. Clearly the end of the sentence, and then at the same time to record the shortest possible number. Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N#define INF 0x3f3f3f3fusing namespace STD;typedef Long LongllintN,m;int MapN [N];ll Num[n][n];intF[n][n];intV[n][n];structpoint{intx, y;} st,ed;BOOLCanarrive[n][n][n][n];intxx[9]={0,-1,-2,-2,-1,1,2,2,1};intyy[9]={0,-2,-1,1,2,2,1,-1,-2};voidFloodFill (intXintY) {memset(V,0,sizeof(v)); queue<point>Q    Point TMP;    Tmp.x=x,tmp.y=y; Q.push (TMP); while(!q.empty ())        {point U=q.front (); Q.pop (); for(intI=1; i<=8; i++) {tmp.x=u.x+xx[i],tmp.y=u.y+yy[i];if(tmp.x<=0|| tmp.y<=0|| tmp.x>n| | tmp.y>m| | V[TMP.X][TMP.Y])Continue;if(!Map[Tmp.x] [tmp.y]| |            (TMP.X==ED.X&AMP;&AMP;TMP.Y==ED.Y)) {canarrive[x][y][tmp.x][tmp.y]=1; }Else if(Map[Tmp.x] [tmp.y]==1) {Q.push (TMP); v[tmp.x][tmp.y]=1; }        }    }}voidBFS () {memset(V,0,sizeof(v)); queue<point>Q    Q.push (ST); v[st.x][st.y]=1; f[st.x][st.y]=0; num[st.x][st.y]=1; while(!q.empty ())        {point U=q.front ();        Q.pop (); v[u.x][u.y]=0; for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) {if(Canarrive[u.x][u.y][i][j]) {if(I==ED.X&AMP;&AMP;J==ED.Y) {if(F[u.x][u.y]<f[i][j])                            {F[I][J]=F[U.X][U.Y];                        NUM[I][J]=NUM[U.X][U.Y]; }Else if(F[u.x][u.y]==f[i][j])                        {NUM[I][J]+=NUM[U.X][U.Y]; }                    }Else{if(f[u.x][u.y]+1<f[i][j]) {f[i][j]=f[u.x][u.y]+1; NUM[I][J]=NUM[U.X][U.Y];if(!v[i][j]) {v[i][j]=1;                                Point TMP;                                Tmp.x=i,tmp.y=j;                            Q.push (TMP); }                        }Else if(f[u.x][u.y]+1==f[i][j]) {NUM[I][J]+=NUM[U.X][U.Y];if(!v[i][j]) {v[i][j]=1;                                Point TMP;                                Tmp.x=i,tmp.y=j;                            Q.push (TMP); }                        }                    }                }            }        }    }}intMain () {scanf("%d%d", &n,&m); for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) {scanf("%d",&MapI [j]);if(MapI [j]==3) St.x=i,st.y=j;Else if(MapI [j]==4) Ed.x=i,ed.y=j; }} floodfill (ST.X,ST.Y); for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) {if(!MapI [j])        FloodFill (I,J); }    }memset(F,0x3f,sizeof(f)); BFS ();if(f[ed.x][ed.y]==0x3f3f3f3f)printf(" -1\n");Else{printf("%d\n", F[ed.x][ed.y]);printf("%lld\n", Num[ed.x][ed.y]); }}

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

Bzoj 1698 [Usaco2007 feb]lilypad Pond Lotus leaf Pond bfs+ Shortest

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.