题意:从左下方1开始,一笔画出圣诞老人的屋子(不过话说,圣诞老人的屋子是这样的吗?这算是个屋子么),输出所有可以的路径。

思路:贴代码。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue> using namespace std;
int head[],tot=,val;//val:用整型值存储结果,因为最后输出是要按大小排序
int vis[];//用来标记边是否已经走过,若已经走过,则接下来就不能走该条边
int len;//满足一笔画的答案个数
int ans[];//存储答案
struct Edge{
int to,next;
}edge[]; //建立双向边
void add(int u,int v){
edge[tot].next=head[u];
edge[tot].to=v;
head[u]=tot++; edge[tot].next=head[v];
edge[tot].to=u;
head[v]=tot++;
} void init(){
memset(head,-,sizeof(head)); //一开始忘记初始化head了。。。
add(,);add(,);add(,);
add(,);add(,);
add(,);add(,);
add(,);
}
//u:当前走到的点;idx表示已经走过了idx条边,总计需要走完8条边
void dfs(int u,int idx){
if(idx==){
val=val*+u;
ans[len++]=val;
val/=;
return;
}
for(int k=head[u];k!=-;k=edge[k].next){
if(vis[k])
continue; //若第k条边已经走过,则继续尝试其它边
int v=edge[k].to;
/*
因为是双向边,相同端点的会有两条,走过其中一条后,另一条也要标记走过,因为每条边只能走一次,一开始就是这里给忽略了。
若k为偶数,第k边端点为u、v,则第k+1边端点也为u、v,要同时标记走过。
若k为奇数,第k边端点为u、v,则第k-1边端点也为u、v,要同时标记走过。
*/
if(k%==){
vis[k]=;
vis[k+]=;
}
else{
vis[k]=;
vis[k-]=;
}
val=val*+u;
dfs(v,idx+);
//最后要恢复原来的值,不能影响之后的遍历
val=val/;
if(k%==){
vis[k]=;
vis[k+]=;
}
else{
vis[k]=;
vis[k-]=;
}
}
} int main()
{
init();
len=;
memset(vis,,sizeof(vis));
val=;
//从左下角1开始dfs
dfs(,); sort(ans,ans+len);
for(int i=;i<len;i++)
printf("%d\n",ans[i]);
return ;
}

贴个书上的参考程序:

#include <iostream>
#include <string>
#include <cstring>
//因为数据量很小,用邻接矩阵,15ms,而且输出顺序即按照大小顺序排。
using namespace std;
int edge[][];
void makeEdge(){
memset(edge,,sizeof(edge));
for(int i=;i<=;i++){
for(int j=;j<=;j++){
if(i!=j)
edge[i][j]=;
}
}
edge[][]=edge[][]=;
edge[][]=edge[][]=;
}
//目前已画了k条边,准备将x扩展为s的第k+1条边的端点
void dfs(int x,int k,string s){
s+=char(x+'');
if(k==){
cout<<s<<endl;
return;
}
for(int y=;y<=;y++){
if(edge[x][y]){
edge[x][y]=edge[y][x]=;
dfs(y,k+,s);
edge[x][y]=edge[y][x]=;
}
}
}
int main()
{
makeEdge();
dfs(,,"");
return ;
}

UVA 291 The House Of Santa Claus (DFS求一笔画)的更多相关文章

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

  2. UVA 291 The House Of Santa Claus(DFS算法)

    题意:从 节点1出发,一笔画出 圣诞老人的家(所谓一笔画,就是遍访所有边且每条边仅访问一次). 思路:深度优先搜索(DFS算法) #include<iostream> #include&l ...

  3. UVa 291 The House Of Santa Claus——回溯dfs

    题意:从左下方的1开始,一笔画出圣诞老人的房子. #include <iostream> #include <cstring> using namespace std; ][] ...

  4. E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  5. codeforces 748E Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  6. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL

    D. Santa Claus and a Palindrome time limit per test 2 seconds memory limit per test 256 megabytes in ...

  7. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  8. Codeforces Round #389 Div.2 E. Santa Claus and Tangerines

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. Codeforces Round #389 Div.2 D. Santa Claus and a Palindrome

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

随机推荐

  1. AOJ 2200 Mr. Rito Post Office

    Mr. Rito Post Office Time Limit : 8 sec, Memory Limit : 65536 KB Problem D: Mr. Rito Post Office あなた ...

  2. unity 3消 游戏

    3消游戏跟着智能手机流行到现在已经有很长一段时间,unity实现的3消 https://github.com/textcube/match3action 截图如下: 在阅读源码的时候不难发现,Game ...

  3. JavaScript 内部对象

    欢迎访问我的个人博客:http://blog.cdswyda.com/ 原文文地址:JavaScript 内部对象

  4. crontab的应用

    当我们需要定时执行某个系统内的php脚本程序时,可以这样设置crontab * 19 * * * /usr/local/php/bin/php /var/www/test.php 此处表示调用php( ...

  5. http 错误编号大全(转)

      状态行包含HTTP版本.状态代码.与状态代码对应的简短说明信息.在大多数情况下,除了Content-Type之外的所有应答头都是可选的.但Content-Type是必需的,它描述的是后面文档的MI ...

  6. WINDOWS下更改MYSQL数据路径(datadir)后服务启动1067解决不能改变mysql数据库存储位置

    晚上安装完MYSQL(系统:深度WINXPSP2, MYSQL版本:5.1.32)后,用MYSQL自带的配置工具配置完发现默认的数据存放路径是:C:/Documents and Settings/Al ...

  7. TCP HelloWord

    client.cpp #include <stdio.h> #include <winsock2.h> #pragma comment (lib,"ws2_32&qu ...

  8. mysql时间处理

    两种方式,一个是在数据库查询的时候就截取,另一个就是在使用的时候截取. 1.数据库 select date_format(日期字段,’%Y-%m-%d’) as ‘日期’ from test 2.ja ...

  9. 小课堂Week9 例外处理设计的逆袭Part2

    小课堂Week9 例外处理设计的逆袭Part2 今天继续阅读<例外处理设计的逆袭>这本书,我们先看两个案例: 案例1 问:如果要设计一个依据学号到数据库中查询学生资料的函数,当找不到符合条 ...

  10. PAT乙级真题1008. 数组元素循环右移问题 (20)

    原题: 1008. 数组元素循环右移问题 (20) 时间限制400 ms内存限制65536 kB 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M&g ...