简单八数码问题深搜实现

                            2 8 3         1 2 3

  编写程序输出由  1 0 4 变为  8 0 4 的最短路径

                            7 6 5         7 6 5

一.简单深搜寻找最短步骤:

/* 一般用广搜寻着最短路径,因为他是按照层次来的(设置一个visit记录访问过的点),
只要找到最先到达目标的就行。
如果用深搜着最短路径,你就需要找一个最深搜到哪里,或者题目自带到哪里不可以再
搜的条件否则你就需要设置一个每条路径的最大深度,同时最后要寻找这些你找到的长 
度里面那个最小,最小的为输出长度。*/ 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int s[10][10];
int MIN = 1000;

bool Check()
{
	int k = 1;
	for(int i = 0; i<3; i++)
	{
		if(s[0][i] != k++)
		{
			return 0;
		}
	}
	k = 3;
	for(int i = 0; i<3; i++)
	{
		if(s[i][2] != k++)
		{
			return 0;
		}
	}
	k = 7;
	for(int i = 0; i<3; i++)
	{
		if(s[2][i] != k--)
		{
			return 0;
		}
	}
	if(s[1][0] != 8)
	return 0;
	return 1;
}

void Change(int x, int y, int z)//z 1234 is left up right down
{
	if(z == 1)//和左交换 
	{
		int i  = s[x][y-1];
		s[x][y-1] = s[x][y];
		s[x][y] = i;
	}
	else if(z == 2)//和上交换 
	{
		int i = s[x-1][y];
		s[x-1][y] = s[x][y];
		s[x][y] = i;
	}
	else if(z == 3)//和右交换 
	{
		int i = s[x][y+1];
		s[x][y+1] = s[x][y];
		s[x][y] = i;
	}
	else if(z == 4)//和下交换 
	{
		int i = s[x+1][y];
		s[x+1][y] = s[x][y];
		s[x][y] = i;
	}
}

void DFS(int x, int y, int step)//寻找最短路径要用广搜, 这是给定寻找方向来搜可以用深搜 
{
	if(Check())//finished
	{
		if(step<MIN)
		MIN = step;
		return;
	}
	if(step>10)
	return ;
	else
	{
		if(y-1>-1)//左 
		{
			Change(x, y, 1);
			DFS(x, y-1, step+1);
			Change(x, y, 1);//回溯 思想 没有找到就退回来 
		}
		if(x-1>-1)//上 
		{
			Change(x, y, 2);
			DFS(x-1, y, step+1);
			Change(x, y, 2);//回溯 
		}
		if(y+1<3)//右 
		{
			Change(x, y, 3);
			DFS(x, y+1, step+1);
			Change(x, y, 3);//回溯 
		}
		if(x+1<3)//下 
		{
			Change(x, y, 4);
			DFS(x+1, y, step+1);
			Change(x, y, 4);//回溯 
		}
	}
}

int main()
{
	int x, y;
	for(int i = 0; i<3; i++)
	{
		for(int j = 0; j<3; j++)
		{
			scanf("%d", &s[i][j]);
	        if(s[i][j] == 0)
	        {
	        	x = i;
	        	y = j;
			}
		}
	}
	DFS(x, y, 0);
	printf("%d\n", MIN);
	return 0;
}

二.用栈实现输出路径

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int s[10][10];
int MIN  = 1000;

typedef struct Outstack//用链表实现栈 
{
	int x, y;
	struct Outstack *next; 
}OStack, *OSt;

typedef struct stack//操作栈 
{
	int x, y;
	struct stack *next;
}Stack, *St;

OSt baseout;//输出栈 
OSt topout;
St base;//操作栈 
St top;

bool Check()
{
	int k = 1;
	for(int i = 0; i<3; i++)
	{
		if(s[0][i] != k++)
		{
			return 0;
		}
	}
	k = 3;
	for(int i = 0; i<3; i++)
	{
		if(s[i][2] != k++)
		{
			return 0;
		}
	}
	k = 7;
	for(int i = 0; i<3; i++)
	{
		if(s[2][i] != k--)
		{
			return 0;
		}
	}
	if(s[1][0] != 8)
	return 0;
	return 1;
}

void Change(int x, int y, int z)//z 1234 is left up right down
{
	if(z == 1)//和左交换 
	{
		int i  = s[x][y-1];
		s[x][y-1] = s[x][y];
		s[x][y] = i;
	}
	else if(z == 2)//和上交换 
	{
		int i = s[x-1][y];
		s[x-1][y] = s[x][y];
		s[x][y] = i;
	}
	else if(z == 3)//和右交换 
	{
		int i = s[x][y+1];
		s[x][y+1] = s[x][y];
		s[x][y] = i;
	}
	else if(z == 4)//和下交换 
	{
		int i = s[x+1][y];
		s[x+1][y] = s[x][y];
		s[x][y] = i;
	}
}

OSt opush(int x, int y, OSt topout)
{
	OSt p;
	p = (OSt)malloc(sizeof(OStack));
	topout->x = x;
	topout->y = y;
	p->next = topout;
	return p;
}

St push(int x, int y, St top)
{
	St p;
	p = (St)malloc(sizeof(Stack));
	top->x = x;
	top->y = y;
	p->next = top;
	return p;
}

void DFS(int x, int y, int step)
{
	if(step == 0)//等于零建立操作栈 
	{
		base = (St)malloc(sizeof(Stack));
		base->next = NULL;
		top = base; 
	}
	if(Check())
	{
		if(step<MIN)
		{
			MIN = step;
			topout = baseout; 
			St p = (St)malloc(sizeof(Stack));
			p = top->next;
			while(p != NULL)
			{
				topout = opush(p->x, p->y, topout);
                p = p->next; 			
			}
			return; 
		}
	}
	if(step>10)
	return; 
	else
	{
		if(y-1>-1)
		{
			Change(x, y, 1);
			top = push(x, y-1, top);//压栈 
			DFS(x, y-1, step+1);
			top = top->next;//回溯出栈 
			Change(x, y, 1);
		}
		if(x-1>-1)
		{
			Change(x, y, 2);
			top = push(x-1, y, top);
			DFS(x-1, y, step+1);
			top = top->next;
			Change(x, y, 2);
		}
		if(y+1<3)
		{
			Change(x, y, 3);
			top = push(x, y+1, top);
			DFS(x, y+1, step+1);
			top = top->next;
			Change(x, y, 3);
		}
		if(x+1<3)
		{
			Change(x, y, 4);
			top = push(x+1, y, top);
			DFS(x+1, y, step+1);
			top = top->next;
			Change(x, y, 4);
		}
	}
}


int main()
{
	int x, y;
	for(int i = 0; i<3; i++)
	{
		for(int j = 0; j<3; j++)
		{
			scanf("%d", &s[i][j]);
			if(s[i][j] == 0)
			{
				x = i;
				y = j;
			}
		}
	}
	baseout = (OSt)malloc(sizeof(OStack));
    baseout->next = NULL;
    topout = baseout;
	DFS(x, y, 0);
	printf("%d\n", MIN);
	topout = topout->next;
	while(topout!=NULL)
	{
		printf("%d, %d\n", topout->x, topout->y);
		topout = topout->next;
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值