博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sudoku Solver
阅读量:4073 次
发布时间:2019-05-25

本文共 1613 字,大约阅读时间需要 5 分钟。

Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.


A sudoku puzzle...


...and its solution numbers marked in red.

Java代码:

public class Solution {boolean[][] row = new boolean[9][9];boolean[][] col = new boolean[9][9];boolean[][] blo = new boolean[9][9];public void solveSudoku(char[][] board) {    for(int i=0;i<9;i++)        for(int j =0;j<9;j++){            int temp =board[i][j]-'1';            if(board[i][j]!='.'){                row[i][temp]=true;                col[j][temp]=true;                blo[3*(i/3)+j/3][temp]=true;            }        }    solveHelper(0,0,board);}public boolean solveHelper(int i, int j, char[][] board){    if(i==9)        return true;    if(board[i][j]!='.'){        if(j<8)            return solveHelper(i,j+1,board);        else             return solveHelper(i+1,0,board);    }else for(int k =0;k<9;k++){            if(row[i][k]==false&&col[j][k]==false&&blo[3*(i/3)+j/3][k]==false){                board[i][j] = (char)(k+'0'+1);                row[i][k]=true;                col[j][k]=true;                blo[3*(i/3)+j/3][k]=true;                if(j<8&&solveHelper(i,j+1,board))                    return true;                else if(j==8&&solveHelper(i+1,0,board))                    return true;                else{                    board[i][j] = '.';                    row[i][k]=false;                    col[j][k]=false;                    blo[3*(i/3)+j/3][k]=false;                }            }        }    return false;}}

转载地址:http://nvuni.baihongyu.com/

你可能感兴趣的文章
Unity新功能|全息模拟器
查看>>
[Unity3D]深度相机 Depth Camera
查看>>
发布和运行HOLOLENS程序注意这里要勾上,不然就成普通的UWP程序了!
查看>>
Hololens入门之语音识别(语音命令)
查看>>
[自学总结] Unity5.3 API 之 Audio Mixer
查看>>
UNITY 之FixedUpdate
查看>>
HOLOLENS程序发布,这个界面调用的图片
查看>>
看出在玩啥了吗?想不想体验下
查看>>
Time.deltaTime 的平均值在0.1-0.2左右
查看>>
向量加减法运算及其几何意义
查看>>
magnitude是精确距离,sqrMagnitude是节省CPU的粗略距离,精度有损失
查看>>
学习和研究下unity3d的四元数 Quaternion
查看>>
一些最最基本的几何图形公式
查看>>
Non-convex MeshCollider with non-kinematic Rigidbody is no longer supported in Unity 5.
查看>>
理解四元数的一些文章
查看>>
Unity Shader入门
查看>>
片元着色器(Fragment Shader)被称为像素着色器(Pixel Shader),但
查看>>
UNITY自带的3D object没有三角形?
查看>>
Lambert(朗伯)光照模型 和Half Lambert的区别
查看>>
float4数据类型
查看>>