C++ 实例之九宫格广度优先遍历

2025-05-27 0 80

C++ 实例之九宫格广度优先遍历

基本思路:

广度优先遍历,每次找到1的位置,分别向上、向下、向左、向右移动。把移动后的每个状态存储到队列中,弹出队头,判断是否为最终结果状态,如果是,输出遍历的层数(即移动步数),如果不是,把现阶段状态继续执行找到1向上向下向左向右移动操作。

C++ 实例之九宫格广度优先遍历

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106
#include<stdio.h>

typedef struct MyType

{

int number[3][3];int level;

}MyType;

MyType queue[10000];

MyType GetHead(int n)

{

return queue[n];

}

//是否为最终结果状态

int IsFind(MyType cur)

{

int flag=1;

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

{

if(cur.number[i][j]!=3*i+j+1)

{

flag=0;

break;

}

}

return flag;

}

int main()

{

int cnt=0;//队列中数量

int flag=0;//是否寻找到标记

int ans=0;//最小步数,也是扩展的层数

int head=0;//因为不是链表,用head来表示第一个

for(int m=0;m<3;m++)

{

for(int n=0;n<3;n++)

{

scanf("%d",&queue[cnt].number[m][n]);

}

}

queue[cnt].level=0;

cnt++;

while(cnt!=0)

{

//出站

MyType cur=GetHead(head++);

//判断是否为最终状态

flag=IsFind(cur);

if(flag==1)

{

printf("最小步数为:%d\\n",cur.level);

break;

}

else //不为最终状态,进行扩展

{

for(int row=0;row<3;row++)

for(int col=0;col<3;col++)

{

if(cur.number[row][col]==1) //找到1,进行扩展

{

//将1向上移

if(row!=0)

{

MyType temp=cur;

temp.number[row][col]=temp.number[row-1][col];

temp.number[row-1][col]=1;

temp.level=cur.level+1;

queue[cnt++]=temp;

}

//将1向右移动

if(col!=2)

{

MyType temp=cur;

temp.number[row][col]=temp.number[row][col+1];

temp.number[row][col+1]=1;

temp.level=cur.level+1;

queue[cnt++]=temp;

}

//将1向下移动

if(row!=2)

{

MyType temp=cur;

temp.number[row][col]=temp.number[row+1][col];

temp.number[row+1][col]=1;

temp.level=cur.level+1;

queue[cnt++]=temp;

}

//将1向左移动

if(col!=0)

{

MyType temp=cur;

temp.number[row][col]=temp.number[row][col-1];

temp.number[row][col-1]=1;

temp.level=cur.level+1;

queue[cnt++]=temp;

}

}

}

}

}

return 0;

}

C++ 实例之九宫格广度优先遍历

有个问题,就是还没弄懂,怎么判断给定初始状态无解,即不可能到达最终结果状态??

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 C++ 实例之九宫格广度优先遍历 https://www.kuaiidc.com/73410.html

相关文章

发表评论
暂无评论