N puzzle algorithm c++ program code to find heuristics value .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include<bits/stdc++.h> using namespace std; int main() { char goal[3][3]= {'1','2','3','8','-','5','6','4','7'}; char initial[3][3]; int h=0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { cin>>initial[i][j]; } } for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(goal[i][j]!=initial[i][j]) { for(int a=0; a<3; a++) { int flag=0; for(int b=0; b<3; b++) { if(goal[i][j]=initial[i][j]) { h+=(abs(a-i)+abs(b-j)); flag=1; break; } } if(flag==1) break; } } } } cout<<"Calculated value of H = "<<h; return 0; } |