Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KazumasaSUGAYA/3930719 to your computer and use it in GitHub Desktop.
Save KazumasaSUGAYA/3930719 to your computer and use it in GitHub Desktop.
数独を解くプログラムの作成
【アルゴリズム】
0. 読み込む。
1. 1つの空マスに対して1~9の値候補を用意する。
2. 横(行)を見て、並んでいる値をそのマスの候補から除く。
3. 縦(列)を見て、並んでいる値をそのマスの候補から除く。
4. ブロック(3×3)を見て、並んでいる値をそのマスの候補から除く。
5. 残った候補をマスに入れる。
6. 0.から5.を繰り返す。
import java.util.ArrayList;
public class Suudoku {
static int[][] question = {
{0, 0, 0, 2, 0, 7, 0, 4, 1},
{0, 0, 3, 0, 0, 0, 0, 0, 9},
{0, 7, 9, 0, 0, 8, 6, 0, 0},
{6, 0, 0, 7, 0, 0, 2, 0, 0},
{0, 0, 0, 0, 6, 0, 0, 0, 0},
{0, 0, 4, 0, 0, 5, 0, 0, 3},
{0, 0, 1, 6, 0, 0, 5, 9, 0},
{2, 0, 0, 0, 0, 0, 7, 0, 0},
{5, 8, 0, 0, 0, 4, 0, 0, 6},
};
public static void main(String[] args){
Suudoku suudoku = new Suudoku();
if(!suudoku.solve(question)){
System.out.println("解けません");
}else{
System.out.println("解けました");
suudoku.printResult();
}
}
private boolean solve(int[][] question2) {
int row = 9;
int col = 9;
int refCont = 0;
ArrayList[][] kouhoList;
//値のあるセルの数を返却する
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(question2[i][j] != 0){
refCont++;
}
}
}
System.out.println("値のあるセルの数 : " + refCont);
return false;
}
private void printResult() {
// TODO Auto-generated method stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment