我需要一个带有注释(文档)的代码,用于使用DFS的8个谜题...谢谢...... [英] I need a code with comments (documentation) for a 8 puzzle using a DFS...thank you...

查看:106
本文介绍了我需要一个带有注释(文档)的代码,用于使用DFS的8个谜题...谢谢......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,但我不明白如何摆脱malloc函数发生的错误。



  #include   <   stdio.h  >  
#include < span class =code-preprocessor> < conio.h >
#include < malloc.h >
< span class =code-keyword> #include < stdlib.h >
int initial [ 9 ] = { 2 8 3 1 6 4 7 0 5 }; // 初始和最终状态
int final [ 9 ] = { 1 2 3 4 5 6 7 8 0 };

int 移动[ 9 ] [ 4 ] = {
{ 1 3 , - 1 , - 1 },
{ 0 2 4 , - 1 },
{ 1 5 , - 1 , - 1 },
{ 4 0 6 , - 1 },
{ 3 1 5 ,< span class =code-digit> 7 },
{ 2 4 8 , - 1 },
{ 3 ,< span class =code-digit> 7 , - 1 , - 1 },
{ 4 6 8 , - 1 },
{ 5 7 , - 1 , - 1 }
};
struct qtype // queue
{
int board [ 9 ];
struct qtype * next;
struct qtype * parent;
} *打印;

struct node // linked_list
{
int check [ 9 ];
struct node * next;
} * list,* p;

typedef struct qtype QUEUE;
typedef struct node NODE;

QUEUE * rear = NULL,* tfront = NULL;

int qempty()
{ return tfront == NULL ; }

void qinsert( int * m)
{
int i;
QUEUE * newnode;
newnode =(QUEUE *)malloc( sizeof (QUEUE));
for (i = 0 ; i< 9; i ++)
newnode- >板[I] = M [I];
newnode-> parent = tfront; // parent
if (后面== NULL)
后面= tfront = newnode;
else
{
rear-> next = newnode;
rear = newnode;
}
}

NODE * getnode( int * m)
{
NODE * tempnode;
int i;
tempnode =(NODE *)malloc( sizeof (NODE));
tempnode-> next = NULL;
for (i = 0 ; i< 9; i ++)
tempnode- >检查[I] = M [I];
return tempnode;
}

void ins_in2_list( int * m)
{

NODE * aux;
int i;
aux = getnode(m);
if (list == NULL)
list = aux;

else
{
aux-> next = list;
list = aux;
}
}
int dublicate( int * m)
{
NODE * temp = list;
int i;
while (temp!= NULL)
{
for ( i = 0 ; i< 9; i ++)
{ if (temp-> check [i]!= m [i])
break ;
}
if (i == 9
return 1 ;
temp = temp-> next;
}
return 0 ;
}



void print_soln()
{
int i;
while (p!= NULL)
{
for ( i = 0 ; i< 9; i ++)
{
printf( %d \ t,p-> check [i]);
如果((i + 1)%3 == 0
printf ( \ n);
}
printf( \ n\ n);
getch();
p = p-> next;
}
}

void bfs()
{
int i,j,t [ 9 ],v,k,l,c = 0 ;
NODE * aux;
p = NULL;
qinsert(初始);
ins_in2_list(初始);

while (!qempty())
{

for (i = 0 ; i< 9; i ++)
if (tfront-> board [i] == 0
break ;
j = 0 ;
while (moves [i] [j]> = 0&& j< = 3
{
for (k = 0 ; k< 9; k ++)
t [k] = tfront-> board [k];
v = t [i];
t [i] = t [moves [i] [j]];
t [moves [i] [j]] = v;

if (!dublicate(t))
{

qinsert(t);
ins_in2_list(t);
for (l = 0 ; l< 9; l ++)
if final [l]!= t [l])
断裂;
if (l == 9
{
printf( soln found ... \ n);
print = rear;
while (print!= NULL)
{
aux = getnode(print-> board);
if (p == NULL)
p = aux;
else
{
aux-> next = p;
p = aux;
}
print = print-> parent;
}
print_soln();
退出( 1 );
}
}
j ++;
}
tfront = tfront-> next;
}


}

main()
{
int i,j;
list = NULL;
// clrscr();
printf( initial state \\\
);
for (i = 0 ; i< 9; i ++)
{
printf( %d \ t,initial [i]);
如果((i + 1)%3 == 0
printf ( \ n);
}
printf( \ n\ n);
printf( final state \ n);
for (i = 0 ; i< 9; i ++)
{
printf( %d \ t最终 [I]);
如果((i + 1)%3 == 0
printf ( \ n);
}
printf( \ n\ n);

/ * for(i = 0; i< 9; i ++)
scanf(%d,& initial [i]); * /

// printf(输入最终状态\ n);
// for(i = 0; i< 9; i ++)
// scanf(%d, & final [i]);

bfs();


getch();
}

解决方案

这是什么意思:我有这个代码

您是否只是在某个地方抓住它?

您不应该这样做。你应该努力做自己的功课。

那就是说,在malloc函数中发生的错误对我们来说并不是很有用。你应该详细说明以获得帮助。

请阅读常见问题解答。


我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!


首先,这是C代码,而不是C ++。



第二,它是重复的,不是刻板的;-)



第三,每个函数,包括你的main函数,都必须有一个返回类型。您的编译器可能只发出警告,但认为是错误。 C / C ++中的主要功能应始终返回 int



四,有两个未使用的变量, i in ins_in2_list(int *) j in main()



第五,你的代码在我的编译器上编译得很好(Visual C ++ 10.0)。您的编译器或设置可能存在问题。您使用什么编译器?



第六,如果没有更多信息,很难说这是什么。您应该只发布您获得的错误消息,导致它的代码行,以及围绕该行的几行代码,以查看上下文。

i got this code, but i do not understand how to get rid of the error occurring at the malloc function.

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
int initial[9]={2,8,3,1,6,4,7,0,5};    //the initial and final states
int final[9]={1,2,3,4,5,6,7,8,0};   
									
int moves[9][4]={                      
		{1,3,-1,-1},             
		{0,2,4,-1},
		{1,5,-1,-1},
		{4,0,6,-1},
		{3,1,5,7},
		{2,4,8,-1},
		{3,7,-1,-1},
		{4,6,8,-1},
		{5,7,-1,-1}
	  };
struct qtype  //queue
{
	int board[9];
	struct qtype *next;
	struct qtype *parent;
}*print;

struct node  //linked_list
{
	   int check[9];
	   struct node *next;
}*list,*p;

typedef struct qtype QUEUE;
typedef struct node NODE;

QUEUE *rear=NULL,*tfront=NULL;

		 int qempty()
		 { return tfront==NULL; }

void qinsert(int *m)
{
	int i;
	QUEUE *newnode;
	newnode=(QUEUE *)malloc(sizeof(QUEUE));
	for(i=0;i<9;i++)
		newnode->board[i]=m[i];
	newnode->parent=tfront; //parent
	if(rear==NULL)
	  rear=tfront=newnode;
	else
	  {
	  rear->next=newnode;
	  rear=newnode;
	  }
}

NODE * getnode(int *m)
{
	 NODE *tempnode;
	 int i;
	 tempnode=(NODE *)malloc(sizeof(NODE));
	 tempnode->next=NULL;
	 for(i=0;i<9;i++)
	tempnode->check[i]=m[i];
	 return tempnode;
}

void ins_in2_list(int *m)
{

	NODE *aux;
	int i;
	aux=getnode(m);
	if(list==NULL)
	   list=aux;

	else
	  {
	aux->next=list;
	list=aux;
	  }
}
			   int dublicate(int *m)
			   {
				   NODE *temp=list;
				   int i;
				   while(temp!=NULL)
				   {
				   for(i=0;i<9;i++)
					   {	  if(temp->check[i]!=m[i])
					  break;
					   }
					  if(i==9)
					 return 1;
					  temp=temp->next;
				   }
				   return 0;
			   }



void print_soln()
{
	int i;
	while(p!=NULL)
	{
	   for(i=0;i<9;i++)
	   {
	   printf("%d\t",p->check[i]);
	   if((i+1)%3==0)
	   printf("\n");
	   }
	   printf("\n\n");
	   getch();
	   p=p->next;
	}
}

void bfs()
{
   int i,j,t[9],v,k,l,c=0;
   NODE *aux;
   p=NULL;
   qinsert(initial);
   ins_in2_list(initial);

   while(!qempty())
   {

	   for(i=0;i<9;i++)
	   if(tfront->board[i]==0)
		  break;
		  j=0;
	   while(moves[i][j]>=0&&j<=3)
	  {
		  for(k=0;k<9;k++)
		  t[k]=tfront->board[k];
		  v=t[i];
		  t[i]=t[moves[i][j]];
		  t[moves[i][j]]=v;

		  if(!dublicate(t))
		   {

		   qinsert(t);
			   ins_in2_list(t);
			   for(l=0;l<9;l++)
			   if(final[l]!=t[l])
			  break;
			   if(l==9)
			   {
			  printf("soln found...\n");
			  print=rear;
			  while(print!=NULL)
			  {
				 aux=getnode(print->board);
				 if(p==NULL)
				   p=aux;
				 else
				 {
				   aux->next=p;
				   p=aux;
				 }
				 print=print->parent;
			  }
			  print_soln();
			  exit(1);
			   }
			}
			j++;
	   }
	  tfront=tfront->next;
   }


}

main()
{
	 int i,j;
	 list=NULL;
	 //clrscr();
	 printf("initial state\n");
	  for(i=0;i<9;i++)
	   {
	   printf("%d\t",initial[i]);
	   if((i+1)%3==0)
	   printf("\n");
	   }
	   printf("\n\n");
	printf("final state\n");
	 for(i=0;i<9;i++)
	   {
	   printf("%d\t",final[i]);
	   if((i+1)%3==0)
	   printf("\n");
	   }
	   printf("\n\n");
	
	/* for(i=0;i<9;i++)
		scanf("%d",&initial[i]);*/
  //	 printf("Enter final state\n");
   //	 for(i=0;i<9;i++)
	 //	scanf("%d",&final[i]);

	 bfs();


	 getch();
}

解决方案

What does it mean: "I got this code"?
Did you simply grab it somewhere?
You shouldn't do that. You should try hard to do yourself your own homework.
That said, the "error occurring at the malloc function" is NOT very informative for us. You should detail it in order to get help.
Please read the FAQ.


We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


First, this is C code, not C++.

Second, it's duplicate, not dublicate ;-)

Third, every function, including your main function, must have a return type. Your compiler may just issue a warning, but consider that an error. The main function in C/C++ should always return an int

Fourth, there are two unused variables, i in ins_in2_list(int*) and j in main()

Fifth, your code compiles just fine on my compiler (Visual C++ 10.0). There may be an issue with your compiler or settings. What compiler do you use?

Sixth, it's hard to tell without more info what this is about. Rather than posting the entire code which tells us nothing, you should have posted just the error message that you get, the line of code that causes it, and maybe a few lines of code surrounding that line, to see the context.


这篇关于我需要一个带有注释(文档)的代码,用于使用DFS的8个谜题...谢谢......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆