3个A国人,3个B国人,3个C国人坐成一排。 要求不能使连续的3个人是同一个国籍.

 这道题本来很容易的,就是一次全排列然后检查而已。

public class Test3
{
	static int kinds=0;
	static int b[]=new int[10];
	static boolean vis[]=new boolean[10];
	public static void main(String[] args)
	{
		int a[]=new int[]{0,1,1,1,2,2,2,3,3,3};
		dfs(1,a);
		System.out.println("kinds:"+kinds);
	}
	static void dfs(int start,int a[])
	{
		if(start==a.length)
		{
			kinds++;
		}
		else
		{
			for(int i=1;i<a.length;i++)
			{
				if(!vis[i])//未上坐
				{
					b[start]=a[i];
					if(start>2 && b[start-2]==b[start-1] && b[start-1]==b[start]) 
						continue;//边排边检查提高效率
					vis[i]=true;
					dfs(start+1,a);
					vis[i]=false;
				}
			}
		}
	}
}
//kinds:283824
 

一不小心以为不可以重复,一个同学给我指出来了,来自同一个国家,但个体可以不同,所以序列是可以重复的。如果要求序列也不重复的话,问题就会复杂些了。

下面是代码。

/**
 * 和上一篇的售票员卖车票很相似
 * 因为有3国人  所以结果必然是3的倍数。
 * 可以使第一个座位是A。这样程序就能快3倍了。
 * 采用回溯,边排边测试,提高效率。
*/
import java.util.Arrays;


public class Test4 
{
	static int kinds=0;
	static int a[]=new int[4];
	static int b[]=new int[4];
	static int c[]=new int[4];
	static int aim[]=new int[10];
	public static void main(String[] args)
	{
//		aim[1]=1;
		Arrays.fill(a,1);
		Arrays.fill(b,2);
		Arrays.fill(c,3);
//		dfs(2,2,3,3);
		dfs(1,3,3,3);
		System.out.println("kinds:"+kinds);
	}
	static void dfs(int start,int a,int b,int c)
	{
		if(start==10)
		{
			kinds++;
//			for(int i=1;i<start;i++)
//			{
//				System.out.printf("%c",(aim[i]+64));
//			}
//			System.out.println();
		}
		else if(start<3)//肯定不会出现三个座来自国家相同的,因为就两座位。
		{
			aim[start]=1;
			a--;
			dfs(start+1,a,b,c);
			a++;
			
			aim[start]=2;
			b--;
			dfs(start+1,a,b,c);
			b++;
			
			aim[start]=3;
			c--;
			dfs(start+1,a,b,c);
			c++;
		}
		else
		{
			aim[start]=1;
			if(!(aim[start-2]==aim[start-1] && aim[start-1]==aim[start]) && a>0)//不是3连号,且A国家还有人未上坐
			{
				a--;
				dfs(start+1,a,b,c);
				a++;
			}
			
			aim[start]=2;
			if(!(aim[start-2]==aim[start-1] && aim[start-1]==aim[start]) && b>0)//同理
			{
				b--;
				dfs(start+1,a,b,c);
				b++;
			}
			
			aim[start]=3;
			if(!(aim[start-2]==aim[start-1] && aim[start-1]==aim[start])  && c>0)//同理
			{
				c--;
				dfs(start+1,a,b,c);
				c++;
			}
		}
	}
}
//计算是1314.好巧的数字啊

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值