PE 96 Su Doku (数独)

Su Doku

Problem 96

Su Doku (Japanese meaning number place) is the name given to a popular puzzle concept. Its origin is unclear, but credit must be attributed to Leonhard Euler who invented a similar, and much more difficult, puzzle idea called Latin Squares. The objective of Su Doku puzzles, however, is to replace the blanks (or zeros) in a 9 by 9 grid in such that each row, column, and 3 by 3 box contains each of the digits 1 to 9. Below is an example of a typical starting puzzle grid and its solution grid.

0 0 3
9 0 0
0 0 1
0 2 0
3 0 5
8 0 6
6 0 0
0 0 1
4 0 0
0 0 8
7 0 0
0 0 6
1 0 2
0 0 0
7 0 8
9 0 0
0 0 8
2 0 0
0 0 2
8 0 0
0 0 5
6 0 9
2 0 3
0 1 0
5 0 0
0 0 9
3 0 0
4 8 3
9 6 7
2 5 1
9 2 1
3 4 5
8 7 6
6 5 7
8 2 1
4 9 3
5 4 8
7 2 9
1 3 6
1 3 2
5 6 4
7 9 8
9 7 6
1 3 8
2 4 5
3 7 2
8 1 4
6 9 5
6 8 9
2 5 3
4 1 7
5 1 4
7 6 9
3 8 2

A well constructed Su Doku puzzle has a unique solution and can be solved by logic, although it may be necessary to employ "guess and test" methods in order to eliminate options (there is much contested opinion over this). The complexity of the search determines the difficulty of the puzzle; the example above is considered easy because it can be solved by straight forward direct deduction.

The 6K text file, sudoku.txt (right click and 'Save Link/Target As...'), contains fifty different Su Doku puzzles ranging in difficulty, but all with unique solutions (the first puzzle in the file is the example above).

By solving all fifty puzzles find the sum of the 3-digit numbers found in the top left corner of each solution grid; for example, 483 is the 3-digit number found in the top left corner of the solution grid above.



代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Main{
	
	public static int[][] sudoku = new int[9][9];
	public static int sum = 0;
	public static ArrayList<int[]> zero;

	public static void solve()
	{
		int i = 0; 
		boolean b = false;
		while (i != zero.size())
		{
			if (b)
			{
				if (sudoku[zero.get(i)[0]][zero.get(i)[1]] == 9)
				{
					sudoku[zero.get(i)[0]][zero.get(i)[1]] = 0;
					i--;
					continue;
				}
				else sudoku[zero.get(i)[0]][zero.get(i)[1]]++;
				//i++;
			}
				
			b = false;

			while (!b && !check( zero.get(i)[0], zero.get(i)[1] ))
			{
				if (sudoku[zero.get(i)[0]][zero.get(i)[1]] < 9)
					sudoku[zero.get(i)[0]][zero.get(i)[1]]++;
				else
				{
					sudoku[zero.get(i)[0]][zero.get(i)[1]] = 0;
					b = true;
					i -= 2;
				}
			}		
			i++;
		}
	}

	public static boolean check(int i, int j)
	{
		if (sudoku[i][j] == 0) return false;
		for (int k=0; k < 9; k++) //行和列没有重复的
			if ((sudoku[i][k] == sudoku[i][j] && k != j) || (sudoku[k][j] == sudoku[i][j] && k != i)) 
				return false;
		//小正方形没有重复的
		int d = i / 3 * 3;
		int e = j / 3 * 3;
		for (int k=d; k < d+3; k++)
			for (int l=e; l < e+3; l++)
				if (sudoku[k][l] == sudoku[i][j] && k != i && l != j)
					return false;
		return true;

	}

	public static void main(String[] args)
	{
		BufferedReader br = null;
		try
		{
			String s;
			br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\workspace1\\Main\\096_sudoku.txt"));

			for (int i=1; i<=50; i++) //50个数独
			{
				zero = new ArrayList<int[]>();
				br.readLine();
				for (int j=0; j<9; j++)
				{
					s = br.readLine();
					for (int k=0; k<9; k++)
					{
						sudoku[j][k] = Character.getNumericValue(s.charAt(k));
						//如果参数 k 不在 0 与 string.length 之间,该方法将返回一个空字符串
						if (Character.getNumericValue(s.charAt(k)) == 0)
							zero.add(new int[]{j,k});
					}
				}		
				solve();	
				sum += sudoku[0][0]*100 + sudoku[0][1]*10 + sudoku[0][2];
			}
			
		   System.out.println(sum);
		  //br.close();
		} catch (IOException e){
			e.printStackTrace();
		}
   }
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值