Permalink
Browse files

solution to crypt1 and more path finding

  • Loading branch information...
1 parent b69958f commit aac0beeba56013cd114555f4ee8ceb4ca5f76739 @sirupsen committed Apr 28, 2012
View
Binary file not shown.
View
@@ -32,6 +32,9 @@ void dfs(int x, int y, int moves)
{
if (x < 0 || x >= n || y < 0 || y >= n) {
return; // out of board
+ } else if (board[y][x] == -2){
+ cout << "(" << x << ";" << y << ")\n";
+ exit(0);
} else if (board[y][x] == -1 || moves < board[y][x]) {
cin.ignore();
print_board();
@@ -86,6 +89,58 @@ Field bfs(int x, int y)
}
}
+int visited[100][100];
+
+Field dls(Field field, int depth)
+{
+ if (visited[field.y][field.x] != 1337) {
+ visited[field.y][field.x] == 1337;
+
+ if (depth >= 0 && board[field.y][field.x] == -2) {
+ return field;
+ } else if (depth > 0) {
+ cin.ignore();
+ cout << "(" << field.x << ";" << field.y << ")\n";
+ print_board();
+
+ board[field.y][field.x] = depth;
+
+ field.x += 1;
+ dls(field, depth - 1);
+
+ field.x -= 2;
+ dls(field, depth - 1);
+
+ field.x += 1;
+ field.y += 1;
+ dls(field, depth - 1);
+
+ field.y -= 2;
+ dls(field, depth - 1);
+
+ field.y += 1;
+ } else {
+ return field;
+ }
+ }
+}
+
+
+Field iddfs(Field root)
+{
+ int depth = 0;
+ Field boom;
+
+ while(true) {
+ boom = dls(root, depth);
+ if (board[boom.y][boom.x] == -2) return boom;
+
+ depth++;
+ }
+
+ return root;
+}
+
void reset_board()
{
for(int y = 0; y < n; y++)
@@ -98,21 +153,23 @@ int main()
cout << "Board dimensions NxN? ";
cin >> n;
+ Field starting_field;
+
int x;
cout << "Start x-coordinate: ";
- cin >> x;
+ cin >> starting_field.x;
int y;
cout << "Start y-coordinate: ";
- cin >> y;
+ cin >> starting_field.y;
reset_board();
+ srand(time(NULL));
board[rand() % (n - 1)][rand() % (n - 1)] = -2;
Field target;
-
- dfs(x,y,0);
+ target = iddfs(starting_field);
printf("Target found at (%d;%d) and you can get there in %d moves\n", target.x, target.y, target.moves);
View
Binary file not shown.
@@ -0,0 +1,73 @@
+/*
+ID: sirup1
+PROG: crypt1
+LANG: C++
+*/
+#include <cmath>
+#include <iostream>
+#include <fstream>
+using namespace std;
+
+int allowed[15];
+int allowed_lookup[15];
+int allowed_length = 5;
+
+bool integers_in_allowed(int val)
+{
+ while(val) {
+ if (!allowed_lookup[val % 10])
+ return false;
+
+ val /= 10;
+ }
+
+ return true;
+}
+
+int main()
+{
+ int possibilities = 0;
+
+ ifstream input("crypt1.in");
+ input >> allowed_length;
+
+ for(int i = 0; i < allowed_length; i++) {
+ input >> allowed[i];
+ allowed_lookup[allowed[i]] = 1;
+ }
+
+ for(int i = 0; i < allowed_length; i++) {
+ for(int j = 0; j < allowed_length; j++) {
+ int second = (allowed[i] * 10) + allowed[j];
+
+ for(int p = 0; p < allowed_length; p++) {
+ for(int k = 0; k < allowed_length; k++) {
+ for(int t = 0; t < allowed_length; t++) {
+ int first = (allowed[p] * 100) + (allowed[k] * 10) + allowed[t];
+
+ int first_partial = allowed[j] * first;
+ int second_partial = allowed[i] * first; // don't multiply with 10, just gives dumb 0 suffix edge-case in find
+
+ int result = first * second;
+
+ // cout << first << "\n";
+ // cout << second << "\n";
+ // cout << first_partial << "\n";
+ // cout << second_partial << "\n";
+ // cout << result << "\n\n";
+
+ // it'll never be under 100 or 1000 anyway
+ if(first_partial <= 999 && second_partial <= 999 && result <= 9999)
+ if (integers_in_allowed(first_partial) && integers_in_allowed(second_partial) && integers_in_allowed(result))
+ possibilities++;
+ }
+ }
+ }
+ }
+ }
+
+ ofstream output("crypt1.out");
+ output << possibilities << "\n";
+
+ return 0;
+}
@@ -0,0 +1,2 @@
+5
+1 2 3 4 5 6 8 9
@@ -0,0 +1 @@
+49

0 comments on commit aac0bee

Please sign in to comment.