Poj 2922 honeymoon hike solution report (Binary + Deep Search)

Source: Internet
Author: User

Source: poj 2922 honeymoon hike

Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2922

ToJ 2344 honeymoon hike

Http://acm.tju.edu.cn/toj/showp.php? PID = 1, 2344

 

Solution Type: Binary Search + deep Priority Search

 

Question:

The height of each vertex in a graph is given. From the top left corner of the graph to the bottom right corner, the path with the minimum height difference between the highest and lowest points in all paths is obtained.

 

Solution:

The height difference of the binary search. Based on the enumerated upper and lower bounds of each height difference, the in-depth search is performed to determine whether the upper and lower bounds can pass through.

 

Submission:

1. Time limit exceeded: no upper and lower bounds are enumerated, but deep searches are performed based on the height difference, constantly updating the upper and lower bounds. It takes too long to perform deep searches. Although it takes some time to enumerate the upper and lower bounds, because the upper and lower bounds of this question are small, the time for deep search can be greatly reduced after enumeration, and the overall time can still be reduced.

2. Wrong answer: the tag array is not updated every time the upstream/downstream enumeration is cleared, leading to confusion in the tag array.

 

Note:

This question requires a high degree of time analysis. If it is not AC, you need to find a possible breakthrough to reduce the time, and use this breakthrough to code and try.

 

SourceProgram:

# Include <iostream>

 

Using namespace STD;

 

Long H [110] [110], t [110] [110], N;

 

Int DFS (long row, long Col, long DN, long up)

{// Perform deep search through the upper and lower bounds

If (H [row] [col]> up | H [row] [col] <DN) return 0;

// If the current search position does not meet the upper and lower bounds, 0 is returned.

T [row] [col] = 1;

// If yes, it indicates that the location has been searched.

If (ROW = N & Col = N) return 1;

// The target location has been found, and 1 is returned.

If (row> = 2 & T [row-1] [col] = 0 & DFS (Row-1, Col, dn, UP) return 1;

If (COL> = 2 & T [row] [col-1] = 0 & DFS (row, col-1, dn, UP) return 1;

If (row + 1 <= N & T [row + 1] [col] = 0 & DFS (row + 1, Col, dn, UP) return 1;

If (COL + 1 <= N & T [row] [col + 1] = 0 & DFS (row, Col + 1, dn, UP) return 1;

Return 0; // search in four directions. If none of them are reachable, 0 is returned.

}

 

Long Search (long min, long max)

{

Long mid, TMP, DN;

While (Min <max) // Binary Search

{

Mid = (MAX + min)/2;

TMP = H [1] [1]-mid;

For (DN = TMP> 0? TMP: 0; DN <= H [1] [1]; DN ++) // enumerate Upper and Lower Bounds

{

Memset (T, 0, sizeof (t); // clear the tag Array

If (DFS (, dn, DN + mid) break; // determines whether the upper and lower bound is feasible.

}

If (DN <= H [1] [1]) max = mid;

Else min = Mid + 1;

}

Return Max;

}

 

Int main ()

{

Long I, J, K, casenum, casecount;

Cin> casenum;

For (casecount = 1; casecount <= casenum; casecount ++)

{

Cin> N;

For (I = 1; I <= N; I ++)

For (j = 1; j <= N; j ++) CIN> H [I] [J]; // input Graph

Cout <"Scenario #" <casecount <":" <Endl;

Cout <search (0,200) <Endl; // output result

}

Return 0;

}

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.