题目描述

问题描述:数独(Sudoku)是一款大众喜爱的数字逻辑游戏。玩家需要根据9X9盘面上的已知数字,推算出所有剩余空格的数字,并且满足每一行、每一列、每一个粗线宫内的数字均含1-9,并且不重复。
输入:
包含已知数字的9X9盘面数组[空缺位以数字0表示]
输出:
完整的9X9盘面数组

输入描述:

包含已知数字的9X9盘面数组[空缺位以数字0表示]

输出描述:

完整的9X9盘面数组

示例1

输入

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

输出

5 9 2 4 8 1 7 6 3
4 1 3 7 6 2 9 8 5
8 6 7 3 5 9 4 1 2
6 2 4 1 9 5 3 7 8
7 5 9 8 4 3 1 2 6
1 3 8 6 2 7 5 9 4
2 7 1 5 3 8 6 4 9
3 8 6 9 1 4 2 5 7
9 4 5 2 7 6 8 3 1 代码如下:
 package com.yaode; 

 import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Suduku { public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[][]=new int[9][9];
Scanner scanner=new Scanner(System.in);
List<Point>pointList=new ArrayList<Point>();
for (int i = 0; i < arr.length; i++) { String[] temp=scanner.nextLine().split(" ");
for (int j = 0; j < arr.length; j++) {
arr[i][j]=Integer.parseInt(temp[j]);
if(arr[i][j]==0){
pointList.add(new Point(i, j));
}
}
}
scanner.close(); int empty=pointList.size();
List<Integer> resultList=new ArrayList<>();//对暂时找到的合适的值进行记录
int value=1;
while (true) {
if (resultList.size()==empty) {//找到所有合适值时退出
break;
}
for (; value < 10; value++) {
if (valid(arr, pointList.get(resultList.size()).row,
pointList.get(resultList.size()).col, value)) {
resultList.add(value);//记录暂时找到的合适的值
//对暂时找到的值放入arr(找后面的值验证时要用到)
arr[pointList.get(resultList.size()-1).row][pointList.get(resultList.size()-1).col]=value;
break;//结束对目前空格的查找
} }
if (value==10) {//如果目前空格没找到合适的值
//将目前空格的前一个空格的值致零(要从新查找合适的值)
arr[pointList.get(resultList.size()-1).row][pointList.get(resultList.size()-1).col]=0;
//在记录resultList中删除目前空格的前一个记录,后面查找时从记录值加一(+1)开始查找
value=resultList.remove(resultList.size()-1)+1;
}else {
value=1;//从1开始查找下一个空格的合适值
}
} for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length-1; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println(arr[i][8]);
} } private static boolean valid(int[][] arr,int row,int col,int value) {//验证value是否合适
for (int i = 0; i < arr.length; i++) {//行
if(arr[row][i]==value){
return false;
}
} for (int i = 0; i < arr.length; i++) {//列
if (arr[i][col]==value) {
return false;
}
} int startRow=row/3*3;
int endRow=startRow+2;
int startCol=col/3*3;
int endCol=startCol+2; for (int i = startRow; i <= endRow; i++) {//所在小宫格
for (int j = startCol; j <= endCol; j++) {
if (arr[i][j]==value) {
return false;
}
}
} return true; } } class Point{
int row;
int col;
public Point(int row,int col) {
// TODO Auto-generated constructor stub
this.row=row;
this.col=col;
}
}

解题1(Suduku)的更多相关文章

  1. SCNU ACM 2016新生赛决赛 解题报告

    新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...

  2. SCNU ACM 2016新生赛初赛 解题报告

    新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...

  3. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  4. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  5. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  6. wechall.net/stegano 解题心得

    /* 转载请注明出处:http://www.cnblogs.com/Martinium/p/wechall_stegano.html */ 最近迷上了 www.wechall.net 网站,里面都是些 ...

  7. Mountains(CVTE面试题)解题报告

    题目大意: 用一个数组代表群山的高度.高度大的地方代表山峰,小的地方代表山谷.山谷可以容水.假设有一天下了大雨,求群山中总共可以容纳多少水? 如图所示情况,a代表该数组,总共可以容纳5个水. 解题思路 ...

  8. timus 1180. Stone Game 解题报告

    1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. The ...

  9. timus 1175. Strange Sequence 解题报告

    1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...

随机推荐

  1. 1.js简介

    1.JavaScript 诞生于1995年,起初主要用于处理网页中的前端验证. 2.前端验证:指检查用户输入的内容是否符合一定规则. 3.JavaScript 由网景公司发明. 4.JS 的标准命名为 ...

  2. <基础> PHP 进阶之 流程控制(Process)

    do-while $sum = 0; $i = 10; do{ $sum += $i; $i--; }while($i > 0); //当这里的值为 false 时跳出循环 echo $sum; ...

  3. Mysql 获取表属性

    获取表字段信息: select column_name from information_schema.COLUMNS where table_name='表名' nformation_schema. ...

  4. hasattr getattr setattr delattr --> (反射)

    class Room: def __init__(self,name): self.name = name def big_room(self): print('bigroot') R = Room( ...

  5. Django笔记(2)Json字段处理

    1) Django里面让Model用于JSON字段,添加一个JSONField自动类型如下: [python] view plain copy class JSONField(models.TextF ...

  6. 软件工程第三个程序:“WC项目” —— 文件信息统计(Word Count ) 命令行程序

    软件工程第三个程序:“WC项目” —— 文件信息统计(Word Count ) 命令行程序 格式:wc.exe [parameter][filename] 在[parameter]中,用户通过输入参数 ...

  7. Excel快捷生成想要的xml或text

    1.新建一个xml格式的A.xml文本,里面有4条以上的内容,注意哦,里面的元素与Excel里面的标题列数是一直的,不然会少了对应的某列数据.如: 2.打开Excel——>新建一个sheetl— ...

  8. (转载)Android下Affinities和Task

    源文链接:http://appmem.com/archives/405 1.Activity和Task task就好像是能包含很多activity的栈. 默认情况下,一个activity启动另外一个a ...

  9. mysql每天凌晨0点准时启动taskeng.exe如何关闭

    MySQL弹出一个taskeng.exe. 内容如下:=====================Start Initialization====================mysql Instal ...

  10. C++11并发之std::thread<转>

    最近技术上没什么大的收获,也是悲催的路过~ 搞一点新东西压压惊吧! C++11并发之std::thread 知识链接: C++11 并发之std::mutex C++11 并发之std::atomic ...