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; +} 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; +} 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; +} 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; +} 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; +} + 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