骑士游历/knight tour - visual basic 解决
在visual baisc 6 how to program 中文版第七章的练习题上看到了这个问题,骑士游历的问题。
在8x8的国际象棋的棋盘上,骑士(走法:一个方向走两格,另一个方向一格)不重复走完棋盘上所有空格的路径。
思路就是选角落的一格为起点,把所有能走的路全部路径全部试一遍。要试8^63次,计算时间太长了。把棋盘调成5x5的,比较好算。另外书里提示,根据空格的可访问的难易(难易由周围可访问它的空格数来决定),先选择更难访问的空格访问。
下面是完全遍历一遍的方法。
Option Explicit
Option Base 1
Dim stepmax As Integer
Const n = 8
Dim anw As Integer
Private Sub Command1_Click()
Dim x As Integer
Dim y As Integer
Dim step As Integer
Dim c(n, n) As Integer
x = 1
y = 1
step = 1
c(x, y) = step
Call knightsol(x, y, step, c())
Text2.Text = stepmax
Label1.Caption = anw
End Sub
Private Sub knightsol(ByVal x As Integer, ByVal y As Integer, ByVal step As Integer, c() As Integer)
Dim i As Integer
Dim xt As Integer, yt As Integer, stept As Integer
'List1.AddItem ("-------------------------")
For i = 1 To 8
xt = x
yt = y
stept = step
'List1.AddItem ("当前位置为" & "(" & x & "," & y & ")")
Call mv(i, x, y)
'List1.AddItem ("-- 往" & i & "方向移动到" & "(" & x & "," & y & ")")
If x > 0 And x <= n And y > 0 And y <= n Then
If c(x, y) = 0 Then
step = step + 1
c(x, y) = step
'List1.AddItem ("-- 赋值" & x & "," & y & "为" & step)
If step > stepmax Then
stepmax = step
End If
If step = n * n Then
'List1.AddItem ("目的完成输出路径")
'List1.AddItem ("-------------")
Call path(c())
c(x, y) = 0
MsgBox ("找到了")
anw = anw + 1
Exit Sub
End If
Call knightsol(x, y, step, c())
'Call path(c())
'List1.AddItem ("还原c(" & x & "," & y & ")")
c(x, y) = 0
x = xt
y = yt
step = stept
'List1.AddItem ("-------------------------")
Else
'List1.AddItem ("** 被占用了")
x = xt
y = yt
End If
Else
'List1.AddItem ("** 越界了")
x = xt
y = yt
End If
Next
End Sub
Private Sub mv(i As Integer, x As Integer, y As Integer)
Select Case i
Case 1
x = x + 2
y = y + 1
Case 2
x = x + 2
y = y - 1
Case 3
x = x - 2
y = y + 1
Case 4
x = x - 2
y = y - 1
Case 5
x = x + 1
y = y + 2
Case 6
x = x + 1
y = y - 2
Case 7
x = x - 1
y = y + 2
Case 8
x = x - 1
y = y - 2
End Select
End Sub
Private Sub path(c() As Integer)
Dim a As Integer, b As Integer
For a = 1 To n
For b = 1 To n
Text1.Text = Text1.Text & Format(c(a, b), "@@@")
Next b
Text1.Text = Text1.Text & vbCrLf
Next a
Text1.Text = Text1.Text & vbCrLf
End Sub
骑士游历/knight tour - visual basic 解决的更多相关文章
- POJ 2488 -- A Knight's Journey(骑士游历)
POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...
- 使用 Async 和 Await 的异步编程(C# 和 Visual Basic)[msdn.microsoft.com]
看到Microsoft官方一篇关于异步编程的文章,感觉挺好,不敢独享,分享给大家. 原文地址:https://msdn.microsoft.com/zh-cn/library/hh191443.asp ...
- VS2017远程调试C#或 Visual Studio 中的 Visual Basic 项目
来源:远程调试C#或 Visual Studio 中的 Visual Basic 项目 若要调试已部署在另一台计算机的 Visual Studio 应用程序,安装和在其中部署您的应用程序的计算机上运行 ...
- 杂项-软件: VBA(Visual Basic for Applications)
ylbtech-杂项-软件: VBA(Visual Basic for Applications) VBA (Visual Basic宏语言) Visual Basic for Application ...
- DFS(二):骑士游历问题
在国际象棋的棋盘(8行×8列)上放置一个马,按照“马走日字”的规则,马要遍历棋盘,即到达棋盘上的每一格,并且每格只到达一次.例如,下图给出了骑士从坐标(1,5)出发,游历棋盘的一种可能情况. [例1] ...
- Visual Basic 2012 借助DataGridView控件将SQL server2012 数据导入到Excel 2010
摘 要: SQL Server 2012 数据和Excel 2010之间的连接和数据的传输,本篇文章主要针对的是SQL Server 2012 数据导入到Excel 2010文件中.Excel软件对 ...
- Visual Basic 2012 借助DataGridView控件将Excel 2010数据导入到SQL server 2012
(注:注释的颜色原本为绿色,在这里变为黑色,有点不便,但不会造成阅读影响.放入Visual Basic2012代码编辑器后会还原成绿色.) 摘 要:DataGridView控件作为数据传输的中介,只 ...
- Delphi、C C++、Visual Basic数据类型的对照 转
Delphi.C C++.Visual Basic数据类型的对照 变量类型 Delphi C/C++ Visual Basic 位有符号整数 ShortInt char -- 位无符号整数 Byte ...
- 2016年4月TIOBE编程语言排行榜 Visual Basic正渐行渐远
COBOL, BASIC 和 FORTRAN 很长一段时间作为主力开发语言被使用.有很多软件使用这些语言来编写,并且发展的不亦乐乎.然而经过多年的发展,COBOL和FORTRAN逐渐被抛弃, 而得益于 ...
随机推荐
- C和指针 第五章 习题
下列输出的值: #include <stdio.h> int func(){ static int count = 1; return ++count; } int main() { in ...
- 详细解读Android中的搜索框—— SearchView
以前总是自己写的 今天看看别人做的 本篇讲的是如何用searchView实现搜索框,其实原理和之前的没啥差别,也算是个复习吧. 一.Manifest.xml 这里我用一个activity进行信息的输入 ...
- Linux下vim 开发环境配置及配色(Ruby)
终端设置 字体:Monospace 10 粗体 内置方案M:黑底白字 内置方案S:Linux控制台 安装vundle git clone https://github.com/VundleVim/Vu ...
- xmind portable
portable : http://dl2.xmind.cn/xmind-7.5-update1-portable.zip
- 二叉树建立,遍历和二叉排序树的判断【c++】
// test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...
- PHP PHPUnit的简单使用
1.window安装pear的教程:http://jingyan.baidu.com/article/ca41422fd8cf3d1eae99ed3e.html 2.在工作目录下,放两个文件: 1)C ...
- Android手机截屏方法
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <u ...
- Gps与地图坐标转换
内容实在是太太了 7.8MB 以至于浏览器 都奔溃 就算浏览器可以 博客园的文章也保存不了 只好保存到百度云 提供下载 地址: 链接:http://pan.baidu.com/s/16ggIq 密码: ...
- 一款全兼容的播放器 videojs
[官网]http://www.videojs.com/ videojs就提供了这样一套解决方案,他是一个兼容HTML5的视频播放工具,早期版本兼容所有浏览器,方法是:提供三个后缀名的视频,并在不支持h ...
- javascript escape()函数和unescape()函数
javascript escape()函数和unescape()函数 escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串. 语法: escape(string) stri ...