From 2ef8c35060e7314c6e392cba26384387c4e6404c Mon Sep 17 00:00:00 2001 From: EduardGeorgescu Date: Thu, 19 Oct 2017 19:51:05 +0300 Subject: [PATCH 01/10] Balanced Brackets Problem For each string, print whether or not the string of brackets is balanced on a new line. If the brackets are balanced, print YES; otherwise, print NO. --- Balanced Brackets | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Balanced Brackets diff --git a/Balanced Brackets b/Balanced Brackets new file mode 100644 index 0000000..e3c8ec5 --- /dev/null +++ b/Balanced Brackets @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace std; + +string isBalanced(string s) { + stack myStack; + string::iterator it; + + // Convention + // 1 ---> ( or ) + // 2 ---> [ or ] + // 3 ---> { or } + + for(it = s.begin(); it < s.end(); it ++) { + switch(*it) { + case '(' : + myStack.push(1); + break; + + case '[' : + myStack.push(2); + break; + + case '{' : + myStack.push(3); + break; + + case ')' : + if(myStack.empty() || myStack.top() != 1) + return "NO"; + myStack.pop(); + break; + + case ']' : + if(myStack.empty() || myStack.top() != 2) + return "NO"; + myStack.pop(); + break; + + case '}' : + if(myStack.empty() || myStack.top() != 3) + return "NO"; + myStack.pop(); + break; + + default : + break; + } + } + + if(myStack.empty()) + return "YES"; + return "NO"; +} + +int main() { + int t; + cin >> t; + for(int a0 = 0; a0 < t; a0++){ + string s; + cin >> s; + string result = isBalanced(s); + cout << result << endl; + } + return 0; +} From 1d23d19fa2c1b817d1fcf8cabe7d1dd5ac69e54d Mon Sep 17 00:00:00 2001 From: EduardGeorgescu Date: Thu, 19 Oct 2017 19:57:36 +0300 Subject: [PATCH 02/10] Create Djikstra's Algorithm --- Djikstra's Algorithm | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Djikstra's Algorithm diff --git a/Djikstra's Algorithm b/Djikstra's Algorithm new file mode 100644 index 0000000..837dccc --- /dev/null +++ b/Djikstra's Algorithm @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +using namespace std; + +#define NMAX 100 +#define INF 100000 +vector > G[NMAX]; + +int d[NMAX]; +int N, M; + +void Dijkstra(int sursa) { + + int i, j; + pair u; + + priority_queue , vector >, greater > > Q; + + for(i = 1; i <= N; i ++) + d[i] = INF; + d[sursa] = 0; + Q.push(make_pair(0, sursa)); + + while(!Q.empty()) { + u = Q.top(); + Q.pop(); + + for(i = 0; i < G[u.second].size(); i ++) { + + + if( d[G[u.second][i].second] > d[u.second] + G[u.second][i].first) { + d[G[u.second][i].second] = d[u.second] + G[u.second][i].first; + Q.push(G[u.second][i]); + } + } + } + +} + +int main(){ + + int i, j; + int x, y, c; + + int start; + ifstream in("in1.txt"); + + in >> N >> M; + for(i = 1; i <= M; i ++) { + in >> x >> y >> c; + G[x].push_back(make_pair(c, y)); + } + + // in >> start; + start = 1; + + Dijkstra(start); + + for(i = 1; i <= N; i ++) + cout << d[i] << " "; + + cout << "\n"; + + return 0; +} From 9e5f5ea9f37edce60d105061bd74551bdd815d98 Mon Sep 17 00:00:00 2001 From: EduardGeorgescu Date: Thu, 19 Oct 2017 20:01:15 +0300 Subject: [PATCH 03/10] Create Bellman-Ford --- Bellman-Ford | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Bellman-Ford diff --git a/Bellman-Ford b/Bellman-Ford new file mode 100644 index 0000000..24ff2f1 --- /dev/null +++ b/Bellman-Ford @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +using namespace std; + +#define NMAX 100 +#define INF 100000 +vector > G[NMAX]; + +vector, int > > Edges; + +int d[NMAX]; +int N, M; + +void Bellman(int sursa) { + + int i, j; + pair u; + + priority_queue , vector >, greater > > Q; + + // std::fill(d.begin(), d.end(), INF); + for(i = 1; i <= N; i ++) + d[i] = INF; + d[sursa] = 0; + Q.push(make_pair(0, sursa)); + + for(i = 1; i <= N-2; i ++) { + for(pair, int> m : Edges) { + pair edge = m.first; + int cost = m.second; + + if(d[edge.second] > d[edge.first] + cost) + d[edge.second] = d[edge.first] + cost; + + } + + } + + for(pair, int> m : Edges) { + pair edge = m.first; + int cost = m.second; + + if(d[edge.second] > d[edge.first] + cost) + cout << "Ciclu negativ\n"; + + } + + +} + +int main(){ + + int i, j; + int x, y, c; + + int start; + + ifstream in("in1.txt"); + + in >> N >> M; + for(i = 1; i <= M; i ++) { + in >> x >> y >> c; + Edges.push_back(make_pair(make_pair(x,y), c)); + } + + start = 1; + + Bellman(start); + + return 0; +} From 5f9f24acd580deb2deb33ebb457e273e0b1bda2c Mon Sep 17 00:00:00 2001 From: EduardGeorgescu Date: Thu, 19 Oct 2017 20:03:48 +0300 Subject: [PATCH 04/10] Create Kruskal's Algorithm --- Kruskal's Algorithm | 87 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Kruskal's Algorithm diff --git a/Kruskal's Algorithm b/Kruskal's Algorithm new file mode 100644 index 0000000..6f54e4b --- /dev/null +++ b/Kruskal's Algorithm @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX_N 100 + +int Rank[MAX_N], Root[MAX_N]; + +int getRoot(int node) { + + int parent = Root[node]; + int current = node; + + while(parent != current) { + current = parent; + parent = Root[current]; + } + + return parent; +} + +void bind(int x, int y) { + + if(Rank[x] > Rank[y]) { + Root[y] = x; + } else if(Rank[x] < Rank[y]) { + Root[x] = y; + } else { + Root[y] = x; + Rank[x] ++; + } +} + +int main() { + + ifstream in("in1.txt"); + + vector > > Edges; + + vector > > Used; + int cost = 0; + + int N, M; + + in >> N >> M; + + int i, x, y, c; + for(i = 1; i <= M; i ++) { + in >> x >> y >> c; + Edges.push_back(make_pair(c, make_pair(x, y))); + } + + sort(Edges.begin(), Edges.end()); + + for(i = 1; i <= N; i ++) { + Rank[i] = 1; + Root[i] = i; + } + + + for(auto p : Edges) { + + c = p.first; + x = p.second.first; + y = p.second.second; + + // cout << x << " " << y << " " << c << "\n"; + + if(getRoot(x) != getRoot(y)) { + + cout << "Add the edge " << x << " " << y << " with the cost " << c << "\n"; + + Used.push_back(p); + cost += c; + + bind(getRoot(x), getRoot(y)); + } + } + + cout << "Total cost is: " << cost << "\n"; + + return 0; +} + From 361fd5f8c6f737b80402e9cadc1ed62a6d7173d2 Mon Sep 17 00:00:00 2001 From: EduardGeorgescu Date: Thu, 19 Oct 2017 20:37:42 +0300 Subject: [PATCH 05/10] Delete Kruskal's Algorithm --- Kruskal's Algorithm | 87 --------------------------------------------- 1 file changed, 87 deletions(-) delete mode 100644 Kruskal's Algorithm diff --git a/Kruskal's Algorithm b/Kruskal's Algorithm deleted file mode 100644 index 6f54e4b..0000000 --- a/Kruskal's Algorithm +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include -#include -#include - -using namespace std; - -#define MAX_N 100 - -int Rank[MAX_N], Root[MAX_N]; - -int getRoot(int node) { - - int parent = Root[node]; - int current = node; - - while(parent != current) { - current = parent; - parent = Root[current]; - } - - return parent; -} - -void bind(int x, int y) { - - if(Rank[x] > Rank[y]) { - Root[y] = x; - } else if(Rank[x] < Rank[y]) { - Root[x] = y; - } else { - Root[y] = x; - Rank[x] ++; - } -} - -int main() { - - ifstream in("in1.txt"); - - vector > > Edges; - - vector > > Used; - int cost = 0; - - int N, M; - - in >> N >> M; - - int i, x, y, c; - for(i = 1; i <= M; i ++) { - in >> x >> y >> c; - Edges.push_back(make_pair(c, make_pair(x, y))); - } - - sort(Edges.begin(), Edges.end()); - - for(i = 1; i <= N; i ++) { - Rank[i] = 1; - Root[i] = i; - } - - - for(auto p : Edges) { - - c = p.first; - x = p.second.first; - y = p.second.second; - - // cout << x << " " << y << " " << c << "\n"; - - if(getRoot(x) != getRoot(y)) { - - cout << "Add the edge " << x << " " << y << " with the cost " << c << "\n"; - - Used.push_back(p); - cost += c; - - bind(getRoot(x), getRoot(y)); - } - } - - cout << "Total cost is: " << cost << "\n"; - - return 0; -} - From aea98b4d639d1ffa7eae8852f0d10d54fdb52d9a Mon Sep 17 00:00:00 2001 From: EduardGeorgescu Date: Thu, 19 Oct 2017 20:38:09 +0300 Subject: [PATCH 06/10] Create Kruskal's Algorithm --- Kruskal's Algorithm | 87 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Kruskal's Algorithm diff --git a/Kruskal's Algorithm b/Kruskal's Algorithm new file mode 100644 index 0000000..6f54e4b --- /dev/null +++ b/Kruskal's Algorithm @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +using namespace std; + +#define MAX_N 100 + +int Rank[MAX_N], Root[MAX_N]; + +int getRoot(int node) { + + int parent = Root[node]; + int current = node; + + while(parent != current) { + current = parent; + parent = Root[current]; + } + + return parent; +} + +void bind(int x, int y) { + + if(Rank[x] > Rank[y]) { + Root[y] = x; + } else if(Rank[x] < Rank[y]) { + Root[x] = y; + } else { + Root[y] = x; + Rank[x] ++; + } +} + +int main() { + + ifstream in("in1.txt"); + + vector > > Edges; + + vector > > Used; + int cost = 0; + + int N, M; + + in >> N >> M; + + int i, x, y, c; + for(i = 1; i <= M; i ++) { + in >> x >> y >> c; + Edges.push_back(make_pair(c, make_pair(x, y))); + } + + sort(Edges.begin(), Edges.end()); + + for(i = 1; i <= N; i ++) { + Rank[i] = 1; + Root[i] = i; + } + + + for(auto p : Edges) { + + c = p.first; + x = p.second.first; + y = p.second.second; + + // cout << x << " " << y << " " << c << "\n"; + + if(getRoot(x) != getRoot(y)) { + + cout << "Add the edge " << x << " " << y << " with the cost " << c << "\n"; + + Used.push_back(p); + cost += c; + + bind(getRoot(x), getRoot(y)); + } + } + + cout << "Total cost is: " << cost << "\n"; + + return 0; +} + From 381e34e54e279913d8bc9d93182ba5ea0027dcf4 Mon Sep 17 00:00:00 2001 From: EduardGeorgescu Date: Thu, 19 Oct 2017 20:41:17 +0300 Subject: [PATCH 07/10] Delete Djikstra's Algorithm --- Djikstra's Algorithm | 68 -------------------------------------------- 1 file changed, 68 deletions(-) delete mode 100644 Djikstra's Algorithm diff --git a/Djikstra's Algorithm b/Djikstra's Algorithm deleted file mode 100644 index 837dccc..0000000 --- a/Djikstra's Algorithm +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include -#include - -using namespace std; - -#define NMAX 100 -#define INF 100000 -vector > G[NMAX]; - -int d[NMAX]; -int N, M; - -void Dijkstra(int sursa) { - - int i, j; - pair u; - - priority_queue , vector >, greater > > Q; - - for(i = 1; i <= N; i ++) - d[i] = INF; - d[sursa] = 0; - Q.push(make_pair(0, sursa)); - - while(!Q.empty()) { - u = Q.top(); - Q.pop(); - - for(i = 0; i < G[u.second].size(); i ++) { - - - if( d[G[u.second][i].second] > d[u.second] + G[u.second][i].first) { - d[G[u.second][i].second] = d[u.second] + G[u.second][i].first; - Q.push(G[u.second][i]); - } - } - } - -} - -int main(){ - - int i, j; - int x, y, c; - - int start; - ifstream in("in1.txt"); - - in >> N >> M; - for(i = 1; i <= M; i ++) { - in >> x >> y >> c; - G[x].push_back(make_pair(c, y)); - } - - // in >> start; - start = 1; - - Dijkstra(start); - - for(i = 1; i <= N; i ++) - cout << d[i] << " "; - - cout << "\n"; - - return 0; -} From 59335b023d3101ade45e61ffe92973202c4c72ae Mon Sep 17 00:00:00 2001 From: EduardGeorgescu Date: Thu, 19 Oct 2017 20:41:41 +0300 Subject: [PATCH 08/10] Create Dijkstra --- Dijkstra | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Dijkstra diff --git a/Dijkstra b/Dijkstra new file mode 100644 index 0000000..837dccc --- /dev/null +++ b/Dijkstra @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +using namespace std; + +#define NMAX 100 +#define INF 100000 +vector > G[NMAX]; + +int d[NMAX]; +int N, M; + +void Dijkstra(int sursa) { + + int i, j; + pair u; + + priority_queue , vector >, greater > > Q; + + for(i = 1; i <= N; i ++) + d[i] = INF; + d[sursa] = 0; + Q.push(make_pair(0, sursa)); + + while(!Q.empty()) { + u = Q.top(); + Q.pop(); + + for(i = 0; i < G[u.second].size(); i ++) { + + + if( d[G[u.second][i].second] > d[u.second] + G[u.second][i].first) { + d[G[u.second][i].second] = d[u.second] + G[u.second][i].first; + Q.push(G[u.second][i]); + } + } + } + +} + +int main(){ + + int i, j; + int x, y, c; + + int start; + ifstream in("in1.txt"); + + in >> N >> M; + for(i = 1; i <= M; i ++) { + in >> x >> y >> c; + G[x].push_back(make_pair(c, y)); + } + + // in >> start; + start = 1; + + Dijkstra(start); + + for(i = 1; i <= N; i ++) + cout << d[i] << " "; + + cout << "\n"; + + return 0; +} From f6037b1f7f7b84084335239bb580aad16f07c64a Mon Sep 17 00:00:00 2001 From: EduardGeorgescu Date: Fri, 20 Oct 2017 08:33:55 +0300 Subject: [PATCH 09/10] Create A* implemented the A* algorithm with the purpose of escaping from a labirinth --- A* | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 A* diff --git a/A* b/A* new file mode 100644 index 0000000..5ca1215 --- /dev/null +++ b/A* @@ -0,0 +1,113 @@ +#include +#include +#include + +using namespace std; + +#define MAX_N 100 +#define INF 100000 + +int dx[4] = {1, 0, -1, 0}; +int dy[4] = {0, 1, 0, -1}; + +int dist(int crt_x, int crt_y, int fin_x, int fin_y) { + int dist_x = fin_x - crt_x; + int dist_y = fin_y - crt_y; + + return (dist_x * dist_x + dist_y * dist_y); +} + +int g[MAX_N][MAX_N]; + +int main() { + + ifstream in("Puzzle.in"); + + /* + TEST INPUT + 4 4 + 0 0 + 3 3 + 1 1 1 0 + 0 1 0 0 + 0 1 1 0 + 1 0 1 1 + */ + + int N, M; + int A[MAX_N][MAX_N]; + + int i, j; + + in >> N >> M; + + int start_x, start_y; + int stop_x, stop_y; + + in >> start_x >> start_y; + in >> stop_x >> stop_y; + + for(i = 1; i <= N; i ++) + for(j = 1; j <= M; j ++) { + in >> A[i][j]; + g[i][j] = INF; + } + + priority_queue >, + vector > >, + greater > > > myPrioQueue; + + start_x ++; + start_y ++; + stop_x ++; + stop_y ++; + + g[start_x][start_y] = 0; + + int new_x, new_y; + + int f, h; + + myPrioQueue.push(make_pair(0, make_pair(start_x, start_y))); + + int current_x, current_y; + pair > current; + + while(!myPrioQueue.empty()) { + current = myPrioQueue.top(); + myPrioQueue.pop(); + + current_x = current.second.first; + current_y = current.second.second; + + f = current.first; + + for(i = 0; i < 4; i ++) { + new_x = current_x + dx[i]; + new_y = current_y + dy[i]; + + if(new_x == 0 || new_x == N + 1 || new_y == 0 || new_y == M + 1 || !A[new_x][new_y]) + continue; + + h = dist(new_x, new_y, stop_x, stop_y); + + if(g[new_x][new_y] > g[current_x][current_y] + 1) { + + g[new_x][new_y] = g[current_x][current_y] + 1; + + f = h + g[new_x][new_y]; + + myPrioQueue.push(make_pair(f, make_pair(new_x, new_y))); + } + } + } + + cout << "Distances from source " << start_x << " " << start_y << " to destination " << stop_x << " " << stop_y << " are:\n"; + for(i = 1; i <= N; i ++) { + for(j = 1; j <= M; j ++) + cout << g[i][j] << " "; + cout << "\n"; + } + + return 0; +} From a8fc8012d41ef9b633a6b9da3fc6bb065c545add Mon Sep 17 00:00:00 2001 From: EduardGeorgescu Date: Fri, 20 Oct 2017 08:34:33 +0300 Subject: [PATCH 10/10] Create Puzzle.in input file to test the A* algorithm --- Puzzle.in | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Puzzle.in diff --git a/Puzzle.in b/Puzzle.in new file mode 100644 index 0000000..f1677cc --- /dev/null +++ b/Puzzle.in @@ -0,0 +1,7 @@ +4 4 +0 0 +3 3 +1 1 1 0 +0 1 0 0 +0 1 1 0 +1 0 1 1