算法题:数独验证

背景

XX学校风靡一款智力游戏,也就是数独(九宫格),先给你一个数独,并需要你验证是否符合规则。

描述

具体规则如下:
每一行都用到1,2,3,4,5,6,7,8,9,位置不限,
每一列都用到1,2,3,4,5,6,7,8,9,位置不限,
每3×3的格子(共九个这样的格子)都用到1,2,3,4,5,6,7,8,9,位置不限,
游戏的过程就是用1,2,3,4,5,6,7,8,9填充空白,并要求满足每行、每列、每个九宫格都用到1,2,3,4,5,6,7,8,9。

如下是一个正确的数独:
5 8 1 4 9 3 7 6 2
9 6 3 7 1 2 5 8 4
2 7 4 8 6 5 9 3 1
1 2 9 5 4 6 3 7 8
4 3 6 1 8 7 2 9 5
7 5 8 3 2 9 1 4 6
8 9 2 6 7 1 4 5 3
6 1 5 9 3 4 8 2 7
3 4 7 2 5 8 6 1 9

格式

输入格式

输入n个数独,你来验证它是否违反规则.
第一行为数独个数,第二行开始为第一个数独,之后为第二个,至第n个.
注意!每个数独之间有一个回车隔开!

输出格式

若正确则输出”Right”若不正确则输出”Wrong” 输出一个换一行

样例1

样例输入1

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

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

样例输出1

Right
Wrong

限制

各个测试点1s

提示

1<=n<=20 (输入的数独个数)

不论输入的数独是错误的还是正确的,数据都保证每个数在1-9之间,即只会出现因为有相同的数而导致违反规则,而不会因为数字超出了1-9的范围而违反规则.

我的答案:

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

class Main
{
    public static void main(String[] argv)
    {
        try {
            InputStreamReader inputStream = new InputStreamReader(System.in);
            BufferedReader buffer = new BufferedReader(inputStream);

            ArrayList<String> result = new ArrayList<String>();
            int number = Integer.parseInt(buffer.readLine());
            newsudoku: for (int i = 1; i <= number; i++) {
                if (i > 1) {
                    buffer.readLine();
                }
                int[][] sudoku = new int[9][9];
                for (int line = 1; line <= 9; line++) {
                    String[] intStr = buffer.readLine().split(" ");
                    for (int column = 1; column <= 9; column++) {
                        sudoku[line - 1][column - 1] = Integer.parseInt(intStr[column - 1]);
                    }
                }
                //verify line
                for (int line = 0; line < 9; line++) {
                    int[] arrLine = new int[9];
                    for (int column = 0; column < 9; column++) {
                        if (arrLine[sudoku[line][column] - 1] > 0) {
                            result.add("Wrong");
                            continue newsudoku;
                        } else {
                            arrLine[sudoku[line][column] - 1] = 1;
                        }
                    }
                }
                //verify column
                for (int column = 0; column < 9; column++) {
                    int[] arrCol = new int[9];
                    for (int line = 0; line < 9; line++) {
                        if (arrCol[sudoku[line][column] - 1] > 0) {
                            result.add("Wrong");
                            continue newsudoku;
                        } else {
                            arrCol[sudoku[line][column] - 1] = 1;
                        }
                    }
                }
                //verify square
                for (int line = 0; line < 9; line += 3) {
                    for (int column = 0; column < 9; column += 3) {
                        int[] arrSquare = new int[9];
                        for (int squareLine = line; squareLine < line + 3; squareLine++) {
                            for (int squareCol = column; squareCol < column + 3; squareCol++) {
                                if (arrSquare[sudoku[squareLine][squareCol] - 1] > 0) {
                                    result.add("Wrong");
                                    continue newsudoku;
                                } else {
                                    arrSquare[sudoku[squareLine][squareCol] - 1] = 1;
                                }
                            }
                        }
                    }
                }
                result.add("Right");
            }
            for (String str:result) {
                System.out.println(str);
            }
        } catch (Exception ex) {
            System.out.println("Error:" + ex.getMessage());
        }
    }
}


猜你喜欢

转载自blog.csdn.net/loophome/article/details/79240009