diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b89fb86 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.*~ +*.out +tags diff --git a/A8code.c b/A8code.c index d860be6..870ba00 100755 --- a/A8code.c +++ b/A8code.c @@ -16,25 +16,37 @@ Node* createNode(int puzzle[N][N]) { Node* newnode = (Node*)malloc(sizeof(Node)); //请实现该函数 - - + for(int i = 0; i < N; i++){ + for(int j = 0; j < N; j++){ + newnode.puzzle[i][j] = puzzle[i][j]; + } + } + newnode.parent = NULL; + newnode.f = 0; + newnode.g = 0; + newnode.h = 0; } // 检查两个拼图状态是否相同 bool isSamePuzzle(int a[N][N], int b[N][N]) { //相同则返回true,否则返回false - - - + for(int i = 0; i < N; i++){ + for(int j = 0; j < N; j++){ + if(a[i][j] != b[i][j]) return false; + } + } + return true; } // 打印拼图状态 void printPuzzle(int puzzle[N][N]) { //双重for循环实现拼图的打印 - - - - printf("\n"); + for(int i = 0; i < N; i++){ + for(int j = 0; j < N; j++){ + printf("%d ",puzzle[i][j]); + } + printf("\n") + } } @@ -43,7 +55,12 @@ int heuristic(Node* current, Node* goal) { int h = 0; // 计算不匹配的拼图块数量 - + for(int i = 0; i < N; i++){ + for(int j = 0; j < N; j++){ + if(current.puzzle[i][j] != goal.puzzle[i][j]) h++; + } + } + return h; } // 移动操作,生成新的拼图状态