人工智能A星算法(C++)(共10页).doc
《人工智能A星算法(C++)(共10页).doc》由会员分享,可在线阅读,更多相关《人工智能A星算法(C++)(共10页).doc(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上#include#include#include#includeusing namespace std;#define M 3 class MatrixNode /定义MatrixNode类public: int m; /在位个数 int d; /深度 int p; /牌与其目标位置直接步数之和 int f; /f=d+p,估价函数 int placeMM; /当前矩阵 int placetrueMM; /目标矩阵 int kong_x; /空位的横坐标 int kong_y; /空位的纵坐标/-public: MatrixNode(); MatrixNode sta
2、rt(MatrixNode M_Matrix); /初始矩阵 int TruePlace(MatrixNode T_place ); /查找在位数 int p_place(MatrixNode P_place); /坐标差绝对值之和 int f_kongx(MatrixNode find_kongx); /找出空格的横坐标 int f_kongy(MatrixNode find_kongy); /找出空格的纵坐标 bool solved(MatrixNode M_Matrix); /判断是否有解,奇偶性相同则有解,否则无解 MatrixNode up_move(MatrixNode M_Mat
3、rix); /空格上移 MatrixNode down_move(MatrixNode M_Matrix); /空格下移 MatrixNode left_move(MatrixNode M_Matrix); /空格左移 MatrixNode right_move(MatrixNode M_Matrix); /空格右移 MatrixNode updata_m(MatrixNode M_Matrix); /移动后更新状态 MatrixNode parents(deque ilist,MatrixNode M_Matrix); /找到该节点的父亲;/= MatrixNode:MatrixNode()
4、 /目标矩阵 placetrue00 = 1; placetrue01 = 2; placetrue02 = 3; placetrue10 = 8; placetrue11 = -1; placetrue12 = 4; placetrue20 = 7; placetrue21 = 6; placetrue22 = 5;/- MatrixNode MatrixNode:start(MatrixNode M_Matrix) /初始矩阵 cout请按如下格式输入初始矩阵(空位用0表示):endl; cout1 2 3n4 5 6n7 0 8endl; cout八数码的初始状态如下: endl; fo
5、r(int a = 0;a M;a+) for(int b = 0;b M_Matrix.placeab; M_Matrix.d = 0; M_Matrix = M_Matrix.updata_m( M_Matrix ); M_Matrix.d=M_Matrix.d-1; /初始更新时深度多加1,应该减去 M_Matrix.f=M_Matrix.f-1; return M_Matrix;/- bool solved(MatrixNode M_Matrix) /判断是否可解 int num8; int target8; int a=0; int b=0; for(int m = 0;m M;m+
6、) for(int n = 0;n M;n+ ) if(M_Matrix.placemn != 0) /不考虑空格 numa+=M_Matrix.placemn; if(M_Matrix.placetruemn != -1) targetb+=M_Matrix.placetruemn; int i,j;int count_num = 0,count_target = 0;for (i = 0;i (8-1);i+)for (j = i+1;j 8;j+)if(numj numi) count_num+;if(targetjtargeti) count_target+;if(count_num%
7、2 = 0&count_target%2 = 0)|(count_num%2 = 1&count_target%2 = 1)return true; else return false; /- int MatrixNode:TruePlace(MatrixNode T_place ) / 查找在位数 T_place.m = 0; int NumT_place = 0; for(int i = 0;i M;i+)for(int j = 0;j M;j+ )if( T_place.placeij = placetrueij) T_place.m = T_place.m + 1;NumT_place
8、 = NumT_place + 1; return NumT_place; /- int MatrixNode:p_place(MatrixNode P_place) /坐标差的绝对值之和 P_place.p = 0; int num = 0; for(int Pa = 0;Pa M;Pa+) for(int Pb = 0;Pb M;Pb+) if(P_place.placePaPb = 1) P_place.p = P_place.p+ (abs(Pa - 0)+abs(Pb - 0); if(P_place.placePaPb = 2) P_place.p = P_place.p+ (ab
9、s(Pa - 0)+abs(Pb - 1); if(P_place.placePaPb = 3) P_place.p = P_place.p+ (abs(Pa - 0)+abs(Pb - 2); if(P_place.placePaPb = 4) P_place.p = P_place.p+ (abs(Pa - 1)+abs(Pb - 2); if(P_place.placePaPb = 5) P_place.p = P_place.p+ (abs(Pa - 2)+abs(Pb - 2); if(P_place.placePaPb = 6) P_place.p = P_place.p+ (ab
10、s(Pa - 2)+abs(Pb - 1); if(P_place.placePaPb = 7) P_place.p = P_place.p+ (abs(Pa - 2)+abs(Pb - 0); if(P_place.placePaPb = 8) P_place.p = P_place.p+ (abs(Pa - 1)+abs(Pb - 0); num = P_place.p; return num; /- int MatrixNode:f_kongx(MatrixNode find_kongx) /返回空格横坐标 int num; for(int i = 0;i M;i+) for(int j
11、 = 0;j M;j+) if(find_kongx.placeij = 0) num = i; return num; /- int MatrixNode:f_kongy(MatrixNode find_kongy) /返回空格纵坐标 int num; for(int i = 0;i M;i+) for(int j = 0;j M;j+) if(find_kongy.placeij = 0) num = j; return num; /- MatrixNode MatrixNode:up_move(MatrixNode M_Matrix) /空格上移 int num; /num为交换的中间变
12、量 MatrixNode up_m = M_Matrix; num = up_m.placeup_m.kong_xup_m.kong_y; up_m.placeup_m.kong_xup_m.kong_y = up_m.placeup_m.kong_x - 1up_m.kong_y ; up_m.placeup_m.kong_x - 1up_m.kong_y = num; up_m = up_m.updata_m(up_m); return up_m; /- MatrixNode MatrixNode:down_move(MatrixNode M_Matrix) /空格下移 int num;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 人工智能 算法 10
限制150内