A星算法 OC实现具体代码

来源:互联网 发布:平面坐标 经纬度 java 编辑:程序博客网 时间:2024/04/30 03:45
//// Ai.h// 答辩iOS第四组//// Created by zhangwenbin on 15/7/14.// Copyright (c) 2015年 mac. All rights reserved.//#import #import "OpenList.h"#import "map.h"@interface Ai : NSObject@property (strong) OpenList *openlist;@property (strong) map *Map;-(void)insertToOpen:(MapNode *)currentNode and:(MapNode *)endNode andx:(int)m andy:(int)n andg:(int)g;//其中G值为消耗值-(void)getNeighbor:(MapNode *)currentNode and:(MapNode *)endNode;-(BOOL)detect:(MapNode *)current;-(void)findPath:(MapNode *)startNode and:(MapNode *)endNode;@end//// Ai.m// 答辩iOS第四组//// Created by zhangwenbin on 15/7/14.// Copyright (c) 2015年 mac. All rights reserved.//#import "Ai.h"@implementation Ai-(BOOL)detect:(MapNode *)current//寻找周围有效节点{ return YES;}-(void)findPath:(MapNode *)startNode and:(MapNode *)endNode//寻路{ }-(void)insertToOpen:(MapNode *)currentNode and:(MapNode *)endNode andx:(int)m andy:(int)n andg:(int)g//往open表中添加邻居节点{ }-(void)getNeighbor:(MapNode *)currentNode and:(MapNode *)endNode//获取该节点附近邻居节点{ }@end//// AStar.h// 答辩iOS第四组//// Created by zhangwenbin on 15/7/14.// Copyright (c) 2015年 mac. All rights reserved.//#import #import "Ai.h"@interface AStar : Ai@end//// AStar.m// 答辩iOS第四组//// Created by zhangwenbin on 15/7/14.// Copyright (c) 2015年 mac. All rights reserved.//#import "AStar.h"@implementation AStar-(void)insertToOpen:(MapNode *)currentNode and:(MapNode *)endNode andx:(int)m andy:(int)n andg:(int)g{ MapNode *tempNode=[[self.Map.mapData objectAtIndex:m] objectAtIndex:n]; if(tempNode.value != 0) //不是障碍物 { if (tempNode.closelList !=1 ) //不在闭表中 { if(tempNode.openList !=1) //如果不在open表中 { tempNode.f_g = tempNode.f_g + 10; tempNode.distance = (fabs(tempNode.point.x - endNode.point.x)+fabs(tempNode.point.y - endNode.point.y)) +tempNode.f_g; tempNode.Road = currentNode; tempNode.openList = 1; //标记为在open表中 [self.openlist addMapNode:tempNode]; } } }}-(void)getNeighbor:(MapNode *)currentNode and:(MapNode *)endNode//八个邻居节点 斜着走耗费14{ int x = currentNode.point.x; int y = currentNode.point.y; if ( ( x + 1 ) >= 0 && ( x + 1 ) < 20 && y >= 0 && y < 20 ) { [self insertToOpen:currentNode and:endNode andx:x+1 andy:y andg:10]; } if ( ( x - 1 ) >= 0 && ( x - 1 ) <20 y="">= 0 && y <20 self="" inserttoopen:currentnode="" and:endnode="" andx:x-1="" andy:y="" andg:10="" if="" x="">= 0 && x < 20&& ( y + 1 ) >= 0 && ( y + 1 ) < 20 ) { [self insertToOpen:currentNode and:endNode andx:x andy:y +1 andg:10]; } if ( x >= 0 && x < 20&& ( y - 1 ) >= 0 && ( y - 1 ) < 20 ) { [self insertToOpen:currentNode and:endNode andx:x andy:y-1 andg:10]; } if ( ( x + 1 ) >= 0 && ( x + 1 ) <20 y="" 1="">= 0 && ( y + 1 ) < 20 ) { [self insertToOpen:currentNode and:endNode andx:x+1 andy:y +1 andg:14]; } if ( ( x + 1 ) >= 0 && ( x + 1 ) < 20 && ( y - 1 ) >= 0 && ( y - 1 ) < 20 ) { [self insertToOpen:currentNode and:endNode andx:x+1 andy:y-1 andg:14]; } if ( ( x - 1 ) >= 0 && ( x - 1 ) < 20&& ( y + 1 ) >= 0 && ( y + 1 ) <20 self="" inserttoopen:currentnode="" and:endnode="" andx:x-1="" andy:y="" 1="" andg:14="" if="" x="" -="" 1="">= 0 && ( x - 1 ) <20 y="" -="" 1="">= 0 && ( y - 1 ) < 20 ) { [self insertToOpen:currentNode and:endNode andx:x-1 andy:y-1 andg:14]; }}-(void)findPath:(MapNode *)startNode and:(MapNode *)endNode;{ [self.openlist addMapNode:startNode]; //起点放入open表 startNode.openList = 1;//1即为真说明在open中 startNode.f_g = 0;//初始G为0; startNode.distance = ABS(startNode.point.x - endNode.point.x)+ABS(startNode.point.y - endNode.point.y); startNode.Road = NULL; if (startNode.point.x==endNode.point.x && startNode.point.y==endNode.point.y) { NSLog(@"起点与终点重合!"); } MapNode *currtNode; NSMutableArray *path =[[NSMutableArray alloc] init];//存放路径的点 MapNode *tempPath = [[MapNode alloc]init]; //取出点 int a = 0 ; while (1) { currtNode = [self.openlist getFirstNode];//获取最优点 currtNode.closelList = 1; [self.openlist.content removeObjectAtIndex:0];//从open中移除 [self getNeighbor:currtNode and:endNode]; if (currtNode.point.x == endNode.point.x && currtNode.point.y == endNode.point.y ) { while (currtNode) { [path addObject:currtNode]; a++; currtNode = currtNode.Road; } for (int x = a-1; x>=0; x--)//反向遍历 回溯路径 { tempPath = [path objectAtIndex:x]; tempPath.value = 2; [NSThread sleepForTimeInterval:0.1];//延时函数 达到动画效果 [self.Map printMap]; printf("\n"); } NSLog(@"亲,走了%d步!",a); NSLog(@"亲,寻路完成喽!飞机飞过来的哦!"); break; } } }@end//// FixedMap.h// 答辩iOS第四组//// Created by zhangwenbin on 15/7/14.// Copyright (c) 2015年 mac. All rights reserved.//#import #import "map.h"#import "OpenList.h"@interface designMap : map@end//// FixedMap.m// 答辩iOS第四组//// Created by zhangwenbin on 15/7/14.// Copyright (c) 2015年 mac. All rights reserved.//#import "designMap.h"@implementation designMap-(void)getMap{ int newNode[20][20]; for (int i = 0; i < 20; i++) { for (int j = 0; j<20 j="" if="" i="=" 0="" i="=" 19="" j="=" 0="" j="=" 19="" newnode="" i="" j="" 0="" else="" if="" j="">= 4 && j <= 8 && (i == 3 || i == 6)) || (j >= 2 && j <= 5 && (i == 3 || i == 6)) || (((i >= 4 && i <= 8) || (i >= 11 && i <= 15)) && (j == 4 || j == 15))) { newNode[i][j]= 0; } else if ((i == 9 && (j >= 8 && j <= 11)) || ((j == 20 ) && (i >=5 && i <= 14))) { newNode[i][j] = 0; } else { newNode[i][j] = 1; } } } for (int i = 0; i < 20; i++) { NSMutableArray *temp = [[NSMutableArray alloc]init]; for (int j = 0; j< 20; j++) { MapNode *tempNode = [[MapNode alloc] init]; tempNode.point = NSMakePoint(i, j); tempNode.value = newNode[i][j]; tempNode.state = 0; [temp addObject:tempNode]; } [self.mapData addObject:temp]; }}@end//// main.m// 答辩iOS第四组//// Created by zhangwenbin on 15/7/14.// Copyright (c) 2015年 mac. All rights reserved.//#import #import "UiView.h"int main(int argc, const char * argv[]){ @autoreleasepool { UiView * ui = [[UiView alloc]init]; [ui selectMath]; } return 0;}//// map.h// 答辩iOS第四组//// Created by zhangwenbin on 15/7/14.// Copyright (c) 2015年 mac. All rights reserved.//#import #import "MapNode.h"@interface map : NSObject@property CGSize size;//使用OC自带方法定义地图的尺寸@property (strong) NSMutableArray *mapData;-(void)getMap;-(void)printMap;@end//// map.m// 答辩iOS第四组//// Created by zhangwenbin on 15/7/14.// Copyright (c) 2015年 mac. All rights reserved.//#import "map.h"@implementation map-(id)init{ self = [super init]; self.mapData = [[NSMutableArray alloc]init]; return self;}-(void)getMap//获取地图{ }-(void)printMap//打印地图{ printf("\n"); for (int i=0 ; i<20; i++) { printf("%2d",i); for (int j = 0; j < 20; j++) { MapNode *tempData= self.mapData[i][j]; if (i == 0 || i == 19 ||j == 0|| j == 19)//围墙 { printf("
0 0
原创粉丝点击