Toll Road Reconstruction problem (all points of the same degree can be found) C Language

Source: Internet
Author: User

A point set of the same degree is the point set that generates the same distance, but is the point set of a different point set.

This is something that has been around for the whole day. It is not very clear till now. But the code is written out, And the Backtracking part is always unconfident.

The core idea is to pass an array pointer to the routine, and then use a static int type variable to save the number of points set. That is, execute count ++ every time the set is empty;

Whether or not a point set is successfully generated, backtracking is performed. by analyzing the decision tree, after each call to place (), the distance is restored through the content of the previous point set. the main logical obstacle is here. well, you can't afford it.

Because it is looking for all possibilities, there may be repeated point sets.

Comments are poorly written. Recently I have been reading English and mathematics.

Okay, post .!/* 10-42-11-02-19-23.55.c -- Chapter 10, question 1 */<br/> # include <stdio. h> <br/> # include <stdlib. h> </P> <p> # define false (0) <br/> # define true (1) </P> <p> typedef int item; <br/> typedef int bool; </P> <p>/* data is a kind of data structures. */<br/> int turnpike (item * const points, const int column, const int row, data * const distance ); <br/> int place (item * const points, const int column, const int row, const int left, const int right, Data * const distance ); <br/> item absolutevalue (const item value); </P> <p>/* It will return the number of lines that can be rebuilded. */<br/>/* In the case where there are some duplicate distances, it is possible that several will be printed. */<br/> int turnpike (item * const points, const int column, const int row, data * const distance) <br/>{< br/> item Max; <br/> int I; </P> <p>/* to cause the COOD expressly, abnegate to optimize the loop. */<br/>/* set starting point. */<br/> for (I = 0; I <column; I ++) <br/> points [I] [0] = 0; <br/> max = findmax (distance); <br/> for (I = 0; I <column; I ++) <br/> points [I] [row-1] = max; <br/> Delete (distance, max); </P> <p> return place (points, column, row, 1, row-2, distance); <br/>}</P> <p> int place (item * const points, const int column, const int row, const int left, const int right, Data * const distance) <br/>{< br/> static int COUNT = 0; <br/> bool Index = false; <br/> item max_in_distance, temp; <br/> int I, J, K; </P> <p> If (isempty (distance) <br/>{< br/> count ++; <br/> return count; <br/>}< br/> max_in_distance = findmax (distance); <br/>/* Check if setting points [count] [right] = dmax is feasible. */<br/>/* testing */<br/> for (I = 0; I <left; I ++) <br/>{ <br/> temp = absolutevalue (points [count] [I]-max_in_distance); <br/> If (true = find (distance, temp )) <br/> Delete (distance, temp); <br/>/* If it hasn' t found temp in distance. */<br/> else <br/> {<br/>/* if it has deleted. */<br/> if (I> 0) <br/> {<br/>/* recover the items that has been deleted. */<br/> for (k = I-1; k> = 0; k --) <br/>{ <br/> temp = absolutevalue (points [count] [k]-max_in_distance); <br/> insert (distance, temp ); <br/>}< br/>/* to guide next step. */<br/> Index = true; <br/> break; <br/>}< br/>/* If has passed through the test on last step. */<br/> If (false = index) <br/>{< br/>/* testing. */<br/> for (j = right + 1; j <row; j ++) <br/>{ <br/> temp = absolutevalue (points [count] [J]-max_in_distance); <br/> If (true = find (distance, temp )) <br/> Delete (distance, temp); <br/>/* If hasn't found temp in distance. */<br/> else <br/> {<br/>/* If has deleted. */<br/> If (j> right + 1) <br/> {<br/>/* recover the items that has been deleted. */<br/> for (k = J-1; k> right; k --) <br/>{ <br/> temp = absolutevalue (points [count] [k]-max_in_distance); <br/> insert (distance, temp ); <br/>}< br/>/* from points [count] [I-1] to points [count] [0] has been deleted certainly. */<br/>/* recover the items that from points [count] [I-1] to points [count] [0]. */<br/> for (k = I-1; k> = 0; k --) <br/>{ <br/> temp = absolutevalue (points [count] [k]-max_in_distance); <br/> insert (distance, temp ); <br/>}< br/>/* to guide next step. */<br/> Index = true; <br/> break; <br/>}< br/>/* If has passed through the test on last step. */<br/> If (false = index) <br/>{< br/>/* undoubted attempt. */<br/> for (I = count; I <column; I ++) <br/> points [I] [right] = max_in_distance; <br/> COUNT = place (points, column, row, left, right-1, distance); <br/>/* unconditional recall. */<br/>/* backtrack. */<br/> for (I = 0; I <left; I ++) <br/>{ <br/> temp = absolutevalue (points [count] [I]-max_in_distance); <br/> insert (distance, temp ); <br/>}< br/> for (I = right + 1; I <row; I ++) <br/>{ <br/> temp = absolutevalue (points [count] [I]-max_in_distance); <br/> insert (distance, temp ); <br/>}< br/>/* even if first attempt is successful, try to setting points [count] [left] = points [N]-dmax is feasible. */<br/>/* initialize "Index" again. */<br/> Index = false; <br/> for (I = 0; I <left; I ++) <br/> {<br/>/* Make sure you has known that temp is the absolute value of distance. */<br/> temp = absolutevalue (points [count] [row-1]-max_in_distance-points [count] [I]); <br/> If (true = find (distance, temp) <br/> Delete (distance, temp ); <br/> else <br/>{< br/> if (I> 0) <br/>{< br/> for (k = I-1; k> = 0; k --) <br/> {<br/> temp = absolutevalue (points [count] [row-1]-max_in_distance-points [count] [k]); <br/> insert (distance, temp); <br/>}< br/> Index = true; <br/> break; <br/>}< br/> If (false = index) <br/>{< br/> for (j = right + 1; j <row; j ++) <br/> {<br/> temp = absolutevalue (points [count] [row-1]-max_in_distance-points [count] [J]); <br/> If (true = find (distance, temp) <br/> Delete (distance, temp ); <br/> else <br/> {<br/> If (j> right + 1) <br/> {<br/> for (k = J-1; k> right; k --) <br/> {<br/> temp = absolutevalue (points [count] [row-1]-max_in_distance-points [count] [k]); <br/> insert (distance, temp); <br/>}< br/> for (k = I-1; k> = 0; k --) <br/>{< br/> temp = absolutevalue (points [count] [row-1]-max_in_distance-points [count] [k]); <br/> insert (distance, temp); <br/>}< br/> Index = true; <br/> break; <br/>}< br/> If (false = index) <br/> {<br/> for (I = count; I <column; I ++) <br/> points [I] [left] = points [count] [row-1]-max_in_distance; <br/> COUNT = place (points, column, row, left + 1, right, distance); <br/>/* backtrack. */<br/> for (I = 0; I <left; I ++) <br/> {<br/> temp = absolutevalue (points [count] [row-1]-max_in_distance-points [count] [I]); <br/> insert (distance, temp); <br/>}< br/> for (I = right + 1; I <row; I ++) <br/> {<br/> temp = absolutevalue (points [count] [row-1]-max_in_distance-points [count] [I]); <br/> insert (distance, temp); <br/>}</P> <p> return count; <br/>}</P> <p> item absolutevalue (const item value) <br/>{< br/> If (value <0) <br/> return 0-value; <br/> else <br/> return value; <br/>}

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.