解题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 ...
随机推荐
- MySql 索引 查询 优化
官方文档: https://dev.mysql.com/doc/refman/5.7/en/explain-output.html#explain_rows type: 连接类型 system 表只有 ...
- 【HQL】常用函数
CONCAT_WS(separator, str1, str2,...) 多列转1列,以分割符分割 使用场景: 1.多列在一列显示: 2.多列转多行作为辅助,结合split和explode使用 SEL ...
- SAPCertifiedTechnologyAssociate-SystemAdministration(SAPHANAasaDatabase)
C_TADM55_75 SAP Certified Technology Associate - System Administration (SAP HANA as a Database) with ...
- 代码:PC HTML——图片列表
图片列表: 2016-6-12 可作为图片列表的规范性写法.这个例子只处理了单行的模式.( 一行多列 ) <link href="css/common.css" rel=&q ...
- 58.纯 CSS 创作一只卡通鹦鹉
原文地址:https://segmentfault.com/a/1190000015339977 优化后效果地址:https://scrimba.com/c/c97Z2vuD 感想:消除了图片外的:h ...
- log4js_Node.js中的日志管理模块使用
{ "appenders": [ // 下面一行应该是用于跟express配合输出web请求url日志的 {"type": "console" ...
- oracle中查询表是否存在
select count(*) from user_tables where table_name='表名' 或者 select 1 from user_tables where table_name ...
- 【spring-boot】spring-boot 整合 ehcache 实现缓存机制
方式一:老 不推荐 参考:https://www.cnblogs.com/lic309/p/4072848.html /*************************第一种 引入 ehcach ...
- 利用strstr和sscanf解析GPS信息
比如说我们要做一个GPS导航的项目,需要读取GPS模块以ASCII码的形式发送过来的数据,然后对这些数据进行处理,提取我们需要的信息.这就涉及到很多操作字符串的问题.下面就以此为例,利用strstr函 ...
- usb之python(pyusb)
电脑系统为WIN7 64位 python:为python3.6 32位 需要插件PyUSB-1.0.0.tar,pywinusb-0.4.2. 按照的步骤我偷懒了,自己百度一下. 我们先看设备管理的 ...