人工智能A星算法(C++)

上传人:1332****573 文档编号:140420364 上传时间:2022-08-23 格式:DOC 页数:9 大小:42.50KB
返回 下载 相关 举报
人工智能A星算法(C++)_第1页
第1页 / 共9页
人工智能A星算法(C++)_第2页
第2页 / 共9页
人工智能A星算法(C++)_第3页
第3页 / 共9页
点击查看更多>>
资源描述
.wd.#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 start(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_Matrix); /空格上移 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() /目标矩阵 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; for(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+) 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%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 = 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+ (abs(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+ (abs(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 = 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为交换的中间变量 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; 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:left_move(MatrixNode M_Matrix) /空格左移 int num; 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 up_m.kong_y - 1 ; up_m.placeup_m.kong_x up_m.kong_y - 1 = num; up_m = up_m.updata_m(up_m); return up_m; /- MatrixNode MatrixNode:right_move(MatrixNode M_Matrix) /空格右移 int num; 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 up_m.kong_y + 1 ; up_m.placeup_m.kong_x up_m.kong_y + 1 = num; up_m = up_m.updata_m(up_m); return up_m; /- MatrixNode MatrixNode:updata_m(MatrixNode M_Matrix) /移动后更新状态 MatrixNode up_m = M_Matrix; up_m.m = up_m.TruePlace(up_m); / 查找在位数 up_m.p = up_m.p_place(up_m); /距离和 up_m.d = M_Matrix.d+1; /深度加1 up_m.f = up_m.p + up_m.d; /估价值 up_m.kong_x = up_m.f_kongx(up_m); /找出空格的横坐标 up_m.kong_y = up_m.f_kongy(up_m); /找出空格的纵坐标 return up_m; /- bool father(deque ilist,MatrixNode M_Matrix) /寻找父节点 MatrixNode M_Matrix1 = ilist.front(); MatrixNode up_m; int m; up_m = M_Matrix1.up_move(M_Matrix1); m = 0; for(int a1 = 0;a1 M;a1+) for(int b1 = 0;b1 M;b1+ ) if(up_m.placea1b1 = M_Matrix.placea1b1) m+; if(m = 9) return true; up_m=M_Matrix1.down_move(M_Matrix1); m = 0; for(int a2 = 0;a2 M;a2+) for(int b2 = 0;b2 M;b2+ ) if(up_m.placea2b2 = M_Matrix.placea2b2) m+; if(m = 9) return true; up_m=M_Matrix1.left_move(M_Matrix1); m = 0; for(int a3 = 0;a3 M;a3+) for(int b3 = 0;b3 M;b3+ ) if(up_m.placea3b3 = M_Matrix.placea3b3) m+; if(m = 9) return true; up_m=M_Matrix1.right_move(M_Matrix1); m = 0; for(int a4 = 0;a4 M;a4+) for(int b4 = 0;b4 M;b4+ ) if(up_m.placea4b4 = M_Matrix.placea4b4) m+; if(m = 9) return true; else return false; /- void printMatrix(const MatrixNode Matrix) /输出矩阵 for(int i = 0;i M;i+) for(int j = 0;j M;j+ ) coutMatrix.placeij,; coutendl; coutendl; /- bool less_f(const MatrixNode M_Matrix1, const MatrixNode M_Matrix2) return M_Matrix1.f M_Matrix2.f; /- - bool lookout(deque ilist, MatrixNode M_Matrix) /检查新生成的节点是否已扩展 deque:iterator Vi = ilist.begin(); int i,j,m; while(Vi != ilist.end() m=0; for(i = 0;i M;i+) for(j = 0;j M;j+ ) if(*Vi).placeij = M_Matrix.placeij) m+; if(m = 9) return true; /不是新扩展的 Vi+; return false; /是新扩展的 /= void main()int step = 0;MatrixNode mat; MatrixNode mat_trn;MatrixNode mat_trn1;MatrixNode mat_trn2;MatrixNode mat_trn3;MatrixNode mat_trn4; MatrixNode parent; mat = mat.start(mat); deque openlist;openlist.push_front(mat); deque closedlist;if(solved(mat) = false)cout无法找到路径!= 1) mat_trn1 = mat_trn1.up_move(mat_trn1); if(lookout(openlist,mat_trn1) = false & lookout(closedlist,mat_trn1) = false) /检查新节点是否已扩展 openlist.push_front(mat_trn1); /-向下移 mat_trn2 = mat_trn; if(mat_trn2.f_kongx(mat_trn2) = 1) mat_trn3 = mat_trn3.left_move(mat_trn3); if(lookout(openlist,mat_trn3) = false & lookout(closedlist,mat_trn3) = false) /检查新节点是否已扩展 openlist.push_front(mat_trn3); /-向右移 mat_trn4 = mat_trn; if(mat_trn4.f_kongy(mat_trn4) = 1) mat_trn4 = mat_trn4.right_move(mat_trn4); if(lookout(openlist,mat_trn4) = false & lookout(closedlist,mat_trn4) = false) /检查新节点是否已扩展 openlist.push_front(mat_trn4); sort(openlist.begin(),openlist.end(),less_f); mat_trn=openlist.front(); cout最优路径如下:endl; /输出路径printMatrix(mat_trn); deque:iterator Vi = closedlist.begin();while(Vi != (closedlist.end()-1) /由于我的father函数中用到首元素,所以不应该是不等于endif(father(closedlist,mat_trn) = true) parent = closedlist.front(); printMatrix(parent); mat_trn = parent;step = step+1; Vi+; closedlist.pop_front();printMatrix(mat);step+;cout走的步数:stependl;
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!