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

Output

So, an outputfile could look like this:

12435123
13245123
...
1512342

分析:用5个点表示圣诞老人的房子,除了1-4和2-4两条边外,所有的边都相连,问能否从左下角(点1)一笔画出房子的路径有哪些,逐个点输出出来。例如:1243512313245123......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,"");
}



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

时间: 2024-08-07 00:47:48

UVA 291 The House Of Santa Claus DFS的相关文章

UVa 291 The House Of Santa Claus 回溯dfs

题意:从左下方的1开始,一笔画出圣诞老人的房子. 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 int edge[6][6]; 5 6 //已画了k条边 准备将端点x加入进来 7 void dfs(int x,int k,string s){ 8 s+=char(x+'0'); 9 if(k==8){ 10 cout<<s<<endl; 11 return; 12 }

291 - The House Of Santa Claus

来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=4&problem=227&mosmsg=Submission+received+with+ID+14026069 The House Of Santa Claus In your childhood you most likely had to solve the ri

UVA 291

A - The House Of Santa Claus(11.2.1)) Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description In your childhood you most likely had to solve the riddle of the house of Santa Claus. Do

[2015-12-24]OMG美语每日笔记-Santa Claus

坚持学习英语,OMG口语非常长不错,坚持每天整理.学英语坚持最重要,学英语坚持最重要,学英语坚持最重要说三遍! Santa Claus is coming to town 圣诞老人进城来! There are so many Christmas songs about Santa .Santa Claus is coming to town.Is a classic example. 关于圣诞老人的歌很多,圣诞老人进城来,是一个比较经典的歌. Santa Baby 圣诞宝贝 I love lis

UVa 12118 nspector&#39;s Dilemma (构造+DFS+欧拉回路)

题意:给定n个点,e条边和每条边的长度t,每两个点之间都有路相连,让你求一条最短的路经过这e条边. 析:刚开始想到要判连通,然后把相应的几块加起来,但是,第二个样例就不过,后来一想,那么有欧拉回路的还得加1啊. 又想每次再判一次是不是欧拉回路,怎么判又是问题,因为并不知道哪些是连在一块的,还得再查找,麻烦啊.... 后来上网看了一下题解,原来是要构造啊,也就是说把每个连通块都构造成一个欧拉回路,那么再减去端点的,就能完全连通了. 真是好方法,欧拉回路满足每个点的度都是偶数,也就是说如果不是偶数那

【Codeforces748D】Santa Claus and a Palindrome [STL]

Santa Claus and a Palindrome Time Limit: 20 Sec  Memory Limit: 512 MB Description 有k个串,串长都是n,每个串有一个ai的贡献. 选出若干个串,若它们可以通过任意组合,形成一个回文串,则可以获得它们的贡献之和. 求最大贡献. Input 第一行两个整数k,n. 之后k行,每行分别是一个串si,与贡献ai. Output 一个整数表示答案. Sample Input 7 3 abb 2 aaa -3 bba -1 z

UVA - 572 - Oil Deposits (图的DFS!)

UVA - 572 Oil Deposits Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works

uva 1462 - Fuzzy Google Suggest(字典树+dfs)

题目链接:uva 1462 - Fuzzy Google Suggest 题目大意:模拟google的模糊搜索,给定给一个字符串集合,然后有n次搜索,每次有一个整数x和一个字符串,表示可以对字符串进行x次修改,包括增加.修改和删除一个字符,问修改后的字符可能是字符集中有多少个字符串的前缀. 解题思路:先建立字典树,对于每次搜索,在字典树上进行dfs,根据参数x和字符串匹配的位置进行处理,对于匹配到末尾的位置标记为2,然后对于第二次dfs,搜索每个分支上最早出现2的位置即可. #include <

UVA - 10308 - Roads in the North (DFS)

题目传送:UVA - 10308 思路:就是树的遍历,DFS即可,注意输入 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <sstream> #include <algorithm> #include <cmath> #include <queue> #include <s