Bzoj 1632: [usaco Feb] Lilypad Pond

Source: Internet
Author: User
Question 1632: [usaco Feb] Lilypad pondtime limit: 5 sec memory limit: 64 MB
Submit: 390 solved: 109
[Submit] [Status] Description

Farmer John built a beautiful pond to make his cows aesthetic and exercise. The pool of this rectangle is divided into m rows and n columns (1 ≤ m ≤ 30; 1 ≤ n ≤ 30) square lattice. There are amazing strong lotus on some grids, and some rocks. The rest is beautiful, pure, blue water. Bessie is practicing ballet. She jumps from a lotus flower to another lotus flower, which is currently located in a lotus flower. She wants to jump one by one on the Lotus, with the goal being another given Lotus. She can skip neither the water nor a rock. To the layman's surprise, every jump of Bessie is like a Chinese chess horse: horizontal movement 1, vertical movement 2, vertical movement 1, horizontal movement 2. Bessie sometimes has up to eight choices. Farmer John was observing the ballet connection with Bessie, and he realized that sometimes Bessie might not be able to jump to the destination she wanted, because there was no lotus on the road. So he wanted to add a few lotus flowers to enable Bessie to complete the task. Farmer John, who has always been frugal, wants to add a minimum number of lotus flowers. Of course, Lotus cannot be placed on stones. Help Farmer John determine the minimum number of lotus flowers to be added. Calculate the minimum number of steps required to jump from the start point to the target point based on the minimum number of lotus flowers added. Finally, calculate the number of hop paths that meet the minimum number of Lotus Notes to be added.

Input

Row 1st: two integers, m, n

2nd .. row M + 1: Row I + 1, row I + 1 has n integers, indicating the status of the position: 0 is water; 1 is Lotus; 2 is rock; 3 is the starting position of Bessie; 4 is the target location of Bessie.

Output

Row 1st: an integer: the minimum number of lotus flowers to be added. If Bessie cannot skip to the next step, output-1.

Row 2nd: an integer: the minimum number of steps required by Bessie to jump from the starting point to the target point based on the minimum number of lotus flowers added. If 1st rows are output-1, this row is not output. Row 3rd: an integer: when the minimum number of lotus flowers is added, the number of hop steps is 2nd rows of the output value. If the number of hop paths is 1st rows of output-1, this row is not output.

Sample input4 8
0 0 0 1 0 0 0
0 0 0 0 2 0 1
0 0 0 0 4 0 0
3 0 0 0 0 0 1 0
Sample output2
6
2

Output description

Add at least two lotus flowers in the 'X' position.

0 0 0 1 0 0 0 0 0 0 1 0 0 0 0
0x0 0 0 2 0 1 0 0 0 0 2 0 1
0 0 0 0x4 0 0 0x0x4 0 0
3 0 0 0 0 0 1 0 3 0 0 0 0 1 0
Bessie should take at least six steps. There are two solutions:

0 0 0 C 0 0 0 0 0 0 C 0 0 0 0
0 B 0 0 0 2 0 F 0 0 0 0 2 0 f
0 0 0 0 d G 0 0 0 B 0 d G 0 0
A 0 0 0 0 0 e 0 A 0 0 0 0 e 0
Question

This is just a bit more judgment when spfa is relaxed. I mistakenly estimated the size of the answer, not long, and Wa had a Qaq.

Code
 1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 using namespace std; 6  7 const int Maxn=30; 8 int n,m; 9 int map[Maxn+10][Maxn+10];10 11 queue<pair<int,int> > que;12 int cost[Maxn+10][Maxn+10];13 int dist[Maxn+10][Maxn+10];14 long long ways[Maxn+10][Maxn+10];15 bool inque[Maxn+10][Maxn+10];16 int dx[]={0,1,2,2,1,-1,-2,-2,-1};17 int dy[]={0,-2,-1,1,2,2,1,-1,-2};18 19 inline void BFS(int s_x,int s_y,int e_x,int e_y){20     que.push(make_pair(s_x,s_y));21     memset(cost,127,sizeof(cost));22     cost[s_x][s_y]=0;23     dist[s_x][s_y]=0;24     ways[s_x][s_y]=1;25     inque[s_x][s_y]=true;26     while(!que.empty()){27         int nowx=que.front().first,nowy=que.front().second;28         que.pop();29         if (nowx==e_x && nowy==e_y) continue;30         for (int k=1;k<=8;k++){31             int x=nowx+dx[k],y=nowy+dy[k];32             if (!(1<=x && x<=n && 1<=y && y<=m)) continue;33             if (map[x][y]==2) continue;34             int w=0;if (map[x][y]==0) w=1;35             if (cost[x][y]>cost[nowx][nowy]+w){36                 cost[x][y]=cost[nowx][nowy]+w;37                 dist[x][y]=dist[nowx][nowy]+1;38                 ways[x][y]=ways[nowx][nowy];39                 if (inque[x][y]==false){40                     inque[x][y]=true;41                     que.push(make_pair(x,y));42                 }43             }else if (cost[x][y]==cost[nowx][nowy]+w){44                  if(dist[x][y]>dist[nowx][nowy]+1){45                      dist[x][y]=dist[nowx][nowy]+1;46                      ways[x][y]=ways[nowx][nowy];47                      if (inque[x][y]==false){48                         inque[x][y]=true;49                         que.push(make_pair(x,y));50                     }51                  }else if (dist[x][y]==dist[nowx][nowy]+1){52                      ways[x][y]+=ways[nowx][nowy];53                      if (inque[x][y]==false){54                         inque[x][y]=true;55                         que.push(make_pair(x,y));56                     }57                  }58             }59         }60         inque[nowx][nowy]=false;61     }62 }63 64 int main(){65     scanf("%d%d",&n,&m);66     int sx,sy,ex,ey;67     for (int i=1;i<=n;i++){68         for (int j=1;j<=m;j++){69             scanf("%d",&map[i][j]);70             if (map[i][j]==3){71                 sx=i;sy=j;72             }73             if (map[i][j]==4){74                 ex=i;ey=j;75             }76         }77     }78     BFS(sx,sy,ex,ey);79     if (cost[ex][ey]==cost[0][0] || ways[ex][ey]==0){80         printf("-1\n");81     }else{82         printf("%d\n%d\n%lld\n",cost[ex][ey],dist[ex][ey],ways[ex][ey]);83     }84     return 0;85 }
View code

 

Bzoj 1632: [usaco 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.