游戏网站: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
}