DFS(二):骑士游历问题
在国际象棋的棋盘(8行×8列)上放置一个马,按照“马走日字”的规则,马要遍历棋盘,即到达棋盘上的每一格,并且每格只到达一次。例如,下图给出了骑士从坐标(1,5)出发,游历棋盘的一种可能情况。

【例1】骑士游历问题。
编写一个程序,对于给定的起始位置(x0,y0),探索出一条路径,沿着这条路径骑士能遍历棋盘上的所有单元格。
(1)编程思路。
采用深度优先搜索进行路径的探索。深度优先搜索用递归描述的一般框架为:
void dfs(int deep) // 对deep层进行搜索
{
if (符合某种要求||已经不能再搜了)
{
按要求进行一些处理,一般为输出;
return ;
}
if (符合某种条件且有地方可以继续搜索的) // 这里可能会有多种情况,可能要循环什么的
{
vis[x][y]=1; // 表示结点(x,y)已访问到
dfs(deep+1); // 搜索下一层
vis[x][y]=0; // 改回来,表示结点(x,y)以后可能被访问
}
}
定义数组int vis[10][10]记录骑士走到的步数,vis[x][y]=num表示骑士从起点开始走到坐标为(x,y)的格子用了num步(设起点的步数为1)。初始时vis数组元素的值全部为0。
(2)源程序。
#include <stdio.h>
#include <stdlib.h>
int N,M;
int vis[10][10]={0};
// 定义马走的8个方向
int dir_x[8] = {-1,-2,-2,-1,1,2,2,1};
int dir_y[8] = {2,1,-1,-2,-2,-1,1,2};
void print()
{
int i,j;
for(i=0; i<N; i++)
{
for(j=0; j<M; j++)
printf("%3d ",vis[i][j]);
printf("\n");
}
}
void DFS(int cur_x,int cur_y,int step)
{
if(step==N*M+1 )
{
print();
exit(1);
}
int next_x,next_y;
for(int i=0; i<8; i++)
{
next_x = cur_x+dir_x[i];
next_y = cur_y+dir_y[i];
if (next_x<0 || next_x>=N || next_y<0 || next_y>=M || vis[next_x][next_y]!=0)
continue;
vis[next_x][next_y] = step;
DFS(next_x,next_y,step+1);
vis[next_x][next_y] = 0;
}
}
int main()
{
printf("请输入棋盘的行数和列数(均小于10):");
scanf("%d %d",&N,&M);
printf("请输入出发点坐标:(0—%d,0-%d):",N-1,M-1);
int x0,y0;
scanf("%d%d",&x0,&y0);
vis[x0][y0] = 1;
DFS(x0,y0,2);
return 0;
}
(3)运行效果。

【例2】A Knight's Journey(POJ 2488)
Description
Background
The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans?
Problem
Find a path such that the knight visits every square once. The knight can start and end on any square of the board.
Input
The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .
Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.
If no such path exist, you should output impossible on a single line.
Sample Input
3
1 1
2 3
4 3
Sample Output
Scenario #1:
A1
Scenario #2:
impossible
Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4
(1)编程思路。
同样用深度优先搜索。但由于题目要输出字典序最小的,所以遍历时8个方向的偏移组合顺序为:{-2,-1}, {-2,1}, {-1,-2}, {-1,2}, {1,-2}, {1,2}, {2,-1}, {2,1}。
(2)源程序。
#include<stdio.h>
int dir_x[8] = {-2,-2,-1,-1, 1, 1, 2, 2};
int dir_y[8] = {-1, 1,-2, 2,-2, 2,-1, 1};
int vis[27][27];
int len,x,y;
bool flag;
struct Node
{
int x,y;
}node[1000];
void DFS(int cur_x,int cur_y)
{
if(len==x*y)
{
flag=true;
return ;
}
for(int i=0; i<8; i++)
{
int next_x=cur_x+dir_x[i];
int next_y=cur_y+dir_y[i];
if(next_x>0 && next_x<=x && next_y>0 && next_y<=y && vis[next_x][next_y]!=1)
{
node[len].x=next_x;
node[len].y=next_y;
vis[next_x][next_y]=1;
++len;
DFS(next_x,next_y);
if(len==x*y)
{
flag=true;
return ;
}
--len;
vis[next_x][next_y]=0;
}
}
}
int main()
{
int nCase;
int n,i,j;
scanf("%d",&nCase);
for(n=1; n<=nCase; n++)
{
flag=false;
len=0;
for (i=0;i<27;i++)
for (j=0;j<27;j++)
vis[i][j]=0;
node[0].x=1;
node[0].y=1;
vis[1][1]=1;
scanf("%d%d",&y,&x);
++len;
DFS(1,1);
printf("Scenario #%d:\n",n);
if(flag==false)
{
printf("impossible\n\n");
continue;
}
for(i=0; i<len; i++)
{
printf("%c%d",(node[i].x-1)+'A',node[i].y);
}
printf("\n\n");
}
return 0;
}
DFS(二):骑士游历问题的更多相关文章
- POJ 2488 -- A Knight's Journey(骑士游历)
POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...
- 骑士游历/knight tour - visual basic 解决
在visual baisc 6 how to program 中文版第七章的练习题上看到了这个问题,骑士游历的问题. 在8x8的国际象棋的棋盘上,骑士(走法:一个方向走两格,另一个方向一格)不重复走完 ...
- 骑士游历 - dp
题目地址:http://www.51cpc.com/web/problem.php?id=1586 Summarize: 1. 题目坐标系所给 x,y与惯用表示横纵坐标相反 2. 搜索超时,使用动规: ...
- hiho #1308 : 搜索二·骑士问题
#1308 : 搜索二·骑士问题 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi:小Ho你会下国际象棋么? 小Ho:应该算会吧,我知道每个棋子的移动方式,马走日象 ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
- codevs 1219 骑士游历 1997年
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 设有一个n*m的棋盘(2≤n≤50,2≤m≤50),如下图,在棋盘上有一个中国象 ...
- poj2488--A Knight's Journey(dfs,骑士问题)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31147 Accepted: 10 ...
- HihoCoder 1504 : 骑士游历 (矩阵乘法)
描述 在8x8的国际象棋棋盘上给定一只骑士(俗称“马”)棋子的位置(R, C),小Hi想知道从(R, C)开始移动N步一共有多少种不同的走法. 输入 第一行包含三个整数,N,R和C. 对于40%的数据 ...
- 【[Offer收割]编程练习赛13 D】骑士游历(矩阵模板,乘法,加法,乘方)
[题目链接]:http://hihocoder.com/problemset/problem/1504 [题意] [题解] 可以把二维的坐标转成成一维的; 即(x,y)->(x-1)*8+y 然 ...
随机推荐
- Select2使用方法汇总
引用: <script src="~/Content/plugins/select2/select2.min.js"></script> 1.简单使用 $. ...
- .NET 上传并解析CSV文件存库
1.前端: 放置浏览按钮 <div class="row inner_table text-center"> <input id="fileId&quo ...
- Emgu-WPF 激光雷达研究-定位实现
原文:Emgu-WPF 激光雷达研究-定位实现 特定位置或障碍物位置定位实现. 读取激光雷达数据并存储于本地作为测试数据.每一帧数据对同一障碍物的定位信息均存在偏差.所以先对需要定位的点进行数据取样. ...
- Web应用程序和网站的区别
1项目就是一个应用程序.在VS中查看的时候,项目中建立的一般处理程序,有两个文件,网站只有一个.写个代码测试,发现在代码层次上没有2再有就是项目中的一般处理程序有命名空间,而网站中的没有.WEB网站每 ...
- XAML的命名空间
原文:XAML的命名空间 一个最简单的XAML例子 <Window x:Class="WpfApplication1.MainWindow" xmlns="ht ...
- 以双斜杠//开头的URL – 依赖协议的URL
原文:以双斜杠//开头的URL – 依赖协议的URL 不知道大家有没有见过下面这种 url 写法: <img src="//domain.com/img/logo.png"& ...
- 在Delphi中静态调用DLL
在Delphi中静态调用DLL top 调用一个DLL比写一个DLL要容易一些.首先给大家介绍的是静态调用方法,稍后将介绍动态调用方法,并就两种方法做一个比较.同样的,我们先举一个静态调用的例子. u ...
- Qt文档系统分析(解释Qt文档的生成工具与过程)
写在前面 只要打开Qt Assistant或Qt Creator的Help,或是打开在线版的 http://doc.qt.nokia.com ,Qt的漂亮的文档就会呈现在我们眼前.而且 Qt的文档,长 ...
- JS function document.onclick(){}报错Syntax error on token "function", delete this token - CSDN博客
原文:JS function document.onclick(){}报错Syntax error on token "function", delete this token - ...
- Windows 10预装应用太多?一个命令删除!
Windows 10预装了很多应用软件,虽然有些其实也不难用,但是使用率可能比较低,很多人也不喜欢预装的东西,而且还占空间,那么这些预装的如何彻底清除呢? 其实微软是自带了命令可以执行这样的操作,用它 ...