poj2676



package work;

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

public class A2676 {
 static int T;
 static int data[][];
 static boolean row[][];
 static boolean col[][];
 static boolean grid[][];
 static boolean flag = false;
 public static void main(String[] args) throws FileNotFoundException {
  /* Scanner sc=new Scanner(System.in); */
  Scanner sc = new Scanner(new File("src/2676.txt"));
  T = sc.nextInt();
  data = new int[9][9];
  row = new boolean[9][11];
  col = new boolean[9][10];
  grid = new boolean[9][10];
  int s;
  for (int t = 0; t < T; t++) {
   /*
    * for (int i = 0; i < 9; i++) { int a = sc.nextInt(); for (int j =
    * 8; j > 0; j--) { s = a % 10; a = a / 10; data[i][j] = s;
    * row[i][data[i][j]] = 1; col[j][data[i][j]] = 1; grid[i / 3 * 3 +
    * j / 3][data[i][j]] = 1; } }
    */
   sc.nextLine();
   for (int i = 0; i < 9; i++) {
    char[] line = sc.nextLine().toCharArray();
    for (int j = 0; j < line.length; j++) {
     data[i][j] = line[j] - 48;
     row[i][data[i][j]] = true;
     col[j][data[i][j]] = true;
     grid[i / 3 * 3 + j / 3][data[i][j]] = true;
    }
   }
   dfs(0);
   if (flag) {
    for (int i = 0; i < 9; i++) {
     for (int j = 0; j < 9; j++) {
      System.out.print(data[i][j]);
     }
     System.out.println();
    }
   }
  }
 }
 private static void dfs(int step) {
  if (step == 81) {
   flag = true;
   return;
  }
  int i = step / 9;
  int j = step % 9;
  if (data[i][j] == 0) {
   for (int a = 1; a <= 9; a++) {
    if ((row[i][a]) && (col[j][a])
      && (grid[i / 3 * 3 + j / 3][a])) {continue;}
    if ((!row[i][a]) && (!col[j][a])
      && (!grid[i / 3 * 3 + j / 3][a])) {
     data[i][j] = a;
     row[i][a] = true;
     col[j][a] = true;
     grid[i / 3 * 3 + j / 3][a] = true;
     dfs(step + 1);
     if (flag)
      break;
     data[i][j] = 0;
     row[i][a] = false;
     col[j][a] = false;
     grid[i / 3 * 3 + j / 3][a] = false;
    }
   }
  } else {
   dfs(step + 1);
  }
 }
 /*
  * private static boolean isafe(int p) { for (int i = 0; i < 9; i++) { for
  * (int j = 0; j < 9; j++) { if (row[i][p] == 1 || col[j][p] == 1 || grid[i
  * / 3 * 3 + j / 3][p] == 1) { return false; } return true; } } return
  * false; }
  */
}

//

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

//

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值