1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
bezimeni [28]
3 years ago
10

Implement a C program to solve the 15-puzzle problem using the A* search algorithm.

Computers and Technology
1 answer:
fomenos3 years ago
7 0

Answer:

#include<stdio.h>

#include<conio.h>

int m=0,n=4;

int cal(int temp[10][10],int t[10][10])

{

               int i,j,m=0;

               for(i=0;i < n;i++)

                               for(j=0;j < n;j++)

                               {

                                               if(temp[i][j]!=t[i][j])

                                               m++;

                               }

               return m;

}

int check(int a[10][10],int t[10][10])

{

               int i,j,f=1;

               for(i=0;i < n;i++)

                               for(j=0;j < n;j++)

                                               if(a[i][j]!=t[i][j])

                                                               f=0;

               return f;

}

void main()

{

               int p,i,j,n=4,a[10][10],t[10][10],temp[10][10],r[10][10];

               int m=0,x=0,y=0,d=1000,dmin=0,l=0;

               clrscr();

               printf("\nEnter the matrix to be solved,space with zero :\n");

               for(i=0;i < n;i++)

                               for(j=0;j < n;j++)

                                               scanf("%d",&a[i][j]);

               printf("\nEnter the target matrix,space with zero :\n");

               for(i=0;i < n;i++)

                               for(j=0;j < n;j++)

                                               scanf("%d",&t[i][j]);

               printf("\nEntered Matrix is :\n");

               for(i=0;i < n;i++)

               {

                               for(j=0;j < n;j++)

                                               printf("%d\t",a[i][j]);

                               printf("\n");

               }

               printf("\nTarget Matrix is :\n");

               for(i=0;i < n;i++)

               {

                               for(j=0;j < n;j++)

                                               printf("%d\t",t[i][j]);

                               printf("\n");

               }

               while(!(check(a,t)))

               {

                               l++;

                               d=1000;

                               for(i=0;i < n;i++)

                                               for(j=0;j < n;j++)

                                               {

                                                               if(a[i][j]==0)

                                                               {

                                                                               x=i;

                                                                               y=j;

                                                               }

                                               }

                               //To move upwards

                               for(i=0;i < n;i++)

                                               for(j=0;j < n;j++)

                                                               temp[i][j]=a[i][j];

                               if(x!=0)

                               {

                                               p=temp[x][y];

                                               temp[x][y]=temp[x-1][y];

                                               temp[x-1][y]=p;

                               }

                               m=cal(temp,t);

                               dmin=l+m;

                               if(dmin < d)

                               {

                                               d=dmin;

                                               for(i=0;i < n;i++)

                                                               for(j=0;j < n;j++)

                                                                               r[i][j]=temp[i][j];

                               }

                               //To move downwards

                               for(i=0;i < n;i++)

                                               for(j=0;j < n;j++)

                                                               temp[i][j]=a[i][j];

                               if(x!=n-1)

                               {

                                               p=temp[x][y];

                                               temp[x][y]=temp[x+1][y];

                                               temp[x+1][y]=p;

                               }

                               m=cal(temp,t);

                               dmin=l+m;

                               if(dmin < d)

                               {

                                               d=dmin;

                                               for(i=0;i < n;i++)

                                                               for(j=0;j < n;j++)

                                                                               r[i][j]=temp[i][j];

                               }

                               //To move right side

                               for(i=0;i < n;i++)

                                               for(j=0;j < n;j++)

                                                               temp[i][j]=a[i][j];

                               if(y!=n-1)

                               {

                                               p=temp[x][y];

                                               temp[x][y]=temp[x][y+1];

                                               temp[x][y+1]=p;

                               }

                               m=cal(temp,t);

                               dmin=l+m;

                               if(dmin < d)

                               {

                                               d=dmin;

                                               for(i=0;i < n;i++)

                                                               for(j=0;j < n;j++)

                                                                               r[i][j]=temp[i][j];

                               }

                               //To move left

                               for(i=0;i < n;i++)

                                               for(j=0;j < n;j++)

                                                               temp[i][j]=a[i][j];

                               if(y!=0)

                               {

                                               p=temp[x][y];

                                               temp[x][y]=temp[x][y-1];

                                               temp[x][y-1]=p;

                               }

                               m=cal(temp,t);

                               dmin=l+m;

                               if(dmin < d)

                               {

                                               d=dmin;

                                               for(i=0;i < n;i++)

                                                               for(j=0;j < n;j++)

                                                                               r[i][j]=temp[i][j];

                               }

                               printf("\nCalculated Intermediate Matrix Value :\n");

                               for(i=0;i < n;i++)

                               {

                                               for(j=0;j < n;j++)

                                               printf("%d\t",r[i][j]);

                                               printf("\n");

                               }

                               for(i=0;i < n;i++)

                                               for(j=0;j < n;j++)

                                               {

                                               a[i][j]=r[i][j];

                                               temp[i][j]=0;

                                               }

                               printf("Minimum cost : %d\n",d);

               }

               getch();

}

Explanation:

You might be interested in
The process of converting information from a form/questionnaire is referred to as data preparation. This process follows a four-
Sergio [31]

Answer:

Data tabulation.

Explanation:

6 0
3 years ago
How do you take a picture on this app
damaskus [11]
The paper clip.

Hope this helped.

Can i have brainliest?
8 0
3 years ago
How to convert 23.125 to binary (Hex) using the double - precision representation
Svet_ta [14]

Answer:-

(10111.001)₂

Explanation:

To convert a decimal number to a binary number we have to constantly divide the decimal number by 2 till the decimal number becomes zero and the binary number is writing the remainders in reverse order of obtaining them on each division.

Hence the binary number is 10111.001

To convert binary to hexa decimal we to make a group 4 binary bits starting from the decimal and moving outwards if the last group is not of 4 then add respective 0's and write the corresponding hexa decimal number.

<u>0001</u>  <u>0111</u> . <u>0010</u>

   1        7         2

Hence the hexadecimal number is 17.2

6 0
3 years ago
What is the output of 1101×10==11000+10
aliya0001 [1]

Answer:

west ward cpt output

Explanation:

i know because i invented it

6 0
3 years ago
To remove any hidden data from your document before sharing it, what should you do?
almond37 [142]
B. becasue the repeation of a b and c i assume its not d, for that could be too easy it says REMOVE so that being the key word i ruled out A and C
4 0
3 years ago
Other questions:
  • Algorithm for converting decimal into binary.
    7·1 answer
  • If a user wants to change one small section of the formatting of a document and leave the rest the same, which process should be
    14·1 answer
  • How do you get the computer to stop opening randomly
    15·2 answers
  • 10. What is "bandwidth"? (
    15·1 answer
  • List three types of Software:
    15·1 answer
  • Maria is creating a program where the user will enter their name to begin. What kind of variable should be used for the user’s n
    15·2 answers
  • Some programmers include scroll bars, title bars, buttons, and menus in a program simply by adding them to a layout through a pr
    10·1 answer
  • Submit your newsletter that includes the following: two or three columns a title at least three graphics, but not more than six
    10·2 answers
  • What happens to testosterone levels of those who lose chess tournaments?
    6·1 answer
  • Applications of the e-government​
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!