首页 > 代码库 > D - Cow Ski Area
D - Cow Ski Area
Description
Farmer John‘s cousin, Farmer Ron, who lives in the mountains of Colorado, has recently taught his cows to ski. Unfortunately, his cows are somewhat timid and are afraid to ski among crowds of people at the local resorts, so FR has decided to construct his own private ski area behind his farm.
FR‘s ski area is a rectangle of width W and length L of ‘land squares‘ (1 <= W <= 500; 1 <= L <= 500). Each land square is an integral height H above sea level (0 <= H <= 9,999). Cows can ski horizontally and vertically between any two adjacent land squares, but never diagonally. Cows can ski from a higher square to a lower square but not the other way and they can ski either direction between two adjacent squares of the same height.
FR wants to build his ski area so that his cows can travel between any two squares by a combination of skiing (as described above) and ski lifts. A ski lift can be built between any two squares of the ski area, regardless of height. Ski lifts are bidirectional. Ski lifts can cross over each other since they can be built at varying heights above the ground, and multiple ski lifts can begin or end at the same square. Since ski lifts are expensive to build, FR wants to minimize the number of ski lifts he has to build to allow his cows to travel between all squares of his ski area.
Find the minimum number of ski lifts required to ensure the cows can travel from any square to any other square via a combination of skiing and lifts.
FR‘s ski area is a rectangle of width W and length L of ‘land squares‘ (1 <= W <= 500; 1 <= L <= 500). Each land square is an integral height H above sea level (0 <= H <= 9,999). Cows can ski horizontally and vertically between any two adjacent land squares, but never diagonally. Cows can ski from a higher square to a lower square but not the other way and they can ski either direction between two adjacent squares of the same height.
FR wants to build his ski area so that his cows can travel between any two squares by a combination of skiing (as described above) and ski lifts. A ski lift can be built between any two squares of the ski area, regardless of height. Ski lifts are bidirectional. Ski lifts can cross over each other since they can be built at varying heights above the ground, and multiple ski lifts can begin or end at the same square. Since ski lifts are expensive to build, FR wants to minimize the number of ski lifts he has to build to allow his cows to travel between all squares of his ski area.
Find the minimum number of ski lifts required to ensure the cows can travel from any square to any other square via a combination of skiing and lifts.
Input
* Line 1: Two space-separated integers: W and L
* Lines 2..L+1: L lines, each with W space-separated integers corresponding to the height of each square of land.
* Lines 2..L+1: L lines, each with W space-separated integers corresponding to the height of each square of land.
Output
* Line 1: A single integer equal to the minimal number of ski lifts FR needs to build to ensure that his cows can travel from any square to any other square via a combination of skiing and ski lifts
Sample Input
9 31 1 1 2 2 2 1 1 11 2 1 2 3 2 1 2 11 1 1 2 2 2 1 1 1
Sample Output
3
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
OUTPUT DETAILS:
FR builds the three lifts. Using (1, 1) as the lower-left corner,
the lifts are (3, 1) <-> (8, 2), (7, 3) <-> (5, 2), and (1, 3) <->
(2, 2). All locations are now connected. For example, a cow wishing
to travel from (9, 1) to (2, 2) would ski (9, 1) -> (8, 1) -> (7,
1) -> (7, 2) -> (7, 3), take the lift from (7, 3) -> (5, 2), ski
(5, 2) -> (4, 2) -> (3, 2) -> (3, 3) -> (2, 3) -> (1, 3), and then
take the lift from (1, 3) - > (2, 2). There is no solution using
OUTPUT DETAILS:
FR builds the three lifts. Using (1, 1) as the lower-left corner,
the lifts are (3, 1) <-> (8, 2), (7, 3) <-> (5, 2), and (1, 3) <->
(2, 2). All locations are now connected. For example, a cow wishing
to travel from (9, 1) to (2, 2) would ski (9, 1) -> (8, 1) -> (7,
1) -> (7, 2) -> (7, 3), take the lift from (7, 3) -> (5, 2), ski
(5, 2) -> (4, 2) -> (3, 2) -> (3, 3) -> (2, 3) -> (1, 3), and then
take the lift from (1, 3) - > (2, 2). There is no solution using
fewer than three lifts.
[cpp] view plaincopyprint?
- #include<stdio.h>
- #include<string.h>
- #include<stack>
- #define INF 1000010
- #define P 261010
- using namespace std;
- stack<int>S;
- int a[550][550],map[P][10],low[P],dfn[P];
- int instack[P];
- int bnt,be[P],n,m;
- int index,in[P],out[P];
- void build(){
- for(int i=1;i<=n;i++)
- for(int j=1;j<=m;j++){
- if(a[i][j]>=a[i-1][j])map[j+m*i-m][++map[j+m*i-m][0]] = j+(i-2)*m;
- if(a[i][j]>=a[i+1][j])map[j+m*i-m][++map[j+m*i-m][0]] = j+i*m;
- if(a[i][j]>=a[i][j+1])map[j+m*i-m][++map[j+m*i-m][0]] = j+(i-1)*m+1;
- if(a[i][j]>=a[i][j-1])map[j+m*i-m][++map[j+m*i-m][0]] = j+(i-1)*m-1;
- }
- }
- void tarjan(int i){
- dfn[i] = low[i] = ++index;
- S.push(i);
- instack[i] = 1;
- for(int j = 1;j<=map[i][0];j++){
- int k = map[i][j];
- if(!dfn[k]){
- tarjan(k);
- low[i] = min(low[i],low[k]);
- }
- else if(instack[k]){
- low[i] = min(low[i],dfn[k]);
- }
- }
- if(dfn[i]==low[i]){
- bnt++;
- int kk;
- do{
- kk = S.top();
- S.pop();
- instack[kk] = 0;
- be[kk] = bnt;
- }while(i!=kk);
- }
- }
- int main(){
- while(~scanf("%d%d",&m,&n)){
- int ans,ans1;
- memset(dfn,0,sizeof(dfn));
- memset(in,0,sizeof(in));
- memset(out,0,sizeof(out));
- memset(instack,0,sizeof(instack));
- bnt = ans = ans1 = index = 0;
- for(int i=0;i<=n+1;i++)a[i][0] = a[i][m+1] = INF;
- for(int j=0;j<=m+1;j++)a[0][j] = a[n+1][j] = INF;
- for(int i=1;i<=n;i++){
- for(int j=1;j<=m;j++){
- scanf("%d",&a[i][j]);
- map[j+(i-1)*m][0] = 0;
- }
- }
- build();
- for(int i=1;i<=n*m;i++)
- if(!dfn[i])
- tarjan(i);
- for(int i=1;i<=n*m;i++){
- for(int j =1;j<=map[i][0];j++)
- if(be[i]!=be[map[i][j]]){
- out[be[i]]++;
- in[be[map[i][j]]]++;
- }
- }
- for(int i=1;i<=bnt;i++){
- if(out[i]==0)ans++;
- if(in[i]==0)ans1++;
- }
- if(bnt==1)printf("0\n");
- else printf("%d\n",max(ans,ans1));
- }
- }
[cpp] view plaincopyprint?
- 在POJ能过,在杭电就过不了,是什么原因啊,。。。。。。??!坑爹啊
D - Cow Ski Area
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。