各位大虾 帮助小弟一把 谁有9宫算法,要求是运用启发式搜索和深度搜索的

yhl1978 2001-03-14 09:09:00
因课程需要急需9宫算法
要求是运用启发式搜索和深度搜索的
如大虾有的话请帮小弟一个忙,小弟一定给分
我的邮箱是martin_yhl@sohu.com 十分感谢!!!!
...全文
143 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
nobel 2001-04-16
  • 打赏
  • 举报
回复
本人写了一个广度优先算法, 用于八数码难题求解,不过使用pascal需要请与
elle_xian@263.net联系
zhangbin 2001-04-06
  • 打赏
  • 举报
回复
#include "stdio.h"
main()
{
int x,y,num,dir[4][4];
x=1;y=2;
for(num=1;num<=9;num++) {
dir[x][y]=num;
if((num%3)==0) y=y+1;
else {
x=x-1;
y=y+1;
if(x==0) x=3;
if(y==4) y=1; }
}

for(x=1;x<=3;x++) {
printf("\n");
for(y=1;y<=3;y++) printf("%d ",dir[x][y]);
}
}
ding 2001-04-04
  • 打赏
  • 举报
回复
这些基本的问题还是自己写的好,可以锻炼一下。不过我的这些代码你可以参考一下:
//这段代码是15个数从一种状态转变成另一种状态,运用了限界剪枝,即A*算法
#include <stdio.h>
#include <stdlib.h>

#define N 4
#define NULL 0

typedef struct node {

int matrix[N+1][N+1];
/* This matrix is the current status. */

int x,y;
/* These dataes will be the position of the ZERO. */

int value;
/* This proterties will be the actual value of this node. */

struct node *parent,*next;
/* The data point its father node. By it, the program
can output all the required nodes when find a desition node. */

} NODE; /* Define the struct NODE */

int action[5][2] = {{0,0},{-1,0},{0,1},{1,0},{0,-1}};
/* Define the action type */

int source[N+1][N+1] = {
{0, 0, 0, 0, 0},
{0, 1, 2, 3, 4},
{0, 5, 6, 0, 8},
{0, 9,10, 7,11},
{0,13,14,15,12}};
/* This is the initilize status */

int desition[N+1][N+1] = {
{0, 0, 0, 0, 0},
{0, 1, 2, 3, 4},
{0, 5, 6, 8, 0},
{0, 9,10, 7,11},
{0,13,14,15,12}};
/* This is the desition status. */

NODE *head,*tail;
/* This list will store all nodes which is moved out
of the priority queue. */

NODE *head1;
/* This point willl point to the priority queue. */

int steps = 0;

void main(void)
{

/* The MAIN function include the control process.
The control process is as follow:
First,the fuction will create a priority queue. All the child nodes of the
current node will be put into the queue. The process will choose the best
node (another function CalVal() will calculate every child's value) to be
the next current node. Continue by this way till find the desition node. */

int CalVal (NODE *);
void PRINT (NODE *);
void INSERT (NODE *);
int EQUAL (NODE *);
int Exist (NODE *);
void DELETEMIN(void);

NODE *p,*current;
/* the variable will point to the priority queue. */
int i,j,k;

system("cls");
printf("The Program Is Searching...");
printf("\n\t\t\t<<<<WAITTING>>>>\n");
head = (NODE *)malloc(sizeof(NODE));
head->next = NULL;

head1 = (NODE *)malloc(sizeof(NODE));
for(i = 1;i <= N;i++)
for(j = 1;j <= N;j++) head1->matrix[i][j] = source[i][j];
/* Copy the source matrix into the head node. */

for(i = 1;i <= N;i++)
for(j = 1;j <= N;j++)
if (source[i][j] == 0)
{
head1->x = i;
head1->y = j;
}
/* Find the initalize position. */

head1->value = -1;
head1->next = NULL;
head1->parent = NULL;
tail = head;

p = (NODE *)malloc(sizeof(NODE));
/* allocte a temp memory. */

current = head1;
while(EQUAL(current) != 1)
/* If the current node is not an answer node. */
{
DELETEMIN();
/* the current value is the best child node in the priority queue. */
for(i = 1;i <= 4;i++)
{
if((current->x+action[i][0] <= N&¤t->x+action[i][0] >= 1)
&&(current->y+action[i][1] <= N&¤t->x+action[i][1] >= 1))
/* If the node is fit */
{
for(j = 1;j <= N;j++)
for(k = 1;k <= N;k++)
p->matrix[j][k] = current->matrix[j][k];
p->x = current->x+action[i][0];
p->y = current->y+action[i][1];
j = p->matrix[p->x][p->y];
p->matrix[p->x][p->y] = 0;
p->matrix[current->x][current->y] = j;
p->value = CalVal(p);
/* Copy the current node into a tempelete node, and make changes.
p->parent will be changed by the fuction DELETEMIN(NODE *). */

if(!Exist(p))
INSERT(p);
/* Insert the tempelete node into the priority queue. */
}
}
/* Extract all child nodes of the current node. */

current = head1;
}

if(EQUAL(current) == 1)
{
printf("\nFIND SUCCESS!\n");
current->parent = tail;
PRINT(current);
printf("\nTHE STEPS NEEDED IS :%d ",steps);
}
else
printf("There is no way!");

getch();
}

int CalVal(NODE *p)
{
int i,j,count = 0;

/* The result is the number of the numbers which is
not equal to the desition. */
for(i = 1;i <= N;i++)
for(j = 1;j <= N;j++)
if(p->matrix[i][j] != desition[i][j]) count++;
/*
while(p->parent != NULL)
{
count++;
p = p->parent;
}
*/
return count;
}

int EQUAL(NODE *p)
{
if(CalVal(p) == 0)
return 1;
else
return -1;
}

void PRINT(NODE *p)
{
int i,j;

if(p->parent == NULL)
{
for(i = 1;i <= 4;i++)
{
printf("\n");
for(j = 1;j <= 4;j++) printf("%5d",p->matrix[i][j]);
}
}
else
{
PRINT(p->parent);
steps++;
printf("\n ======>>");
for(i = 1;i <= 4;i++)
{
printf("\n");
for(j = 1;j <= 4;j++) printf("%5d",p->matrix[i][j]);
}
}
}

void INSERT(NODE *p)
{
NODE *p1,*t;
int i,j;

p1 = head1;

while(p1->value <= p->value && p1->next != NULL) p1 = p1->next;
/* Locate the first node whose value is less than the new node. */

t = (NODE *)malloc(sizeof(NODE));
for(i = 1;i <= N;i++)
for(j = 1;j <= N;j++)
t->matrix[i][j] = p->matrix[i][j];
t->value = p->value;
t->x = p->x; t->y = p->y;

if(p1 == head1&&p1 == NULL) {
head1 = t;
t->next = NULL;
head1->parent = NULL;
}

if(p1->next == NULL)
/* If the new node is greater than all other nodes. */
{
t->next = NULL;
p1->next = t;
t->parent = p1;
}

else
/* If the new node is less than a specious node. */
{
if(p1 != head1)
{
t->next = p1;
t->parent = p1->parent;
p1->parent = t;
t->parent->next = t;
}
else
{
t->next = head1;
t->parent = NULL;
head1->parent = t;
head1 = t;
}
}
}

void DELETEMIN()
{
if(head == tail&&head == NULL)
{
head = head1;
head1 = head1->next;
head->next = NULL;
tail = head;
head->parent = 0;
}
else
{
tail->next = head1;
head1->parent = tail;
tail = head1;
if(head1->next == NULL) head1 = NULL;
else head1 = head1->next;
tail->next = NULL;
}
}

int Exist(NODE *p)
{
NODE *p1 = head;
int result = 1,i,j;

while(p1 != NULL)
{
result = 1;
for(i = 1;i <= N;i++)
for(j = 1;j <= N;j++)
if(p1->matrix[i][j] != p->matrix[i][j]) result = 0;
if (result == 1) return 1;
p1 = p1->next;
}

return 0;
}
xyjdn 2001-04-04
  • 打赏
  • 举报
回复
hi
hyqryq 2001-03-15
  • 打赏
  • 举报
回复
自己写一个吧,用A*算法.

33,028

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧