题
In your childhood you most likely had to solve the riddle of the house of Santa Claus. Do you remember that the importance was on drawing the house in a stretch without lifting the pencil and not drawing a line twice? As a reminder it has to look like shown in Figure 1.
Figure: The House of Santa Claus
Well, a couple of years later, like now, you have to ``draw'' the house again but on the computer. As one possibility is not enough, we require all the possibilities when starting in the lower left corner. Follow the example in Figure 2 while defining your stretch.
Figure: This Sequence would give the Outputline 153125432
All the possibilities have to be listed in the outputfile by increasing order, meaning that 1234... is listed before 1235... .
Output
So, an outputfile could look like this:
12435123 13245123 ... 15123421
其实就是一个简单的dfs,注意将这几个点的通行情况放进邻接矩阵中,然后,就没有然后了。
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=4&problem=227&mosmsg=Submission+received+with+ID+17724776
代码如下:
#include<iostream> #include<string> using namespace std; int mark[6][6] = { 0 }; void dfs(string str, int j, int deep) { str[deep] = char(j + '0'); if (deep >= 8) { cout << str << endl; return; } else { for (int i = 1; i < 6; i++) { if (mark[j][i]) { mark[j][i] = mark[i][j] = 0;dfs(str, i, deep + 1); mark[j][i] = mark[i][j] = 1; } } return;} } int main() { for (int i = 1; i < 7; i++) { for (int j = 0; j < 7; j++) if (i != j)mark[i][j] = 1; } mark[2][4] = mark[4][2] = 0; mark[1][4] = mark[4][1] = 0; dfs("123456789", 1, 0); return 0; }