UVA 291 The House Of Santa Claus DFS
Posted cautx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 291 The House Of Santa Claus DFS相关的知识,希望对你有一定的参考价值。
题目:
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... .
OutputSo, an outputfile could look like this:
12435123 13245123 ... 1512342
分析:
用5个点表示圣诞老人的房子,除了1-4和2-4两条边外,所有的边都相连,问能否从左下角(点1)一笔画出房子的路径有哪些,逐个点输出出来。
例如:
12435123
13245123
......
15123421
分析:
从点1开始,深搜遍历所有的边,直到遍历的边的个数达到8时递归结束。
AC code:
#include<bits/stdc++.h> using namespace std; int m[6][6]; void init() for(int i=1;i<=5;i++) for(int j=1;j<=5;j++) if(i!=j) m[i][j]=1; else m[i][j]=0; m[1][4]=m[4][1]=0; m[2][4]=m[4][2]=0; void dfs(int e,int k,string s) s+=(k+‘0‘); if(e==8) cout<<s<<endl; return; for(int i=1;i<=5;i++) if(m[k][i]) m[i][k]=m[k][i]=0; dfs(e+1,i,s); m[i][k]=m[k][i]=1; int main() init(); dfs(0,1,"");
以上是关于UVA 291 The House Of Santa Claus DFS的主要内容,如果未能解决你的问题,请参考以下文章
The goddess, the bird, the bell, the temptation of the house
UVa 437 The Tower of Babylon(动态规划)