码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 291 The House Of Santa Claus DFS

时间:2019-09-16 09:59:12      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:mem   line   ==   sed   oss   opened   递归   alt   include   

题目:

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
...
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,"");
}
View Code

 




UVA 291 The House Of Santa Claus DFS

标签:mem   line   ==   sed   oss   opened   递归   alt   include   

原文地址:https://www.cnblogs.com/cautx/p/11525309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!