同度點集,就是產生相同的距離集合,但卻是不同點集的那些點集.
這個東西,整個白天都在想.到現在,也不是很清楚.但,代碼寫出來了,回溯部分總是不自信的感覺.
核心的思想就是:傳遞給常式一個數組指標,之後用一個 static int 類型變數儲存點集的個數.即每次使集合為空白,就執行 count++ ;
無論是否成功產生點集,都進行回溯.通過對決策樹的分析,在每次調用 place () 結束之後,都通過上一個點集的內容對 distance 進行恢複.主要的邏輯障礙就在這裡了.哎,不深扣了,扣不起啊.
因為是尋找所有的可能,所以可能會有重複的點集出現.
注釋寫得很爛,最近有看英語跟數學.
好吧,帖.!/*10-42-11-02-19-23.55.c -- 第十章第四十二題*/<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 * const points, const int column, const int row, Data * const distance) ;<br />int place (Item * const * 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 symmetries will be printed.*/<br />int turnpike (Item * const * 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 * 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 />}<br />/*To guide next step.*/<br />index = TRUE ;<br />break ;<br />}<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 />}<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 />}<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 />}<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 />}<br />index = TRUE ;<br />break ;<br />}<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 />}<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 />}<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 />}<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 />}