import java.util.*; /** transformation是一个Map<Character,char[][]>,存放每一种字符对应的变换. * char[][]是一个长度为3的二维数组,char[0]是自身变换,char[1]是减一根火柴的变换,char[2]是加一根火柴对就的变换. * 比如对于'6',对应的char[][]是{ {'0','9'}, {'5'}, {'8'}},表示自身可以变为'0','9',减一根可以变为'5',加一根可一变为'5','8' * * symbolPattern<Character,char[][]>,存放每一种字符对应的火柴摆放图案. */ public class MatchMove{ public MatchMove(){ transformation.put('0',new char[][]{ {'6','9'}, null, {'8'}}); transformation.put('1',new char[][]{ null, null, {'7'}}); transformation.put('2',new char[][]{ {'3'}, null, {'6','9'}}); transformation.put('3',new char[][]{ {'2'}, null, {'9'}}); transformation.put('4',new char[][]{ null, null, null}); transformation.put('5',new char[][]{ {'3'}, null, {'6','9'}}); transformation.put('6',new char[][]{ {'0','9'}, {'5'}, {'8'}}); transformation.put('7',new char[][]{ null, {'1'}, null}); transformation.put('8',new char[][]{ null, {'0','6','9'}, null}); transformation.put('9',new char[][]{ {'0','6'}, {'3','5'}, {'8'}}); transformation.put('+',new char[][]{ null, {'-'}, null}); transformation.put('-',new char[][]{ null, null, {'+'}}); transformation.put('=',new char[][]{ null, null, null}); symbolPattern.put('0',new char[][]{{' ','-',' '}, {'|',' ','|'}, {' ',' ',' '}, {'|',' ','|'}, {' ','-',' '} }); symbolPattern.put('1',new char[][]{{' ',' ',' '}, {' ',' ','|'}, {' ',' ',' '}, {' ',' ','|'}, {' ',' ',' '} }); symbolPattern.put('2',new char[][]{{' ','-',' '}, {' ',' ','|'}, {' ','-',' '}, {'|',' ',' '}, {' ','-',' '} }); symbolPattern.put('3',new char[][]{{' ','-',' '}, {' ',' ','|'}, {' ','-',' '}, {' ',' ','|'}, {' ','-',' '} }); symbolPattern.put('4',new char[][]{{' ',' ',' '}, {'|',' ','|'}, {' ','-',' '}, {' ',' ','|'}, {' ',' ',' '} }); symbolPattern.put('5',new char[][]{{' ','-',' '}, {'|',' ',' '}, {' ','-',' '}, {' ',' ','|'}, {' ','-',' '} }); symbolPattern.put('6',new char[][]{{' ','-',' '}, {'|',' ',' '}, {' ','-',' '}, {'|',' ','|'}, {' ','-',' '} }); symbolPattern.put('7',new char[][]{{' ','-',' '}, {' ',' ','|'}, {' ',' ',' '}, {' ',' ','|'}, {' ',' ',' '} }); symbolPattern.put('8',new char[][]{{' ','-',' '}, {'|',' ','|'}, {' ','-',' '}, {'|',' ','|'}, {' ','-',' '} }); symbolPattern.put('9',new char[][]{{' ','-',' '}, {'|',' ','|'}, {' ','-',' '}, {' ',' ','|'}, {' ','-',' '} }); symbolPattern.put('-',new char[][]{{' ',' ',' '}, {' ',' ',' '}, {' ','-',' '}, {' ',' ',' '}, {' ',' ',' '} }); symbolPattern.put('+',new char[][]{{' ',' ',' '}, {' ',' ',' '}, {' ','+',' '}, {' ',' ',' '}, {' ',' ',' '} }); symbolPattern.put('=',new char[][]{{' ',' ',' '}, {' ',' ',' '}, {' ','=',' '}, {' ',' ',' '}, {' ',' ',' '} }); } /**把str表示的等式通过移动火柴,使等式成立. */ public void move(String str){ if(str==null||str.trim().length()==0){ throw new RuntimeException("字符串为空!"); } String regex="[0-9]+[+-][0-9]+=[0-9]+"; if(!str.matches(regex)){ throw new RuntimeException("给定的字符串格式不对"); } char[] chars=str.toCharArray(); if(isEquation(chars)){ System.out.println("所给式子本身就是等式"); } for(int i=0;i<chars.length;i++){ char c=chars[i]; // char[][] tran=transformation.get(c); //取出关于c字符的所有变换方案. if(tran[0]!=null){ //字符本身的自变换. for(char temp : tran[0]){ chars[i]=temp; //自变换的结果替换掉原来的字符. if(isEquation(chars)){ System.out.println("结果:"); printPattern(chars); } chars[i]=c; //复原回到原来的状态. } } if(tran[1]!=null){ //减一根火柴的变换. for(char temp :tran[1]){ chars[i]=temp; //减一根火柴后的变换结果替换掉原来的字符. //下面的循环试着把火柴加到每一个除chars[i]之外的字符之上,看等式成立否. for(int j=0;j<chars.length;j++){ if(i==j){ continue; } char c2=chars[j]; char[][] tran2=transformation.get(c2); if(tran2[2]!=null){ for(char temp2 : tran2[2]){ chars[j]=temp2; //chars[j]加一根火柴. if(isEquation(chars)){ System.out.println("结果"); printPattern(chars); } chars[j]=c2; //复原chars[j]. } } } chars[i]=c; //复原chars[i]. } } } return; } //测试: public static void main(String[] args){ MatchMove mm=new MatchMove(); System.out.println("测试一"); System.out.println("原等式如下:"); mm.printPattern("3+6=3"); mm.move("3+6=3"); System.out.println("测试二"); System.out.println("原等式如下:"); mm.printPattern("12+3=5"); mm.move("12+3=5"); System.out.println("测试三"); System.out.println("原等式如下:"); mm.printPattern("90+12=69"); mm.move("90+12=69"); } public void printPattern(String str){ printPattern(str.toCharArray()); } private Map<Character,char[][]> transformation=new HashMap<Character,char[][]>(); private Map<Character,char[][]> symbolPattern=new HashMap<Character,char[][]>(); /**判断equation字符数组中存放的等式能不能成立. */ private boolean isEquation(char[] equation){ int num1=0; int num2=0; int num3=0; char operator; int index=0; for(;Character.isDigit(equation[index]);index++){ num1=num1*10+equation[index]-'0'; } operator=equation[index++]; for(;Character.isDigit(equation[index]);index++){ num2=num2*10+equation[index]-'0'; } index++; for(;index<equation.length;index++){ num3=num3*10+equation[index]-'0'; } //System.out.println(num1+" "+operator+num2+"="+num3); if(operator=='-'){ return num1-num2==num3; }else{ return num1+num2==num3; } } /**打印cs中存放的等式 */ private void printPattern(char[] cs){ for(int i=0;i<5;i++){ for(int j=0;j<cs.length;j++){ char[][] temp=symbolPattern.get(cs[j]); for(int k=0;k<temp[i].length;k++){ System.out.print(temp[i][k]); } } System.out.println(); } } }