CODE
strcat(goal,gtop);
strcat(goal,gmid);
strcat(goal,gbot);
for (line=0; line <300; line++)
{ action[line][0] = 0;
opena [line][0] = 0; }
strcpy(opena[0],initial);
for(;;)
{
strcpy(current,opena[0]);
strcpy(closed[close_num++],current);
if (strcmp(goal,current) == 0)
{ printf("Solution found : %s \n",action[0]);
exit(0); }
strcpy(store,current);
s_pos = -1;
for(s_loop=0; s_loop<10; s_loop++)
{ if(*(current+s_loop) == '.') s_pos = s_loop; }
if (s_pos == -1)
{ printf("Illegal input.");
exit(0); }
/* Configuration where empty square goes UP. */
if (s_pos > 2) {
current[s_pos] = current[s_pos - 3];
*(current + s_pos - 3) = '.';
got_flag = 0;
for (closed_check=0; closed_check<=close_num; closed_check++)
{ if(strcmp(current,closed[closed_check]) == 0) got_flag=1; }
if (got_flag == 0) { strcpy(opena[++open_num],current);
strcpy(action[open_num],action[0]);
strcat(action[open_num],"U"); } }
/* Configuration where empty square goes DOWN. */
strcpy(current,store);
if (s_pos < 6) {
current[s_pos] = current[s_pos+3];
*(current + s_pos + 3) = '.';
got_flag = 0;
for (closed_check=0; closed_check<=close_num; closed_check++)
{ if(strcmp(current,closed[closed_check]) == 0) got_flag=1; }
if(got_flag == 0) { strcpy(opena[++open_num],current);
strcpy(action[open_num],action[0]);
strcat(action[open_num],"D"); } }
/* Configuration where empty square goes LEFT. */
strcpy(current,store);
if((s_pos!=0) && (s_pos!=3) && (s_pos!=6)) {
current[s_pos] = current[s_pos-1];
*(current + s_pos - 1) = '.';
got_flag = 0;
for (closed_check=0; closed_check<=close_num; closed_check++)
{ if(strcmp(current,closed[closed_check]) == 0) got_flag=1; }
if(got_flag==0) { strcpy(opena[++open_num],current);
strcpy(action[open_num],action[0]);
strcat(action[open_num],"L"); } }
/* Configuration where empty square goes RIGHT. */
strcpy(current,store);
if((s_pos!=2) && (s_pos!=5) && (s_pos!=8)) {
current[s_pos] = current[s_pos+1];
*(current + s_pos + 1) = '.';
got_flag=0;
for(closed_check=0; closed_check<=close_num; closed_check++)
{ if(strcmp(current,closed[closed_check]) == 0) got_flag=1; }
if(got_flag == 0) { strcpy(opena[++open_num],current);
strcpy(action[open_num],action[0]);
strcat(action[open_num],"R"); } }
for(shift=0; shift<open_num; shift++)
{ strcpy(opena[shift], opena[shift+1]);
strcpy(action[shift],action[shift+1]); } } }
This post has been edited by PsychoCoder: 5 Apr, 2008 - 01:43 PM