八数码问题 宽度优先遍历 状态压缩 双端遍历

 

#include<iostream>
#include<map>
using namespace std;
typedef struct
{
	int x;
	int w;
	char s;
}aaa;
map<int,int>a;
aaa dui[1000000],du[1000000];//两个队列
char www[1000000];
int main()
{
	int i,j;
	int many,ji,ji1,zan,zan2,x,y,tt,ww,ta,wa,q;
	int fang1[4][2]={-1,0,0,-1,1,0,0,1};//方向数组
	int fang2[4][2]={0,-1,-1,0,0,1,1,0};//方向数组
	int jin[9]={100000000,10000000,1000000,100000,10000,1000,100,10,1};
	char w[3][3];
	while(cin>>w[0][0])
	{
		cin>>w[0][1]>>w[0][2];
		for(i=1;i<3;i++)
			for(j=0;j<3;j++)
				cin>>w[i][j];
		many=0;
		char w2[9];
		for(i=0;i<3;i++)
			for(j=0;j<3;j++)
				w2[many++]=w[i][j];
		many=0;
		for(i=0;i<9;i++)
			for(j=0;j<i;j++)
				if((w2[i]<w2[j])&&w2[i]!='x'&&w2[j]!='x')
					many+=1;
		if(many%2==1)
		{
			printf("unsolvable\n");
			return(0);
		}
		int e,s;
		s=0;
		for(i=0;i<3;i++)
			for(j=0;j<3;j++)
			{
				if(w[i][j]!='x')s=10*s+w[i][j]-'0';
				else s=10*s+9;
			}
		tt=ww=ta=wa=1;
		e=123456789;
		dui[ww].w=s;
		du[wa].w=e;
		a[s]=1;
		a[e]=2;
		bool neng=false;
		if(s==e)neng=true;
		while(!neng)
		{
			ji=0;
			ji1=0;
			for(i=0;i<9;i++)
				if((dui[tt].w/jin[i])%10==9)
				{
					ji=i;
					break;
				}
			for(i=0;i<9;i++)
				if((du[ta].w/jin[i])%10==9)
				{
					ji1=i;
					break;
				}
			for(i=0;i<4;i++)
			{
				x=ji/3;
				y=ji%3;
				if(x+fang1[i][0]<3&&x+fang1[i][0]>=0&&y+fang1[i][1]<3&&y+fang1[i][1]>=0&&neng==false)
				{
					s=dui[tt].w;
					zan=s/jin[ji];
					zan2=s/jin[(x+fang1[i][0])*3+(y+fang1[i][1])];
					zan=zan%10;
					zan2=zan2%10;
					s=s-zan*jin[ji]-zan2*jin[(x+fang1[i][0])*3+(y+fang1[i][1])];
					s=s+zan2*jin[ji]+zan*jin[(x+fang1[i][0])*3+(y+fang1[i][1])];
					if(a[s]!=1)
					{
						if(a[s]==2)neng=true;
						a[s]=1;
						ww+=1;
						dui[ww].w=s;
						dui[ww].x=tt;
						if(i==0)dui[ww].s='u';
						if(i==1)dui[ww].s='l';
						if(i==2)dui[ww].s='d';
						if(i==3)dui[ww].s='r';
					}
				}
				x=ji1/3;
				y=ji1%3;
				if(x+fang2[i][0]<3&&x+fang2[i][0]>=0&&y+fang2[i][1]<3&&y+fang2[i][1]>=0&&neng==false)
				{
					s=du[ta].w;
					zan=s/jin[ji1];
					zan2=s/jin[(x+fang2[i][0])*3+(y+fang2[i][1])];
					zan=zan%10;
					zan2=zan2%10;
					s=s-zan*jin[ji1]-zan2*jin[(x+fang2[i][0])*3+(y+fang2[i][1])];
					s=s+zan2*jin[ji1]+zan*jin[(x+fang2[i][0])*3+(y+fang2[i][1])];
					if(a[s]==0)
					{
						a[s]=2;
						wa+=1;
						du[wa].w=s;
						du[wa].x=ta;
						if(i==0)du[wa].s='r';
						if(i==1)du[wa].s='d';
						if(i==2)du[wa].s='l';
						if(i==3)du[wa].s='u';
					}
				}
			}
			ta+=1;
			tt+=1;
		}
		ji=0;
		q=ww;
		while(q!=1)
		{
			ji+=1;
			www[ji]=dui[q].s;
			q=dui[q].x;
		}
		for(i=ji;i>=1;i--)printf("%c",www[i]);
		ji=0;
		q=1;
		while(du[q].w!=dui[ww].w)q++;
		while(q!=1)
		{
			ji+=1;
			www[ji]=du[q].s;
			q=du[q].x;
		}
		for(i=1;i<=ji;i++)printf("%c",www[i]);
		printf("\n");
		while(!a.empty())a.erase(a.begin());
	}
	return(0);
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是图的深度优先遍历宽度优先遍历的介绍: 深度优先遍历(DFS): 深度优先遍历是一种用于遍历或搜索树或图的算法。在这种搜索方法中,从根结点开始,尽可能深地搜索每个分支,直到到达最深的结点,然后回溯到前一个结点,继续搜索另一个分支,直到所有结点都被访问为止。深度优先遍历使用栈来实现,因此也称为“栈搜索”。 以下是深度优先遍历的Python代码示例: ```python # Python3 program to print DFS traversal # from a given given graph from collections import defaultdict # This class represents a directed graph using # adjacency list representation class Graph: # Constructor def __init__(self): # default dictionary to store graph self.graph = defaultdict(list) # function to add an edge to graph def addEdge(self, u, v): self.graph[u].append(v) # A function used by DFS def DFSUtil(self, v, visited): # Mark the current node as visited # and print it visited.add(v) print(v, end=' ') # Recur for all the vertices # adjacent to this vertex for neighbour in self.graph[v]: if neighbour not in visited: self.DFSUtil(neighbour, visited) # The function to do DFS traversal. It uses # recursive DFSUtil() def DFS(self, v): # Create a set to store visited vertices visited = set() # Call the recursive helper function # to print DFS traversal self.DFSUtil(v, visited) ``` 宽度优先遍历(BFS): 宽度优先遍历是一种用于遍历或搜索树或图的算法。在这种搜索方法中,从根结点开始,逐层遍历每个结点的所有子结点,直到到达最深的结点。宽度优先遍历使用队列来实现。 以下是宽度优先遍历的Python代码示例: ```python # Python3 program to print BFS traversal # from a given source vertex. BFS(int s) # traverses vertices reachable from s. from collections import defaultdict # This class represents a directed graph # using adjacency list representation class Graph: # Constructor def __init__(self): # default dictionary to store graph self.graph = defaultdict(list) # function to add an edge to graph def addEdge(self, u, v): self.graph[u].append(v) # Function to print a BFS of graph def BFS(self, s): # Mark all the vertices as not visited visited = [False] * (max(self.graph) + 1) # Create a queue for BFS queue = [] # Mark the source node as # visited and enqueue it queue.append(s) visited[s] = True while queue: # Dequeue a vertex from # queue and print it s = queue.pop(0) print(s, end=" ") # Get all adjacent vertices of the # dequeued vertex s. If a adjacent # has not been visited, then mark it # visited and enqueue it for i in self.graph[s]: if visited[i] == False: queue.append(i) visited[i] = True ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值