diff --git a/W1/.vscode/settings.json b/W1/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..4a037dde51e05d2f775494ae610076693d45f5c1 --- /dev/null +++ b/W1/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "files.associations": { + "iostream": "cpp", + "iosfwd": "cpp", + "array": "cpp", + "string_view": "cpp", + "vector": "cpp" + } +} \ No newline at end of file diff --git a/W1/Ljutnja/.vscode/settings.json b/W1/Ljutnja/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..0cba2e6834fb66c3e0efef2684351d81ef47c4b3 --- /dev/null +++ b/W1/Ljutnja/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "iostream": "cpp" + } +} \ No newline at end of file diff --git a/W1/Ljutnja/Ljutunja.cc b/W1/Ljutnja/Ljutunja.cc new file mode 100644 index 0000000000000000000000000000000000000000..fb11b576246a4adf9d89e37d6f2cff40c53daa9b --- /dev/null +++ b/W1/Ljutnja/Ljutunja.cc @@ -0,0 +1,54 @@ +#include <iostream> +#include <sstream> +#include <vector> +#include <math.h> + +using namespace std; + +int main() +{ + int M; + int N; + long int anger{0}; + long int sumOfNeeds{0}; + cin >> M; + cin >> N; + vector<int> needs; + vector<int> taken; + for (int i{0}; i < N; ++i) + { + int temp{0}; + cin >> temp; + needs.push_back(temp); + taken.push_back(0); + sumOfNeeds += temp; + } + + int j{0}; + for (int i{0}; i < sumOfNeeds-M; ++i) + { + while(needs[j]-1<0) + { + j++; + if (j > taken.size()) + { + j = 0; + } + } + needs[j] -= 1; + taken[j] += 1; + j++; + if (j > taken.size()) + { + j = 0; + } + } + + for (auto const & element : taken) + { + anger += element * element; + } + + cout << anger << "\n"; + return 0; +} \ No newline at end of file diff --git a/W1/Ljutnja/a.out b/W1/Ljutnja/a.out new file mode 100755 index 0000000000000000000000000000000000000000..a2be06d480bdd18de2509c27bf271c462bfb5a63 Binary files /dev/null and b/W1/Ljutnja/a.out differ diff --git a/W1/Ljutnja/ljutnja.1.ans b/W1/Ljutnja/ljutnja.1.ans new file mode 100644 index 0000000000000000000000000000000000000000..d00491fd7e5bb6fa28c517a0bb32b8b506539d4d --- /dev/null +++ b/W1/Ljutnja/ljutnja.1.ans @@ -0,0 +1 @@ +1 diff --git a/W1/Ljutnja/ljutnja.1.in b/W1/Ljutnja/ljutnja.1.in new file mode 100644 index 0000000000000000000000000000000000000000..9b0464ddf875daa897042eacab87389cf8acbddf --- /dev/null +++ b/W1/Ljutnja/ljutnja.1.in @@ -0,0 +1,4 @@ +5 3 +1 +3 +2 diff --git a/W1/Ljutnja/ljutnja.2.ans b/W1/Ljutnja/ljutnja.2.ans new file mode 100644 index 0000000000000000000000000000000000000000..b8626c4cff2849624fb67f87cd0ad72b163671ad --- /dev/null +++ b/W1/Ljutnja/ljutnja.2.ans @@ -0,0 +1 @@ +4 diff --git a/W1/Ljutnja/ljutnja.2.in b/W1/Ljutnja/ljutnja.2.in new file mode 100644 index 0000000000000000000000000000000000000000..7b655b9dcfc35323fdc3bc9bfb78a19744eda7af --- /dev/null +++ b/W1/Ljutnja/ljutnja.2.in @@ -0,0 +1,7 @@ +10 6 +7 +6 +5 +3 +1 +8 diff --git a/W1/help2/a.out b/W1/help2/a.out new file mode 100755 index 0000000000000000000000000000000000000000..5415b146ebace8dff673f0d13d38c97b978f0edf Binary files /dev/null and b/W1/help2/a.out differ diff --git a/W1/help2/help.ans b/W1/help2/help.ans new file mode 100644 index 0000000000000000000000000000000000000000..63b9891fddc1137d45c3e52d7491964bd8c456f5 --- /dev/null +++ b/W1/help2/help.ans @@ -0,0 +1,3 @@ +how now brown cow +- +c b diff --git a/W1/help2/help.in b/W1/help2/help.in new file mode 100644 index 0000000000000000000000000000000000000000..e7b53feea6688f43d70da16bef08389ab3abec72 --- /dev/null +++ b/W1/help2/help.in @@ -0,0 +1,9 @@ +4 +how now brown <animal> +<foo> now <color> cow +you are <a> home +<a> <b> <a> <a> +<2> <4> <2> <4> +<1> g <3> <3> +<9> <9> <2> +<2> <3> <3> \ No newline at end of file diff --git a/W1/help2/help2.cc b/W1/help2/help2.cc new file mode 100644 index 0000000000000000000000000000000000000000..042abcccf694fcb49b8415eaeb760bc83d93fddb --- /dev/null +++ b/W1/help2/help2.cc @@ -0,0 +1,151 @@ +#include <iostream> +#include <sstream> +#include <string> +#include <vector> +#include <map> + +using namespace std; + +bool isKey(string s, map<string, string> m) +{ + return (m.find(s) != m.end()); +} + +bool isPlaceholder(string const &s) +{ + return (s.front() == '<' && s.back() == '>'); +} + +bool replaceForced(string &string1, string &string2) +{ + bool change = false; + istringstream s1{string1}; + istringstream s2{string2}; + string temp1; + string temp2; + map<string, string> m1; + map<string, string> m2; + string out1; + string out2; + + while (s1 >> temp1) + { + s2 >> temp2; + if (isPlaceholder(temp1) && !isPlaceholder(temp2)) + { + m1[temp1] = temp2; + } + + if (isPlaceholder(temp2) && !isPlaceholder(temp1)) + { + m2[temp2] = temp1; + } + } + + istringstream s3{string1}; + istringstream s4{string2}; + while (s3 >> temp1) + { + s4 >> temp2; + if (isKey(temp1, m1)) + { + out1 += m1.at(temp1) + " "; + change = true; + } + else + { + out1 += temp1 + " "; + } + + if (isKey(temp2, m2)) + { + out2 += m2.at(temp2) + " "; + change = true; + } + else + { + out2 += temp2 + " "; + } + } + string1 = out1.substr(0, out1.length() - 1); + string2 = out2.substr(0, out2.length() - 1); + return change; +} + +bool replaceCircular(string & string1, string & string2) +{ + bool change = false; + istringstream s1{string1}; + istringstream s2{string2}; + string temp1; + string temp2; + map<string, string> m1; + map<string, string> m2; + string out1; + string out2; + + while (s1 >> temp1) + { + s2 >> temp2; + if (isPlaceholder(temp1) && isPlaceholder(temp2)) + { + m1[temp1] = "x"; + m2[temp2] = "x"; + } + } + + istringstream s3{string1}; + istringstream s4{string2}; + while (s3 >> temp1) + { + s4 >> temp2; + if (isKey(temp1, m1)) + { + out1 += m1.at(temp1) + " "; + } + else + { + out1 += temp1 + " "; + } + + if (isKey(temp2, m2)) + { + out2 += m2.at(temp2) + " "; + } + else + { + out2 += temp2 + " "; + } + } + string1 = out1.substr(0, out1.length() - 1); + string2 = out2.substr(0, out2.length() - 1); + return change; +} + +int main() +{ + int its; + string line1; + string line2; + cin >> its; + cin.get(); + for (int i{0}; i < its; ++i) + { + getline(cin, line1); + getline(cin, line2); + while (replaceForced(line1, line2)); + replaceCircular(line1, line2); + + if (line1 == line2) + { + cout << line1 << "\n"; + } + else + { + cout << "-\n"; + } + } + + cout << flush; + return 0; +} diff --git a/W3/.vscode/settings.json b/W3/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..1be854fd2ce7df6048a2496eecdbcf417ad9900c --- /dev/null +++ b/W3/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "cmath": "cpp" + } +} \ No newline at end of file diff --git a/W3/garbage/a.out b/W3/garbage/a.out new file mode 100755 index 0000000000000000000000000000000000000000..6c972bfe3e5b4d57ab008951bd1d40e048245680 Binary files /dev/null and b/W3/garbage/a.out differ diff --git a/W3/garbage/garbage.cc b/W3/garbage/garbage.cc new file mode 100644 index 0000000000000000000000000000000000000000..63807c80f4c4540a170a783e1d315ebcf20f2fd4 --- /dev/null +++ b/W3/garbage/garbage.cc @@ -0,0 +1,35 @@ +#include <cmath> +#include <iostream> +#include <vector> + +using namespace std; + + +int main() +{ + int nums[7] = {0, 1, 2, 5, 9, 8, 6}; + int input; + while(cin >> input) + { + vector<int> order; + int digits = log(input)/log(7); + cerr << digits << "\n"; + for (int i = digits; i >= 0; --i) + { + int power = pow(7,i); + int digit = input/power; + order.push_back(digit); + input %= power; + } + + for (int size = order.size()-1; size >= 0; --size) + { + cout << nums[order[size]]; + } + cout << "\n"; + } + + + + return 0; +} \ No newline at end of file diff --git a/W3/garbage/input.in b/W3/garbage/input.in new file mode 100644 index 0000000000000000000000000000000000000000..e2e107ac61ac259b87c544f6e7a4eb03422c6c21 --- /dev/null +++ b/W3/garbage/input.in @@ -0,0 +1 @@ +123456789 \ No newline at end of file diff --git a/W3/garbage/output.out b/W3/garbage/output.out new file mode 100644 index 0000000000000000000000000000000000000000..5d6e2d35f35d7a22345311829e3c709bca36ec8e --- /dev/null +++ b/W3/garbage/output.out @@ -0,0 +1,2 @@ +661 +002 \ No newline at end of file diff --git a/W3/output.out b/W3/output.out new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/W3/powers.cc b/W3/powers.cc new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/W4/cudak/a.out b/W4/cudak/a.out new file mode 100755 index 0000000000000000000000000000000000000000..22aba2941c2138559e7c6335c7632b0100dc5e45 Binary files /dev/null and b/W4/cudak/a.out differ diff --git a/W4/cudak/cudak.cc b/W4/cudak/cudak.cc new file mode 100644 index 0000000000000000000000000000000000000000..5a8a038d95b7be0e5c8e7760bc6e1bf1b06f42aa --- /dev/null +++ b/W4/cudak/cudak.cc @@ -0,0 +1,61 @@ +#include <iostream> +#include <string> +#include <sstream> + +using namespace std; + +long int allocated{100000000}; +int saved [100000000]; + + +int recursive(long int number) +{ + if (number == -1 || number == 0) + { + return 0; + } + + if (number <= allocated && saved[number-1] != 0) + { + return saved[number-1]; + } + + stringstream s {to_string(number)}; + long int remainder{-1}; + int left {s.get()-48}; + s >> remainder; + + if (number <= allocated) + { + saved[number-1] = left + recursive(remainder); + return saved[number-1]; + } + + return left + recursive(remainder); +} + +int main() +{ + long int start{0}; + long int end{0}; + int goal{0}; + long int min{-1}; + long int ammount{0}; + cin >> start; + cin >> end; + cin >> goal; + for (long int number{end}; number >= start; --number) + { + int sum{recursive(number)}; + if (sum == goal) + { + ++ammount; + if (number < min || min < 0) + { + min = number; + } + } + } + cout << ammount << "\n" << min; + return 0; +} \ No newline at end of file diff --git a/W4/cudak/input.in b/W4/cudak/input.in new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/W4/cudak/output.out b/W4/cudak/output.out new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/W4/input.in b/W4/input.in new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/W4/uxuhul.cc b/W4/uxuhul.cc new file mode 100644 index 0000000000000000000000000000000000000000..abaa6045ddd5761fd9c8e8fc0722545c19dc76f7 --- /dev/null +++ b/W4/uxuhul.cc @@ -0,0 +1,22 @@ +#include <iostream> +#include <sstream> + + +using namespace std; + +bool first[] = {false, false, false, false, true, true, true, true}; +bool second[] = {false, false, true, true, false, false, true, true}; +bool third[] = {false, true, false, true, false, true, false, true}; +bool stones[] = {false, false, false}; + + +int main() +{ + + + + + + + return 0; +} \ No newline at end of file diff --git a/W4/uxuhul/.vscode/settings.json b/W4/uxuhul/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..d95e77b359277e8b96e86f121791a835043f38b2 --- /dev/null +++ b/W4/uxuhul/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "*.tcc": "cpp", + "iostream": "cpp" + } +} \ No newline at end of file diff --git a/W4/uxuhul/a.out b/W4/uxuhul/a.out new file mode 100755 index 0000000000000000000000000000000000000000..532c83c518dfabe924117358f933b46eb55cd24d Binary files /dev/null and b/W4/uxuhul/a.out differ diff --git a/W4/uxuhul/input.in b/W4/uxuhul/input.in new file mode 100644 index 0000000000000000000000000000000000000000..6785b8f8edc2a76c01eaddcb08e7865420a92c07 --- /dev/null +++ b/W4/uxuhul/input.in @@ -0,0 +1,4 @@ +1 +1 +1 2 3 4 5 6 7 8 +8 7 6 5 4 3 2 1 diff --git a/W4/uxuhul/test.py b/W4/uxuhul/test.py new file mode 100644 index 0000000000000000000000000000000000000000..249dd0090a5ef2ea84aaf6154ae774b6bfea477f --- /dev/null +++ b/W4/uxuhul/test.py @@ -0,0 +1,64 @@ +from sys import stdin + +def middle_priests(pref_in, pref_out): + #print('middle priests') + next_step = {} + next_step['NNN'] = ['YNN', 'NYN', 'NNY'] + next_step['NNY'] = ['YNY', 'NYY', 'NNN'] + next_step['NYN'] = ['YYN', 'NNN', 'NYY'] + next_step['NYY'] = ['YYY', 'NYN', 'NNY'] + next_step['YNN'] = ['YYN', 'YNY', 'NNN'] + next_step['YNY'] = ['YYY', 'YNN', 'NNY'] + next_step['YYN'] = ['YNN', 'YYY', 'NYN'] + next_step['YYY'] = ['YYN', 'NYY', 'YNY'] + + pref_translate = ["NNN", "NNY", "NYN", "NYY", "YNN", "YNY", "YYN", "YYY"] + list_out = ["0", "0", "0", "0", "0", "0", "0", "0"] + #print(pref_out) + + for i in range(1,9,1): + best = pref_in.index(str(i)) + #print(best) + if pref_translate[best] in pref_out: + new_best = pref_translate[pref_out.index(pref_translate[best])] + indices = [i for i, x in enumerate(pref_out) if x == pref_translate[best]] + for i in indices: + values = next_step[pref_translate[i]] + #print(values) + for v in values: + + if(list_out[pref_translate.index(v)] == "0"): + #print("index") + #print(pref_translate.index(v)) + list_out[pref_translate.index(v)] = pref_translate[best] #pref_out[i] + #print(list_out) + return list_out + + +def main(): + lines = [] + + for line in stdin: + lines.append(line) + n_problems = int(lines[0]) + #print(n_problems) + line_iterator = 1 + state_V = 'NNN' + all_lookups = [] + + for i in range(n_problems): + start_translate = ["NNN", "NNY", "NYN", "NYY", "YNN", "YNY", "YYN", "YYY"] + n_priests = int(lines[line_iterator]) + #print(n_priests) + for x in range(n_priests,0,-1): + inp = lines[line_iterator+x].split() + #print("input") + #print(inp) + #print(start_translate) + pref = middle_priests(inp, start_translate) + start_translate = pref + #print("results after iteration") + print(start_translate[0]) + line_iterator +=(n_priests+1) + +main() \ No newline at end of file diff --git a/W4/uxuhul/uxuhul.cc b/W4/uxuhul/uxuhul.cc new file mode 100644 index 0000000000000000000000000000000000000000..3ee03b8056e4ac06f49a7efd70886bcccaa8566f --- /dev/null +++ b/W4/uxuhul/uxuhul.cc @@ -0,0 +1,74 @@ +#include <iostream> +#include <hash_map> + +using namespace std; + +string key[8] = {"NNN", "NNY", "NYN", "NYY", "YNN", "YNY", "YYN", "YYY"}; + +int map[8][3] = {{0b001, 0b010, 0b100}, {0b101, 0b011, 0b000}, {0b110, 0b000, 0b011}, {0b111, 0b001, 0b010}, {0b000, 0b110, 0b101}, {0b001, 0b111, 0b100}, {0b010, 0b100, 0b101}, {0b011, 0b101, 0b110}}; + +int main() +{ + int cases, priests; + cin >> cases; + cerr << "Cases: " << cases << "\n"; + for (int i = 0; i < cases; ++i) + { + cin >> priests; + cout << "Priests: " << priests << "\n"; + int table[8][priests] = {}; + + for (int k = 1; k < 9; ++k) + { + cout << priests << "|" << k-1 << "\n"; + table[k-1][priests] = k; + } + + for (int j = priests-1; j >= 0; --j) + { + cerr << "Priest #: " << j << "\n"; + int one, two, three, four, five, six, seven, eight; + cin >> one >> two >> three >> four >> five >> six >> seven >> eight; + unordered_map<int, int> preference; + preference[one] = 1; + preference[two] = 2; + preference[three] = 3; + preference[four] = 4; + preference[five] = 5; + preference[six] = 6; + preference[seven] = 7; + preference[eight] = 8; + + for (int k = 1; k <= 8; ++k) + { + for (int n = 0; n < 9; ++n) + { + cerr << "J+1 = "<< j+1 << "\n"; + cerr << "Table N J+1 = " << table[n][j + 1] << "\n"; + if (table[n][j + 1] == preference[k]) + { + for (int l = 0; l < 3; ++l) + { + if (table[map[preference[k]][l]][j] == -1) + { + table[map[preference[k]][l]][j] = preference[k]; + } + } + break; + } + } + } + } + + for (int j = 0; j < priests + 1; ++j) + { + for (int k = 0; k < 8; ++k) + { + cout << table[k][j] << " "; + } + cout << "-\n"; + } + } + + return 0; +} \ No newline at end of file diff --git a/W4/uxuhul/uxuhul2.cc b/W4/uxuhul/uxuhul2.cc new file mode 100644 index 0000000000000000000000000000000000000000..cf84a8d61ac14f3717de74c28c92599e3df0a99f --- /dev/null +++ b/W4/uxuhul/uxuhul2.cc @@ -0,0 +1,87 @@ +#include <iostream> +#include <sstream> +#include <unordered_map> + +using namespace std; + +string key[8] = {"NNN", "NNY", "NYN", "NYY", "YNN", "YNY", "YYN", "YYY"}; + +int map[8][3] = {{0b001, 0b010, 0b100}, {0b101, 0b011, 0b000}, {0b110, 0b000, 0b011}, {0b111, 0b001, 0b010}, {0b000, 0b110, 0b101}, {0b001, 0b111, 0b100}, {0b010, 0b100, 0b111}, {0b011, 0b101, 0b110}}; + +int main() +{ + int cases, priests; + cin >> cases; + for (int number = 0; number < cases; ++number) + { + cin >> priests; + int table[8][priests+1] = {}; + + for (int vote = 0; vote < 8; ++vote) + { + for (int priest = 0; priest < priests; ++priest) + { + table[vote][priest] = -1; + } + } + + for (int k = 0; k < 8; ++k) + { + table[k][priests] = k; + } + + string strings[priests]; + cin.get(); + for (int j = 0; j < priests; ++j) + { + getline(cin, strings[j]); + } + + for (int priest = priests-1; priest >= 0; --priest) + { + istringstream s {strings[priest]}; + + int one, two, three, four, five, six, seven, eight; + s >> one >> two >> three >> four >> five >> six >> seven >> eight; + unordered_map<int, int> preferences; + preferences[one] = 0; + preferences[two] = 1; + preferences[three] = 2; + preferences[four] = 3; + preferences[five] = 4; + preferences[six] = 5; + preferences[seven] = 6; + preferences[eight] = 7; + + for (int preference = 1; preference <= 8; ++preference) + { + for (int element = 0; element < 8; ++element) + { + if(table[element][priest+1] == preferences[preference]) + { + if (priest == 0 && (element == 1 || element == 2 || element == 4)) + { + cout << key[preferences[preference]] << "\n"; + goto label3; + } + for (int index = 0; index < 3; ++index) + { + if (table[map[element][index]][priest] == -1) + { + table[map[element][index]][priest] = preferences[preference]; + } + } + + } + } + } + label3: + int b; + } + } + + + + + return 0; +} \ No newline at end of file diff --git a/W5/.vscode/settings.json b/W5/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..155e0f35653b8496162dfa5f75d5b452e297b0a1 --- /dev/null +++ b/W5/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "*.tcc": "cpp", + "iostream": "cpp", + "iosfwd": "cpp" + } +} \ No newline at end of file diff --git a/W5/a.out b/W5/a.out new file mode 100755 index 0000000000000000000000000000000000000000..26892ad6043bb8b3c9290294cb648ab9ab7ac4fb Binary files /dev/null and b/W5/a.out differ diff --git a/W5/gold.cc b/W5/gold.cc new file mode 100644 index 0000000000000000000000000000000000000000..d3c8a2c9b2c4c081037eb93204bf7fadc3fb8ebc --- /dev/null +++ b/W5/gold.cc @@ -0,0 +1,152 @@ +#include <iostream> +#include <sstream> + +using namespace std; + +struct Node +{ + Node *up; + Node *down; + Node *left; + Node *right; + bool leaf; + bool calculated; + int value; + int calc() + { + if (calculated) + { + return 0; + } + calculated = true; + if (leaf) + { + return value; + } + int u{0}; + int d{0}; + int l{0}; + int r{0}; + if (left) + { + l = left->calc(); + } + + if (right) + { + r = right->calc(); + } + + if (up) + { + u = up->calc(); + } + + if (down) + { + d = down->calc(); + } + + return value + u + d + l + r; + } +}; + +int main() +{ + Node *player; + int rows, cols; + cin >> cols >> rows; + cin.get(); + Node *nodes[rows][cols]; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + nodes[i][j] = new Node{}; + } + } + + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + if (j - 1 >= 0) + { + nodes[i][j]->left = nodes[i][j - 1]; + } + if (j + 1 < cols) + { + nodes[i][j]->right = nodes[i][j + 1]; + } + if (i - 1 >= 0) + { + nodes[i][j]->up = nodes[i - 1][j]; + } + if (i + 1 < rows) + { + nodes[i][j]->down = nodes[i + 1][j]; + } + } + } + + for (int i = 0; i < rows; ++i) + { + string row; + getline(cin, row); + for (int j = 0; j < cols; ++j) + { + char c = row.at(j); + + if (c == '#') + { + if (nodes[i][j]->up) + { + nodes[i][j]->up->down = nullptr; + } + if (nodes[i][j]->left) + { + nodes[i][j]->left->right = nullptr; + } + if (nodes[i][j]->down) + { + nodes[i][j]->down->up = nullptr; + } + if (nodes[i][j]->right) + { + + nodes[i][j]->right->left = nullptr; + } + } + else if (c == 'G') + { + nodes[i][j]->value = 1; + } + else if (c == 'P') + { + player = nodes[i][j]; + } + else if (c == 'T') + { + if (nodes[i][j]->up) + { + nodes[i][j]->up->leaf = true; + } + if (nodes[i][j]->left) + { + nodes[i][j]->left->leaf = true; + } + if (nodes[i][j]->down) + { + nodes[i][j]->down->leaf = true; + } + if (nodes[i][j]->right) + { + nodes[i][j]->right->leaf = true; + } + } + } + } + + cout << player->calc(); + return 0; +} \ No newline at end of file diff --git a/W5/input.in b/W5/input.in new file mode 100644 index 0000000000000000000000000000000000000000..b6e3aedd5470550df078b54d573fb38367fa0091 --- /dev/null +++ b/W5/input.in @@ -0,0 +1,7 @@ +8 6 +######## +#...GTG# +#..PG.G# +#...G#G# +#..TG.G# +######## \ No newline at end of file diff --git a/W6/.vscode/settings.json b/W6/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..dd6ab2e9ccc2645e8cdf48f110980d168d51e2b4 --- /dev/null +++ b/W6/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "files.associations": { + "cmath": "cpp", + "stack": "cpp", + "deque": "cpp", + "string": "cpp", + "vector": "cpp", + "iostream": "cpp" + } +} \ No newline at end of file diff --git a/W6/a.out b/W6/a.out new file mode 100755 index 0000000000000000000000000000000000000000..24251126f0fe98220eb9e2255ef450ef2094e997 Binary files /dev/null and b/W6/a.out differ diff --git a/W6/input.in b/W6/input.in new file mode 100644 index 0000000000000000000000000000000000000000..4c3ac99a1de007baf58bdc0c9d3fdc307fb28b52 --- /dev/null +++ b/W6/input.in @@ -0,0 +1,16 @@ +2 +3 +0.0 0.0 +0.0 1.0 +1.0 0.0 +10 +30.0 38.0 +43.0 72.0 +47.0 46.0 +49.0 69.0 +52.0 42.0 +58.0 17.0 +73.0 7.0 +84.0 81.0 +86.0 75.0 +93.0 50.0 \ No newline at end of file diff --git a/W6/islands.cc b/W6/islands.cc new file mode 100644 index 0000000000000000000000000000000000000000..8b1f7286da1fadc8bb3d442c0522c39a988a30ad --- /dev/null +++ b/W6/islands.cc @@ -0,0 +1,203 @@ +#include <cmath> +#include <algorithm> +#include <vector> +#include <utility> +#include <iostream> +#include <iomanip> +#include <bits/stdc++.h> + +#define MAX 100000 + +using namespace std; + +class Graph +{ +private: + int numberOfNodes; + int numberOfEdges; + + vector<pair<float, pair<int, int>>> edges; + + float **adj; + + int getKey(int key[], bool connected[]); + +public: + Graph(int nodes, int edges); + void addEdge(float weight, int from, int to); + float kruskal(); + float prim(); +}; + +class Sets +{ +private: + int dataPoints; + int *parent; + int *size; + +public: + Sets(int dataPoints); + int find(int target); + void mergeSets(int left, int right); +}; + +Graph::Graph(int nodes, int edges) + : numberOfNodes{nodes}, numberOfEdges{edges} +{ + adj = new float *[nodes]; + for (int i{0}; i < nodes; ++i) + { + adj[i] = new float[nodes]; + memset(adj[i], 0, nodes * sizeof(float)); + } +} + +void Graph::addEdge(float weight, int from, int to) +{ + edges.push_back({weight, {from, to}}); + adj[from][to] = weight; + adj[to][from] = weight; +} + +float Graph::kruskal() +{ + float minimum{0}; + + Sets sets{numberOfNodes}; + sort(edges.begin(), edges.end()); + + for (auto edge : edges) + { + int left{edge.second.first}; + int right{edge.second.second}; + int leftSet{sets.find(left)}; + int rightSet{sets.find(right)}; + + if (leftSet != rightSet) + { + sets.mergeSets(left, right); + minimum += edge.first; + } + } + + return minimum; +} + +float Graph::prim() +{ + float minimum{0}; + bool connected[numberOfNodes]; + memset(connected, false, numberOfNodes * sizeof(bool)); + connected[0] = true; + + for (int i{0}; i < numberOfNodes - 1; ++i) + { + float min = MAX; + int row{0}; + int col{0}; + + for (int j{0}; j < numberOfNodes; ++j) + { + if (connected[j]) + { + for (int k{0}; k < numberOfNodes; ++k) + { + if (!connected[k] && adj[j][k] != 0 && min > adj[j][k]) + { + min = adj[j][k]; + row = j; + col = k; + } + } + } + } + connected[col] = true; + minimum += min; + } + + return minimum; +} + +Sets::Sets(int dataPoints) + : dataPoints{dataPoints}, parent{new int[dataPoints]}, size{new int[dataPoints]} +{ + for (int i{0}; i < dataPoints; ++i) + { + parent[i] = i; + size[i] = 0; + } +} + +int Sets::find(int target) +{ + if (parent[target] != target) + { + parent[target] = find(parent[target]); + } + + return parent[target]; +} + +void Sets::mergeSets(int left, int right) +{ + int leftSet = find(left); + int rightSet = find(right); + + if (size[leftSet] > size[rightSet]) + { + parent[rightSet] = leftSet; + } + else + { + parent[leftSet] = rightSet; + } + + if (size[leftSet] == size[rightSet]) + { + ++size[rightSet]; + } +} + +float dist(int j, int k, float xcords[], float ycords[]) +{ + float d = sqrt(pow(xcords[j] - xcords[k], 2) + pow(ycords[j] - ycords[k], 2)); + return d; +} + +int main() +{ + int cases; + cin >> cases; + for (int i{0}; i < cases; ++i) + { + int islands{0}; + cin >> islands; + Graph graph{islands, (islands - 1) * islands * 0.5}; + float xcords[islands]; + float ycords[islands]; + for (int j{0}; j < islands; ++j) + { + cin >> xcords[j] >> ycords[j]; + } + + for (int j{0}; j < islands; ++j) + { + for (int k{j + 1}; k < islands; ++k) + { + graph.addEdge(dist(j, k, xcords, ycords), j, k); + } + } + + cout << graph.prim() << "\n"; + } + return 0; +} +/* for (int i = 0; i < numberOfNodes; ++i) + { + for (int j = 0; j < numberOfNodes; ++j) + { + cerr << adj[i][j] << " "; + } + cerr << "\n"; + }*/ diff --git a/W6/islands2.cc b/W6/islands2.cc new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391