解题思路:
注意事项:
参考代码:
import java.util.Scanner; public class Main { public static int cnt, n, time; public static int [] arr = new int [10]; public static int [] brr = new int [10]; public static int [] crr = new int [10]; public static int [] vis = new int [10]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); for(int i = 1; i <= 9; i++){ arr[i] = sc.nextInt(); if(arr[i]!=0){ cnt++; } } DFS(1); if(time == 1){ System.out.println(crr[1]+" "+crr[2]+" "+crr[3]); System.out.println(crr[4]+" "+crr[5]+" "+crr[6]); System.out.println(crr[7]+" "+crr[8]+" "+crr[9]); }else{ System.out.println("Too Many"); } } public static void DFS(int step){ if(step == 10){ int count = 0; for(int i = 1; i <= 9; i++){ if(arr[i] == brr[i]){ count++; } } if(count == cnt){ int a = brr[1] + brr[2] + brr[3]; int b = brr[4] + brr[5] + brr[6]; int c = brr[7] + brr[8] + brr[9]; int d = brr[1] + brr[4] + brr[7]; int e = brr[2] + brr[5] + brr[8]; int f = brr[3] + brr[6] + brr[9]; int g = brr[1] + brr[5] + brr[9]; int h = brr[3] + brr[5] + brr[7]; if(a==b&&b==c&&c==d&&d==e&&e==f&&f==g&&g==h){ time++; if(time == 1){ for(int i = 1; i <= 9; i++){ crr[i] = brr[i]; } } } } }else{ for(int i = 1; i <= 9; i++){ if(vis[i] == 0){ vis[i] = 1; brr[step] = i; DFS(step + 1); vis[i] = 0; } } } } }
0.0分
3 人评分