日韩性视频-久久久蜜桃-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(二):骑士游历问题的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 亚洲在线观看免费 | 无码gogo大胆啪啪艺术 | 午夜三级在线观看 | 美女久久视频 | 黄金网站在线观看 | 国产一区二区毛片 | 天天操天天操天天操天天操天天操 | 精品国产毛片 | 九九热re| 精品九九视频 | 久久精品无码一区 | av片大全| 亚洲一二三四在线观看 | 人妻一区二区三 | 日日好av| 尤物精品在线观看 | 亚洲国产精品久久人人爱 | 国产精品福利一区二区三区 | 校园春色综合 | 欧美大尺度做爰啪啪床戏明星 | 免费看污的网站 | 欧美色综合网 | 在线观看国产小视频 | 3d欧美精品动漫xxxx无尽 | 国产精品福利一区二区 | 这里只有精品久久 | 欧美日本另类 | 国产精品无码无卡无需播放器 | 黄色三级视频在线观看 | 毛片网在线| 伊人网免费视频 | 亚洲天堂免费视频 | www.久草.com | 亚洲69视频 | 免费观看国产精品视频 | 国产奶头好大揉着好爽视频 | 欧美日韩精品在线观看 | 国产你懂得 | 视频一区二区三区精品 | 黄色三级三级三级三级 | 丰满女人又爽又紧又丰满 | 极品91尤物被啪到呻吟喷水 | 特级精品毛片免费观看 | 泰坦尼克号3小时49分的观看方法 | 日韩欧美高清在线 | 色婷视频 | 欧美高清hd19 | 午夜精品福利电影 | 男女激情视频网站 | 91网站免费 | 人操人 | 91久久久久久久久久久 | 男人的天堂影院 | 久久青青热| 欧美一级不卡视频 | 天天拍天天色 | 农村妇女精品一区二区 | 热热av| 国产精品视频入口 | 欧美色图中文字幕 | 激情小说在线 | 免费看国产曰批40分钟 | 亲嘴扒胸摸屁股免费视频日本网站 | 一区二区av在线 | 麻豆传媒网页 | 日韩色一区 | 午夜日韩精品 | www.com在线观看 | 久久99精品久久久久久水蜜桃 | 欧美一区二区在线视频 | 国产尤物在线 | 成人国产精品免费观看视频 | 午夜国产一区二区 | 亚洲天堂av在线免费观看 | 日日干夜夜爱 | 超碰福利在线 | 97香蕉碰碰人妻国产欧美 | 午夜不卡久久精品无码免费 | www.99re7.com| 亚洲自拍偷拍视频 | 国产精品一区二区免费看 | 国产做爰免费观看 | 亚洲男人皇宫 | 日韩av一区二区三区在线 | av大片网 | 91视频区 | 国产精品视频全国免费观看 | 久免费一级suv好看的国产 | 国产精品一区二区无码对白 | 一级性生活免费视频 | 91丨九色丨丰满 | 色女人天堂 | 久久精品视频一区二区三区 | 国产一级做a爰片久久毛片男男 | 天天想你在线观看完整版电影免费 | 黄色福利| 69精品丰满人妻无码视频a片 | 黄色av网站免费观看 | 欧美精品做受xxx性少妇 |