"Algorithm, BFS" two elder brother in Huangshan

Source: Internet
Author: User

1031. Second brother in Huangshan description

Two elder brother and girlfriend to Huangshan travel. They spent the whole day on the mountain and found it was late and it was time to go home. And suddenly began to rain, two elder brother's girlfriend said very uncomfortable: "All of you, I knew I would not come with you." ”

Two elder brother certainly can't leave a girlfriend regardless, and two elder brother also don't want to sleep on the mountain. So he spread out a map that had been drenched with rain.

The Huangshan map is a n*n matrix in which each item in the matrix represents the height of that place. Second brother and girlfriend in the upper left corner, their residence in the lower right corner. In the matrix you can walk up and down, but not along the diagonal. Two elder brother's girlfriend does not like the bump, therefore two elder brother needs to find a return to the dwelling path, causes the path highest point and the lowest spot difference to be small, but does not need the tube this path to have how long.

Input Format

The first line: n the next n rows of n*n integer matrices, (0 ≤ per point high degrees ≤110 )。 ( 2≤N≤)

Output Format

An integer that represents the height difference between the highest and lowest points in the least bumpy path.

Sample Input
51 1 3 6 81 2 2 5 54 4 0 3 38 0 2 3 44 3 0 2 1
Sample Output
2

======================
Start to think of the shortest path problem immediately, and then want to be able to use dynamic planning, but found that the situation is not good, because the path is fixed. Finally solved with similar dijstra, the essence is breadth first.
In the implementation of the map and the path of the highest and lowest values (can not directly store the difference, otherwise it can not be compared with the current point) are stored in an array, slightly cumbersome.
The results did not pass, after others to find that this really does not, because only according to the difference in judging whether the optimal is a bit similar to greedy, can not guarantee the choice of the following path is correct, because the following path selection will also consider the maximum and minimum values. Corrected later.
==========================
The C + + code is as follows:
1 //two elder brother in Huangshan2#include <iostream>3 using namespacestd;4 5 intMain () {6     intn,i,j;7     BOOLFlag =1;8Cin>>N;9     //Map for Maps open for whether to choose highest and lowest to indicate the highest and lowest values of the pathTen     //Initialize One     intMap[n][n],open[n][n],highest[n][n],lowest[n][n]; A      for(i=0; i<n;++i) -          for(j=0; j<n;++j) { -Cin>>Map[i][j]; theOPEN[I][J] =0; -LOWEST[I][J] =0; -HIGHEST[I][J] = the; -         } +open[0][0] =1; -highest[0][0]=lowest[0][0]=map[0][0]; +      A      while(flag) { at         intCol,raw,min =1100, Temhigh,temlow; -Flag =0; -         //traverse the Open table to find the most stable path that already exists -          for(i=0; i<n;++i) -              for(j=0; j<n;++j) -                 if(Open[i][j] = =1&& highest[i][j]-lowest[i][j]<min) { inCol =J; -Raw =i; toMin = highest[i][j]-Lowest[i][j]; +Flag =1; -                 } the         if(Raw = = N1&& col = = N1) Break; *         //Scan the front and back of the point four points, change their highest and lowest values and join the Open Table $Open[raw][col] =0;//cout<<raw+1<<col+1<< ' <Panax Notoginseng         if(raw>0) { -Temhigh = highest[raw][col]>map[raw-1][col]? highest[raw][col]:map[raw-1][col]; theTemlow = lowest[raw][col]<map[raw-1][col]? lowest[raw][col]:map[raw-1][col]; +             if(temhigh-temlow1][col]-lowest[raw-1][col]) { Aopen[raw-1][col] =1; thehighest[raw-1][col] =Temhigh; +lowest[raw-1][col] =Temlow; -             } $         } $         if(col>0) { -Temhigh = highest[raw][col]>map[raw][col-1] ? highest[raw][col]:map[raw][col-1]; -Temlow = lowest[raw][col]<map[raw][col-1] ? lowest[raw][col]:map[raw][col-1]; the             if(temhigh-temlow1]-lowest[raw][col-1]) { -open[raw][col-1] =1;Wuyihighest[raw][col-1] =Temhigh; thelowest[raw][col-1] =Temlow; -             } Wu         } -         if(raw<n-1) { AboutTemhigh = highest[raw][col]>map[raw+1][col]? highest[raw][col]:map[raw+1][col]; $Temlow = lowest[raw][col]<map[raw+1][col]? lowest[raw][col]:map[raw+1][col]; -             if(temhigh-temlow1][col]-lowest[raw+1][col]) { -open[raw+1][col] =1; -highest[raw+1][col] =Temhigh; Alowest[raw+1][col] =Temlow; +             } the         } -         if(col<n-1) { $Temhigh = highest[raw][col]>map[raw][col+1] ? highest[raw][col]:map[raw][col+1]; theTemlow = lowest[raw][col]<map[raw][col+1] ? lowest[raw][col]:map[raw][col+1]; the             if(temhigh-temlow1]-lowest[raw][col+1]) { theopen[raw][col+1] =1; thehighest[raw][col+1] =Temhigh; -lowest[raw][col+1] =Temlow; in             } the         }     the     } Aboutcout<1][n-1]-lowest[n-1][n-1]; the     return 0; the}

"Algorithm, BFS" two elder brother in Huangshan

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.