穷举法破解集合小游戏~

游戏网站:http://www.setgame.com/puzzle/set.htm

游戏规则:

1、三种颜色(红、绿、紫)

2、三种外形(方形、椭圆形、花形)

3、三种背景阴影(实心、点、轮廓)

4、三种个数(1、2、3)

找出其中 3 个,满足:要么其中属性全相同,要么属性全不相同。

 

破解思路:穷举所有3个一组的情形,找出满足的。

源代码:

package com.gq.algorithm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SetPlay {

	List<Item> items = new ArrayList<Item>();
	
	private void init()
	{
		items.add( new Item(Color.RED, 		Shape.FLORIATED, 	Shading.DOT, 	3) );
		items.add( new Item(Color.PURPLE, 	Shape.SQUARENESS, 	Shading.LINE, 	2) );
		items.add( new Item(Color.RED, 		Shape.FLORIATED, 	Shading.DOT, 	2) );
		items.add( new Item(Color.GREE, 	Shape.ELLIPSE, 		Shading.LINE, 	2) );
		
		items.add( new Item(Color.PURPLE, 	Shape.FLORIATED, 	Shading.DOT, 	3) );
		items.add( new Item(Color.RED, 		Shape.ELLIPSE, 		Shading.DOT, 	2) );
		items.add( new Item(Color.GREE, 	Shape.FLORIATED, 	Shading.DOT, 	3) );
		items.add( new Item(Color.RED, 		Shape.FLORIATED, 	Shading.DOT, 	1) );
		
		items.add( new Item(Color.GREE, 	Shape.ELLIPSE, 		Shading.SOLID, 	2) );
		items.add( new Item(Color.RED, 		Shape.SQUARENESS, 	Shading.LINE, 	1) );
		items.add( new Item(Color.GREE, 	Shape.ELLIPSE, 		Shading.LINE, 	3) );
		items.add( new Item(Color.PURPLE, 	Shape.SQUARENESS, 	Shading.SOLID, 	3) );
	}
	
	private void play()
	{
		for( int i=0 ; i<items.size()-2 ; i++ )
		{
			for( int j=i+1 ; j<items.size()-1 ; j++ )
			{
				for( int k=j+1 ; k<items.size() ; k++ )
				{
					if( isSet( items.get(i), items.get(j), items.get(k)) )
					{
						System.out.println( "{" + (i+1) + ", " + (j+1) + ", " + (k+1) + "}" );
					}
				}
			}
		}
	}
	
	private boolean isSet( Item... items)
	{
		Map<Color, Integer> colorCount = new HashMap<Color, Integer>();
		Map<Shape, Integer> shapeCount = new HashMap<Shape, Integer>();
		Map<Shading,Integer> shadingCount = new HashMap<Shading,Integer>();
		Map<Integer,Integer> numCount = new HashMap<Integer,Integer>();
		
		for( Item item : items )
		{
			// 颜色
			if( colorCount.get( item.getColor() ) == null )
			{
				colorCount.put( item.getColor(), 1);
			}
			else
			{
				int count = colorCount.get( item.getColor() );
				colorCount.put( item.getColor(), ++count );
			}
			
			// 形状
			if( shapeCount.get( item.getShape()) == null )
			{
				shapeCount.put( item.getShape(), 1 );
			}
			else
			{
				int count = shapeCount.get( item.getShape() );
				shapeCount.put( item.getShape(), ++count );
			}
			
			// 背景阴影
			if( shadingCount.get( item.getShading()) == null )
			{
				shadingCount.put( item.getShading(), 1 );
			}
			else
			{
				int count = shadingCount.get( item.getShading() );
				shadingCount.put( item.getShading(), ++count );
			}
			
			// 个数
			if( numCount.get( item.getNum()) == null )
			{
				numCount.put( item.getNum(), 1 );
			}
			else
			{
				int count = numCount.get( item.getNum() );
				numCount.put( item.getNum(), ++count );
			}
		}
		
		if( isAllSameOrAllDiff( colorCount.size() ) 
				&& isAllSameOrAllDiff( shapeCount.size()) 
				&& isAllSameOrAllDiff( shadingCount.size())
				&& isAllSameOrAllDiff( numCount.size()) )
		{
			return true;
		}
		
		return false;
	}
	
	private boolean isAllSameOrAllDiff( Integer count )
	{
		if( count == null )
		{
			return false;
		}
		
		if( count == 1 || count == 3 )
		{
			return true;
		}
		
		return false;
	}

	
	public static void main(String[] args) 
	{
		SetPlay setPlay = new SetPlay();
		setPlay.init();
		setPlay.play();
	}

}

class Item{
	private final Color color;
	
	private final Shape shape;
	
	private final Shading shading;
	
	private final int num;

	public Item(Color color, Shape shape, Shading shading, int num) {
		super();
		this.color = color;
		this.shape = shape;
		this.shading = shading;
		this.num = num;
	}

	public Color getColor() {
		return color;
	}

	public Shape getShape() {
		return shape;
	}

	public Shading getShading() {
		return shading;
	}

	public int getNum() {
		return num;
	}
	
}

enum Color{
	/**
	 * 红色
	 */
	RED,
	
	/**
	 * 绿色
	 */
	GREE,
	/**
	 * 紫色
	 */
	PURPLE
}

enum Shape{
	/**
	 * 方形
	 */
	SQUARENESS,
	/**
	 * 椭圆形
	 */
	ELLIPSE,
	/**
	 * 花形
	 */
	FLORIATED
}

enum Shading{
	/**
	 * 实心
	 */
	SOLID,
	
	/**
	 * 点
	 */
	DOT,
	
	/**
	 * 轮廓
	 */
	LINE
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值