骑士游历/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逐渐被抛弃, 而得益于 ...
随机推荐
- Asp.Net Core--自定义基于策略的授权
翻译如下: 在封面下,角色授权和声明授权使用需求,需求的处理程序和预配置的策略. 这些构建块允许您在代码中表示授权评估,从而允许更丰富,可重用和容易测试的授权结构. 授权策略由一个或多个需求组成,并在 ...
- [vijos1459]车展
[vijos1459]车展 试题描述 遥控车是在是太漂亮了,韵韵的好朋友都想来参观,所以游乐园决定举办m次车展.车库里共有n辆车,从左到右依次编号为1,2,-,n,每辆车都有一个展台.刚开始每个展台都 ...
- yii模块下面的组件
模块的定义就不写了,直接进入主题看目录和文件: application/modules/client/controllers/UserController.php <?php class Use ...
- ios显示或隐藏导航栏的底线
根据产品需求要求把这个界面导航栏的底线去掉,下个控制器还需要有底线. 使用下面的代码实现 //在页面出现的时候就将黑线隐藏起来 -(void)viewWillAppear:(BOOL)animated ...
- Huffman的应用_Huffman编码
//最优二叉树 #include <iostream> #include <iomanip> using namespace std; //定义结点类型 //[weight | ...
- Rest(表述性状态转移)
本文的主要内容有: 1.了解Rest 2.了解RESTful WebService 3.使用SpringMvc实现RESTful ------------------------------我是华丽的 ...
- JS 中没有按地址(引用)传递,只有按值传递
很多人,包括我,受书本知识消化不彻底的影响,认为 JS 中参数有两种传递方式:数字.字符串等按值传递:数组.对象等按地址(引用)传递.对此种观点,我们要谨慎. var v1 = [] var v2 = ...
- MongoDB replication set副本集(主从复制)(8)(转)
转载地址:http://www.cnblogs.com/huangxincheng/p/4870557.html replicattion set 就是多台服务器维护相同的数据副本,提高服务器的可用性 ...
- (一)安卓小app开发之基础环境搭建
一.准备工作: 1.下载Android Studio开发环境 https://dl.google.com/dl/android/studio/ide-zips/2.1.1.0/android-stud ...
- 给11gR2 Dataguard打psu补丁的步骤及注意事项
参考文档278641.1 0.备份备主备库的spfile备份主库的数据 1.在主库上暂停向备库传日志alter system set log_archive_dest_state_X=defer sc ...