解题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 ...
随机推荐
- 在虚拟机中安装ubuntu
初始安装: 1.安装新虚拟机时,选择稍后安装操作系统,这可以自己设置语言等信息 2.修改自定义硬件:为网卡生成一个mac地址,(这里需要注意,有时网卡会冲突,导致连接时好时坏,以后可以删除掉网卡,重新 ...
- 5.分析mitmproxy的优势及工具mitmdump的使用(参照书籍)
mitmproxy优势:功能和charles.fiddler相似,强大之处在于它的工具 mitmdump 可以直接对接python 对请求做处理. mitmdump使用 : 1.mitmdump是mi ...
- 《算法》第三章部分程序 part 6
▶ 书中第三章部分程序,加上自己补充的代码,包含双向索引表.文建索引.稀疏向量类型 ● 双向索引表 package package01; import edu.princeton.cs.algs4.S ...
- Xcode OpenGL ES Frame Capture的使用
一.使用背景 近期在Xcode中使用OpenGL ES 2.0实现一些效果,刚开始存在一些性能问题(CPU和GPU),幸运的是Xcode中自带了免费的性能工具Instruments,其中包含OpenG ...
- sparksql进阶
scala> val df=spark.read.json("/tmp/pdf1json")df: org.apache.spark.sql.DataFrame = [age ...
- 8. Object转Map,Map转Object
法一:使用reflect进行转换 public static Object mapToObject(Map<String, Object> map, Class<?> bean ...
- 13. 字符串转为json对象或json数组
##########1.json字符串转json数组########### var str="[{name:'zhangsan',age:'24'},{name:'lisi',age:'30 ...
- CoordinateLayout简介
CoordinateLayout简介 参考:CoordinatorLayout CoordinatorLayout is a super-powered FrameLayout. Coordinato ...
- Retrofit简介与使用方法(翻译)
简介 Retrofit 是一个Square开发的类型安全的REST安卓客户端请求库.这个库为网络认证.API请求以及用OkHttp发送网络请求提供了强大的框架.Retrofit库让从web api下载 ...
- vue-router 动态导航 router-link :to属性
经常碰到这类需求,从后台获取数据后再前程连接,参数id动态获取 <el-row v-for="item in Travels"> <el-col :span=&q ...