UVA 291 The House Of Santa Claus (DFS求一笔画)
题意:从左下方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求一笔画)的更多相关文章
- 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 ...
- UVA 291 The House Of Santa Claus(DFS算法)
题意:从 节点1出发,一笔画出 圣诞老人的家(所谓一笔画,就是遍访所有边且每条边仅访问一次). 思路:深度优先搜索(DFS算法) #include<iostream> #include&l ...
- UVa 291 The House Of Santa Claus——回溯dfs
题意:从左下方的1开始,一笔画出圣诞老人的房子. #include <iostream> #include <cstring> using namespace std; ][] ...
- E. Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- codeforces 748E Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- <linux下sysctl指令详解>
Sysctl指令是对系统核心参数的设置: 用法: -a 参数列出系统中所有核心设置 当然了这些核心的设置都是文件,存放于/proc/sys/net目录下. 举个有代表性的例子: net.ipv4.ic ...
- RGB颜色二值化
原理:RGB颜色根据计算'灰度'的公式,可以转化为黑白2种颜色,实现二值化. 业务场景的应用:可以根据背景颜色,取一个黑色或白色的颜色,作为背景色上的文案字体颜色 具体代码: function get ...
- C#中Delegate
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 11g RAC R2 体系结构---Grid
基于agent的管理方式 从oracle 11.2开始出现了多用户的概念,oracle开始使用一组多线程的daemon来同时支持多个用户的使用.管理资源,这些daemon叫做Agent.这些Agent ...
- C string 函数大全
PS:本文包含了大部分strings函数的说明,并附带举例说明.本来想自己整理一下的,发现已经有前辈整理过了,就转了过来.修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时 ...
- C# 使用隐式或显示实现接口的区别
通俗的来讲,"显示接口实现"就是使用接口名称作为方法名的前缀;而传统的实现方式称之为:"隐式接口实现".费话不说,例子如下: interface IA ...
- pipe/popen/fifo
pipe(管道) 专用于父子进程通信, 函数原型 int pipe(int fd[2]) fd[0]表示输入, fd[1]表示输出 如果父子进程要双向通信, 可以通过类似信号的功能进行控制, 也可以简 ...
- Windows7 sp1 64位下安装配置eclipse+jdk+CDT+minGW
需要的工具: jdk-7u11-windows-x64.exe eclipse-SDK-4.2.2-win32-x86_64.zip cdt-master-8.1.2.zip mingw-get-i ...
- [SQL_Server_Question]Msg 1105无法为数据库 'tempdb' 中的对象分配空间,因为 'PRIMARY' 文件组已满
错误消息: Msg 1105, Level 17, State 2, Line 266Could not allocate space for object 'dbo.Large Object Sto ...
- epoll分析
Epoll详解及源码分析 1.什么是epoll epoll是当前在Linux下开发大规模并发网络程序的热门人选,epoll 在Linux2.6内核中正式引入,和select相似,都是I/O多路复用 ...