油克小学期(三)图搜索(3)九宫格路径求解及基于递归的求解

 代码实现:

(1)九宫格路径求解:

```c
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAXSIZE 3
#define ZERO 0
typedef int NODETYPE[MAXSIZE][MAXSIZE];
typedef struct  //用启发式搜索的结构,方便复用 
{
	NODETYPE node;
	//float evaluate;//不符的方格数,记录未来开销
	//float step;//直到当前节点为止搜索过的步数
}NODE;
typedef struct NEIGHBORNODE
{
	NODE node;
	struct NEIGHBORNODE* next;
}*NEIGHBOR, * PATH;
typedef struct ANODE
{
	NODE node;
	NEIGHBOR adjacents;
}ANYNODE;
typedef struct GRAPHNODE
{
	ANYNODE node;
	struct GRAPHNODE* next;
}*GRAPH;
typedef struct Paths
{
	PATH path;
	struct Paths* next;
}*PATHS, * STACK;



void setvalue(NODE* node1, NODE* node2)/**/
{
	int i, j;
	for (i = 0; i < MAXSIZE; i++)
	{
		for (j = 0; j < MAXSIZE; j++)
		{
			node1->node[i][j] = node2->node[i][j];
		}
	}
	//node1->evaluate = node2->evaluate;
	//node1->step = node2->step;
}
int equal(NODE* node1, NODE* node2)/**/
{
	int i, j;
	int flag = 1;
	for (i = 0; i < MAXSIZE; i++)
	{
		for (j = 0; j < MAXSIZE; j++)
		{
			if (node1->node[i][j] != node2->node[i][j])
			{
				flag = 0;
				break;
			}
		}
		if (flag == 0) break;
	}
	return flag;
}
void priNode(NODE* node)/**/
{
	int i, j;
	for (i = 0; i < MAXSIZE; i++)
	{
		for (j = 0; j < MAXSIZE; j++)
		{
			printf("   %d", node->node[i][j]);
		}
		printf("\n");
	}
	printf("===================\n");
}


int Locate(NODE* node, int* hori, int* vert, int zero)/**/
{
	int i, j;
	int flag = 0;
	for (i = 0; i < MAXSIZE; i++)
	{
		for (j = 0; j < MAXSIZE; j++)
			if (node->node[i][j] == zero)
			{
				*hori = i;
				*vert = j;
				flag = 1;
				break;
			}
		if (flag) break;
	}
	return flag;
}
void Exchange(NODE* node, int hori1, int vert1, int hori2, int vert2)/**/
{
	int tempnode;
	tempnode = node->node[hori1][vert1];
	node->node[hori1][vert1] = node->node[hori2][vert2];
	node->node[hori2][vert2] = tempnode;
}



int isInPath(NODE node, PATH path)
{
	PATH p = path;
	int flag = 0;
	while (p)
	{
		if (equal(&node, &p->node) == 1)
		{
			flag = 1;
			break;
		}
		else
		{
			p = p->next;
		}
	}
	return flag;
}
NEIGHBOR deleteNodeInPath(NEIGHBOR adjacents, PATH path)
{
	NEIGHBOR n1 = adjacents, n2 = NULL;
	int flag = 0;
	while (n1)
	{
		flag = isInPath(n1->node, path);
		if (flag == 1)
		{
			if (n1 == adjacents)
			{
				adjacents = n1->next;
				free(n1);
				n1 = adjacents;
			}
			else
			{
				n2->next = n1->next;
				free(n1);
				n1 = n2->next;
			}
		}
		else
		{
			n2 = n1;
			n1 = n1->next;
		}
	}
	return adjacents;
}



NEIGHBOR addANEIGHBOR(NEIGHBOR br, NODE* node) //给兄弟节点群加节点   
{
	NEIGHBOR b, pb;
	b = (NEIGHBOR)malloc(sizeof(struct NEIGHBORNODE));
	setvalue(&(b->node), node);//Set(b->node,node,SetValue);//node赋值给b 
	b->next = NULL;
	if (br == NULL) //无兄弟节点 
		br = b;
	else
	{
		pb = br;
		while (pb->next)pb = pb->next;
		pb->next = b;
	}
	return br;
}
NEIGHBOR clearNeighbors(NEIGHBOR br)
{
	NEIGHBOR br1 = NULL;
	while (br)
	{
		br1 = br;
		br = br->next;
		free(br1);
	}
	return br;
}



NEIGHBOR copyNEIGHBORs(NEIGHBOR children)    //复制子节点集
{
	NEIGHBOR copynode, lastnode = NULL, head = NULL;
	while (children)
	{
		copynode = (NEIGHBOR)malloc(sizeof(struct NEIGHBORNODE));
		setvalue(&(copynode->node), &(children->node));//Set(copynode->node,children->node,SetValue);
		copynode->next = NULL;
		if (head == NULL)
			head = copynode;
		else
			lastnode->next = copynode;
		lastnode = copynode;
		children = children->next;
	}
	return head;
}
NEIGHBOR ExpandNodes(NODE* node, int zero) /**/
{
	NEIGHBOR adjacents = NULL;
	int r = 0, c = 0;
	NODE* tp = (NODE*)malloc(sizeof(NODE));
	if (!Locate(node, &r, &c, zero))
	{
		return adjacents;
	}
	if (r == 0 && c == 0)
	{
		setvalue(tp, node);							//对九宫格赋值
		Exchange(tp, r, c, r + 1, c);			    //交换位置
		adjacents = addANEIGHBOR(adjacents, tp);	//收集新的节点

		setvalue(tp, node);
		Exchange(tp, r, c, r, c + 1);
		adjacents = addANEIGHBOR(adjacents, tp);
	}
	else if (r == 0 && c == 1)
	{
		setvalue(tp, node);
		Exchange(tp, r, c, r, c - 1);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r, c + 1);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r + 1, c);
		adjacents = addANEIGHBOR(adjacents, tp);
	}
	else if (r == 0 && c == 2)
	{
		setvalue(tp, node);
		Exchange(tp, r, c, r, c - 1);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r + 1, c);
		adjacents = addANEIGHBOR(adjacents, tp);
	}
	else if (r == 1 && c == 0)
	{
		setvalue(tp, node);
		Exchange(tp, r, c, r - 1, c);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r + 1, c);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r, c + 1);
		adjacents = addANEIGHBOR(adjacents, tp);
	}
	else if (r == 1 && c == 1)
	{
		setvalue(tp, node);
		Exchange(tp, r, c, r - 1, c);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r + 1, c);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r, c - 1);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r, c + 1);
		adjacents = addANEIGHBOR(adjacents, tp);
	}
	else if (r == 1 && c == 2)
	{
		setvalue(tp, node);
		Exchange(tp, r, c, r - 1, c);
		adjacents = addANEIGHBOR(adjacents, tp);


		setvalue(tp, node);
		Exchange(tp, r, c, r + 1, c);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r, c - 1);
		adjacents = addANEIGHBOR(adjacents, tp);
	}
	else if (r == 2 && c == 0)
	{


		setvalue(tp, node);
		Exchange(tp, r, c, r - 1, c);
		adjacents = addANEIGHBOR(adjacents, tp);
		setvalue(tp, node);
		Exchange(tp, r, c, r, c + 1);
		adjacents = addANEIGHBOR(adjacents, tp);
	}
	else if (r == 2 && c == 1)
	{
		setvalue(tp, node);
		Exchange(tp, r, c, r - 1, c);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r, c - 1);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r, c + 1);
		adjacents = addANEIGHBOR(adjacents, tp);
	}
	else if (r == 2 && c == 2)
	{
		setvalue(tp, node);
		Exchange(tp, r, c, r - 1, c);
		adjacents = addANEIGHBOR(adjacents, tp);

		setvalue(tp, node);
		Exchange(tp, r, c, r, c - 1);
		adjacents = addANEIGHBOR(adjacents, tp);
	}
	return adjacents;
}


PATH addANodeToPath(NODE* node, PATH path)
{
	PATH p;
	p = (NEIGHBOR)malloc(sizeof(struct NEIGHBORNODE));
	setvalue(&(p->node), node);
	if (path == NULL)//路径上的第一个节点
		p->next = NULL;
	else
		p->next = path;
	path = p;//倒序加入
	return path;
}
PATH copyPath(PATH path)//复制路径   
{
	PATH tempath;
	tempath = (PATH)copyNEIGHBORs((NEIGHBOR)path);
	return tempath;
}
struct Paths* copyPaths(struct Paths* Paths)//复制路径集合  
{
	struct Paths* copynode, * lastnode = NULL, * head = NULL;
	while (Paths)
	{
		copynode = (struct Paths*)malloc(sizeof(struct Paths));
		copynode->path = copyPath(Paths->path);
		copynode->next = NULL;
		if (head == NULL)
		{
			head = copynode;
		}
		else
		{
			lastnode->next = copynode;
		}
		lastnode = copynode;
		Paths = Paths->next;
	}
	return head;
}

void RevPath(PATH path)
{
	int num = 0, j = 0;
	NODE* nodes;
	PATH p = path;
	while (p)
	{
		p = p->next;
		num++;   //计数器 
	}
	nodes = (NODE*)malloc(num * sizeof(NODE));//存路径
	for (j = 0, p = path; p; p = p->next, j++)
	{
		setvalue(&(nodes[j]), &(p->node));
	}
	for (j = num - 1, p = path; p; p = p->next, j--)
	{
		setvalue(&(p->node), &(nodes[j]));//倒序存入 

	}
	free(nodes);
}
void priNEIGHBORs(NEIGHBOR NEIGHBORs)
{
	NEIGHBOR p = NEIGHBORs;
	if (p == NULL)
		printf("NULL");
	while (p)
	{
		priNode(&(p->node));
		p = p->next;
	}
}


struct Paths* addAPathToPaths(PATH path, struct Paths* Paths)
{
	PATH copypath;
	struct Paths* ps = NULL, * p;
	if (path == NULL) return Paths;
	copypath = copyPath(path);
	ps = (struct Paths*)malloc(sizeof(struct Paths));
	ps->path = copypath;
	ps->next = NULL;
	if (Paths == NULL) Paths = ps;
	else
	{
		p = Paths;
		while (p->next) p = p->next;
		p->next = ps;
	}
	return Paths;

}
struct Paths* formPathsFromNodes(NEIGHBOR adjacents, PATH path, struct Paths* Paths)
{
	PATH tempath;
	adjacents = deleteNodeInPath(adjacents, path);
	while (adjacents)
	{
		tempath = copyPath(path);
		tempath = addANodeToPath(&(adjacents->node), tempath);
		Paths = addAPathToPaths(tempath, Paths);
		adjacents = adjacents->next;
	}
	return Paths;
}
PATH clearPath(PATH path)
{
	path = (PATH)clearNeighbors((NEIGHBOR)path);
	return path;
}
struct Paths* clearPaths(struct Paths* Paths)
{
	struct Paths* Paths1 = Paths;
	while (Paths)
	{
		Paths1 = Paths;
		clearPath(Paths1->path);
		Paths = Paths->next;
		free(Paths1);
	}
	return Paths;
}
STACK pushAPath(STACK stack, PATH path)
{
	PATH tempath;
	STACK st;
	tempath = copyPath(path);
	st = (struct Paths*)malloc(sizeof(struct Paths));
	st->path = tempath;
	if (stack == NULL) st->next = NULL;
	else
		st->next = stack;
	stack = st;
	return stack;
}
STACK pushPaths(STACK stack, struct Paths* Paths)
{
	struct Paths* p, * head;
	head = copyPaths(Paths);
	p = head;
	if (p != NULL) //每个栈链一起 
	{
		while (p->next) p = p->next;
		p->next = stack;
		stack = head;
	}
	return stack;
}

STACK PopANode_DepthfirstSearch_Sudoku(STACK stack, NODE* node, PATH* path)/**/
{
	STACK p = stack;
	PATH tempath;
	if (p != NULL)
	{
		tempath = p->path;
		setvalue(node, &(tempath->node));
		*path = copyPath(tempath);
		stack = p->next;
		free(p);
	}
	return stack;
}

STACK clearStack(STACK stack)
{
	stack = clearPaths((struct Paths*)stack);
	return stack;
}

int SearchPath(NODE* start, NODE* end, PATH* path, int zero)
{
	NODE node;
	NEIGHBOR adjacents;
	STACK stack = NULL;
	int flag = 0;
	PATH tempath = NULL;
	struct Paths* paths = NULL;
	tempath = addANodeToPath(start, tempath);
	stack = pushAPath(stack, tempath);
	while (stack)
	{
		tempath = clearPath(tempath);
		stack = PopANode_DepthfirstSearch_Sudoku(stack, &node, &tempath);//弹出
		if (equal(end, &node) == 1)
		{
			flag = 1;
			*path = copyPath(tempath);
			break;
		}
		adjacents = ExpandNodes(&node, zero);
		paths = formPathsFromNodes(adjacents, tempath, paths);
		stack = pushPaths(stack, paths);
		paths = clearPaths(paths);
	}
	clearStack(stack);
	return flag;
}
int LengthOfPath(PATH path)
{
	int count = 0;
	PATH p = path;
	while (p)
	{
		count++;
		p = p->next;
	}
	return count;
}

int main(void) {
	int count;
	STACK stack = NULL;
	PATH path = NULL;
	int flag;
	NODE start = { {{1,2,3},{8,4,0},{7,6,5}} };
	NODE end = { {{1,2,3},{8,0,4},{7,6,5}} };
	printf("开始:\n");
	priNode(&start);
	printf("结束:\n");
	priNode(&end);
	flag = SearchPath(&start, &end, &path, ZERO);
	printf("路径:\n");
	RevPath(path);
	priNEIGHBORs(path);
	printf("\n总步数= %d ,查找结果= %d\n", LengthOfPath(path), flag);
	printf("======================\n");
	clearPath(path);
}

 

(2)基于递归的九宫格路径求解

```c
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAXSIZE 3
#define ZERO 0
typedef int NODETYPE[MAXSIZE][MAXSIZE];
typedef struct  //用启发式搜索的结构,方便复用 
{
	NODETYPE node;
	float evaluate;//不符的方格数,记录未来开销
	double step;//直到当前节点为止搜索过的步数
}NODE;
typedef struct NEIGHBORNODE
{
	NODE node;
	struct NEIGHBORNODE* next;
}*NEIGHBOR, * PATH;
typedef struct ANODE
{
	NODE node;
	NEIGHBOR adjacents;
}ANYNODE;
typedef struct GRAPHNODE
{
	ANYNODE node;
	struct GRAPHNODE* next;
}*GRAPH;




void setvalue(NODE* node1, NODE* node2)/**/
{
	int i, j;
	for (i = 0; i < MAXSIZE; i++)
	{
		for (j = 0; j < MAXSIZE; j++)
		{
			node1->node[i][j] = node2->node[i][j];
		}
	}
	node1->evaluate = node2->evaluate;
	node1->step = node2->step;
}
int equal(NODE* node1, NODE* node2)/**/
{
	int i, j;
	int flag = 1;
	for (i = 0; i < MAXSIZE; i++)
	{
		for (j = 0; j < MAXSIZE; j++)
		{
			if (node1->node[i][j] != node2->node[i][j])
			{
				flag = 0;
				break;
			}
		}
		if (flag == 0) break;
	}
	return flag;
}
void priNode(NODE* node)/**/
{
	int i, j;
	for (i = 0; i < MAXSIZE; i++)
	{
		for (j = 0; j < MAXSIZE; j++)
		{
			printf("   %d", node->node[i][j]);
		}
		printf("\n");
	}
	//printf("难度=%f\n", node->evaluate);
	//printf("步数=%f\n", node->step);
	printf("===================\n");
}


int Locate(NODE* node, int* hori, int* vert, int zero)/**/
{
	int i, j;
	int flag = 0;
	for (i = 0; i < MAXSIZE; i++)
	{
		for (j = 0; j < MAXSIZE; j++)
			if (node->node[i][j] == zero)
			{
				*hori = i;
				*vert = j;
				flag = 1;
				break;
			}
		if (flag) break;
	}
	return flag;
}
void Exchange(NODE* node, int hori1, int vert1, int hori2, int vert2)/**/
{
	int tempnode;
	tempnode = node->node[hori1][vert1];
	node->node[hori1][vert1] = node->node[hori2][vert2];
	node->node[hori2][vert2] = tempnode;
}



float Evaluate(NODE* node1, NODE* node2)  //计算难度   /**/
{
	int i, j;
	float res = 0;
	for (i = 0; i < MAXSIZE; i++)
		for (j = 0; j < MAXSIZE; j++)
			if (node1->node[i][j] != node2->node[i][j])
				res++;
	return res;
}
void EvaluateAdjacents(NEIGHBOR adjacents, NODE* node, NODE* end)//获取难度与已走步数   /**/
{
	NEIGHBOR br = adjacents;
	while (br)
	{
		br->node.evaluate = Evaluate(&(br->node), end);
		br->node.step = node->step + 1;
		br = br->next;
	}
}


int isInPath(NODE node, PATH path)
{
	PATH p = path;
	int flag = 0;
	while (p)
	{
		if (equal(&node, &p->node) == 1)
		{
			flag = 1;
			break;
		}
		else
		{
			p = p->next;
		}
	}
	return flag;
}
NEIGHBOR deleteNodeInPath(NEIGHBOR adjacents, PATH path)
{
	NEIGHBOR n1 = adjacents, n2 = NULL;
	int flag = 0;
	while (n1)
	{
		flag = isInPath(n1->node, path);
		if (flag == 1)
		{
			if (n1 == adjacents)
			{
				adjacents = n1->next;
				free(n1);
				n1 = adjacents;
			}
			else
			{
				n2->next = n1->next;
				free(n1);
				n1 = n2->next;
			}
		}
		else
		{
			n2 = n1;
			n1 = n1->next;
		}
	}
	return adjacents;
}



NEIGHBOR addANEIGHBOR(NEIGHBOR br, NODE* node) //给兄弟节点群加节点   
{
	NEIGHBOR b, pb;
	b = (NEIGHBOR)malloc(sizeof(struct NEIGHBORNODE));
	setvalue(&(b->node), node);//Set(b->node,node,SetValue);//node赋值给b 
	b->next = NULL;
	if (br == NULL) //无兄弟节点 
		br = b;
	else
	{
		pb = br;
		while (pb->next)pb = pb->next;
		pb->next = b;
	}
	return br;
}
NEIGHBOR clearNeighbors(NEIGHBOR br)
{
	NEIGHBOR br1 = NULL;
	while (br)
	{
		br1 = br;
		br = br->next;
		free(br1);
	}
	return br;
}



NEIGHBOR copyNEIGHBORs(NEIGHBOR children)    //复制子节点集
{
	NEIGHBOR copynode, lastnode = NULL, head = NULL;
	while (children)
	{
		copynode = (NEIGHBOR)malloc(sizeof(struct NEIGHBORNODE));
		setvalue(&(copynode->node), &(children->node));//Set(copynode->node,children->node,SetValue);
		copynode->next = NULL;
		if (head == NULL)
			head = copynode;
		else
			lastnode->next = copynode;
		lastnode = copynode;
		children = children->next;
	}
	return head;
}
NEIGHBOR ExpandNodes(NODE *node, int zero) /**/ 
{
    NEIGHBOR adjacents = NULL;
    int h, v;
    NODE *tempnode = (NODE*)malloc(sizeof(NODE));
    if(!Locate(node, &h, &v, zero))
        return adjacents;
    
    if(h == 0 && v == 0)    
	{
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h + 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v + 1);
        adjacents = addANEIGHBOR(adjacents, tempnode);
    } else if(h == 0 && v == 1)  
	{
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v - 1);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v + 1);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        Exchange(tempnode, h, v, h + 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
    } else if(h == 0 && v == 2)    
	{
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v - 1);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h + 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
    } else if(h == 1 && v == 0) 
	{
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h - 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h + 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v + 1);
        adjacents = addANEIGHBOR(adjacents, tempnode);
    } else if(h == 1 && v == 1) 
	{
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h - 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h + 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v - 1);
        adjacents =addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v + 1);
        adjacents = addANEIGHBOR(adjacents, tempnode);
    } else if(h == 1 && v == 2) 
	{
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h - 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h + 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v - 1);
        adjacents = addANEIGHBOR(adjacents, tempnode);
    } else if(h == 2 && v == 0) 
	{
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h - 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v + 1);
        adjacents = addANEIGHBOR(adjacents, tempnode);
    } else if(h == 2 && v == 1) 
	{
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h - 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v - 1);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v + 1);
        adjacents = addANEIGHBOR(adjacents, tempnode);
    } else if(h == 2 && v == 2) 
	{
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h - 1, v);
        adjacents = addANEIGHBOR(adjacents, tempnode);
        
        setvalue(tempnode, node);
        Exchange(tempnode, h, v, h, v - 1);
        adjacents = addANEIGHBOR(adjacents, tempnode);
    }

    return adjacents;
}

PATH addANodeToPath(NODE* node, PATH path)
{
	PATH p;
	p = (NEIGHBOR)malloc(sizeof(struct NEIGHBORNODE));
	setvalue(&(p->node), node);
	if (path == NULL)//路径上的第一个节点
		p->next = NULL;
	else
		p->next = path;
	path = p;//倒序加入
	return path;
}
PATH copyPath(PATH path)//复制路径   
{
	PATH tempath;
	tempath = (PATH)copyNEIGHBORs((NEIGHBOR)path);
	return tempath;
}
 
void priNEIGHBORs(NEIGHBOR NEIGHBORs)
{
	NEIGHBOR p = NEIGHBORs;
	if (p == NULL)
		printf("NULL");
	while (p)
	{
		priNode(&(p->node));
		p = p->next;
	}
}




PATH clearPath(PATH path)
{
	path = (PATH)clearNeighbors((NEIGHBOR)path);
	return path;
}



int SearchSubGraph(NODE *current, NODE *end, PATH* path, PATH* searchpath, int zero) {
	NEIGHBOR adjacents;
	int flag = 0;
	if (isInPath(*current, *searchpath) == 1) {
		return flag;
	}
	if (equal(current,end) == 1)
		flag = 1;
	else {
		*searchpath = addANodeToPath(current, *searchpath);
		adjacents = ExpandNodes(current, zero);
		while (adjacents) {
			if (isInPath(adjacents->node, *searchpath) == 0) {
				flag = SearchSubGraph(&adjacents->node, end, path, searchpath, zero);
				if (flag == 1) {
					*path = addANodeToPath(&adjacents->node, *path);
					break;
				}
			}
			adjacents = adjacents->next;
		}
	}
	return flag;
}
int SearchPath(NODE *start,NODE *end,PATH *path,int zero)
{
	int flag;
	PATH searchpath=NULL;
	flag=SearchSubGraph(start,end,path,&searchpath,zero);
	if(flag==1) *path=addANodeToPath(start,*path);
	clearPath(searchpath);
	return flag;
}



int LengthOfPath(PATH path)
{
	int count = 0;
	PATH p = path;
	while (p)
	{
		count++;
		p = p->next;
	}
	return count;
}

int main(void) 
{
	PATH path = NULL;
	int flag;
	NODE start = { {{1,2,3},{8,4,0},{7,6,5}},0,0 };
	NODE end = { {{1,2,3},{0,8,4},{7,6,5}} };
	printf("开始:\n");
	priNode(&start);
	printf("结束:\n");
	priNode(&end);
	flag = SearchPath(&start, &end, &path, ZERO);
	printf("路径:\n");
	//RevPath(path);
	priNEIGHBORs(path);
	printf("总结:\n\n总步数= %d ,查找结果= %d\n", LengthOfPath(path), flag);  //为什么path->node.step显示不出来,flag也跟着显示不出来??? 
	printf("======================\n");                                       //肯定是指针的问题,可明明传了path的地址去操作了啊??? 
	clearPath(path);
}


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北城学神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值