Word2ANY转换工具

Word2ANY.vbs

Option Explicit

Word2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.docx","PATH_TO_OUTFILE\NEOHOPE.COM.OUT.xps","XPS"
Word2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.docx","PATH_TO_OUTFILE\NEOHOPE.COM.OUT.PDF","PDF"
Word2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.docx","PATH_TO_OUTFILE\NEOHOPE.COM.OUT.HTML","HTML"
Word2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.docx","PATH_TO_OUTFILE\NEOHOPE.COM.OUT.XML","XML"
Word2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.docx","PATH_TO_OUTFILE\NEOHOPE.COM.OUT.RTF","RTF"
Word2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.docx","PATH_TO_OUTFILE\NEOHOPE.COM.OUT.TXT","TEXT"

Sub Word2ANY( inFile, outFile, outFormat)
	Dim objFSO, objWord, objDoc, wdFormat

	Const wdFormatDocument                    =  0
	Const wdFormatDocument97                  =  0
	Const wdFormatDocumentDefault             = 16
	Const wdFormatDOSText                     =  4
	Const wdFormatDOSTextLineBreaks           =  5
	Const wdFormatEncodedText                 =  7
	Const wdFormatFilteredHTML                = 10
	Const wdFormatFlatXML                     = 19
	Const wdFormatFlatXMLMacroEnabled         = 20
	Const wdFormatFlatXMLTemplate             = 21
	Const wdFormatFlatXMLTemplateMacroEnabled = 22
	Const wdFormatHTML                        =  8
	Const wdFormatPDF                         = 17
	Const wdFormatRTF                         =  6
	Const wdFormatTemplate                    =  1
	Const wdFormatTemplate97                  =  1
	Const wdFormatText                        =  2
	Const wdFormatTextLineBreaks              =  3
	Const wdFormatUnicodeText                 =  7
	Const wdFormatWebArchive                  =  9
	Const wdFormatXML                         = 11
	Const wdFormatXMLDocument                 = 12
	Const wdFormatXMLDocumentMacroEnabled     = 13
	Const wdFormatXMLTemplate                 = 14
	Const wdFormatXMLTemplateMacroEnabled     = 15
	Const wdFormatXPS                         = 18

	' Create a File System object
	Set objFSO = CreateObject( "Scripting.FileSystemObject" )

	' Create a Word object
	Set objWord = CreateObject( "Word.Application" )

	With objWord
		' True: make Word visible; False: invisible
		.Visible = True
 
		' Check if the Word document exists
		If not( objFSO.FileExists( inFile ) ) Then
			WScript.Echo "FILE OPEN ERROR: The file does not exist" & vbCrLf
			' Close Word
			.Quit
			Exit Sub
		End If
 
		' Open the Word document
		.Documents.Open inFile
 
		' Make the opened file the active document
		Set objDoc = .ActiveDocument
 
		If StrComp(Ucase( outFormat ),"PDF") = 0 then
			wdFormat = wdFormatPDF 
		ElseIf StrComp(Ucase( outFormat ),"XPS") = 0 then
			wdFormat = wdFormatXPS
		ElseIf StrComp(Ucase( outFormat ),"TXT") = 0 then
			wdFormat= wdFormatTEXT
		ElseIf StrComp(Ucase( outFormat ),"HTML") = 0 then
			wdFormat= wdFormatHTML
		ElseIf StrComp(Ucase( outFormat ),"XML") = 0 then
			wdFormat= wdFormatXML
		ElseIf StrComp(Ucase( outFormat ),"RTF") = 0 then
			wdFormat= wdFormatXML
		Else
			WScript.Echo "FILE FORTMART ERROR: Unknown file format" & vbCrLf
			' Close Word
			.Quit
			Exit Sub
		End If

		' Save in PDF/XPS format
		objDoc.SaveAs outFile, wdFormat
 
		' Close the active document
		objDoc.Close
 
		' Close Word
		.Quit
	End With
End Sub

CMD常用命令17常用软件注册为Widows服务

1、Apache注册为Widows服务

httpd -k install

2、MySQL注册为Widows服务

mysqld --install MySQL --defaults-file="D:\MySQL\MySQL Server 5.1\my.ini"

3、PostgreSQL注册为Widows服务

pg_ctl.exe register -N "postgresql-8.4" -D "D:/PostgreSQL/8.4/data" -w

4、SVN注册为Widows服务

sc create svnserve binPath= "\"D:\Subversion\bin\svnserve.exe\" --service -r \"D:\Subversion\repository\"" displayname= "Subversion Service" depend= Tcpip start= auto 
sc start svnserve 
sc stop svnserve 
sc delete svnserve

5、redis注册为Widows服务

#loglevel 分为debug, notice, warning三级
redis-server.exe --service-install D:\Database\Redis2.8\db\redis.windows.conf --loglevel notice
redis-server --service-start
redis-server --service-stop
redis-server --service-uninstall

6、mongodb注册为Widows服务

mongod --dbpath=D:\Database\MongoDB3\db --logpath=D:\Database\MongoDB3\log\mongo.log --port 27027 --noauth --install -serviceName MongoDB01 --serviceDisplayName MongoDB01 
net start MongoDB01

MongoDB副本集(Java)

还是蛮简单的,驱动把任务全部做掉了

package com.djhu.mongodb.test;

import java.util.Arrays;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class ReplTest
{
	private static void testInsert() 
	{
		MongoClient mongoClient = new MongoClient(Arrays.asList(
				   new ServerAddress("172.16.172.4", 27017),
				   new ServerAddress("172.16.172.4", 27018),
				   new ServerAddress("172.16.172.4", 27019)));

		MongoDatabase db = mongoClient.getDatabase("test");
		MongoCollection collection = db.getCollection("person");
		
		Document doc = new Document();
		doc.put("name", "tuzi");
		doc.put("age", 27);
		doc.put("sex", "Female");
		collection.insertOne(doc);
	}

	public static void main(String[] args)
	{
		testInsert();
	}
}

如果遇到下面的错误,是因为用了localhost作为replSet的地址,重新config一下就好了

Caused by: java.net.ConnectException: Connection refused: connect
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
	at java.net.Socket.connect(Socket.java:519)
	at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
	at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
	... 3 more
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches PrimaryServerSelector. Client view of cluster state is {type=REPLICA_SET, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}, {address=localhost:27018, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}, {address=localhost:27019, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}, {address=localhost:27020, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]
	at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
	at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
	at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
	at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
	at com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68)
	at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:175)
	at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:141)
	at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:72)
	at com.mongodb.Mongo.execute(Mongo.java:747)
	at com.mongodb.Mongo$2.execute(Mongo.java:730)
	at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:482)
	at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:277)
	at com.djhu.mongodb.test.ReplTest.testInsert(ReplTest.java:28)
	at com.djhu.mongodb.test.ReplTest.main(ReplTest.java:33)

Redis分片(Jedis)

Redis的分片技术一般是通过客户端或代理来实现的

1、用jedis实现分片的时候,服务端不需要做任何配置即可

package com.djhu.redis.test;

import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;

public class JedisShardTest
{
	public static void main(String[] args)
	{
		List<JedisShardInfo> jedisShardInfoList = new ArrayList<JedisShardInfo>();
		jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6379));
		jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6380));

		ShardedJedis sharded = new ShardedJedis(jedisShardInfoList);
		sharded.set("key01", "a");
		sharded.set("key02", "b");
		sharded.set("key03", "c");
		sharded.set("key04", "d");
		sharded.set("key05", "e");
		
		System.out.println(sharded.get("key03"));
	}
}

2、用Jedis连接池实现分片

package com.djhu.redis.test;

import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.util.Hashing;
import redis.clients.util.Sharded;

public class JedisSharedFactory
{
	// 最大可用连接数,默认值为8,如果赋值为-1则表示不限制
	private static int MAX_TOTAL = 256;
	// 最大空闲连接数,默认值为8
	private static int MAX_IDLE = 32;
	// 最小空闲连接数
	private static int MIN_IDLE = 4;
	// 最大等待连接毫秒数,默认值为-1表示永不超时
	private static int MAX_WAIT = 3000;
	// 连接redis超时时间
	private static int TIMEOUT = 3000;
	// true表示验证连接
	private static boolean TEST_ON_BORROW = true;

	//连接池
	private static ShardedJedisPool jedisPool = null;
	public static void initJedisPool()
	{
		try
		{
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxTotal(MAX_TOTAL);
			config.setMaxIdle(MAX_IDLE);
			config.setMinIdle(MIN_IDLE);
			config.setMaxWaitMillis(MAX_WAIT);
			config.setTestOnBorrow(TEST_ON_BORROW);
			
			List<JedisShardInfo> jedisShardInfoList = new ArrayList<JedisShardInfo>();
			jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6379));
			jedisShardInfoList.add(new JedisShardInfo("172.16.172.4", 6380));
			jedisPool = new ShardedJedisPool(config, jedisShardInfoList,Hashing.MURMUR_HASH,Sharded.DEFAULT_KEY_TAG_PATTERN);
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public synchronized static ShardedJedis getConnection()
	{
		try
		{
			if (jedisPool != null)
			{
				ShardedJedis resource = jedisPool.getResource();
				return resource;
			} else
			{
				return null;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static void returnResource(final ShardedJedis jedis)
	{
		if (jedis != null)
		{
			jedis.close();
		}
	}
	
	public static void main(String[] args)
	{
		initJedisPool();
		ShardedJedis redis = getConnection();
		redis.set("key10", "j");
		redis.set("key11", "k");
		redis.set("key12", "l");
		redis.set("key13", "m");
		redis.set("key14", "n");
		
		System.out.print(redis.get("key12"));
		
		returnResource(redis);
	}
}

Jedis连接Redis3 Cluster

1、源码如下

package com.djhu.redis.test;

import java.util.Set;
import java.util.HashSet;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class JedisClusterTest
{
	public static void main(String[] args)
	{
		Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 6379));  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 6380));  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 6381));  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 6382));  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 6383));  
        jedisClusterNodes.add(new HostAndPort("172.16.172.4", 7384));  
        
		//JedisCluster cluster = new JedisCluster(jedisClusterNodes,3000,1000);
        JedisCluster cluster = new JedisCluster(jedisClusterNodes);
		cluster.set("key10", "j");
		cluster.set("key11", "k");
		cluster.set("key12", "l");
		cluster.set("key13", "m");
		cluster.set("key14", "n");
		
		System.out.println(cluster.get("key12"));
		
	}
}

2、如果遇到下面错误,主要是因为建立cluster时,ip用了127.0.0.1。用其他ip重建一下cluster,就可以解决了。

Exception in thread "main" redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections?
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:34)
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:68)
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:85)
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:68)
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:85)
	at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:68)
	at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:29)
	at redis.clients.jedis.JedisCluster.set(JedisCluster.java:75)

Hadoop增删改查(Java)

需要的jar包在hadoop里都可以找到,下面的例子中,至少需要这些jar包:

commons-cli-1.2.jar
commons-collections-3.2.1.jar
commons-configuration-1.6.jar
commons-io-2.4.jar
commons-lang-2.6.jar
commons-logging-1.1.3.jar
guava-11.0.2.jar
hadoop-auth-2.7.1.jar
hadoop-common-2.7.1.jar
hadoop-hdfs-2.7.1.jar
htrace-core-3.1.0-incubating.jar
log4j-1.2.17.jar
protobuf-java-2.5.0.jar
servlet-api.jar
slf4j-api-1.7.10.jar
slf4j-log4j12-1.7.10.jar

代码如下:

package com.neohope.hadoop.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class HDFSTest {

	static Configuration hdfsConfig;
	static {
		hdfsConfig = new Configuration();
		hdfsConfig.addResource(new Path("etc/hadoop/core-site.xml"));
		hdfsConfig.addResource(new Path("etc/hadoop/hdfs-site.xml"));
	}

	// 创建文件夹
	public static void createDirectory(String dirPath) throws IOException {
		FileSystem fs = FileSystem.get(hdfsConfig);
		Path p = new Path(dirPath);
		try {
			fs.mkdirs(p);
		} finally {
			fs.close();
		}
	}

	// 删除文件夹
	public static void deleteDirectory(String dirPath) throws IOException {
		FileSystem fs = FileSystem.get(hdfsConfig);
		Path p = new Path(dirPath);
		try {
			fs.deleteOnExit(p);
		} finally {
			fs.close();
		}
	}

	// 重命名文件夹
	public static void renameDirectory(String oldDirPath, String newDirPath)
			throws IOException {
		renameFile(oldDirPath, newDirPath);
	}

	// 枚举文件
	public static void listFiles(String dirPath) throws IOException {
		FileSystem hdfs = FileSystem.get(hdfsConfig);
		Path listf = new Path(dirPath);
		try {
			FileStatus statuslist[] = hdfs.listStatus(listf);
			for (FileStatus status : statuslist) {
				System.out.println(status.getPath().toString());
			}
		} finally {
			hdfs.close();
		}
	}

	// 新建文件
	public static void createFile(String filePath) throws IOException {
		FileSystem fs = FileSystem.get(hdfsConfig);
		Path p = new Path(filePath);
		try {
			fs.createNewFile(p);
		} finally {
			fs.close();
		}
	}

	// 删除文件
	public static void deleteFile(String filePath) throws IOException {
		FileSystem fs = FileSystem.get(hdfsConfig);
		Path p = new Path(filePath);
		try {
			fs.deleteOnExit(p);
		} finally {
			fs.close();
		}
	}

	// 重命名文件
	public static void renameFile(String oldFilePath, String newFilePath)
			throws IOException {
		FileSystem fs = FileSystem.get(hdfsConfig);
		Path oldPath = new Path(oldFilePath);
		Path newPath = new Path(newFilePath);
		try {
			fs.rename(oldPath, newPath);
		} finally {
			fs.close();
		}
	}

	// 上传文件
	public static void putFile(String locaPath, String hdfsPath)
			throws IOException {
		FileSystem fs = FileSystem.get(hdfsConfig);
		Path src = new Path(locaPath);
		Path dst = new Path(hdfsPath);
		try {
			fs.copyFromLocalFile(src, dst);
		} finally {
			fs.close();
		}
	}

	// 取回文件
	public static void getFile(String hdfsPath, String locaPath)
			throws IOException {
		FileSystem fs = FileSystem.get(hdfsConfig);
		Path src = new Path(hdfsPath);
		Path dst = new Path(locaPath);
		try {
			fs.copyToLocalFile(false, src, dst, true);
		} finally {
			fs.close();
		}
	}

	// 读取文件
	public static void readFile(String hdfsPath) throws IOException {
		FileSystem hdfs = FileSystem.get(hdfsConfig);
		Path filePath = new Path(hdfsPath);

		InputStream in = null;
		BufferedReader buff = null;
		try {
			in = hdfs.open(filePath);
			buff = new BufferedReader(new InputStreamReader(in));
			String str = null;
			while ((str = buff.readLine()) != null) {
				System.out.println(str);
			}
		} finally {
			buff.close();
			in.close();
			hdfs.close();
		}
	}

	public static void main(String[] args) throws IOException {
		System.setProperty("HADOOP_USER_NAME", "hadoop");
		// createDirectory("hdfs://hadoop-master:9000/usr");
		// createDirectory("hdfs://hadoop-master:9000/usr/hansen");
		// createDirectory("hdfs://hadoop-master:9000/usr/hansen/test");
		// renameDirectory("hdfs://hadoop-master:9000/usr/hansen/test","hdfs://hadoop-master:9000/usr/hansen/test01");
		// createFile("hdfs://hadoop-master:9000/usr/hansen/test01/hello.txt");
		// renameFile("hdfs://hadoop-master:9000/usr/hansen/test01/hello.txt","hdfs://hadoop-master:9000/usr/hansen/test01/hello01.txt");
		// putFile("hello.txt","hdfs://hadoop-master:9000/usr/hansen/test01/hello02.txt");
		// getFile("hdfs://hadoop-master:9000/usr/hansen/test01/hello02.txt","hello02.txt");
		// readFile("hdfs://hadoop-master:9000/usr/hansen/test01/hello02.txt");
		listFiles("hdfs://hadoop-master:9000/usr/hansen/test01/");
	}

}

Xcode7文档下载地址

具体的下载地址,可以在这里查到
https://developer.apple.com/library/downloads/docset-index.dvtdownloadableindex

对于Xcode7,对应文档的下载地址为
watchOS 2.0 Documentation
iOS 9.0 Documentation
OS X 10.11 Documentation
Xcode 7 Documentation

已知问题
1、文档下载安装后可以使用,但Xcode无法识别。
2、从apple官网下,还是太慢了

Prolog求解Sudoku(七周七语言版本)

1、代码实现

:- use_module(library(clpfd)).
:- use_module(library(lists)).

%求解函数
sudoku(Puzzle, Solution) :-
  length(Puzzle, L),                         
  Size is floor(sqrt(L)),                    %计算矩阵大小                   
  Solution = Puzzle,
  Puzzle ins 1..Size,                        %输入必须符合规范(比如9阶Sudoku,元素必须在1到9之间)
  slice(Puzzle, Rows, Size, 'row'),          %将输入拆分为行,
  slice(Puzzle, Cols, Size, 'col'),          %列,
  slice(Puzzle, Squares, Size, 'square'),    %方格。
  valid(Rows),                               %每行,每列,每个方格不可重复
  valid(Cols),
  valid(Squares),
  pretty_print(Rows).                        %输出

%校验,一个List不可重复
valid([]).
valid([Head | Tail]) :- all_different(Head), valid(Tail).

%List截取
sublist_length([], _).
sublist_length([Head | Tail], Length) :- length(Head, Length), sublist_length(Tail, Length).

%List拼接
insert_into_slice(Item, Values, X, Y) :-
  nth0(X, Values, Bucket),
  nth0(Y, Bucket, Item).

%按行分割坐标
slice_position('row', Size, I, X, Y) :-   
  X is I // Size,
  Y is I mod Size.

%按列分割坐标
slice_position('col', Size, I, X, Y) :- 
  X is I mod Size,
  Y is I // Size.

%按方格分割坐标
slice_position('square', Size, I, X, Y) :- 
  Size_Sqrt is floor(sqrt(Size)),
  X is (I mod Size // Size_Sqrt) + (Size_Sqrt * (I // (Size * Size_Sqrt))),
  Y is (I mod Size_Sqrt) + (Size_Sqrt * ((I mod (Size * Size_Sqrt)) // Size)).

%数据分割函数
slice(Puzzle, Slice, Size, Type) :- slice(Puzzle, Slice, Size, Type, 0).
slice(_, Slice, Size, _, I) :- I is Size * Size, length(Slice, Size), sublist_length(Slice, Size).
slice([Head | Tail], Slice, Size, Type, I) :-
  slice_position(Type, Size, I, X, Y), 
  insert_into_slice(Head, Slice, X, Y),
  I1 is I + 1,
  slice(Tail, Slice, Size, Type, I1).

%输出函数
pretty_print([Head | Tail]) :-
  print(Head),
  nl,
  pretty_print(Tail).

2、测试一下

1 ?- sudoku([5, 3, _, _, 7, _, _, _, _, 
             6, _, _, 1, 9, 5, _, _, _, 
             _, 9, 8, _, _, _, _, 6, _, 
             8, _, _, _, 6, _, _, _, 3,
             4, _, _, 8, _, 3, _, _, 1,
             7, _, _, _, 2, _, _, _, 6,
             _, 6, _, _, _, _, 2, 8, _,
             _, _, _, 4, 1, 9, _, _, 5,
             _, _, _, _, 8, _, _, 7, 9], 
             Solution).

[5,3,4,6,7,8,9,1,2]
[6,7,2,1,9,5,3,4,8]
[1,9,8,3,4,2,5,6,7]
[8,5,9,7,6,1,4,2,3]
[4,2,6,8,5,3,7,9,1]
[7,1,3,9,2,4,8,5,6]
[9,6,1,5,3,7,2,8,4]
[2,8,7,4,1,9,6,3,5]
[3,4,5,2,8,6,1,7,9]
false.

Prolog求解Sudoku

1、求解源码如下

:- use_module(library(clpfd)).

%求解函数
sudoku(Rows) :-
        length(Rows, 9),                                         %输入为9行
        maplist(length_(9), Rows),                               %每行长度必须为9
        append(Rows, Vs), Vs ins 1..9,                           %所有行拼接为一个List,数值都必须在1到9之间
        maplist(all_distinct, Rows),                             %每行没有重复数据
        transpose(Rows, Columns), maplist(all_distinct, Columns),%矩阵转置,每列没有重复数据
        Rows = [R1,R2,R3,R4,R5,R6,R7,R8,R9],                     %输入拆分为9行
        blocks(R1,R2,R3), blocks(R4,R5,R6), blocks(R7,R8,R9).    %按每3行进行处理

%为了适应maplist函数,调整参数顺序
length_(L, Ls) :- length(Ls, L).

%对于每3行,每次从各行抽取前3个值,恰好为1个3×3的矩阵,共抽取3个矩阵
%对于矩阵,拼接为一个List,并要求没有重复数据
blocks([], [], []).
blocks([A,B,C|Bs1], [D,E,F|Bs2], [G,H,I|Bs3]) :-
        all_distinct([A,B,C,D,E,F,G,H,I]),
        blocks(Bs1, Bs2, Bs3).

%这个是输入函数,就不需要命令行输入了
%第1个是一个sudoku问题
problem(1, [[_,_,_,_,_,_,_,_,_],
            [_,_,_,_,_,3,_,8,5],
            [_,_,1,_,2,_,_,_,_],
            [_,_,_,5,_,7,_,_,_],
            [_,_,4,_,_,_,1,_,_],
            [_,9,_,_,_,_,_,_,_],
            [5,_,_,_,_,_,_,7,3],
            [_,_,2,_,1,_,_,_,_],
            [_,_,_,_,4,_,_,_,9]]).
            
%第2个是一个多解的sudoku问题           
problem(2, [[_,9,1,_,4,7,_,_,_],
            [7,_,_,_,1,_,_,_,_],
            [_,_,_,6,5,_,_,_,_],
            [9,_,_,4,_,6,_,_,7],
            [_,_,_,7,3,5,_,_,6],
            [_,_,7,9,8,1,_,_,_],
            [1,_,6,5,7,8,9,_,4],
            [_,_,9,1,6,_,8,_,_],
            [_,_,_,3,9,4,_,_,1]]).
            
%第3个是通过2得到的唯一解sudoku问题            
problem(3, [[8,9,1,_,4,7,6,_,_],
            [7,_,_,_,1,_,_,_,_],
            [2,_,_,6,5,_,_,_,_],
            [9,_,_,4,_,6,_,_,7],
            [_,_,_,7,3,5,_,_,6],
            [_,_,7,9,8,1,_,_,_],
            [1,_,6,5,7,8,9,_,4],
            [_,_,9,1,6,_,8,_,_],
            [_,_,_,3,9,4,_,_,1]]).

2、尝试一下

1 ?- problem(1, Rows), sudoku(Rows), maplist(writeln, Rows).
[9,8,7,6,5,4,3,2,1]
[2,4,6,1,7,3,9,8,5]
[3,5,1,9,2,8,7,4,6]
[1,2,8,5,3,7,6,9,4]
[6,3,4,8,9,2,1,5,7]
[7,9,5,4,6,1,8,3,2]
[5,1,9,2,8,6,4,7,3]
[4,7,2,3,1,9,5,6,8]
[8,6,3,7,4,5,2,1,9]
Rows = [[9, 8, 7, 6, 5, 4, 3, 2|...], [2, 4, 6, 1, 7, 3, 9|...], [3, 5, 1, 9, 2, 8|...], [1, 2, 8, 5, 3|...], [6, 3, 4, 8|...], [7, 9, 5|...], [5, 1|...], [4|...], [...|...]].

2 ?- problem(2, Rows), sudoku(Rows), maplist(writeln, Rows).
[_G296,9,1,_G320,4,7,_G344,_G368,_G392]
[7,_G416,_G440,_G464,1,_G488,_G512,_G536,_G560]
[_G584,_G608,_G632,6,5,_G656,7,1,_G728]
[9,_G752,_G776,4,2,6,_G824,8,7]
[_G872,_G896,_G920,7,3,5,_G944,9,6]
[_G992,_G1016,7,9,8,1,_G1040,_G1064,_G1088]
[1,_G1112,6,5,7,8,9,_G1136,4]
[_G1160,_G1184,9,1,6,2,8,_G1232,_G1256]
[_G1280,_G1304,_G1328,3,9,4,_G1352,_G1376,1]
Rows = [[_G24117, 9, 1, _G24126, 4, 7, _G24135, _G24138|...], [7, _G24150, _G24153, _G24156, 1, _G24162, _G24165|...], [_G24177, _G24180, _G24183, 6, 5, _G24192|...], [9, _G24210, _G24213, 4, 2|...], [_G24237, _G24240, _G24243, 7|...], [_G24267, _G24270, 7|...], [1, _G24300|...], [_G24327|...], [...|...]],
_G24117 in 2..3\/5..6\/8,
all_distinct([_G24117, 9, 1, 7, _G24150, _G24153, _G24177, _G24180|...]),
all_distinct([_G24117, 7, _G24177, 9, _G24237, _G24267, 1, _G24327|...]),
all_distinct([_G24117, 9, 1, _G24126, 4, 7, _G24135, _G24138|...]),
_G24150 in 2..6\/8,
all_distinct([9, _G24150, _G24180, _G24210, _G24240, _G24270, _G24300, _G24330|...]),
all_distinct([7, _G24150, _G24153, _G24156, 1, _G24162, _G24165, _G24168|...]),
_G24153 in 2..5\/8,
all_distinct([1, _G24153, _G24183, _G24213, _G24243, 7, 6, 9|...]),
_G24177 in 2..4\/8,
all_distinct([_G24177, _G24180, _G24183, 6, 5, _G24192, 7, 1|...]),
_G24180 in 2..4\/8,
_G24183 in 2..4\/8,
_G24213 in 3\/5,
all_distinct([9, _G24210, _G24213, _G24237, _G24240, _G24243, _G24267, _G24270|...]),
all_distinct([9, _G24210, _G24213, 4, 2, 6, _G24225, 8|...]),
_G24237 in 2\/4\/8,
all_distinct([_G24237, _G24240, _G24243, 7, 3, 5, _G24255, 9|...]),
_G24240 in 1..2\/4\/8,
_G24243 in 2\/4\/8,
_G24267 in 2..6,
all_distinct([_G24267, _G24270, 7, 9, 8, 1, _G24285, _G24288|...]),
_G24270 in 2..6,
_G24210 in 1\/3\/5,
_G24300 in 2..3,
all_distinct([1, _G24300, 6, _G24327, _G24330, 9, _G24357, _G24360|...]),
all_distinct([1, _G24300, 6, 5, 7, 8, 9, _G24318|...]),
_G24327 in 3..5,
all_distinct([_G24327, _G24330, 9, 1, 6, 2, 8, _G24348|...]),
_G24330 in 3..5\/7,
_G24357 in 2\/5\/8,
all_distinct([_G24357, _G24360, _G24363, 3, 9, 4, _G24375, _G24378|...]),
_G24360 in 2\/5\/7..8,
_G24363 in 2\/5\/8,
_G24375 in 2\/5..6,
all_distinct([9, _G24318, 4, 8, _G24348, _G24351, _G24375, _G24378|...]),
all_distinct([_G24135, _G24165, 7, _G24225, _G24255, _G24285, 9, 8|...]),
_G24378 in 2\/5..7,
all_distinct([_G24138, _G24168, 1, 8, 9, _G24288, _G24318, _G24348|...]),
_G24351 in 3\/5,
all_distinct([_G24141, _G24171, _G24201, 7, 6, _G24291, 4, _G24351|...]),
_G24348 in 3\/5\/7,
_G24318 in 2..3,
_G24288 in 2..5,
all_distinct([_G24225, 8, 7, _G24255, 9, 6, _G24285, _G24288|...]),
_G24291 in 2..3\/5,
_G24285 in 2..5,
_G24255 in 1..2\/4,
_G24225 in 1\/3\/5,
_G24165 in 2..6,
all_distinct([_G24135, _G24138, _G24141, _G24165, _G24168, _G24171, 7, 1|...]),
_G24168 in 2..6,
_G24171 in 2..3\/5\/8..9,
_G24201 in 2..3\/8..9,
_G24141 in 2..3\/5\/8,
_G24138 in 2..3\/5..6,
_G24135 in 2..3\/5..6,
_G24126 in 2\/8,
all_distinct([_G24126, 4, 7, _G24156, 1, _G24162, 6, 5|...]),
all_distinct([_G24126, _G24156, 6, 4, 7, 9, 5, 1|...]),
_G24156 in 2\/8,
_G24162 in 3\/9,
all_distinct([7, _G24162, _G24192, 6, 5, 1, 8, 2|...]),
_G24192 in 3\/9.

%根据2的提示,填写部分不确定的值后,可以得到问题3,根据选择不同,答案也不同
3 ?- problem(3, Rows), sudoku(Rows), maplist(writeln, Rows).
[8,9,1,2,4,7,6,5,3]
[7,6,5,8,1,3,4,2,9]
[2,3,4,6,5,9,7,1,8]
[9,1,3,4,2,6,5,8,7]
[4,8,2,7,3,5,1,9,6]
[6,5,7,9,8,1,3,4,2]
[1,2,6,5,7,8,9,3,4]
[3,4,9,1,6,2,8,7,5]
[5,7,8,3,9,4,2,6,1]
Rows = [[8, 9, 1, 2, 4, 7, 6, 5|...], [7, 6, 5, 8, 1, 3, 4|...], [2, 3, 4, 6, 5, 9|...], [9, 1, 3, 4, 2|...], [4, 8, 2, 7|...], [6, 5, 7|...], [1, 2|...], [3|...], [...|...]].