骑士游历/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逐渐被抛弃, 而得益于 ...
随机推荐
- 超简单,安卓模拟器手动root
本文转载自:http://quantoubao.blog.163.com/blog/static/2083211702013870501987/ 安装Android SDK安卓模拟器的方法很简单,网上 ...
- MineCraft note
客户端:http://pan.baidu.com/s/1hqgS8sshttp://pan.baidu.com/s/1mgmkduC 材质包:R3D小地图MODCraftGuide mod 内置合成表 ...
- K closest points
Find the K closest points to a target point in a 2D plane. class Point { public int x; public int y; ...
- RobotFrameWork(五)控制流之if语句——Run Keyword If
5.1 语句简介 robotframework中的if语句是使用关键字Run Keyword If来代替的 Run Keyword If 函数释义:如果给出的判断条件满足,就执行给出的关键字. 函数结 ...
- JAVA SSM 示例代码
SSM 即spring+spring mvc+mybatis,开发工具IDEA 1.先看下项目结构如图: 2.主要配置文件 spring-mvc.xml <?xml version=" ...
- yii框架中验证器声明一组内置验证器可以使用短名称引用
1.内置验证器的短名称分别有: boolean: yii\validators\BooleanValidator captcha: yii\captcha\CaptchaValidator compa ...
- 使用json把php数据传给js处理
先创建下面的两个文件,并将代码拷贝进去,然后打开json.html文件: json.html文件: <!DOCTYPE html> <html> <head> &l ...
- 教你分分钟开发一个属于自己的python模块(一)——能够直接在浏览器打印的方法
曾经,用惯了python print命令的人,惊叹于python语法的精简:后来,用过了tornado.django等web开发框架,不得不佩服当初开发这些框架的人们.于是,我们开始使用它们的框架== ...
- IP地址的分类
IPv4 地址的分类: 一,组成 1. 使用32位地址 2. 以点分十进制表示,如172.16.0.0,每一个数字对应于8个二进制的比特串,称为一个位组(octets).如某一台主机的IP地址 ...
- maven-replacer-plugin
今天多认识了下这个maven插件. 基本用法: <plugin> <groupId>com.google.code.maven-replacer-plugin</grou ...