mfcheer

hdu-2181

版权声明:转载请注明出处。 https://blog.csdn.net/u014427196/article/details/41547775

http://acm.hdu.edu.cn/showproblem.php?pid=2181

dfs的应用

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

using namespace std;

int m;
int p[25][5];
int res[30];
int vis[30];
int ca = 1;

void dfs(int m,int len,int c)
{
	res[len] = m;
	vis[m]=1;
	for(int i=0;i<3;i++)
	{
		int temp = p[m][i];
		if (temp == c && len ==19)
		{
			cout<<ca++<<":  ";
			for(int i=0;i<=19;i++)
				cout<<res[i]<<" ";
			cout<<c<<endl;
		}
		if (!vis[temp])
			dfs(temp,len+1,c);
	}
	vis[m]=0;
}


int main ()
{
	for(int i=1;i<=20;i++)
	{
		cin>>p[i][0];
		cin>>p[i][1];
		cin>>p[i][2];
	}
	while (cin>>m && m!=0)
	{
		memset(vis,0,sizeof(vis));
		dfs (m,0,m);
	}

	return 0;
}
← 全部文章

留言