From 01c5cb3ec00c9e173ec794dd03e08914a36deeb7 Mon Sep 17 00:00:00 2001 From: Nikita Jain Date: Wed, 25 Oct 2017 00:26:03 +0530 Subject: [PATCH 1/4] pacman_dfs --- pacman_dfs.cpp | 299 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 299 insertions(+) create mode 100644 pacman_dfs.cpp diff --git a/pacman_dfs.cpp b/pacman_dfs.cpp new file mode 100644 index 0000000..2628d30 --- /dev/null +++ b/pacman_dfs.cpp @@ -0,0 +1,299 @@ +#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 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 Date: Wed, 25 Oct 2017 00:30:55 +0530 Subject: [PATCH 2/4] pacman_bfs.cpp pacman using bfs is added --- pacman_bfs.cpp | 248 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 pacman_bfs.cpp diff --git a/pacman_bfs.cpp b/pacman_bfs.cpp new file mode 100644 index 0000000..3b69551 --- /dev/null +++ b/pacman_bfs.cpp @@ -0,0 +1,248 @@ +#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 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 Date: Wed, 25 Oct 2017 00:33:06 +0530 Subject: [PATCH 3/4] pacman_astar pacman_astar is added --- pacman_astar.cpp | 294 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 294 insertions(+) create mode 100644 pacman_astar.cpp 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 Date: Wed, 25 Oct 2017 00:34:33 +0530 Subject: [PATCH 4/4] pacman-ucs pacman-ucs is added --- pacman_ucs.cpp | 294 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 294 insertions(+) create mode 100644 pacman_ucs.cpp diff --git a/pacman_ucs.cpp b/pacman_ucs.cpp new file mode 100644 index 0000000..c483a92 --- /dev/null +++ b/pacman_ucs.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 = 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