解题1(Suduku)
题目描述
问题描述:数独(Sudoku)是一款大众喜爱的数字逻辑游戏。玩家需要根据9X9盘面上的已知数字,推算出所有剩余空格的数字,并且满足每一行、每一列、每一个粗线宫内的数字均含1-9,并且不重复。
输入:
包含已知数字的9X9盘面数组[空缺位以数字0表示]
输出:
完整的9X9盘面数组
输入描述:
包含已知数字的9X9盘面数组[空缺位以数字0表示]
输出描述:
完整的9X9盘面数组
输入
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)的更多相关文章
- SCNU ACM 2016新生赛决赛 解题报告
新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...
- SCNU ACM 2016新生赛初赛 解题报告
新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...
- CH Round #56 - 国庆节欢乐赛解题报告
最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...
- wechall.net/stegano 解题心得
/* 转载请注明出处:http://www.cnblogs.com/Martinium/p/wechall_stegano.html */ 最近迷上了 www.wechall.net 网站,里面都是些 ...
- Mountains(CVTE面试题)解题报告
题目大意: 用一个数组代表群山的高度.高度大的地方代表山峰,小的地方代表山谷.山谷可以容水.假设有一天下了大雨,求群山中总共可以容纳多少水? 如图所示情况,a代表该数组,总共可以容纳5个水. 解题思路 ...
- timus 1180. Stone Game 解题报告
1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. The ...
- timus 1175. Strange Sequence 解题报告
1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...
随机推荐
- linux常用命令总结-updating
Linux 命令总结 启动终端: ctr+alt+t终端字体放大: ctr+shift+'+',终端字体缩小: ctr+'-'ls: 查看当前目录下的文件信息pwd: 查看目录所在的路径touch: ...
- 《算法》第五章部分程序 part 8
▶ 书中第五章部分程序,包括在加上自己补充的代码,适用于基因序列的 2-Bit 压缩算法,行程长压缩算法,Huffman 压缩算法,LZW 压缩算法 ● 适用于基因序列的 2-Bit 压缩算法 pac ...
- 配置Eclipse的Maven环境
- Jenkins 之邮件配置
Jenkins 之邮件配置其实还是有些麻烦的,坑比较多,一不小心就...我是走了很多弯路的. 这里记录下来,希望大家以后不要重蹈覆辙: 我测试过,这里的 Extended E-mail Notific ...
- jenkins部署配置
https://www.cnblogs.com/rslai/p/8135460.html 修改jenkins的默认端口号: https://blog.csdn.net/qq_32440951/arti ...
- Centos 7 下创建LVM流程
https://www.cnblogs.com/ssslinppp/p/5853312.html
- mybatis 3.2.2_环境搭建
1.创建一个工程 utf-8 2.导入jar mybatis-3.2.2.jar 核心包 依赖包: asm-3.3.1.jar cglib-2.2.2.jar commons-logging-1.1. ...
- NRF51822之使用外部32Mhz晶振
硬件平台为微雪BLE400的(将原来的16mhz晶振改为32mhz.两个旁电容改为22pf) 以nRF51_SDK_10.0.0_dc26b5e\examples\ble_peripheral\ble ...
- ScheduledThreadPoolExecutor 使用线程池执行定时任务
转自:https://segmentfault.com/a/1190000008038848 在现实世界里,我们总是免不了要定期去做一件事情(比如上课)—— 在计算机的世界里,更是如此.比如我们手机每 ...
- Mac上python2和python3的版本切换
在命令行执行 vi ~/.bash_profile 在文件下面加上: alias python2='/system/Library/Frameworks/Python.framework/Versio ...