骑士游历/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逐渐被抛弃, 而得益于 ...
随机推荐
- javascript基础06
javascript基础06 splice var del_arr = del.splice(0,2); //删除从指定位置deletePos开始的指定数量deleteCount的元素,数组形式返 ...
- Hadoop家族的各个成员
官方定义:hadoop是一个开发和运行处理大规模数据的软件平台.核心词语是平台,也就是说我们有大量的数据,又有好几个电脑,我们知道应该把处理数据的任务分解到各个电脑上,但是不知道怎样分配任务,怎样回收 ...
- Proj.4 API 中文参考
ProjAPI https://github.com/OSGeo/proj.4/wiki/ProjAPI Tom Kralidis在2015年5月27日编辑此页·修订4 简介 执行pj_init()选 ...
- CSS代码片段【图文】
1.垂直对齐 .verticalcenter{ position: relative; top: 50%; -webkit-transform: translateY(-50%); -o-transf ...
- Spring PropertyPlaceholderConfigurer数据库配置
pom.xml中添加依赖 <!-- mysql-connector-java --> <dependency> <groupId>mysql</groupId ...
- MST 001
一.String,StringBuffer, StringBuilder 的区别是什么?String为什么是不可变的? 答: 1.String是字符串常量,StringBuffer和StringB ...
- linuxqq
centos7下安装linuxqq出现一大堆依赖包都没有,腾讯搞的这个产品真不给力.寒心. >>>以下来自百度知道:http://zhidao.baidu.com/question/ ...
- Redirect url 路径简单介绍
问题:Response.redirect 用法asp 中 用response.redirect () 跳转到不同的页面是怎么写的,比如从index.asp跳到admin目录下的a.asp 还有从a跳回 ...
- ASP.NET MVC中的错误处理
ASP.NET MVC中的错误的错误处理跨越了两个主要领域:程序异常和路由异常的处理.前者是关于在控制器和视图中捕获错误的;而后者更多是有关重定向和HTTP错误的. 1.在WebConfig中把过滤器 ...
- mysql explain用法
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了,如: explai ...