Skip to content
Browse files

Adds solutions to easy, medium (ugly implementaion) of SRM 519 (div2)

So I implemented Djikstra for the first time. Yay baby!
  • Loading branch information...
1 parent 5fffbbf commit 767ada9a9413c15a6df56505d48bcfc646ab2952 @shuaib committed
Showing with 405 additions and 0 deletions.
  1. +259 −0 SRM/519/ThreeTeleports.cpp
  2. +146 −0 SRM/519/WhichDay.cpp
View
259 SRM/519/ThreeTeleports.cpp
@@ -0,0 +1,259 @@
+#include <vector>
+#include <list>
+#include <map>
+#include <set>
+#include <queue>
+#include <deque>
+#include <stack>
+#include <bitset>
+#include <algorithm>
+#include <functional>
+#include <numeric>
+#include <utility>
+#include <sstream>
+#include <iostream>
+#include <iomanip>
+#include <cstdio>
+#include <cmath>
+#include <cstdlib>
+#include <ctime>
+
+using namespace std;
+
+
+
+class ThreeTeleports {
+public:
+
+ map<pair<int, int>, pair<int, int> > m;
+ long long distance(pair<int, int> x, pair<int, int> y)
+ {
+ if(m.find(x)!=m.end() && m[x]==y)
+ return 10;
+ else
+ return abs(x.first-y.first)+abs(x.second-y.second);
+
+ }
+ int shortestDistance(int xMe, int yMe, int xHome, int yHome, vector <string> tp) {
+ vector<pair<pair<int, int>, int> > v;
+
+ pair<pair<int, int>, pair<int, int> > t1, t2, t3;
+ int s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16;
+ stringstream ss1, ss2, ss3;
+ ss1<<tp[0];
+ ss2<<tp[1];
+ ss3<<tp[2];
+ ss1>>s1>>s2>>s3>>s4;
+ ss2>>s5>>s6>>s7>>s8;
+ ss3>>s9>>s10>>s11>>s12;
+ t1.first = make_pair(s1, s2);
+ t1.second = make_pair(s3, s4);
+ t2.first = make_pair(s5, s6);
+ t2.second = make_pair(s7, s8);
+ t3.first = make_pair(s9, s10);
+ t3.second = make_pair(s11, s12);
+ /*
+ tp[0]>>t1.first.first>>t1.first.second>>t1.second.first>>t1.second.second;
+ tp[1]>>t2.first.first>>t2.first.second>>t2.second.first>>t2.second.second;
+ tp[2]>>t3.first.first>>t3.first.second>>t3.second.first>>t3.second.second;
+ */
+ pair<int, int> source = make_pair(xMe, yMe);
+ pair<int, int> target = make_pair(xHome, yHome);
+
+
+
+ m[t1.first] = t1.second;
+ m[t1.second] = t1.first;
+ m[t2.first] = t2.second;
+ m[t2.second] = t2.first;
+ m[t3.first] = t3.second;
+ m[t3.second] = t3.first;
+
+ vector<pair<int, int> > g;
+ g.push_back(source);
+ g.push_back(target);
+ g.push_back(t1.first);
+ g.push_back(t1.second);
+ g.push_back(t2.first);
+ g.push_back(t2.second);
+ g.push_back(t3.first);
+ g.push_back(t3.second);
+
+ vector<pair<int, int> > visited;
+ visited.push_back(source);
+ pair<int, int> current = source;
+
+ map<pair<int, int>, long long> w;
+ w[target] = w[t1.first] = w[t1.second] = w[t2.first] = w[t2.second] = w[t3.first] = w[t3.second] = INT_MAX;
+ w[source] = 0;
+
+ while(!g.empty())
+ {
+ pair<int, int> smallest = g[0];
+ for(int i=0;i<g.size();i++)
+ {
+ if(w[g[i]]<w[smallest])
+ smallest = g[i];
+ }
+ g.erase(find(g.begin(), g.end(), smallest));
+ for(int i=0;i<g.size();i++)
+ {
+ long long alt = w[smallest] + distance(smallest, g[i]);
+ if (alt<w[g[i]])
+ {
+ w[g[i]] = alt;
+
+ }
+ }
+
+ }
+
+
+
+ return w[target];
+ }
+};
+
+
+// BEGIN KAWIGIEDIT TESTING
+// Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
+bool KawigiEdit_RunTest(int testNum, int p0, int p1, int p2, int p3, vector <string> p4, bool hasAnswer, int p5) {
+ cout << "Test " << testNum << ": [" << p0 << "," << p1 << "," << p2 << "," << p3 << "," << "{";
+ for (int i = 0; int(p4.size()) > i; ++i) {
+ if (i > 0) {
+ cout << ",";
+ }
+ cout << "\"" << p4[i] << "\"";
+ }
+ cout << "}";
+ cout << "]" << endl;
+ ThreeTeleports *obj;
+ int answer;
+ obj = new ThreeTeleports();
+ clock_t startTime = clock();
+ answer = obj->shortestDistance(p0, p1, p2, p3, p4);
+ clock_t endTime = clock();
+ delete obj;
+ bool res;
+ res = true;
+ cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
+ if (hasAnswer) {
+ cout << "Desired answer:" << endl;
+ cout << "\t" << p5 << endl;
+ }
+ cout << "Your answer:" << endl;
+ cout << "\t" << answer << endl;
+ if (hasAnswer) {
+ res = answer == p5;
+ }
+ if (!res) {
+ cout << "DOESN'T MATCH!!!!" << endl;
+ } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
+ cout << "FAIL the timeout" << endl;
+ res = false;
+ } else if (hasAnswer) {
+ cout << "Match :-)" << endl;
+ } else {
+ cout << "OK, but is it right?" << endl;
+ }
+ cout << "" << endl;
+ return res;
+}
+int main() {
+ bool all_right;
+ all_right = true;
+
+ int p0;
+ int p1;
+ int p2;
+ int p3;
+ vector <string> p4;
+ int p5;
+
+ {
+ // ----- test 0 -----
+ p0 = 3;
+ p1 = 3;
+ p2 = 4;
+ p3 = 5;
+ string t4[] = {"1000 1001 1000 1002","1000 1003 1000 1004","1000 1005 1000 1006"};
+ p4.assign(t4, t4 + sizeof(t4) / sizeof(t4[0]));
+ p5 = 3;
+ all_right = KawigiEdit_RunTest(0, p0, p1, p2, p3, p4, true, p5) && all_right;
+ // ------------------
+ }
+
+ {
+ // ----- test 1 -----
+ p0 = 0;
+ p1 = 0;
+ p2 = 20;
+ p3 = 20;
+ string t4[] = {"1 1 18 20","1000 1003 1000 1004","1000 1005 1000 1006"};
+ p4.assign(t4, t4 + sizeof(t4) / sizeof(t4[0]));
+ p5 = 14;
+ all_right = KawigiEdit_RunTest(1, p0, p1, p2, p3, p4, true, p5) && all_right;
+ // ------------------
+ }
+
+ {
+ // ----- test 2 -----
+ p0 = 0;
+ p1 = 0;
+ p2 = 20;
+ p3 = 20;
+ string t4[] = {"1000 1003 1000 1004","18 20 1 1","1000 1005 1000 1006"};
+ p4.assign(t4, t4 + sizeof(t4) / sizeof(t4[0]));
+ p5 = 14;
+ all_right = KawigiEdit_RunTest(2, p0, p1, p2, p3, p4, true, p5) && all_right;
+ // ------------------
+ }
+
+ {
+ // ----- test 3 -----
+ p0 = 10;
+ p1 = 10;
+ p2 = 10000;
+ p3 = 20000;
+ string t4[] = {"1000 1003 1000 1004","3 3 10004 20002","1000 1005 1000 1006"};
+ p4.assign(t4, t4 + sizeof(t4) / sizeof(t4[0]));
+ p5 = 30;
+ all_right = KawigiEdit_RunTest(3, p0, p1, p2, p3, p4, true, p5) && all_right;
+ // ------------------
+ }
+
+ {
+ // ----- test 4 -----
+ p0 = 3;
+ p1 = 7;
+ p2 = 10000;
+ p3 = 30000;
+ string t4[] = {"3 10 5200 4900","12212 8699 9999 30011","12200 8701 5203 4845"};
+ p4.assign(t4, t4 + sizeof(t4) / sizeof(t4[0]));
+ p5 = 117;
+ all_right = KawigiEdit_RunTest(4, p0, p1, p2, p3, p4, true, p5) && all_right;
+ // ------------------
+ }
+
+ {
+ // ----- test 5 -----
+ p0 = 0;
+ p1 = 0;
+ p2 = 1000000000;
+ p3 = 1000000000;
+ string t4[] = {"0 1 0 999999999","1 1000000000 999999999 0","1000000000 1 1000000000 999999999"};
+ p4.assign(t4, t4 + sizeof(t4) / sizeof(t4[0]));
+ p5 = 36;
+ all_right = KawigiEdit_RunTest(5, p0, p1, p2, p3, p4, true, p5) && all_right;
+ // ------------------
+ }
+
+ if (all_right) {
+ cout << "You're a stud (at least on the example cases)!" << endl;
+ } else {
+ cout << "Some of the test cases had errors." << endl;
+ }
+ return 0;
+}
+// END KAWIGIEDIT TESTING
+//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
View
146 SRM/519/WhichDay.cpp
@@ -0,0 +1,146 @@
+#include <vector>
+#include <list>
+#include <map>
+#include <set>
+#include <queue>
+#include <deque>
+#include <stack>
+#include <bitset>
+#include <algorithm>
+#include <functional>
+#include <numeric>
+#include <utility>
+#include <sstream>
+#include <iostream>
+#include <iomanip>
+#include <cstdio>
+#include <cmath>
+#include <cstdlib>
+#include <ctime>
+
+using namespace std;
+
+
+class WhichDay {
+public:
+ string getDay(vector <string> nD) {
+
+ map<string, int> m;
+
+ string days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
+ for (int i=0;i<7;i++)
+ {
+ m[days[i]] = 0;
+ }
+
+ for(int i=0;i<nD.size();i++)
+ {
+ m[nD[i]]++;
+ }
+
+ for(int i=0;i<7;i++)
+ {
+ if(m[days[i]]==0)
+ return days[i];
+ }
+
+ }
+};
+
+
+// BEGIN KAWIGIEDIT TESTING
+// Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
+bool KawigiEdit_RunTest(int testNum, vector <string> p0, bool hasAnswer, string p1) {
+ cout << "Test " << testNum << ": [" << "{";
+ for (int i = 0; int(p0.size()) > i; ++i) {
+ if (i > 0) {
+ cout << ",";
+ }
+ cout << "\"" << p0[i] << "\"";
+ }
+ cout << "}";
+ cout << "]" << endl;
+ WhichDay *obj;
+ string answer;
+ obj = new WhichDay();
+ clock_t startTime = clock();
+ answer = obj->getDay(p0);
+ clock_t endTime = clock();
+ delete obj;
+ bool res;
+ res = true;
+ cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
+ if (hasAnswer) {
+ cout << "Desired answer:" << endl;
+ cout << "\t" << "\"" << p1 << "\"" << endl;
+ }
+ cout << "Your answer:" << endl;
+ cout << "\t" << "\"" << answer << "\"" << endl;
+ if (hasAnswer) {
+ res = answer == p1;
+ }
+ if (!res) {
+ cout << "DOESN'T MATCH!!!!" << endl;
+ } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
+ cout << "FAIL the timeout" << endl;
+ res = false;
+ } else if (hasAnswer) {
+ cout << "Match :-)" << endl;
+ } else {
+ cout << "OK, but is it right?" << endl;
+ }
+ cout << "" << endl;
+ return res;
+}
+int main() {
+ bool all_right;
+ all_right = true;
+
+ vector <string> p0;
+ string p1;
+
+ {
+ // ----- test 0 -----
+ string t0[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
+ p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
+ p1 = "Saturday";
+ all_right = KawigiEdit_RunTest(0, p0, true, p1) && all_right;
+ // ------------------
+ }
+
+ {
+ // ----- test 1 -----
+ string t0[] = {"Sunday","Monday","Tuesday","Wednesday","Friday","Thursday"};
+ p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
+ p1 = "Saturday";
+ all_right = KawigiEdit_RunTest(1, p0, true, p1) && all_right;
+ // ------------------
+ }
+
+ {
+ // ----- test 2 -----
+ string t0[] = {"Sunday","Monday","Tuesday","Thursday","Friday","Saturday"};
+ p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
+ p1 = "Wednesday";
+ all_right = KawigiEdit_RunTest(2, p0, true, p1) && all_right;
+ // ------------------
+ }
+
+ {
+ // ----- test 3 -----
+ string t0[] = {"Sunday","Friday","Tuesday","Wednesday","Monday","Saturday"};
+ p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
+ p1 = "Thursday";
+ all_right = KawigiEdit_RunTest(3, p0, true, p1) && all_right;
+ // ------------------
+ }
+
+ if (all_right) {
+ cout << "You're a stud (at least on the example cases)!" << endl;
+ } else {
+ cout << "Some of the test cases had errors." << endl;
+ }
+ return 0;
+}
+// END KAWIGIEDIT TESTING
+//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!

0 comments on commit 767ada9

Please sign in to comment.
Something went wrong with that request. Please try again.