UVA 291 The House Of Santa Claus DFS

topic:

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.

  figure20
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.

  figure33
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:

12,435,123 
13,245,123 
... 
1512342 

Analysis:
represents Santa house with 5 points, in addition to the two edges 1-4 and 2-4, all the sides are connected, whether a sum of Q from the lower left (point 1) Draw What are the paths of the house, one by one point output out.
For example:
12,435,123
13,245,123
......
15123421
Analysis:
From a start point, deep search through all the edges, until the number of edges reaches a recursive traversal of the end 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,"");
}
View Code

 




Guess you like

Origin www.cnblogs.com/cautx/p/11525309.html
dfs