DFS(二):骑士游历问题
? ? ? 在國際象棋的棋盤(8行×8列)上放置一個馬,按照“馬走日字”的規(guī)則,馬要遍歷棋盤,即到達棋盤上的每一格,并且每格只到達一次。例如,下圖給出了騎士從坐標(1,5)出發(fā),游歷棋盤的一種可能情況。
【例1】騎士游歷問題。
? ? ? ?編寫一個程序,對于給定的起始位置(x0,y0),探索出一條路徑,沿著這條路徑騎士能遍歷棋盤上的所有單元格。
? ? ? ?(1)編程思路。
? ? ? ? 采用深度優(yōu)先搜索進行路徑的探索。深度優(yōu)先搜索用遞歸描述的一般框架為:
? ? void? dfs(int deep)????? //? 對deep層進行搜索
??? {
????????? if? (符合某種要求||已經(jīng)不能再搜了)
???????? {
?????????????? 按要求進行一些處理,一般為輸出;
?????????????? return ;
???????? }
???????? if?? (符合某種條件且有地方可以繼續(xù)搜索的)?? // 這里可能會有多種情況,可能要循環(huán)什么的
??????? {
? ? ? ? ? ? ?vis[x][y]=1;?? ????????????????????//? 表示結(jié)點(x,y)已訪問到
? ? ? ? ? ? ?dfs(deep+1);??????????????????? //? 搜索下一層
? ? ? ? ? ? ?vis[x][y]=0;????????????????????? // 改回來,表示結(jié)點(x,y)以后可能被訪問
??????? }
??? }?
? ? ? 定義數(shù)組int vis[10][10]記錄騎士走到的步數(shù),vis[x][y]=num表示騎士從起點開始走到坐標為(x,y)的格子用了num步(設(shè)起點的步數(shù)為1)。初始時vis數(shù)組元素的值全部為0。
#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("請輸入棋盤的行數(shù)和列數(shù)(均小于10):");
?? scanf("%d %d",&N,&M);
?? printf("請輸入出發(fā)點坐標:(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)編程思路。
? ? ? ?同樣用深度優(yōu)先搜索。但由于題目要輸出字典序最小的,所以遍歷時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;?
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/cs-whut/p/11153529.html
總結(jié)
以上是生活随笔為你收集整理的DFS(二):骑士游历问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 值传递 引用传递(传地址,传引用)的区别
- 下一篇: svn服务安装和配置 以及搭配Eclip