From eb4ea3fa4e80d36992a742ddd54516fbe317a2b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E4=B8=96=E6=B3=A2?= <2783626707@qq.com> Date: Sun, 19 Nov 2023 09:32:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A5=E9=AA=A41-4=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +++ A8code.c | 37 +++++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 .gitignore 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; } // 移动操作,生成新的拼图状态