MoveClocks

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class MoveClocks {

static int[][] moves = new int[][] { { 1, 1, 0, 1, 1, 0, 0, 0, 0 },
        { 1, 1, 1, 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 0, 1, 1, 0, 0, 0 },
        { 1, 0, 0, 1, 0, 0, 1, 0, 0 }, { 0, 1, 0, 1, 1, 1, 0, 1, 0 },
        { 0, 0, 1, 0, 0, 1, 0, 0, 1 }, { 0, 0, 0, 1, 1, 0, 1, 1, 0 },
        { 0, 0, 0, 0, 0, 0, 1, 1, 1 }, { 0, 0, 0, 0, 1, 1, 0, 1, 1 } };
static int[] clocks = new int[9];
static int[] tmpc = new int[9];
static int count = 28;
static int tmpcot = 0;
static int[] seq = new int[9];
static int[] tmpseq = new int[9];
static boolean isAll12 = true;

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(new File("src/clock"));
    for (int i = 0; i < 9; i++) {
        clocks[i] = sc.nextInt();
    }
    MoveClock(0);
    boolean b = false;
    for (int i = 0; i < 9; i++) {
        if (seq[i] != 0) {
            for (int j = 0; j < seq[i]; j++) {
                if (b)
                    System.out.print(" ");
                b = true;
                System.out.print(i + 1);
            }
        }
    }
}

private static void MoveClock(int step) {
    // TODO Auto-generated method stub
    if (step == 9) {
        for (int i = 0; i < 9; i++) {
            tmpc[i] = clocks[i];
        }
        for (int i = 0; i < 9; i++) {
            if (tmpseq[i] != 0) {
                tmpcot += tmpseq[i];
                for (int j = 0; j < 9; j++) {
                    tmpc[j] = (tmpc[j] + tmpseq[i] * moves[i][j]) % 4;
                }
            }
        }
        for (int i = 0; i < 9; i++) {
            if (tmpc[i] != 0)
                isAll12 = false;
        }
        if (isAll12) {
            if (count > tmpcot) {
                count = tmpcot;
                for (int i = 0; i < 9; i++) {
                    seq[i] = tmpseq[i];
                }
            }
        }
        isAll12 = true;
        tmpcot = 0;
        return;
    }
    tmpseq[step] = 0;
    MoveClock(step + 1);
    tmpseq[step] = 1;
    MoveClock(step + 1);
    tmpseq[step] = 2;
    MoveClock(step + 1);
    tmpseq[step] = 3;
    MoveClock(step + 1);
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章