圣诞恋歌的答辩画房子记录-The House Of Santa Claus

The House Of Santa Claus
描述

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 requireallthe possibilities when starting in the lower left corner. Follow the example in Figure 2 while defining your scetch.


Figure:This Sequence would give the Outputline 153125432

All the possibilities have to be listed in the outputfile by increasing order, meaning that1234...is listed before1235....


深度优先搜索,从顶点1开始,输出一笔画出小房子的所有方案,各方案顶点串,按字典序排列。

 做了半坤小时,然后费尽千辛万苦做出来。

  • 建立该无向图的矩阵存储式
  • 建立该图的记录矩阵,全部置0
  • 建立点集遍历栈
  • 首先的for循环寻找第一个有效伸展节点
  • 其次的while-for结构寻找有效一笔画路径,并输出
  • for平级if用来控制深搜的后退操作与更换路径+1操作;另一个if用来控制合法退出while循环操作

注意点:

  1. 栈是从1开始存数据,栈底0用来初始化使用
  2. 因为无向图的关系,记录矩阵需要保证双向置1操作,即isMat[i][j]=1,isMat[j][i]=1。
  3. 一笔画路径输出后接着循环迭代,此为无效循环。改进方案为追溯前三个节点的分配,而不是只追溯两个节点,由于这道题的节点较少(且定死),速度影响不大。
  4. 如需改动再在for平级添加if(t==9){}进行回溯操作,并且在输出路径序列以后break离开for循环。
#include<iostream>
using namespace std;
int mat[6][6]={
	{0,0,0,0,0,0},
	{0,0,1,1,0,1},
	{0,1,0,1,0,1},
	{0,1,1,0,1,1},
	{0,0,0,1,0,1},
	{0,1,1,1,1,0}
};
int isMat[6][6]={0};
int index[10]={0};
int main(){
	int i,j,t,iter;
	t=0,iter=1;
	index[++t]=1;
	for(i=2;i<6;i++){
		if(mat[1][i]!=0){
			isMat[1][i]=1,isMat[i][1]=1;
			index[++t]=i;
			break;
		}else continue;
	}
		while(true){
			for(j=iter;j<6;j++){
				if(mat[index[t]][j]!=0&&isMat[index[t]][j]==0){
					isMat[index[t]][j]=1,isMat[j][index[t]]=1;
					iter=1;
					index[++t]=j;
					if(t==9){
						for(int for_i=1;for_i<10;for_i++) cout<<index[for_i];
						cout<<endl;
					}
					break;
				}else continue;
			}
			if(j==6){
				isMat[index[t]][index[t-1]]=0,isMat[index[t-1]][index[t]]=0;
				iter=index[t]+1;
				--t;
			}
			if(t==0) break;	
		}
	return 0;	
} 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值