日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DFS(二):骑士游历问题

發布時間:2025/5/22 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;?

}

?

轉載于:https://www.cnblogs.com/cs-whut/p/11153529.html

總結

以上是生活随笔為你收集整理的DFS(二):骑士游历问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产喷白浆一区二区三区 | 激情文学av | 床戏高潮做进去大尺度视频 | 亚洲国产成人av | 国产精品视频网站 | 国产精品久久福利 | 久久综合99 | 看了让人下面流水的视频 | 殴美一级视频 | 久久精品国产亚洲AV黑人 | 久久6视频 | 黄色大片视频网站 | 艳母日本动漫在线观看 | 亚洲精品国产日韩 | 亚洲激情黄色 | 日韩啊v | 中文字幕一区二区在线老色批影视 | 中文字幕三级电影 | 国产一级免费在线观看 | 亚洲操 | 国产成人久久久 | 米奇久久 | av中文在线 | 国产精品污 | 精品人伦一区二区三区蜜桃网站 | 欧美猛操 | 经典三级第一页 | 午夜激情一区二区 | 欧美一级免费黄色片 | 日韩欧美一级大片 | 大黑人交xxx极品hd | 日韩视频免费观看高清完整版 | 神马久久久久久久久久 | 国产又爽又猛又粗的视频a片 | 亚洲国产欧美在线 | 特级西西444www大胆免费看 | 火影忍者羞羞漫画 | 欧美日韩中日 | 亚洲一区精品在线 | 在线天堂在线 | 高清视频一区二区 | 日韩欧美在线中文字幕 | 亚洲精品欧美精品 | 国产探花在线观看 | 色戒未删节版 | 成年人免费看视频 | 日韩一区二区三区精 | 99热最新在线 | 香蕉视频一区二区三区 | 天天艹天天操 | 少妇被躁爽到高潮无码文 | 动漫玉足吸乳羞免费网站玉足 | 色婷婷精品久久二区二区密 | 谁有毛片网站 | 国产黑丝91| 一区二区三区国产精品 | 痴女扩张宫交脱垂重口小说 | 另类捆绑调教少妇 | 色94色欧美 | 免费三片在线视频 | 日本丰满少妇裸体自慰 | 熟女毛片| 欧美性色黄大片手机版 | 日韩爽爽视频 | 原来神马电影免费高清完整版动漫 | 日韩久| 国产日韩在线播放 | 国产麻豆精品一区 | av番号网 | 日本亚洲一区二区三区 | 日韩国产欧美在线观看 | 91在线免费看 | 99热国产| 激情欧美一区二区 | 亚洲精品aaaaa | 久久b| 91一区二区三区在线观看 | 亚洲国产中文字幕在线观看 | 欧美黑人又粗又大高潮喷水 | 麻豆视频观看 | 久久人人爽人人爽人人片亚洲 | av网在线| 青青草在线免费观看 | 日韩三级网 | 日韩精品啪啪 | a级黄色小说 | 一区二区久久久 | 色啪网站 | 日本视频免费观看 | 蜜臀久久99精品久久久画质超高清 | 国产高清在线视频观看 | 在线看成人 | 国产成人精品免费网站 | 国产精品乱码一区二区三区 | 恶虐女帝安卓汉化版最新版本 | 成人激情片 | 久久超碰精品 | 黄色视屏在线播放 | 成人免费一区 |