基本思路:
广度优先遍历,每次找到1的位置,分别向上、向下、向左、向右移动。把移动后的每个状态存储到队列中,弹出队头,判断是否为最终结果状态,如果是,输出遍历的层数(即移动步数),如果不是,把现阶段状态继续执行找到1向上向下向左向右移动操作。
?
|
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;
}
|
有个问题,就是还没弄懂,怎么判断给定初始状态无解,即不可能到达最终结果状态??
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
猜你喜欢
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 17
-
2025-05-25 62
-
2025-05-29 56
-
2025-05-26 76
-
2025-05-29 33
热门评论



