bzoj:1632: [Usaco2007 feb]lilypad Pond

Source: Internet
Author: User

Description

Farmer John built a beautiful pond for his cows to appreciate and exercise. The rectangular pool is divided into M-rows and N-columns (1≤m≤30; 1≤n≤30) square squares. Some of the squares have amazing sturdy lotus flowers, some rocks, and the rest are just beautiful, pure, blue water. Bessie was practicing ballet, and she jumped from one Lotus to another, presently in a Lotus. She wants a jump on the lotus One, and the target is another given Lotus. She could jump neither into the water nor to a rock. To the layman's surprise, Bessie's jumps were like Chinese chess horses: moving horizontally 1, moving vertically 2, or moving vertically 1, moving horizontally 2. Bessie may sometimes have up to 8 jumps of choice. Farmer John, observing Bessie's ballet connection, realized that sometimes Bessie might not be able to jump where she wanted to go because there were no Lotus flowers on the road. So he wanted to add a few lotus flowers to enable Betsy to complete the task. The consistently frugal farmer John wants to add a minimum number of lotus flowers. Of course, the Lotus cannot be placed on the stone. Please help farmer John determine the minimum number of lotus that must be added. The minimum number of steps that Bessie needs to jump from the starting point to the target point is calculated based on the minimum amount of Lotus added. Finally, we also calculate the number of hops with the least number of hops to jump when the minimum quantity of added Lotus is satisfied.

Input

Line 1th: Two integers M, N

2..M + 1 lines: line i + 1, the line i + 1 has N integers, indicating the state of the position: 0 is water; 1 for Lotus; 2 for the Rock; 3 The position for Bessie to begin; 4 for Bessie to go to the target position.

Output

Line 1th: An integer: The minimum number of lotus that needs to be added. If at any rate Bessie could not jump, output-1.

Line 2nd: An integer: The minimum number of steps that Bessie will need to jump from the starting point to the target point, based on the minimum amount of Lotus added. If line 1th outputs-1, this line is not output. Line 3rd: An integer: The number of hops that are added when the minimum number of Lotus, the jump number of steps to the 2nd row of the output value of the jump path if the 1th line output-1, this line does not output.

Sample Input4 8
0 0 0 1 0 0 0 0
0 0 0 0 0 2 0 1
0 0 0 0 0 4 0 0
3 0 0 0 0 0 1 0
Sample Output2
6
2
by the Silver group can brush the question brush empty souvenirOne person accounted for two 233 BFS directly on, the number of routes to hit the mark is good ...
#include <queue>#include<cstdio>#include<algorithm>using namespacestd;structna{intx, y;};Const intfx[8]={1,2,1,2,-1,-2,-1,-2},fy[8]={2,1,-2,-1,2,1,-2,-1};intN,m,sx,sy,tx,ty;intmap[ to][ to];Charcc;intf[ to][ to],l[ to][ to];Long Longp[ to][ to];BOOLbo[ to][ to];intRead () {intA=0; CC=GetChar ();  while(cc<'0'|| Cc>'9') cc=GetChar ();  while(cc>='0'&&cc<='9') a=a*Ten+cc- -, cc=GetChar (); returnA;} Queue<na>Q;Const intinf=10000;intMain () {n=read (); m=read ();  for(intI=0; i<n;i++)     for(intj=0; j<m;j++) {Map[i][j]=read (); if(map[i][j]==3) sx=i,sy=j,map[i][j]=1; if(map[i][j]==4) tx=i,ty=j,map[i][j]=1; F[I][J]=l[i][j]=INF;    } na cmp; Cmp.x=sx;cmp.y=Sy; F[sx][sy]=l[sx][sy]=0;p [sx][sy]=1; Bo[sx][sy]=1;    Q.push (CMP);  while(!Q.empty ()) {na k=Q.front (); Q.pop (); BO[K.X][K.Y]=0; if(K.x==tx&&k.y==ty)Continue;  for(intI=0;i<8; i++) {na now=K; now.x+=fx[i];now.y+=Fy[i]; if(now.x<0|| now.y<0|| now.x>=n| | NOW.Y&GT;=M)Continue; if(map[now.x][now.y]==2)Continue; if(F[now.x][now.y]>f[k.x][k.y]+ (map[now.x][now.y]==0) ) {F[NOW.X][NOW.Y]=f[k.x][k.y]+ (map[now.x][now.y]==0); L[NOW.X][NOW.Y]=l[k.x][k.y]+1; P[NOW.X][NOW.Y]=P[K.X][K.Y]; if(bo[now.x][now.y]==0) {Q.push (now); BO[NOW.X][NOW.Y]=1; }            }Else if(F[now.x][now.y]==f[k.x][k.y]+ (map[now.x][now.y]==0)){                if(l[now.x][now.y]>l[k.x][k.y]+1) {L[NOW.X][NOW.Y]=l[k.x][k.y]+1; P[NOW.X][NOW.Y]=P[K.X][K.Y]; if(bo[now.x][now.y]==0) {Q.push (now); BO[NOW.X][NOW.Y]=1; }                }Else if(l[now.x][now.y]==l[k.x][k.y]+1) {P[NOW.X][NOW.Y]+=P[K.X][K.Y]; if(bo[now.x][now.y]==0) {Q.push (now); BO[NOW.X][NOW.Y]=1; }                }            }        }    }    if(F[tx][ty]==inf) printf ("-1");Elseprintf"%d\n%d\n%lld", F[tx][ty],l[tx][ty],p[tx][ty]);}

bzoj:1632: [Usaco2007 feb]lilypad Pond

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.