├── 1 - Easy └── Judging Troubles │ ├── README.md │ └── Solution.cpp ├── 2 - Medium └── Cent Savings │ ├── README.md │ └── Solution.java ├── 3 - Advanced ├── Gathering │ ├── README.md │ └── Solution.java └── Indoorienteering │ ├── README.md │ └── Solution.java ├── 4 - Hard ├── Algorithmic Crush │ ├── README.md │ └── Solution.py └── Knapsack Collection │ ├── README.md │ └── Solution.java ├── 5 - Expert ├── Around the Track │ ├── README.md │ └── Solution.cpp └── Biking Duck │ ├── README.md │ └── Solution.cpp ├── Artificial Intelligence └── Bot Building │ ├── Bot saves princess 1 │ └── Solution.cpp │ ├── Bot saves princess 2 │ └── Solution.cpp │ └── BotClean │ └── Solution.py ├── LICENSE └── README.md /1 - Easy/Judging Troubles/README.md: -------------------------------------------------------------------------------- 1 | # Judging Troubles 2 | 3 | The NWERC organisers have decided that they want to improve the automatic grading of the submissions for the contest, so they now use two systems: DOMjudge and Kattis. Each submission is judged by both systems and the grading results are compared to make sure that the systems agree. However, something went wrong in setting up the connection between the systems, and now the jury only knows all results of both systems, but not which result belongs to which submission! You are therefore asked to help them figure out how many results could have been consistent. 4 | 5 | ## Input Format 6 | 7 | The input consists of: 8 | 9 | * one line with one integer _n_ (1 ≤ n ≤ 105), the number of submissions; 10 | * _n_ lines, each with a result of the judging by DOMjudge, in arbitrary order; 11 | * _n_ lines, each with a result of the judging by Kattis, in arbitrary order. 12 | Each result is a string of length between 5 and 15 characters (inclusive) consisting of lowercase letters. 13 | 14 | ## Output Format 15 | 16 | Output one line with the maximum number of judging results that could have been the same for both systems. 17 | 18 | ## Sample Input 19 | 20 | 5 21 | correct 22 | wronganswer 23 | correct 24 | correct 25 | timelimit 26 | wronganswer 27 | correct 28 | timelimit 29 | correct 30 | timelimit 31 | 32 | ## Sample Output 33 | 34 | 4 35 | -------------------------------------------------------------------------------- /1 - Easy/Judging Troubles/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | auto main(int argc, char *argv[]) -> int { 8 | std::map dom; 9 | std::map kat; 10 | std::string w; 11 | int t, r = 0; 12 | 13 | std::cin >> t; 14 | std::cin.ignore(std::numeric_limits::max(), '\n'); 15 | 16 | for (auto i = 0; i < t; ++i) { 17 | std::getline(std::cin, w); 18 | dom[w]++; 19 | } 20 | 21 | for (auto i = 0; i < t; ++i) { 22 | std::getline(std::cin, w); 23 | kat[w]++; 24 | } 25 | 26 | for (auto it = dom.begin(); it != dom.end(); 27 | ++it) { 28 | r += std::min(it->second, kat[it->first]); 29 | } 30 | 31 | std::cout << r; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /2 - Medium/Cent Savings/README.md: -------------------------------------------------------------------------------- 1 | # Cent Savings 2 | 3 | To host a regional contest like NWERC a lot of preparation is necessary: organizing rooms and computers, making a good problem set, inviting contestants, designing T-shirts, booking hotel rooms and so on. I am responsible for going shopping in the supermarket. 4 | 5 | When I get to the cash register, I put all my n items on the conveyor belt and wait until all the other customers in the queue in front of me are served. While waiting, I realize that this supermarket recently started to round the total price of a purchase to the nearest multiple of 10 cents (with 5 cents being rounded upwards). For example, 94 cents are rounded to 90 cents, while 95 are rounded to 100. 6 | 7 | It is possible to divide my purchase into groups and to pay for the parts separately. I managed to find d dividers to divide my purchase in up to d + 1 groups. I wonder where to place the dividers to minimize the total cost of my purchase. As I am running out of time, I do not want to rearrange items on the belt. 8 | 9 | ## Input Format 10 | 11 | The input consists of: 12 | 13 | * one line with two integers _n_ (1 ≤ _n_ ≤ 2000) and _d_ (1 ≤ _d_ ≤ 20), the number of items and the number of available dividers; 14 | * one line with _n_ integers _p_1, . . . , _p_n (0 ≤ _p_i ≤ 104 for 1 ≤ _i_ ≤ _n_), the prices of the items in cents. The prices are given in the same order as the items appear on the belt. 15 | 16 | ## Output Format 17 | 18 | Output the minimum amount of money needed to buy all the items, using up to _d_ dividers. 19 | 20 | ## Sample Input 1 21 | 22 | 5 1 23 | 13 21 55 60 42 24 | 25 | ## Sample Output 1 26 | 27 | 190 28 | 29 | ## Sample Input 2 30 | 31 | 5 2 32 | 1 1 1 1 1 33 | 34 | ## Sample Output 2 35 | 36 | 0 37 | -------------------------------------------------------------------------------- /2 - Medium/Cent Savings/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class CentSavings { 4 | 5 | Scanner in = new Scanner(System.in); 6 | 7 | public static void main(String[] args) { 8 | new CentSavings().go(); 9 | } 10 | private void go() { 11 | int n = in.nextInt(); 12 | int d = in.nextInt(); 13 | int[] p = new int[n + 1]; 14 | 15 | for(int i = 0; i < n; ++i) { 16 | p[i] = in.nextInt(); 17 | } 18 | 19 | int[][] dp = new int[n + 1][d + 1]; 20 | int total = 0; 21 | 22 | for(int i = n - 1; i >= 0; --i) { 23 | total += p[i]; 24 | dp[i][0] = round(total); 25 | } 26 | 27 | for(int i = 1; i <= d; ++i) { 28 | for(int j = 0;j <= n; ++j) { 29 | int sum = 0; 30 | dp[j][i] = Integer.MAX_VALUE; 31 | 32 | for(int k = j; k <= n; ++k) { 33 | dp[j][i] = Math.min(dp[j][i], round(sum) + dp[k][i - 1]); 34 | sum += p[k]; 35 | } 36 | } 37 | } 38 | System.out.println(dp[0][d]); 39 | } 40 | 41 | private int round(int total) { 42 | int mod = total % 10; 43 | 44 | if(mod < 5) { 45 | return total - mod; 46 | } 47 | 48 | return total - mod + 10; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /3 - Advanced/Gathering/README.md: -------------------------------------------------------------------------------- 1 | # Indoorienteering 2 | 3 | The citizens of Fictitia have had enough! The city keeps getting bigger and bigger, and all the more boring. Fictitia consists of horizontal and vertical streets only. The distance between each pair of neighboring parallel streets is always the same; we take this as the unit distance. Surely some variation could not hurt? 4 | 5 | In order to draw more support and make their unhappiness known to the municipality, a group of citizens has agreed to gather at an intersection of the city to protest. The question is: which intersection? Since there is not much difference between them, the idea was raised to select an intersection (_x_, _y_) that minimizes the total distance everyone has to travel. Since everyone lives close to an intersection, the individual distance travelled by someone who lives at (_x_, _y_) is given by |_x_ − _x_| + |_y_ − _y_|. 6 | 7 | However, this could present a problem for the people who live far away, since they might have trouble getting there in time. Therefore it was decided that the intersection should be at most a certain distance d away from everyone. Given that restriction, can you help them identify an intersection that minimizes the total distance everyone has to travel? 8 | 9 | ## Input Format 10 | 11 | The input consists of: 12 | 13 | * one line with one integer _n_ (2 ≤ _n_ ≤ 105), the number of citizens; 14 | * _n_ lines each with 2 integers _x_ and _y_ (0 ≤ _x_, _y_ ≤ 109), the coordinates of each citizen's house; 15 | * one line with one integer _d_ (0 ≤ _d_ ≤ 2 * 109), the maximum distance that each citizen should have to travel. 16 | 17 | It is possible for multiple citizens to live at the same intersection. 18 | 19 | ## Output Format 20 | 21 | Output one line with a single integer: the smallest possible total distance that all citizens need to travel. If there is no intersection that everyone lives within a distance d of, output “impossible” instead. 22 | 23 | ## Sample Input 1 24 | 25 | 5 26 | 3 1 27 | 4 1 28 | 5 9 29 | 2 6 30 | 5 3 31 | 10 32 | 33 | ## Sample Output 1 34 | 35 | 18 36 | 37 | ## Sample Input 2 38 | 39 | 5 40 | 3 1 41 | 4 1 42 | 5 9 43 | 2 6 44 | 5 3 45 | 5 46 | 47 | ## Sample Output 2 48 | 49 | 20 50 | 51 | ## Sample Input 3 52 | 53 | 5 54 | 3 1 55 | 4 1 56 | 5 9 57 | 2 6 58 | 5 3 59 | 4 60 | 61 | ## Sample Output 3 62 | 63 | impossible 64 | -------------------------------------------------------------------------------- /3 - Advanced/Gathering/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.awt.geom.Point2D; 3 | import java.awt.geom.Rectangle2D; 4 | import java.awt.geom.Rectangle2D.Double; 5 | 6 | public class Solution { 7 | 8 | Scanner in = new Scanner(System.in); 9 | 10 | public static void main(String[] args) { 11 | new Solution().go(); 12 | } 13 | 14 | private void go() { 15 | int n = in .nextInt(); 16 | long[][] p = new long[n][2]; 17 | ArrayList < Long > x = new ArrayList < Long > (), y = new ArrayList < Long > (); 18 | 19 | for (int i = 0; i < n; i++) { 20 | p[i][0] = in .nextInt(); 21 | p[i][1] = in .nextInt(); 22 | x.add(p[i][0]); 23 | y.add(p[i][1]); 24 | } 25 | 26 | long d = in .nextInt(); 27 | Collections.sort(x); 28 | Collections.sort(y); 29 | Point2D.Double bot = new Point2D.Double(p[0][0], p[0][1] - d); 30 | Rectangle2D.Double r = new Rectangle2D.Double(bot.y + bot.x, bot.y - bot.x, 2 * d, 2 * d); 31 | 32 | for (int i = 1; i < n; i++) { 33 | bot = new Point2D.Double(p[i][0], p[i][1] - d); 34 | r = (Double) r.createIntersection(new Rectangle2D.Double(bot.y + bot.x, bot.y - bot.x, 2 * d, 2 * d)); 35 | } 36 | 37 | if (r.getHeight() < 0 || r.getWidth() < 0) { 38 | System.out.println("impossible"); 39 | System.exit(0); 40 | } 41 | 42 | if (x.size() % 2 == 0) { 43 | long xl = x.get(n / 2 - 1), xh = x.get(n / 2), yl = y.get(n / 2 - 1), yh = y.get(n / 2); 44 | if (r.contains(yl + xl, yl - xl) && r.contains(yl + xh, yl - xh) && r.contains(yh + xl, yh - xl) && r.contains(yh + xh, yh - xh)) { 45 | System.out.println(tot(xl, yl, p, d)); 46 | System.exit(0); 47 | } 48 | } else { 49 | long xx = x.get(n / 2), yy = y.get(n / 2); 50 | if (r.contains(yy + xx, yy - xx)) { 51 | System.out.println(tot(xx, yy, p, d)); 52 | System.exit(0); 53 | } 54 | } 55 | 56 | long min = Long.MAX_VALUE; 57 | double[][] bs = new double[4][2]; 58 | bs[0][0] = r.getMinX() - r.getMinY(); 59 | bs[0][1] = r.getMinX() + r.getMinY(); 60 | bs[1][0] = r.getMaxX() - r.getMinY(); 61 | bs[1][1] = r.getMaxX() + r.getMinY(); 62 | bs[2][0] = r.getMaxX() - r.getMaxY(); 63 | bs[2][1] = r.getMaxX() + r.getMaxY(); 64 | bs[3][0] = r.getMinX() - r.getMaxY(); 65 | bs[3][1] = r.getMinX() + r.getMaxY(); 66 | 67 | for (int i = 0; i < 4; i++) { 68 | for (int j = 0; j < 2; j++) { 69 | bs[i][j] /= 2; 70 | } 71 | } 72 | 73 | for (int i = 0; i < 4; i++) { 74 | min = Math.min(min, tern(bs[i][0], bs[i][1], bs[(i + 1) % 4][0], bs[(i + 1) % 4][1], p, d)); 75 | } 76 | 77 | System.out.println(min); 78 | } 79 | 80 | private long tern(double d, double e, double f, double g, long[][] p, long dd) { 81 | while (Math.abs(f - d) > .1 || Math.abs(g - e) > .1) { 82 | double h = d + (f - d) / 3, i = e + (g - e) / 3, j = d + 2 * (f - d) / 3, k = e + 2 * (g - e) / 3; 83 | if (tot(h, i, p, dd) < tot(j, k, p, dd)) { 84 | f = j; 85 | g = k; 86 | } else { 87 | d = h; 88 | e = i; 89 | } 90 | } 91 | 92 | return Math.min(tot(Math.floor(d), Math.floor(e), p, dd), Math.min(tot(Math.floor(d), Math.ceil(e), p, dd), Math.min(tot(Math.ceil(d), Math.floor(e), p, dd), tot(Math.ceil(d), Math.ceil(e), p, dd)))); 93 | } 94 | 95 | private long tot(double x, double y, long[][] p, long d) { 96 | long out = 0; 97 | 98 | for (int i = 0; i < p.length; i++) { 99 | long temp = (long)(Math.abs(x - p[i][0]) + Math.abs(y - p[i][1])); 100 | if (temp > d) return Long.MAX_VALUE; 101 | out += temp; 102 | } 103 | 104 | return out; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /3 - Advanced/Indoorienteering/README.md: -------------------------------------------------------------------------------- 1 | # Indoorienteering 2 | 3 | Lukáš really likes orienteering, a sport that requires locating control points in rough terrain. To entertain the NWERC participants Lukáš wants to organize an orienteering race. However, it would be too harsh for the participants to be outdoors in this cold Swedish November weather, so he decided to jump on the new trend of indoor races, and set the race inside the B building of Linköping University. 4 | 5 | Lukáš has already decided on the locations of the control points. He has also decided on the exact length of the race, so the only thing remaining is to decide in which order the control points should be visited such that the length of the total race is as he wishes. Because this is not always possible, he asks you to write a program to help him. 6 | 7 | Note from the organizer: the NWERC indoorienteering race for this year has been cancelled since we neglected to apply for an orienteering permit in time from the university administration. (We still need you to solve the problem so that we can organize it for next year.) 8 | 9 | ## Input Format 10 | 11 | The input consists of: 12 | 13 | * one line with two integers _n_ (2 ≤ _n_ ≤ 14) and _L_ (1 ≤ _L_ ≤ 1015), the number of control points and the desired length of the race, respectively; 14 | * _n_ lines with _n_ integers each. The _j_th integer on the _i_th line, _d_ij , denotes the distance between control point _i_ and _j_ (1 ≤ _d_ij ≤ _L_ for _i_ ≤ _j_, and _d_ii = 0). For all 1 ≤ _i_, _j_, _k_ ≤ _N_ it is the case that _d_ij = _d_ji and _d_ij ≤ _d_ik + _d_kj . 15 | 16 | There may be several knapsacks stacked on top of each other in the same slot, but Gerald can still only pick up one knapsack at a time. 17 | 18 | ## Output Format 19 | 20 | Output one line with “possible” if it is possible to visit all control points once in some order and directly return to the first one such that the total distance is exactly _L_, and “impossible” otherwise. 21 | 22 | ## Sample Input 1 23 | 24 | 4 10 25 | 0 3 2 1 26 | 3 0 1 3 27 | 2 1 0 2 28 | 1 3 2 0 29 | 30 | ## Sample Output 1 31 | 32 | possible 33 | 34 | ## Sample Input 2 35 | 36 | 3 5 37 | 0 1 2 38 | 1 0 3 39 | 2 3 0 40 | 41 | ## Sample Output 2 42 | 43 | impossible 44 | -------------------------------------------------------------------------------- /3 - Advanced/Indoorienteering/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Indoorienteering { 4 | 5 | Scanner in = new Scanner(System.in); 6 | 7 | public static void main(String[] args) { 8 | new Indoorienteering().go(); 9 | } 10 | 11 | private void go() { 12 | int n = in.nextInt(); 13 | long l = in.nextLong(); 14 | long[][] d = new long[n][n]; 15 | 16 | for (int i = 0; i < n; ++i) { 17 | for (int j = 0; j < n; ++j) { 18 | d[i][j] = in.nextLong(); 19 | } 20 | } 21 | 22 | if (n == 2) { 23 | System.out.printf("%s\n", d[0][1] + d[1][0] == l ? "possible" : "impossible"); 24 | System.exit(0); 25 | } 26 | 27 | for (int i = 1; i < n; ++i) { 28 | for (int j = 0; j < 1 << (n - 2); ++j) { 29 | if (Integer.bitCount(j) == (n - 2) / 2 && j < (j ^ ((1 << (n - 2)) - 1))) { 30 | int[] inn = new int[(n - 2) / 2], out = new int[n - 2 - (n - 2) / 2]; 31 | int inni = 0, outi = 0; 32 | 33 | for (int k = 1; k < n; k++) if (k != i) { 34 | if (((j >> (k - (k > i ? 2 : 1))) & 1) == 1) { 35 | inn[inni++] = k; 36 | } 37 | else { 38 | out[outi++] = k; 39 | } 40 | } 41 | 42 | HashSet l1 = solve(inn, d, 0, i); 43 | HashSet l2 = solve(out, d, i, 0); 44 | 45 | for (long ls : l1) { 46 | if (l2.contains(l - ls)) { 47 | System.out.println("possible"); 48 | System.exit(0); 49 | } 50 | } 51 | } 52 | } 53 | } 54 | 55 | System.out.println("impossible"); 56 | } 57 | 58 | private HashSet solve(int[] inn, long[][] d, int f, int l) { 59 | HashSet r = new HashSet(); 60 | 61 | if (inn.length == 0) { 62 | r.add(d[f][l]); 63 | return r; 64 | } 65 | 66 | do { 67 | long ans = 0; 68 | ans += d[f][inn[0]]; 69 | ans += d[inn[inn.length - 1]][l]; 70 | for (int i = 0; i < inn.length - 1; i++) ans += d[inn[i]][inn[i + 1]]; 71 | r.add(ans); 72 | } while (nextPermutation(inn)); 73 | 74 | return r; 75 | } 76 | 77 | public boolean nextPermutation(int[] in) { 78 | for (int i = in.length - 2; i >= 0; --i) { 79 | if ( in[i] < in[i + 1]) { 80 | for (int j = in .length - 1; j > i; --j) { 81 | if ( in[j] > in[i]) { 82 | int t = in[i]; 83 | in[i] = in[j]; 84 | in[j] = t; 85 | break; 86 | } 87 | } 88 | 89 | for (int j = i + 1; j < in.length - (j - i); ++j) { 90 | int t = in[j]; 91 | in[j] = in[in.length - (j - i)]; 92 | in[in.length - (j - i)] = t; 93 | } 94 | 95 | return true; 96 | } 97 | } 98 | 99 | return false; 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /4 - Hard/Algorithmic Crush/README.md: -------------------------------------------------------------------------------- 1 | # AlgorithmicCrush 2 | 3 | You are given a list of size _N_, initialized with zeroes. You have to perform _M_ operations on the list and output the maximum of final values of all the _N_ elements in the list. For every operation, you are given three integers _a_, _b_ and _k_ and you have to add value _k_ to all the elements ranging from index _a_ to _b_ (both inclusive). 4 | 5 | ## Input Format 6 | 7 | First line will contain two integers _N_ and _M_ separated by a single space. 8 | Next _M_ lines will contain three integers _a_, _b_ and _k_ separated by a single space. 9 | Numbers in list are numbered from 1 to _N_. 10 | 11 | ## Constraints 12 | 13 | * 3 <= _N_ <= 107 14 | * 1 <= _M_ <= 2 x 105 15 | * 1 <= _a_ <= _b_ <= _N_ 16 | * 0 <= _k_ <= 109 17 | 18 | ## Output Format 19 | 20 | A single line containing maximum value in the updated list. 21 | 22 | ## Sample Input 23 | 24 | 5 3 25 | 1 2 100 26 | 2 5 100 27 | 3 4 100 28 | 29 | ## Sample Output 30 | 31 | 200 32 | 33 | ## Explanation 34 | 35 | After first update list will be 100 100 0 0 0. 36 | After second update list will be 100 200 100 100 100. 37 | After third update list will be 100 200 200 200 100. 38 | So the required answer will be 200. 39 | -------------------------------------------------------------------------------- /4 - Hard/Algorithmic Crush/Solution.py: -------------------------------------------------------------------------------- 1 | n, inputs = [int(n) for n in input().split(" ")] 2 | 3 | list = [0] * (n + 1) 4 | 5 | for _ in range(inputs): 6 | x, y, incr = [int(n) for n in input().split(" ")] 7 | list[x - 1] += incr 8 | 9 | if (y <= len(list)): 10 | list[y] -= incr; 11 | 12 | max = x = 0 13 | 14 | for i in list: 15 | x = x + i; 16 | 17 | if (max < x): 18 | max = x; 19 | 20 | print(max) 21 | -------------------------------------------------------------------------------- /4 - Hard/Knapsack Collection/README.md: -------------------------------------------------------------------------------- 1 | # Knapsack Collection 2 | 3 | Gerald’s job is to welcome the teams for this year’s NWERC at the airport in Linköping. One of his duties is to stand at the luggage carousel and collect all the knapsacks that the teams are bringing. Gerald is a lazy person, so he just stands at the same position of the carousel and waits for bags to pass by so he can pick them up. 4 | 5 | The baggage carousel consists of s luggage slots, numbered in ascending order from 0 to _s_ − 1. Since the baggage carousel is cyclic, luggage slots _s_ − 1 and 0 also lie side by side. The carousel turns in such a way that if Gerald stands in front of slot _i_ at some point in time, he will stand in front of slot (_i_ + 1) _mod_ _s_ one time unit later. 6 | 7 | In the beginning Gerald prepares a huge baggage cart at some position and stands there to wait for luggage. When a knapsack arrives in front of Gerald, he needs _t_ time units to take it and put it on the baggage cart. After these t time units he is ready to pick up another knapsack. As long as there are remaining knapsacks on the luggage carousel, Gerald always takes the next one to arrive at his position as soon as he is ready after putting away the previous one. 8 | 9 | Now Gerald wonders about the effect of his choice of position on the time it will take him to finish this task. It is up to you to help Gerald calculate the minimum, maximum, and average time to pick up all knapsacks, taken over all _s_ possible slots, which can appear in front of Gerald after preparation. Time starts when he has prepared the baggage cart at some slot of the baggage carousel and ends after he has put the last knapsack on the cart. 10 | 11 | ## Input Format 12 | 13 | The input consists of: 14 | 15 | * one line with three integers _n_ (1 ≤ _n_ ≤ 2000), _s_ (1 ≤ _s_ ≤ 107) and _t_ (1 ≤ _t_ ≤ 107), where _n_ is the number of knapsacks to pick up, _s_ is the number of slots of the carousel, and _t_ is the number of time units Gerald needs to pick up a knapsack from the carousel and put it on the cart; 16 | * one line with _n_ integers _k_1, . . . , _k_n (0 ≤ _k_i ≤ _s_ − 1 for 1 ≤ _i_ ≤ _n_), the slots of the knapsacks. 17 | 18 | There may be several knapsacks stacked on top of each other in the same slot, but Gerald can still only pick up one knapsack at a time. 19 | 20 | ## Output Format 21 | 22 | Output three lines of output containing the minimum, maximum, and average time to pick up all the luggage, over all _s_ positions. The average time should be output as a reduced fraction in the form _p_/_q_. 23 | 24 | ## Sample Input 1 25 | 26 | 7 10 10000000 27 | 0 0 0 0 0 0 1 28 | 29 | ## Sample Output 1 30 | 31 | 70000001 32 | 70000009 33 | 350000027/5 34 | 35 | ## Sample Input 2 36 | 37 | 10 10 3 38 | 0 0 2 2 4 4 6 6 8 8 39 | 40 | ## Sample Output 2 41 | 42 | 39 43 | 40 44 | 79/2 45 | 46 | ## Sample Input 3 47 | 48 | 9 10000000 1 49 | 0 7 2 3 4 5 6 1 8 50 | 51 | ## Sample Output 3 52 | 53 | 9 54 | 10000000 55 | 12500021249991/2500000 56 | -------------------------------------------------------------------------------- /4 - Hard/Knapsack Collection/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.awt.*; 3 | 4 | public class KnapsackCollection { 5 | Scanner in = new Scanner(System.in); 6 | 7 | public static void main(String[] args) { 8 | new KnapsackCollection().go(); 9 | } 10 | 11 | private void go() { 12 | int n = in.nextInt(), s = in.nextInt(), t = in.nextInt(); 13 | int[] k = new int[n]; 14 | long[] ans = new long[n]; 15 | long max = 0; 16 | long min = Long.MAX_VALUE; 17 | 18 | for (int i = 0; i < n; ++i) { 19 | k[i] = in.nextInt(); 20 | } 21 | 22 | for (int i = 0; i < n; ++i) { 23 | long time = 0; 24 | Point pos = new Point(k[i], -1); 25 | TreeSet ts = new TreeSet (new Comparator() { 26 | public int compare(Point arg0, Point arg1) { 27 | if (arg0.x != arg1.x) { 28 | return arg0.x - arg1.x; 29 | } 30 | 31 | return arg0.y - arg1.y; 32 | } 33 | }); 34 | 35 | for (int j = 0; j < n; ++j) { 36 | ts.add(new Point(k[j], j)); 37 | } 38 | 39 | while (!ts.isEmpty()) { 40 | Point next = ts.higher(pos); 41 | 42 | if (next == null) { 43 | time += s - pos.x; 44 | pos.x = 0; 45 | continue; 46 | } 47 | 48 | time += next.x - pos.x; 49 | ts.remove(next); 50 | pos.x = (next.x + t) % s; 51 | time += t; 52 | } 53 | 54 | ans[i] = time; 55 | } 56 | 57 | TreeSet ts = new TreeSet (new Comparator() { 58 | public int compare(Point arg0, Point arg1) { 59 | if (arg0.x != arg1.x) { 60 | return arg0.x - arg1.x; 61 | } 62 | 63 | return arg0.y - arg1.y; 64 | } 65 | }); 66 | 67 | long num = 0; 68 | 69 | for (int j = 0; j < n; ++j) { 70 | ts.add(new Point(k[j], j)); 71 | } 72 | 73 | for (int i = 0; i < s; ++i) { 74 | long time; 75 | Point on = ts.higher(new Point(i, -1)); 76 | 77 | if (on != null) { 78 | time = on.x - i + ans[on.y]; 79 | } 80 | else { 81 | time = s - i + ts.first().x + ans[ts.first().y]; 82 | } 83 | 84 | max = Math.max(max, time); 85 | min = Math.min(min, time); 86 | num += time; 87 | } 88 | 89 | long gcd = greatestCommonDenominator(num, s); 90 | 91 | System.out.printf("%d\n%d\n%d/%d\n", min, max, num / gcd, s / gcd); 92 | } 93 | 94 | long greatestCommonDenominator(long a, long b) { 95 | while (b != 0) { 96 | long t = a % b; 97 | a = b; 98 | b = t; 99 | } 100 | 101 | return a; 102 | } 103 | } -------------------------------------------------------------------------------- /5 - Expert/Around the Track/README.md: -------------------------------------------------------------------------------- 1 | # Around the Track 2 | 3 | In order to compare race tracks, we wish to compute their lengths. A racetrack is strictly two-dimensional (no elevation). It is described by two simple polygons, where one is completely contained inside the other. The track is the region between these two polygons. We define the length of the track as the absolute minimum distance that one needs to travel in order to complete a lap. This could involve traveling on the very edge of the track and arbitrarily sharp cornering (see Figure A.1). 4 | 5 | ![FigA1](https://s3.amazonaws.com/hr-challenge-images/0/1448834023-23201b5337-a0.png "Figure A1") 6 | 7 | Figure A.1: Illustration of sample input number 3 together with the shortest route around the track (dashed). 8 | 9 | ## Input Format 10 | 11 | The input consists of: 12 | 13 | * one line with one integer _n_ (3 ≤ _n_ ≤ 50), the number of vertices of the inner polygon; 14 | * _n_ lines, the _i_th of which contains two integers _x_i and _y_i (−5000 ≤ _x_i, _y_i ≤ 5000): the coordinates of the _i_th vertex of the inner polygon; 15 | * one line with one integer _m_ (3 ≤ _m_ ≤ 50), the number of vertices of the outer polygon; 16 | * _m_ lines, the _i_th of which contains two integers _x_i and _y_i (−5000 ≤ _x_i, _y_i ≤ 5000): the coordinates of the _i_th vertex of the outer polygon; 17 | 18 | For both polygons, the vertices are given in counterclockwise order. The borders of the two polygons do not intersect or touch each other. 19 | 20 | ## Output Format 21 | 22 | Output one line with one floating point number: the length of the race track. Your answer should have an absolute or relative error of at most 10−6. 23 | 24 | ## Sample Input 1 25 | 26 | 3 27 | 1 1 28 | 2 1 29 | 1 2 30 | 3 31 | 0 0 32 | 4 0 33 | 0 4 34 | 35 | ## Sample Output 1 36 | 37 | 3.41421356237309 38 | 39 | ## Sample Input 2 40 | 41 | 5 42 | 1 1 43 | 5 1 44 | 5 5 45 | 3 3 46 | 1 5 47 | 4 48 | 0 0 49 | 6 0 50 | 6 6 51 | 0 6 52 | 53 | ## Sample Output 2 54 | 55 | 16 56 | 57 | ## Sample Input 3 58 | 59 | 5 60 | 1 1 61 | 5 1 62 | 5 5 63 | 3 3 64 | 1 5 65 | 5 66 | 0 0 67 | 6 0 68 | 6 6 69 | 3 4 70 | 0 6 71 | 72 | ## Sample Output 3 73 | 74 | 16.4721359549996 75 | -------------------------------------------------------------------------------- /5 - Expert/Around the Track/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | #define rep(i, a, b) for (__typeof(b) i = a; i < (b); ++i) 13 | 14 | typedef long long ll; 15 | typedef complex point; 16 | typedef complex pointD; 17 | typedef pair pdd; 18 | 19 | auto vs(const point &a, const point &b) -> ll { 20 | return imag(conj(a) * b); 21 | } 22 | 23 | auto vs(const pointD &a, const pointD &b) -> double { 24 | return imag(conj(a) * b); 25 | } 26 | 27 | auto ss(const point &a, const point &b) -> ll { 28 | return real(conj(a) * b); 29 | } 30 | 31 | auto ss(const pointD &a, const pointD &b) -> double { 32 | return real(conj(a) * b); 33 | } 34 | 35 | auto priam(point a, point b, point c) -> bool { 36 | return vs(c - a, b - a) == 0 && ss(a - c, b - c) < 0; 37 | } 38 | 39 | auto pret(point a, point b, point c, point d) -> bool { 40 | if (priam(a, b, c)) { 41 | return true; 42 | } 43 | if (priam(a, b, d)) { 44 | return true; 45 | } 46 | if (priam(c, d, a)) { 47 | return true; 48 | } 49 | if (priam(c, d, b)) { 50 | return true; 51 | } 52 | 53 | return vs(c - a, b - a) * vs(d - a, b - a) < 0 && 54 | vs(a - c, d - c) * vs(b - c, d - c) < 0; 55 | } 56 | 57 | auto angle(point x, point y) -> double { 58 | double r = acos(ss(x, y) / sqrt(ss(x, x)) / sqrt(ss(y, y))); 59 | return vs(x, y) < 0 ? -r : r; 60 | } 61 | 62 | auto angle(pointD x, pointD y) -> double { 63 | double dif = arg(x) - arg(y); 64 | if (dif < -M_PI) { 65 | dif += 2 * M_PI; 66 | } 67 | if (dif > M_PI) { 68 | dif -= 2 * M_PI; 69 | } 70 | return dif; 71 | } 72 | 73 | auto inside(point x, vector &a, bool strict = true) -> bool { 74 | double uh = 0; 75 | rep(k, 0, a.size()) { 76 | if (priam(a[k], a[(k + 1) % a.size()], x)) { 77 | return !strict; 78 | } 79 | uh += angle(a[k] - x, a[(k + 1) % a.size()] - x); 80 | } 81 | 82 | return fabs(uh) < 6 ? false : true; 83 | } 84 | 85 | auto main(int argc, char *argv[]) -> int { 86 | int n; 87 | scanf("%d", &n); 88 | vector a(n); 89 | vector ad(n); 90 | rep(i, 0, n) { 91 | int x, y; 92 | scanf("%d %d", &x, &y); 93 | ad[i] = pointD(x, y); 94 | x *= 2; 95 | y *= 2; 96 | a[i] = point(x, y); 97 | } 98 | 99 | int start = 0; 100 | rep(i, 1, n) { 101 | if (real(a[i]) < real(a[start]) || 102 | (real(a[i]) == real(a[start]) && imag(a[i]) < imag(a[start]))) { 103 | start = i; 104 | } 105 | } 106 | 107 | int p = (start + 1) % n, q = (start + n - 1) % n; 108 | pointD vp = ad[p] - ad[start], vq = ad[q] - ad[start]; 109 | vp /= abs(vp); 110 | vq /= abs(vq); 111 | pointD origin = ad[start] + 5e-5 * (vp + vq); 112 | 113 | int m; 114 | scanf("%d", &m); 115 | rep(i, 0, m) { 116 | int x, y; 117 | scanf("%d %d", &x, &y); 118 | ad.push_back(pointD(x, y)); 119 | x *= 2; 120 | y *= 2; 121 | a.push_back(point(x, y)); 122 | } 123 | 124 | double d[n + m][n + m]; 125 | double rot[n + m][n + m]; 126 | rep(i, 0, n + m) { 127 | rep(j, 0, i) { 128 | d[i][j] = d[j][i] = abs(ad[i] - ad[j]); 129 | rot[i][j] = angle(ad[j] - origin, ad[i] - origin); 130 | rot[j][i] = -rot[i][j]; 131 | } 132 | } 133 | rep(i, 0, n + m) { d[i][i] = 1e20; } 134 | 135 | vector inner(a.begin(), a.begin() + n), outer(a.begin() + n, a.end()); 136 | rep(i, 0, n + m) { 137 | rep(j, 0, i) { 138 | bool ok = true; 139 | 140 | rep(k, 0, n + m) { 141 | int l = k + 1; 142 | l = (k < n) ? l % n : (l - n) % m + n; 143 | 144 | set all = {i, j, k, l}; 145 | if (all.size() == 4 && pret(a[i], a[j], a[k], a[l])) { 146 | ok = false; 147 | } 148 | } 149 | 150 | point mid = a[i] + a[j]; 151 | mid = point(real(mid) / 2, imag(mid) / 2); 152 | if (ok && inside(mid, inner)) { 153 | ok = false; 154 | } 155 | if (ok && !inside(mid, outer, false)) { 156 | ok = false; 157 | } 158 | 159 | if (!ok) { 160 | d[i][j] = d[j][i] = 1e30; 161 | } 162 | } 163 | } 164 | 165 | vector > dist(n + m); 166 | dist[start].push_back(pdd(0, 0)); 167 | vector seen(n + m); 168 | 169 | double res = 1e20; 170 | rep(iter, 0, 2 * (n + m)) { 171 | int mi = -1, mj = -1; 172 | rep(i, 0, n + m) { 173 | if (seen[i] < (int)dist[i].size()) { 174 | if (mi == -1 || dist[i][seen[i]] < dist[mi][mj]) { 175 | mi = i; 176 | mj = seen[i]; 177 | } 178 | } 179 | } 180 | 181 | if (dist[mi][mj].first > res) break; 182 | 183 | rep(j, 0, n + m) { 184 | pdd w = dist[mi][seen[mi]]; 185 | w.first += d[mi][j]; 186 | w.second += rot[mi][j]; 187 | 188 | bool update = true; 189 | for (pdd &o : dist[j]) 190 | if (abs(o.second - w.second) < 1e-6) { 191 | if (o.first > w.first) o = w; 192 | update = false; 193 | } 194 | 195 | if (update && (dist[j].size() < 2 || dist[j].back().first > w.first)) { 196 | dist[j].push_back(w); 197 | } 198 | 199 | sort(dist[j].begin(), dist[j].end()); 200 | 201 | if (dist[j].size() > 2) { 202 | dist[j].resize(2); 203 | } 204 | } 205 | 206 | if (++seen[mi] == 2) { 207 | res = min(res, dist[mi][0].first + dist[mi][1].first); 208 | assert(abs(abs(dist[mi][0].second - dist[mi][1].second) - M_PI * 2) < 1e-7); 209 | } 210 | } 211 | 212 | cout.precision(15); 213 | cout << res << endl; 214 | 215 | return 0; 216 | } 217 | -------------------------------------------------------------------------------- /5 - Expert/Biking Duck/README.md: -------------------------------------------------------------------------------- 1 | # Biking Duck 2 | 3 | Gladstone Gander is walking through Duckburg and needs to get to his date with Daisy Duck as soon as possible. If he doesn’t get there in time, Donald might show up and take his place instead. 4 | 5 | Duckburg has recently started providing a very eco-friendly way of public transport: bikes. At many bike stations throughout the city, one can pick up a free bike, ride it to another bike station, and drop it there. This gives Gladstone two ways of transportion: on foot or by bike. Biking is faster, of course, but he must pick up and leave the bikes at the designated stations. Gladstone can walk or bike between any two points in a straight line. 6 | 7 | Gladstone possesses a map of the (rectangular) center of Duckburg. His current position is on this map and so is the meeting point with Daisy. The map also contains the locations of all bike stations within the boundaries of the map. 8 | 9 | There can be way more bike stations though, that are not within the boundaries of the map. Considering his luck, you can assume that the moment Gladstone walks (or bikes) off the map, he encounters a bike station if that suits him well. The bike stations not on the map can be located anywhere outside the map, they do not have to lie on integer coordinates. 10 | 11 | That leaves Gladstone with the task of figuring out which route to take. Can you help him out? Given the map and his infinite amount of luck, what is the fastest time to his date with Daisy? 12 | 13 | ## Input Format 14 | 15 | The input consists of: 16 | 17 | * one line with two integers _v_walk and _v_bike (1 ≤ _v_walk ≤ _v_bike ≤ 1000), the speeds of walking and of biking; 18 | * one line with four integers _x_1, _y_1, _x_2 and _y_2 (-106 ≤ _x_1 < _x_2 ≤ 106; -106 ≤ _y_1 < _y_2 ≤ 106), the bounding coordinates of the map of the center of Duckburg; 19 | * one line with one integer _m_ (3 ≤ _m_ ≤ 50), the number of vertices of the outer polygon; 20 | * one line with two integers _x_G and _y_G, Gladstone’s position; 21 | * one line with two integers _x_D and _y_D, Daisy’s position; 22 | * one line with one integer _n_ (0 ≤ _n_ ≤ 1000), the number of known bike stations; 23 | * _n_ lines with two integers _x_station, _y_station each, the coordinates of the known bike stations. 24 | 25 | All coordinates are on the map of the center, i.e., _x_1 ≤ _x_ ≤ _x_2 and _y_1 ≤ _y_ ≤ _y_2. 26 | 27 | ## Output Format 28 | 29 | Output one line with the shortest possible time for Gladstone to get to Daisy. Your answer should have an absolute or relative error of at most 10−6. 30 | 31 | ## Sample Input 1 32 | 33 | 1 8 34 | 0 0 10 10 35 | 5 1 36 | 5 9 37 | 3 38 | 5 8 39 | 2 2 40 | 9 6 41 | 42 | ## Sample Output 1 43 | 44 | 3.000000000 45 | 46 | ## Sample Input 2 47 | 48 | 5 100 49 | 0 -100000 100000 0 50 | 5 -30000 51 | 40000 -5 52 | 0 53 | 54 | ## Sample Output 2 55 | 56 | 501.9987496 57 | -------------------------------------------------------------------------------- /5 - Expert/Biking Duck/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct point { 9 | double x, y; 10 | point() : x(0.0), y(0.0) {} 11 | point(double x_, double y_) : x(x_), y(y_) {} 12 | }; 13 | 14 | auto operator+(point a, point b) -> point { 15 | return point(a.x + b.x, a.y + b.y); 16 | } 17 | 18 | auto operator*(double a, point b) -> point { 19 | return point(a * b.x, a * b.y); 20 | } 21 | 22 | double v_walk, v_metro; 23 | double x_min, y_min, x_max, y_max; 24 | point start, endp; 25 | int num_metro; 26 | vector metro; 27 | double solution; 28 | 29 | auto read() -> void { 30 | cin >> v_walk >> v_metro; 31 | cin >> x_min >> y_min >> x_max >> y_max; 32 | cin >> start.x >> start.y; 33 | cin >> endp.x >> endp.y; 34 | cin >> num_metro; 35 | 36 | metro = vector(num_metro); 37 | for (int i = 0; i < num_metro; i++) { 38 | cin >> metro[i].x >> metro[i].y; 39 | } 40 | } 41 | 42 | auto dist(point a, point b) -> double { 43 | return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); 44 | } 45 | 46 | auto boundary_point(int index) -> point { 47 | if (index % 4 == 0) { 48 | return point(x_min, y_min); 49 | } 50 | if (index % 4 == 1) { 51 | return point(x_max, y_min); 52 | } 53 | if (index % 4 == 2) { 54 | return point(x_max, y_max); 55 | } 56 | 57 | return point(x_min, y_max); 58 | } 59 | 60 | auto time_to_station(point foot, point metro) -> double { 61 | double result = dist(foot, metro) / v_walk; 62 | for (int b = 0; b < 4; b++) { 63 | point lo = boundary_point(b); 64 | point hi = boundary_point(b + 1); 65 | for (int times = 0; times < 100; times++) { 66 | point p1 = (2.0 / 3.0) * lo + (1.0 / 3.0) * hi; 67 | point p2 = (1.0 / 3.0) * lo + (2.0 / 3.0) * hi; 68 | double t1 = dist(foot, p1) / v_walk + dist(p1, metro) / v_metro; 69 | double t2 = dist(foot, p2) / v_walk + dist(p2, metro) / v_metro; 70 | if (t1 < t2) { 71 | hi = p2; 72 | } else { 73 | lo = p1; 74 | } 75 | result = min(result, (t1 + t2) / 2.0); 76 | } 77 | } 78 | return result; 79 | } 80 | 81 | auto time_without_stations(point foot1, point foot2) -> double { 82 | double result = dist(foot1, foot2) / v_walk; 83 | for (int b = 0; b < 4; b++) { 84 | point lo = boundary_point(b); 85 | point hi = boundary_point(b + 1); 86 | for (int times = 0; times < 100; times++) { 87 | point p1 = (2.0 / 3.0) * lo + (1.0 / 3.0) * hi; 88 | point p2 = (1.0 / 3.0) * lo + (2.0 / 3.0) * hi; 89 | double t1 = dist(foot1, p1) / v_walk + time_to_station(foot2, p1); 90 | double t2 = dist(foot1, p2) / v_walk + time_to_station(foot2, p2); 91 | if (t1 < t2) { 92 | hi = p2; 93 | } else { 94 | lo = p1; 95 | } 96 | result = min(result, (t1 + t2) / 2.0); 97 | } 98 | } 99 | return result; 100 | } 101 | 102 | auto solve() -> void { 103 | solution = time_without_stations(start, endp); 104 | 105 | vector time_from(num_metro), time_to(num_metro); 106 | for (int i = 0; i < num_metro; i++) { 107 | time_from[i] = time_to_station(start, metro[i]); 108 | time_to[i] = time_to_station(endp, metro[i]); 109 | } 110 | 111 | for (int i = 0; i < num_metro; i++) { 112 | for (int j = 0; j < num_metro; j++) { 113 | solution = min(solution, time_from[i] + dist(metro[i], metro[j]) / v_metro + time_to[j]); 114 | } 115 | } 116 | } 117 | 118 | auto main(int argc, char *argv[]) -> int { 119 | read(); 120 | solve(); 121 | cout << setprecision(10) << solution << endl; 122 | return 0; 123 | } 124 | -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/Bot saves princess 1/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define UP -1 8 | #define DOWN 1 9 | #define LEFT -1 10 | #define RIGHT 1 11 | 12 | #define pb(x) push_back(x) 13 | 14 | typedef struct { 15 | char val; 16 | int posX; 17 | int posY; 18 | } cell; 19 | 20 | auto displayPathtoPrincess(int n, std::vector grid) -> void { 21 | int vMove, vMoveTimes, hMove, hMoveTimes; 22 | cell m; 23 | cell p; 24 | 25 | for (cell x : grid) { 26 | if (x.val == 'm') 27 | m = x; 28 | else if (x.val == 'p') 29 | p = x; 30 | } 31 | 32 | vMoveTimes = std::abs(m.posX - p.posX); 33 | if (m.posX < p.posX) { 34 | vMove = DOWN; 35 | } else if (m.posX > p.posX) { 36 | vMove = UP; 37 | } 38 | 39 | hMoveTimes = std::abs(m.posY - p.posY); 40 | if (m.posY < p.posY) { 41 | hMove = RIGHT; 42 | } else if (m.posY > p.posY) { 43 | hMove = LEFT; 44 | } 45 | 46 | for (int i = 0; i < vMoveTimes; ++i) { 47 | if (vMove == UP) 48 | std::cout << "UP\n"; 49 | else if (vMove == DOWN) 50 | std::cout << "DOWN\n"; 51 | } 52 | 53 | for (int i = 0; i < hMoveTimes; ++i) { 54 | if (hMove == LEFT) 55 | std::cout << "LEFT\n"; 56 | else if (hMove == RIGHT) 57 | std::cout << "RIGHT\n"; 58 | } 59 | } 60 | 61 | auto main(void) -> int { 62 | std::vector grid = {}; 63 | 64 | int m; std::cin >> m; 65 | 66 | for (int i = 0; i < m * m; ++i) { 67 | cell x; 68 | char c; std::cin >> c; 69 | 70 | if (c == 'm' || c == 'p' || c == '-') { 71 | x.val = c; 72 | x.posX = i / m; 73 | x.posY = i % m; 74 | 75 | grid.pb(x); 76 | } else 77 | continue; 78 | } 79 | 80 | displayPathtoPrincess(m, grid); 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/Bot saves princess 2/Solution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define pb(x) push_back(x) 8 | 9 | typedef enum { 10 | UP = 0, 11 | DOWN 12 | } VMove; 13 | 14 | typedef enum { 15 | LEFT = 0, 16 | RIGHT 17 | } HMove; 18 | 19 | typedef struct { 20 | char val; 21 | int posX; 22 | int posY; 23 | } cell; 24 | 25 | auto printVDir(int dir) -> void { 26 | if (dir == UP) 27 | std::cout << "UP\n"; 28 | else if (dir == DOWN) 29 | std::cout << "DOWN\n"; 30 | } 31 | 32 | auto printHDir(int dir) -> void { 33 | if (dir == LEFT) 34 | std::cout << "LEFT\n"; 35 | else if (dir == RIGHT) 36 | std::cout << "RIGHT\n"; 37 | } 38 | 39 | auto nextMove(int n, std::vector grid) -> void { 40 | int vMoveTimes,hMoveTimes; 41 | VMove vMove; 42 | HMove hMove; 43 | cell m; 44 | cell p; 45 | 46 | for (cell x : grid) { 47 | if (x.val == 'm') 48 | m = x; 49 | else if (x.val == 'p') 50 | p = x; 51 | } 52 | 53 | vMoveTimes = std::abs(m.posX - p.posX); 54 | if (m.posX < p.posX) { 55 | vMove = DOWN; 56 | } else if (m.posX > p.posX) { 57 | vMove = UP; 58 | } 59 | 60 | hMoveTimes = std::abs(m.posY - p.posY); 61 | if (m.posY < p.posY) { 62 | hMove = RIGHT; 63 | } else if (m.posY > p.posY) { 64 | hMove = LEFT; 65 | } 66 | 67 | if (hMoveTimes == 0) 68 | printVDir(vMove); 69 | else if (vMoveTimes == 0) 70 | printHDir(hMove); 71 | else if (vMoveTimes == hMoveTimes) 72 | printVDir(vMove); 73 | else if (vMoveTimes > hMoveTimes) 74 | printHDir(hMove); 75 | else if (vMoveTimes < hMoveTimes) 76 | printVDir(vMove); 77 | } 78 | 79 | auto main(void) -> int { 80 | std::vector grid = {}; 81 | 82 | int m; std::cin >> m; 83 | int r; std::cin >> r; 84 | int c; std::cin >> c; 85 | 86 | for (int i = 0; i < m * m; ++i) { 87 | cell x; 88 | char c; std::cin >> c; 89 | 90 | if (c == 'm' || c == 'p' || c == '-') { 91 | x.val = c; 92 | x.posX = i / m; 93 | x.posY = i % m; 94 | 95 | grid.pb(x); 96 | } else 97 | continue; 98 | } 99 | 100 | nextMove(m, grid); 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/BotClean/Solution.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | def manhattan_dist(x1, y1, x2, y2): 4 | return (abs(x1 - x2) + abs(y1 - y2), x2, y2) 5 | 6 | def next_move(x, y, board, dirty): 7 | dist = [manhattan_dist(x, y, d[0], d[1]) for d in dirty] 8 | dist.sort() 9 | dx = dist[0][1] 10 | dy = dist[0][2] 11 | if x == dx and y == dy: 12 | print "CLEAN" 13 | elif x < dx: 14 | print "DOWN" 15 | elif x > dx: 16 | print "UP" 17 | elif y < dy: 18 | print "RIGHT" 19 | elif y > dy: 20 | print "LEFT" 21 | 22 | if __name__ == "__main__": 23 | pos = [int(i) for i in raw_input().strip().split()] 24 | board = [[j for j in raw_input().strip()] for i in range(5)] 25 | dirty = [(i, j) for i in range(5) for j in range(5) if board[i][j] == 'd'] 26 | next_move(pos[0], pos[1], board, dirty) 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Raul Butuc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SolvedProblemsArchive 2 | Repository contains recently solved problems sorted by difficulty 3 | --------------------------------------------------------------------------------