八数码问题的过程表示及其实现
来源:互联网 发布:手机如何注册淘宝会员 编辑:程序博客网 时间:2024/06/04 01:02
过程式知识表示是将有关某一问题领域的知识, 连同如何使用这些知识的方法,均隐式的表达为 一个求解问题的过程,每个过程是一段程序,完 成对具体情况的处理。过程式不像陈述式那样具有固定的形式,如何描述知识完 全取决于具体问题。
例:八数码问题 人工智能及其应用
c语言实现:
#include<stdio.h>//空格按箭头方向移动,回到起始位置void zero_back(int start[], int array[], int *zero_place, int length){int i;int temp;for (i = 0; i < length-1; i++){temp = start[*zero_place];start[*zero_place] = start[array[i + 1]];start[array[i + 1]] = temp;*zero_place = array[i + 1];}temp = start[*zero_place];start[*zero_place] = start[array[0]];start[array[0]] = temp;*zero_place = array[0];return;}//_flag 为1则按abcdefgh方式移动;_flag不为1则使1和0不在位置上void zero_move(int start[], int array[], int *zero_place, int length, int _flag){int i;int j = 0;//int length;int temp;//length = sizeof(array)/sizeof(array[0]);for (i = 0; i < length; i++){if (array[i] == *zero_place){j = i;break;}}if (j < length - 1){temp = start[*zero_place];start[*zero_place] = start[array[j + 1]];start[array[j + 1]] = temp;*zero_place = array[j + 1];}else{if (_flag == 1){temp = start[*zero_place];start[*zero_place] = start[array[0]];start[array[0]] = temp;*zero_place = array[0];}else{temp = start[*zero_place];start[*zero_place] = start[array[5]];start[array[5]] = temp;*zero_place = array[5];}}return;}void _input(int start[]){int i;printf("请给定初始状态:\n");printf("3*3矩阵形式输入,空格用0代替\n");for (i = 0; i < 9; i++){scanf("%d", &start[i]);if ((i + 1) % 3 == 0){printf("\n");}}}void _print(int start[]){int i;for (i = 0; i < 9; i++){ printf("%d\t", start[i]);if ((i + 1) % 3 == 0){printf("\n");}}}void main(){int i;int switch_case;int start[9];int zero_place;int array_m[9] = { 8,7,6,3,0,1,2,5,4 };int array_a[8] = { 0,3,6,7,8,5,4,1 };int array_b[8] = { 3,6,7,8,5,2,1,4 };int array_c[6] = { 3,6,7,8,5,4 };int array_d[8] = { 3,0,1,4,5,2,1,0 };int array_e[6] = { 3,6,7,8,5,4 };int array_f[4] = { 3,6,7,4 };int array_g[13] = { 3,0,1,2,5,4,7,8,5,2,1,0,3 };int array_h[4] = { 3,6,7,4 };_input(start);for (i = 0; i < 9; i++){if (start[i] == 0){zero_place = i;break;}}switch_case = 1;while (switch_case < 10){switch (switch_case){case 1:for (i = 0; i < 50; i++){if (start[2] != 0 && start[2] != 1){switch_case = 2;break;}else{zero_move(start, array_m, &zero_place, 9, 0);}}printf("step 1\n");_print(start);break;case 2:for (i = 0; i < 50; i++){if (start[0] == 1){switch_case = 3;break;}else{zero_move(start, array_a, &zero_place, 8, 1);}}printf("step 2\n");_print(start);break;case 3:for (i = 0; i < 50; i++){if (start[1] == 2){if (start[2] == 3){switch_case = 6;}else{switch_case = 4;}break;}else{zero_move(start, array_b, &zero_place, 8, 1);}}printf("step 3\n");_print(start);break;case 4:for (i = 0; i < 50; i++){if (start[4] == 3){switch_case = 5;break;}else{zero_move(start, array_c, &zero_place, 6, 1);}}printf("step 4\n");_print(start);break;case 5:zero_back(start, array_d, &zero_place, 8);switch_case = 6;printf("step 5\n");_print(start);break;case 6:for (i = 0; i < 50; i++){if (start[5] == 4){if (start[8] == 5){switch_case = 9;}else{switch_case = 7;}break;}else{zero_move(start, array_e, &zero_place, 6, 1);}}printf("step 6\n");_print(start);break;case 7:for (i = 0; i < 100; i++){if (start[4] == 5){switch_case = 8;break;}else{zero_move(start, array_f, &zero_place, 4, 1);}}printf("step 7\n");_print(start);break;case 8:zero_back(start, array_g, &zero_place, 13);switch_case = 9;printf("step 8\n");_print(start);break;case 9:for (i = 0; i < 50; i++){if (start[7] == 6&&start[4]==0){if (start[3] == 8 && start[6] == 7){printf("问题有解,达到目标状态\n");}else{printf("问题无解,达不到目标状态\n");}switch_case = 10;break;}else{zero_move(start, array_h, &zero_place, 4, 1);}}printf("step 9\n");_print(start);break;default:break;}}}/**************************测试情况:201 465 378 有解; **************************/
运行结果如下:
请给定初始状态:
3*3矩阵形式输入,空格用0代替
2 0 1
3*3矩阵形式输入,空格用0代替
2 0 1
4 6 5
3 7 8
step 1
2 1 5
4 6 0
3 7 8
step 2
1 6 5
0 8 7
2 4 3
step 3
1 2 8
4 0 6
3 7 5
step 4
1 2 8
0 3 4
7 5 6
step 5
1 2 3
0 4 8
7 5 6
step 6
1 2 3
7 0 4
5 6 8
step 7
1 2 3
0 5 4
6 7 8
step 8
1 2 3
0 7 4
6 8 5
问题有解,达到目标状态
step 9
1 2 3
8 0 4
7 6 5
请按任意键继续. . .
2 1 5
4 6 0
3 7 8
step 2
1 6 5
0 8 7
2 4 3
step 3
1 2 8
4 0 6
3 7 5
step 4
1 2 8
0 3 4
7 5 6
step 5
1 2 3
0 4 8
7 5 6
step 6
1 2 3
7 0 4
5 6 8
step 7
1 2 3
0 5 4
6 7 8
step 8
1 2 3
0 7 4
6 8 5
问题有解,达到目标状态
step 9
1 2 3
8 0 4
7 6 5
请按任意键继续. . .
0 0
- 八数码问题的过程表示及其实现
- 八数码问题及其扩展
- 八数码问题的A*算法实现
- 八数码问题有解的条件及其推广
- 八数码问题有解的条件及其推广
- 八数码问题有解的条件及其推广
- Java实现八数码问题
- 八数码问题的可解性
- 八数码问题的可解行
- 经典的八数码问题
- 八数码问题的解决方案
- 简单的用c++实现的八数码问题
- 八数码难题的实现
- A*算法解决八数码问题的C++实现
- A*算法来实现八数码的问题 C++
- A*算法的简单实现(八数码问题)
- 用C语言实现八数码问题
- AStar解决八数码问题(java实现)
- IntelliJ 创建main函数快捷
- 容器生态系统
- 规划之博客定期更新
- 算法中的递归分析和分治法的原理
- HDU 4548 Shoot the Airplane 计算几何
- 八数码问题的过程表示及其实现
- java思想IO--目录列表器带有简单过滤
- Ubuntu下Hive-2.1.1安装
- 一个小案例:用SysGen做一个DDS信号发生器并用FFT频谱分析 1.原理图
- node---http模块,如何用node.js做简单的服务器环境http.createServer
- 利用Base64对文本进行加密解密操作
- HDU 5253 连接的管道(最小生成树)
- java数组中根据元素查找位置 索引
- poj 1556 线段相交 + dijstra