骑士游历/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逐渐被抛弃, 而得益于 ...
随机推荐
- 巧用dimens适配多个分辨率
让应用自动适配多个分辨率的屏幕,是每个android程序员的基本功,就好像前端工程师熟练编写CSS Hack一样.适配工作中一个重要的工作就是对页面的调整. 对于页面的适配,有很多的方法和技巧.比 ...
- 练习题(登陆-进度条-微信接口判断qq-微信接口判断列车时刻表-)
1.写一个用户的登陆注册的界面,用户的密码用hashlib加密存在文件中,登陆时候,用户的密码要和文件中的密码一致才行 def sha(password): #加密函数 passwd = hashli ...
- Spring预处理
当需要在某些Spring项目一启动,就执行某些操作时,需要实现改接口ApplicationListener,重写onApplicationEvent方法,将需要的预处理操作全部写在该方法中 当初始化完 ...
- PHPStorm XDebug的安装
环境: 我的系统: 4.4.0-43-generic #63-Ubuntu SMP Wed Oct 12 13:48:03 UTC 2016 x86_64 x86_64 x86_64 GNU/Linu ...
- MySql: 忘记root密码
win7 + mysql 5.6.35 C:\Windows\system32>mysql --versionmysql Ver 14.14 Distrib 5.6.35, for Win64 ...
- poj 3038
http://poj.org/problem?id=3038 这个题我是在一个关于并查集的博客中找到的,结果我就觉得这个应该是个贪心,真想不出这个与并查集有什么鬼关系,看discuss里面也都是贪心, ...
- 在测试框架中使用Log4J 2
之前的测试框架:http://www.cnblogs.com/tobecrazy/p/4553444.html 配合Jenkins可持续集成:http://www.cnblogs.com/tobecr ...
- C#通过事件跨类调用WPF主窗口中的控件
xaml.cs文件: using System; using System.Timers; using System.Windows; using System.Windows.Forms; name ...
- linux 汇编
nasm的语法和大学教材上8086的汇编伪指令有些差别,指令都是一样的. 编辑器就是普通的编辑器,vim,emacs,gedit,kate源文件类型为ascii码的plain text 编译用gcc或 ...
- 51nod1073(约瑟夫环)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1073 题意: 中文题诶~ 思路: 直接模拟的话O(n*k)的 ...