穷举法破解集合小游戏~
生活随笔
收集整理的這篇文章主要介紹了
穷举法破解集合小游戏~
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
游戲網站: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 }
?
總結
以上是生活随笔為你收集整理的穷举法破解集合小游戏~的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何阅读一本书~阅读的层次
- 下一篇: linux 计算集群搭建,使用cento