package yukicoder; import java.util.ArrayList; import java.util.Comparator; import java.util.Scanner; public class Main{ public static void main(String[] args) { new Main().solve(); // new Q359().naive(); } void naive() { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int i = 0; i < T; i++) { int[][] table = new int[3][3]; int L = sc.nextInt(); int x1 = -1; int y1 = -1; int x2 = -1; int y2 = -1; for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { table[j][k] = sc.nextInt(); if (table[j][k] == 0) { if (x1 == -1 && y1 == -1) { x1 = k; y1 = j; } else { x2 = k; y2 = j; } } } } int sum = 0; ArrayList ans=new ArrayList<>(); for (int j = 1; j < L; j++) { int[][] d = table; d[y1][x1] = j; d[y2][x2] = L - j; if (ok(d)) { sum++; ans.add(d[y1][x1]); } } ans.sort(new Comparator(){ @Override public int compare(Integer o1, Integer o2) { return Integer.compare(o1, o2); } }); // for(int j=0;j singu = new ArrayList<>(); ArrayList checked = new ArrayList<>(); int L = sc.nextInt(); int[][] table = new int[3][3]; int x1 = -1; int y1 = -1; int x2 = -1; int y2 = -1; ArrayList cand = new ArrayList<>(); for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { table[j][k] = sc.nextInt(); if (table[j][k] == 0) { if (x1 == -1 && y1 == -1) { x1 = k; y1 = j; } else { x2 = k; y2 = j; } } else { cand.add(table[j][k]); } } } cand.add(L/2); cand.add(0); cand.add(L); for (int j = 0; j < cand.size(); j++) { singu = add_singu(cand.get(j), table, x1, y1, x2, y2, L, singu, checked); } singu.sort(new Comparator() { @Override public int compare(Singularity o1, Singularity o2) { return Integer.compare(o1.num, o2.num); } }); // for(int j=0;j c) return true; if (a > b && b < c) return true; return false; } boolean ok(int[][] table) { for (int i = 0; i < 3; i++) { if (!isKaramatsu(table[i][0], table[i][1], table[i][2])) { return false; } if (!isKaramatsu(table[0][i], table[1][i], table[2][i])) { return false; } } if (!isKaramatsu(table[0][0], table[1][1], table[2][2])) { return false; } if (!isKaramatsu(table[0][2], table[1][1], table[2][0])) { return false; } return true; } class Singularity { int num; boolean isKado; Singularity(int num, boolean isKado) { this.num = num; this.isKado = isKado; } } ArrayList add_singu(int x, int[][] table, int x1, int y1, int x2, int y2, int L, ArrayList singu, ArrayList used) { int[][] d = table; for (int l = 0; l < 3; l++) { int dd = 1; d[y1][x1] = x + (l == 0 ? dd : l == 1 ? -dd : 0); d[y2][x2] = L - d[y1][x1]; if (!used.contains(d[y1][x1])) { singu.add(new Singularity(d[y1][x1], ok(d))); used.add(d[y1][x1]); } } for (int l = 0; l < 3; l++) { int dd = 1; d[y2][x2] = x + (l == 0 ? dd : l == 1 ? -dd : 0); d[y1][x1] = L - d[y2][x2]; if (!used.contains(d[y1][x1])) { singu.add(new Singularity(d[y1][x1], ok(d))); used.add(d[y1][x1]); } } return singu; } // void showMt(int[][] table){ // for(int i=0;i<3;i++){ // for(int j=0;j<3;j++){ // System.out.print(table[i][j]+(j!=2?" ":"\n")); // } // } // } }