diff --git a/pacman_astar.cpp b/pacman_astar.cpp new file mode 100644 index 0000000..23d01ef --- /dev/null +++ b/pacman_astar.cpp @@ -0,0 +1,294 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int px,py; +int fx,fy; +int n,m; +char arr[40][40]; + +struct node { + struct node* par; + int fn,hn,gn; + int cur_row, cur_col; + //struct node* next; +}*head; + +struct node *initialize(struct node*par, int i,int j){ + struct node* p = (struct node*) malloc(sizeof(struct node)); + p->cur_row = i; + p->cur_col = j; + p->par = par; + p->fn = p->gn = p->hn = 0; + return p; +} + +vector closedlist; +vector openlist; +vector generated; + +bool move_up(int a,int b){ + return (a-1 >= 0 && arr[a-1][b] != '%'); +} +bool move_left(int a,int b){ + return (b-1 >=0 && arr[a][b-1] != '%'); +} +bool move_right(int a,int b){ + return (b+1 < m && arr[a][b+1] != '%'); +} +bool move_down(int a,int b){ + return (a+1 cur_row == fx && p->cur_col == fy) + return true; + return false; +} +void printans(struct node *p){ + + int len; + stack ss; + while(p && p->par){ + ss.push(p); + p = p->par; + } + ss.push(p); + len = ss.size()-1; + cout <cur_row <<" "<cur_col<cur_row == p->cur_row && generated[i]->cur_col == p->cur_col ) + return true; + } + return false; +} + +int bfs_pacman(){ + closedlist.clear(); + openlist.clear(); + generated.clear(); + head = initialize(NULL, px,py); + openlist.push_back(head); + generated.push_back(head); + while(! openlist.empty()){ + struct node * p = openlist.front(); + openlist.erase(openlist.begin()); + int a =p->cur_row; + int b = p->cur_col; + //cout <cur_row; + int b = p->cur_col; + //cout <fn < b->fn; +} +int hn_func(struct node* p){ + return abs(p->cur_row - fx )+ abs(p->cur_col -fy); +} +int gn_func(struct node* p){ + int c=0; + while(p ){ + p = p->par; + c++; + } + return c; +} +int fn_func(struct node* p){ + p->hn = hn_func(p); + p->gn = gn_func(p); + p->fn = p->hn +p->gn; + return p->fn; +} + +int astar_pacman(){ + closedlist.clear(); + openlist.clear(); + generated.clear(); + head = initialize(NULL, px,py); + fn_func(head); + openlist.push_back(head); + generated.push_back(head); + while(! openlist.empty()){ + sort(openlist.begin(), openlist.end(), cmp); + struct node * p = openlist.front(); + openlist.erase(openlist.begin()); + int a =p->cur_row; + int b = p->cur_col; + //cout <> px >> py; + cin >> fx >>fy; + cin >>n >>m; + int i,j; + string str; + getchar(); + //getline(cin,str); + for(i =0; i +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int px,py; +int fx,fy; +int n,m; +char arr[40][40]; + +struct node { + struct node* par; + int fn,hn,gn; + int cur_row, cur_col; + //struct node* next; +}*head; + +struct node *initialize(struct node*par, int i,int j){ + struct node* p = (struct node*) malloc(sizeof(struct node)); + p->cur_row = i; + p->cur_col = j; + p->par = par; + p->fn = p->gn = p->hn = 0; + return p; +} + +vector closedlist; +vector openlist; +vector generated; + +bool move_up(int a,int b){ + return (a-1 >= 0 && arr[a-1][b] != '%'); +} +bool move_left(int a,int b){ + return (b-1 >=0 && arr[a][b-1] != '%'); +} +bool move_right(int a,int b){ + return (b+1 < m && arr[a][b+1] != '%'); +} +bool move_down(int a,int b){ + return (a+1 cur_row == fx && p->cur_col == fy) + return true; + return false; +} +void printans(struct node *p){ + int c= closedlist.size(); + int d = generated.size(); + cout << c <cur_row <<" "<< closedlist[i]->cur_col < ss; + while(p && p->par){ + ss.push(p); + p = p->par; + } + ss.push(p); + len = ss.size()-1; + cout <cur_row <<" "<cur_col<cur_row == p->cur_row && generated[i]->cur_col == p->cur_col ) + return true; + } + return false; +} + +int bfs_pacman(){ + closedlist.clear(); + openlist.clear(); + generated.clear(); + head = initialize(NULL, px,py); + openlist.push_back(head); + generated.push_back(head); + while(! openlist.empty()){ + struct node * p = openlist.front(); + openlist.erase(openlist.begin()); + int a =p->cur_row; + int b = p->cur_col; + //cout <fn < b->fn; +} +int hn_func(struct node* p){ + return abs(p->cur_row - fx )+ abs(p->cur_col -fy); +} +int gn_func(struct node* p){ + int c=0; + while(p ){ + p = p->par; + c++; + } + return c; +} +int fn_func(struct node* p){ + p->hn = hn_func(p); + p->gn = gn_func(p); + p->fn = p->hn +p->gn; + return p->fn; +} + +int astar_pacman(){ + closedlist.clear(); + openlist.clear(); + generated.clear(); + head = initialize(NULL, px,py); + fn_func(head); + openlist.push_back(head); + generated.push_back(head); + while(! openlist.empty()){ + sort(openlist.begin(), openlist.end(), cmp); + struct node * p = openlist.front(); + openlist.erase(openlist.begin()); + int a =p->cur_row; + int b = p->cur_col; + //cout <> px >> py; + cin >> fx >>fy; + cin >>n >>m; + int i,j; + string str; + getchar(); + //getline(cin,str); + for(i =0; i +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int px,py; +int fx,fy; +int n,m; +char arr[40][40]; + +struct node { + struct node* par; + int fn,hn,gn; + int cur_row, cur_col; + //struct node* next; +}*head; + +struct node *initialize(struct node*par, int i,int j){ + struct node* p = (struct node*) malloc(sizeof(struct node)); + p->cur_row = i; + p->cur_col = j; + p->par = par; + p->fn = p->gn = p->hn = 0; + return p; +} + +vector closedlist; +vector openlist; +vector generated; + +bool move_up(int a,int b){ + return (a-1 >= 0 && arr[a-1][b] != '%'); +} +bool move_left(int a,int b){ + return (b-1 >=0 && arr[a][b-1] != '%'); +} +bool move_right(int a,int b){ + return (b+1 < m && arr[a][b+1] != '%'); +} +bool move_down(int a,int b){ + return (a+1 cur_row == fx && p->cur_col == fy) + return true; + return false; +} +void printans(struct node *p){ + int c= closedlist.size(); + int d = generated.size(); + cout << c <cur_row <<" "<< closedlist[i]->cur_col < ss; + while(p && p->par){ + ss.push(p); + p = p->par; + } + ss.push(p); + len = ss.size()-1; + cout <cur_row <<" "<cur_col<cur_row == p->cur_row && generated[i]->cur_col == p->cur_col ) + return true; + } + return false; +} + +int bfs_pacman(){ + closedlist.clear(); + openlist.clear(); + generated.clear(); + head = initialize(NULL, px,py); + openlist.push_back(head); + generated.push_back(head); + while(! openlist.empty()){ + struct node * p = openlist.front(); + openlist.erase(openlist.begin()); + int a =p->cur_row; + int b = p->cur_col; + //cout <cur_row; + int b = p->cur_col; + //cout <fn < b->fn; +} +int hn_func(struct node* p){ + return abs(p->cur_row - fx )+ abs(p->cur_col -fy); +} +int gn_func(struct node* p){ + int c=0; + while(p ){ + p = p->par; + c++; + } + return c; +} +int fn_func(struct node* p){ + p->hn = hn_func(p); + p->gn = gn_func(p); + p->fn = p->hn +p->gn; + return p->fn; +} + +int astar_pacman(){ + closedlist.clear(); + openlist.clear(); + generated.clear(); + head = initialize(NULL, px,py); + fn_func(head); + openlist.push_back(head); + generated.push_back(head); + while(! openlist.empty()){ + sort(openlist.begin(), openlist.end(), cmp); + struct node * p = openlist.front(); + openlist.erase(openlist.begin()); + int a =p->cur_row; + int b = p->cur_col; + //cout <> px >> py; + cin >> fx >>fy; + cin >>n >>m; + int i,j; + string str; + getchar(); + //getline(cin,str); + for(i =0; i +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int px,py; +int fx,fy; +int n,m; +char arr[40][40]; + +struct node { + struct node* par; + int fn,hn,gn; + int cur_row, cur_col; + //struct node* next; +}*head; + +struct node *initialize(struct node*par, int i,int j){ + struct node* p = (struct node*) malloc(sizeof(struct node)); + p->cur_row = i; + p->cur_col = j; + p->par = par; + p->fn = p->gn = p->hn = 0; + return p; +} + +vector closedlist; +vector openlist; +vector generated; + +bool move_up(int a,int b){ + return (a-1 >= 0 && arr[a-1][b] != '%'); +} +bool move_left(int a,int b){ + return (b-1 >=0 && arr[a][b-1] != '%'); +} +bool move_right(int a,int b){ + return (b+1 < m && arr[a][b+1] != '%'); +} +bool move_down(int a,int b){ + return (a+1 cur_row == fx && p->cur_col == fy) + return true; + return false; +} +void printans(struct node *p){ + + int len; + stack ss; + while(p && p->par){ + ss.push(p); + p = p->par; + } + ss.push(p); + len = ss.size()-1; + cout <cur_row <<" "<cur_col<cur_row == p->cur_row && generated[i]->cur_col == p->cur_col ) + return true; + } + return false; +} + +int bfs_pacman(){ + closedlist.clear(); + openlist.clear(); + generated.clear(); + head = initialize(NULL, px,py); + openlist.push_back(head); + generated.push_back(head); + while(! openlist.empty()){ + struct node * p = openlist.front(); + openlist.erase(openlist.begin()); + int a =p->cur_row; + int b = p->cur_col; + //cout <cur_row; + int b = p->cur_col; + //cout <fn < b->fn; +} +int hn_func(struct node* p){ + return abs(p->cur_row - fx )+ abs(p->cur_col -fy); +} +int gn_func(struct node* p){ + int c=0; + while(p ){ + p = p->par; + c++; + } + return c; +} +int fn_func(struct node* p){ + p->hn = hn_func(p); + p->gn = 0; + p->fn = p->hn +p->gn; + return p->fn; +} + +int astar_pacman(){ + closedlist.clear(); + openlist.clear(); + generated.clear(); + head = initialize(NULL, px,py); + fn_func(head); + openlist.push_back(head); + generated.push_back(head); + while(! openlist.empty()){ + sort(openlist.begin(), openlist.end(), cmp); + struct node * p = openlist.front(); + openlist.erase(openlist.begin()); + int a =p->cur_row; + int b = p->cur_col; + //cout <> px >> py; + cin >> fx >>fy; + cin >>n >>m; + int i,j; + string str; + getchar(); + //getline(cin,str); + for(i =0; i