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

题意:从 节点1出发,一笔画出 圣诞老人的家(所谓一笔画,就是遍访所有边且每条边仅访问一次)。

思路:深度优先搜索(DFS算法)


#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int map[6][6];
void makemap(){
    memset(map,0,sizeof(map));
    for(int i=1;i<=5;i++)
		for(int j=1;j<=5;j++) if(i!=j) map[i][j]=1;
		map[4][1]=map[1][4]=0;
		map[4][2]=map[2][4]=0;
}
void dfs(int x,int k,string s){ // x 为 出发点/经过点,k为边的条数,s记录路径
    s+=char(x+'0');
    if(k==8){
        cout<<s<<endl;
        return ;
    }
    for(int y=1;y<=5;y++)
		if(map[x][y]){
			map[x][y]=map[y][x]=0;//画过的的边标记为 0
			dfs(y,k+1,s);
			map[x][y]=map[y][x]=1;//递归完之后,返回原来未画的状态 1
		}
}
int main(){
    makemap();
    dfs(1,0,"");
    return 0;
}



 

转载于:https://www.cnblogs.com/bofengyu/p/4477426.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值