要安排:3个A国人,3个B国人,3个C国人坐成一排,要求不能使连续的3个人是同一个国籍.求所有不同方案的总数?

package com.dati;

public class test11 {

	static int sum = 0;	// 不同方案总个数
	// 检查是否有同一国人连续3个
	public static boolean check(char[] c){
		int count = 1;	// 初始个数
		for(int i=0;i<c.length-1;i++){
			if(c[i]==c[i+1]){
				count++;
			}else{
				count = 1;	// 初始个数
			}
			if(count>=3) return true;
		}
		return false;
	}
	// 全排列
	public static void allSort(char[] c,int start,int end){
		if(start>end){
			if(!check(c)){	// 检查是否有同一国人连续3个
				sum++;		// 不同方案总个数加1
			}
			return ;
		}else{
			for(int i=start;i<=end;i++){
				char temp = c[i];
				c[i] = c[start];
				c[start] = temp;
				allSort(c,start+1,end);	// 递归
				temp = c[i];
				c[i] = c[start];
				c[start] = temp;
			}
		}
	}
	public static void main(String[] args){
		char[] c = {'A','A','A','B','B','B','C','C','C'};
		allSort(c,0,c.length-1);	// 全排列
		System.out.println(sum);
	}


}
class te{
	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;
				}
			}
		}
	}
	
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值