Poj 3271 Lilypad Pond bfs

Source: Internet
Author: User

Because of the existence of 1, the problem becomes more difficult, so it is relatively simple to remove 1, first bfs, process the points that each point can reach in one step (it must be one step ). then, we can use bfs on the New Graph to calculate the number of the shortest paths and the shortest paths between the two points. (As to why this is the original question, it is very simple. Because the construction of the xiangpu should be the least, it will not be repeated, and there will be no more construction, so it is to find the shortest path. As for the number of paths, because the path length is simply incrementing, you can simply accumulate it ).

 #include <iostream>  #include <cstdio>  #include <cstring>  using namespace std;  const int maxn=30+9;  int dist[maxn][maxn],a[maxn][maxn];  int n,m;  bool d[maxn][maxn][maxn][maxn];  int quex[maxn*maxn],quey[maxn*maxn];  long long ans[maxn][maxn];  bool text[maxn][maxn];  void bfs2(int t,int s)  {      int front=1,end=0;      quex[++end]=t;      quey[end]=s;      memset(dist,50,sizeof(dist));      memset(ans,0,sizeof(ans));      dist[t][s]=0;      ans[t][s]=1;      while(front<=end)      {          int x=quex[front],y=quey[front++];          if(a[x][y]==2) continue;          for(int i=1;i<=n;i++)          for(int j=1;j<=m;j++)          if(d[x][y][i][j])          {              if(dist[i][j]==dist[x][y]+1)              ans[i][j]+=ans[x][y];              else if(dist[i][j]>dist[x][y]+1)              {                  dist[i][j]=dist[x][y]+1;                  ans[i][j]=ans[x][y];                  quex[++end]=i;                  quey[end]=j;              }          }      }  }    void bfs(int t,int s)  {      memset(text,0,sizeof(text));      int front=1,end=0;      quex[++end]=t;      quey[end]=s;      text[t][s]=1;      while(front<=end)      {          int x=quex[front],y=quey[front++];          for(int i=-1;i<=1;i+=2)          for(int j=-1;j<=1;j+=2)          for(int k=1;k<=2;k++)          {              int tmp=1;              if(k==1) tmp=2;              int tox=x+i*k,toy=j*tmp+y;              if(tox>=1&&tox<=n&&toy>=1&&toy<=m)              if(!text[tox][toy])              {                  text[tox][toy]=1;                  if(a[tox][toy]==1)                  {                      quex[++end]=tox;                      quey[end]=toy;                  }                  else                  {                      d[t][s][tox][toy]=1;                  }              }          }      }  }    int main()  {      while(scanf("%d %d",&n,&m)!=EOF)      {          int t,s,tox,toy;          for(int i=1;i<=n;i++)          for(int j=1;j<=m;j++)          {              scanf("%d",&a[i][j]);              if(a[i][j]==3)              {                  t=i;                  s=j;              }              else if(a[i][j]==4)              {                  tox=i;                  toy=j;              }          }          memset(d,0,sizeof(d));          for(int i=1;i<=n;i++)          for(int j=1;j<=m;j++)          bfs(i,j);          bfs2(t,s);          if(dist[tox][toy]>1e3)          {              printf("-1\n");          }          else          {              printf("%d\n%lld\n",dist[tox][toy]-1,ans[tox][toy]);          }      }      return 0;  }  

 


Related Article

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.