题意:从左下方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. HashMap 读后感

    HashMap是一个哈希表,内部通过链地址法解决哈希冲突.内部用Entry数组保存数据,每个Entry是一个单向链表. HashMap不保证插入的顺序,线程不同步,允许null 下面是几个重要点: 保 ...

  2. java synchronized关键字浅探

    synchronized 是 java 多线程编程中用于使线程之间的操作串行化的关键字.这种措施类似于数据库中使用排他锁实现并发控制,但是有所不同的是,数据库中是对数据对象加锁,而 java 则是对将 ...

  3. nyoj_t218(Dinner)

    描述 Little A is one member of ACM team. He had just won the gold in World Final. To celebrate, he dec ...

  4. mfc110ud.dll not found

    mfc110ud.dll not found while debugging vs2012 MFC application. Possible Solutions: 1) >>>&g ...

  5. [Guava学习笔记]Strings: 字符串处理

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3861502.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  6. IOS数据解析JSON

    //非原创 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSO ...

  7. WP开发笔记——字符串 转 MD5 加密

    将字符串进行MD5加密,返回加密后的字符串. 从这里下载Md5.cs文件:http://pan.baidu.com/s/1hq3gpnu. 添加到Windows Phone 7项目中,在代码里面这样调 ...

  8. Discuz 3.X 门户文章插入图片自动添加 alt 标签

    最近用 Discuz 搭建了个网站--儿童安全座椅网(www.bbseat.com.cn),用到了门户功能,不得不说Discuz 的功能还是非常强大的,但在使用过程中发现在发表文章时添加了图片却不能像 ...

  9. 一款jquery编写图文下拉二级导航菜单特效

    一款jquery编写图文下拉二级导航菜单特效,效果非常简洁大气,很不错的一款jquery导航菜单特效. 这款jquery特效适用于很多的个人和门户网站. 适用浏览器:IE8.360.FireFox.C ...

  10. php获取网页中图片并保存到本地的代码

    php获取网页中图片并保存到本地的代码,将网页中图片保存本地文件夹: <?php /** * 获取网页中图片,并保存至本地 * by www.jbxue.com */ header(" ...