Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iomanip>
- #include <iostream>
- #include <cmath>
- #define MAZE_HEIGHT 14
- #define PLAYER 1
- using namespace std;
- bool check(long long);
- int solve(long long);
- void print(long long);
- int main(){
- //A maze can be thought of as a 14-digit number in base 5
- //Each digit represents a layer in the pipe maze
- //0 means no pipes in that layer
- //1, 2, or 3 means 1 pipe in that layer either left, middle, or right respectively
- //4 means 2 pipes in that layer, on the left and right
- long long maze = 0;
- long long max = pow(5, MAZE_HEIGHT);
- long long sum = 0;
- long long solution[] = {0, 0, 0, 0};
- while(maze < max){
- if(check(maze)){
- sum++;
- solution[solve(maze) - 1]++;
- //print(maze);
- }
- maze++;
- }
- cout << "Found: " << sum << endl;
- cout << "1: " << solution[0] << endl;
- cout << "2: " << solution[1] << endl;
- cout << "3: " << solution[2] << endl;
- cout << "4: " << solution[3] << endl;
- //system("PAUSE");
- return 0;
- }
- bool check(long long maze){
- int pipes = 0;
- //Check for a valid number of pipes
- while(maze > 0){
- int layer = maze%5;
- if(layer == 4){ //Left and Right pipes
- pipes += 2;
- }
- else if(layer > 0){ //Left, Right, or Middle pipes
- pipes++;
- }
- //Remove the layer
- maze = maze/5;
- }
- if(pipes == 12){
- return true;
- }
- else{
- return false;
- }
- }
- int solve(long long maze){
- //Starting location
- int location = PLAYER;
- while(maze > 0){
- int layer = maze%5;
- switch(location){
- case 1: if(layer == 1 | layer == 4){
- location = 2;
- }
- break;
- case 2: if(layer == 1 | layer == 4){
- location = 1;
- }
- else if(layer == 2){
- location = 3;
- }
- break;
- case 3: if(layer == 2){
- location = 2;
- }
- else if(layer == 3 | layer == 4){
- location = 4;
- }
- break;
- case 4: if(layer == 3 | layer == 4){
- location = 3;
- }
- break;
- }
- maze = maze/5;
- }
- return location;
- }
- void print(long long maze){
- while(maze > 0){
- int layer = maze%5;
- switch(layer){
- case 0: cout << "| | | |" << endl;
- break;
- case 1: cout << "|-| | |" << endl;
- break;
- case 2: cout << "| |-| |" << endl;
- break;
- case 3: cout << "| | |-|" << endl;
- break;
- case 4: cout << "|-| |-|" << endl;
- break;
- }
- maze = maze/5;
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement