马的遍历问题
在n*m的棋盘中,马只能走“日” 字。马从位置(x,y)处出发,把棋盘的每一格都走一次,且只走一次。找出所有路径。
问题解的搜索空间?
棋盘的规模是n*m,是指行有n条边,列有m条边。
马在棋盘的点上走,所以搜索空间是整个棋盘上的n*m个点。
用n*m的二维数组记录马行走的过程,初值为0表示未经过。
在寻找路径过程中,活结点的扩展规则?
对于棋盘上任意一点A(x,y),有八个扩展方向:
A(x+1,y+2),A(x+2,y+1)
A(x+2,y-1),A(x+1,y-2)
A(x-1,y-2),A(x-2,y-1)
A(x-2,y+1),A(x-1,y+2)
为构造循环体,用数组fx[8]={1,2,2,1,-1,-2,-2,-1},fy[8]= {2,1,-1,-2,-2,-1,1,2}来模拟马走“日”时下标的变化过程。
扩展的约束条件
不出边界;
每个点只经过一次。
棋盘点对应的数组元素初值为0,对走过的棋盘点的值置为所走步数,起点存储“1”,终点存储“n*m”。
函数check,检查当前状态是否合理
输入
0 0
输出
1 90 82 75 88 83 78 85 89
71 74 2 64 81 76 87 84 79
44 55 72 69 3 63 80 77 86
73 70 45 56 65 68 4 62 59
54 43 66 51 46 57 60 49 5
41 37 53 31 67 50 47 58 61
35 12 42 38 52 30 25 6 48
20 39 36 13 32 23 16 29 26
11 34 21 18 9 14 27 24 7
40 19 10 33 22 17 8 15 28
#include
#include
int matrix[10][9];
int journey = 1;
int step_x[]={1,2,2,1,-1,-2,-2,-1},step_y[]={2,1,-1,-2,-2,-1,1,2};
void outMatrix(){
int i,j;
for (i=0;i<10;i++)
{
for (j=0;j<9;j++)
{
printf("%-2d ",matrix[i][j]);
}
printf("\n");
}
}
bool outofbounds(int x,int y){
return x < 0 || y < 0 || x >= 10 || y >= 9;
}
bool isCome(int x,int y){
return matrix[x][y];
}
void gotoend(int x, int y ){
if(journey>90) return;
int i;
matrix[x][y]=journey++;//当前是第几步
for (i = 0;i<8;i++)
{
int next_x = x+step_x[i];
int next_y = y+step_y[i];
if(!outofbounds(next_x,next_y) && !matrix[next_x][next_y]){
gotoend(next_x,next_y);
}
}
}
int main(){
int start_x,start_y;
int i;
scanf("%d%d",&start_x,&start_y);
for (i = 0;i<10;i++) {
memset(matrix[i],0,sizeof(matrix[0]));
}
gotoend(start_x,start_y);
outMatrix();
return 0;
}
洛谷 P1443 马的遍历
P1443 马的遍历 题目描述 有一个n*m的棋盘(1
leetcode 236. 二叉树的最近公共祖先LCA(后序遍历,回溯)
LCA(Least Common Ancestors),即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. 题目描述 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百 ...
最少步数&;P1443 马的遍历
1330:[例8.3]最少步数 s数组:记录(1,1)到达每一点需要的最少步数 s[1][1]自然为 0,其余初始化为 -1 que数组:que[#][1] 表示(1,1)可到达点的 x 坐标 q ...
马的遍历 洛谷 p1443
题目描述 有一个n*m的棋盘(1
洛谷P1443 马的遍历
https://www.luogu.org/problemnew/show/P1443 很经典的搜索题了,蒟蒻用广搜打的 不说了,上代码! #include ...
[luoguP1443]马的遍历
首先来看一下题目描述: 题目描述 有一个n*m的棋盘(1
【洛谷】P1443 马的遍历
题目:https://www.luogu.org/problemnew/show/P1443 简单的BFS模板题——因为我写出来了. 分析过程: n*m矩阵,用二维数组 数据不大,二维数组稳了 先把二 ...
【洛谷P1443 马的遍历】
题目链接(%%%jyy大佬) 题目描述 有一个n*m的棋盘(1
洛谷 P1443 马的遍历
终于遇到一个简单纯粹一点的bfs了...... 题目链接:https://www.luogu.org/problemnew/show/P1443 题目是求到达一个点的最短步数 也就是说我只要bfs遍历 ...
随机推荐
两个viewport的故事(第二部分)
原文:http://www.quirksmode.org/mobile/viewports2.html 在这个迷你系列的文章里边我将会解释viewport,以及许多重要元素的宽度是如何工作的,比如&l ...
Eclipse打开xml文件报校验错误解决办法
XML文件在Eclipse中报校验错误: The content of element type "web-app" must match "(icon?,display ...
POJ2886 Who Gets the Most Candies? 线段树 反素数
题意:有一群小朋友围成一个环,编号1,2,3…N.每个人手上握着一个非0的数字,首先第K个人出列,然后看他手上的数字,假设为m,则从下一个开始第m个人出列,一直如此.并设i为小于等于N的最大反素数,问 ...
WPF - ViewModle中关闭Window
在Binding close event时候,需要从ViewModel关闭Window. 一个很简洁的解决方案就是,将Window 当做CommandParameter传过去. Command=&qu ...
Server对象(是属性)
html, body { font-size: 10.5pt; } body { font-family: 微软雅黑, Helvetica, "Hiragino Sans GB", ...
Apache生产配置
httpd.conf # # This is the main Apache HTTP server configuration file. It contains the # configurati ...
[原]shell批量文件增删改前后缀
改后缀 for files in `ls *`; do mv ${files} ${files}.bak; done 改后缀 rename .bak .bak2 * 去除后缀 for files in ...
android 根据滑动隐藏或显示导航 类似手机QQ好友个人信息
//重写ScrollView public class NotifyingScrollView extends ScrollView { /** * @author Cyril Mottier */ ...
Hadoop HA 高可用集群的搭建
hadoop部署服务器 系统 主机名 IP centos6.9 hadoop01 192.168.72.21 centos6.9 hadoop02 192.168.72.22 centos6.9 ha ...
introduction to python for statistics,analysis笔记2
一.行列式连接concatenate函数,axis=0是垂直拼接,axis=1是水平拼接 x=np.array([[],[,]]); y=np.array([[],[,]]); z=np.concat ...