why bruteforce losts its fuctionlaity at some point in this code?

I am trying to solve a puzzle again! this is the link: http://www.puzzleup.com/2015/puzzle/?9

my method requires vectors. Because I use this method: http://i.stack.imgur.com/Z2oaV.png

I valued all the corners, and I track each iteration thrugh the end(15 th corner). I start from 1 and record all the corners I ve been through in the possibilities 2d array.


My problem is this: The code actually iterates fine till a point. 1,2 2,4 4,3 3,1 1,5 5,6 6,8 8,7 7,5 7,3 7,5 7,3 7,5 7,3 ....

it seems it losts his iteration capability in 10th iteration. I am on this for almost one week and still have not came up with anything. So I found this forum and asking for your help. If you see what I do not see, please share with me and I will be grateful. Thanks.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <vector>
#include <cmath>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

/*
possibilities will keep the possible combinations, routes like {1,4}{4,8}{8,7} etc till it arrives to 15
*/

vector< vector<int> > possibilities;

vector< vector<int> >::iterator row;

vector<int>::iterator col;


int cubes[15][6] = { {2,3,5,0,0,0}, {1,4,6,0,0,0}, {1,4,7,0,0,0}, {2,3,8,0,0,0}, 
{1,6,7,0,0,0}, {2,5,8,0,0,0}, {3,5,8,0,0,0}, {4,6,7,9,10,13}, {8,11,12,0,0,0}, 
{8,11,14,0,0,0}, {9,10,15,0,0,0}, {9,13,15,0,0,0}, {8,12,14,0,0,0}, 
{10,13,15,0,0,0}, {11,12,14,0,0,0} };

int counterSet, i, j, temp, temp2, counter, sizeOfVec;

int routeCheck(int a, int b)
{
	//
		//
		if(!possibilities.empty())
		{
			//
			for(i=0; i< (int)possibilities.size(); i++)
			{
				//
				if(((possibilities[i][0] == a) && (possibilities[i][1] == b)) ||
 ((possibilities[i][0] == b) && (possibilities[i][1] == a)))
				{
					//
					return 0;
				}
			}
			return 1;
		}
		else
		{
			//
			return 1;
		}

}

void routeKeeper(int a, int b)
{
	//
	if(routeCheck(a,b) == 1)
	{
		//
		possibilities.push_back({a,b});
	}
}

void createRouteMap(int start, int end)
{
	//
	temp = j;
	
	for(j=0; j<6; j++)
	{

		if(cubes[start-1][j]==end)   // if it is destination
		{
			//
			counter+=1;
			cout << "counter is: " << counter << endl;
			j++;
		}
		else if( (routeCheck(start, cubes[start-1][j])==1) && (cubes[start-1][j] != end) &&
 (cubes[start-1][j] != 0)  )
		{
			//
			routeKeeper(start, cubes[start-1][j]);
			for(row = possibilities.begin() ; row != possibilities.end() ; row++)
			{
				//
				for(col = row->begin(); col != row->end(); col++)
				{
					//
					cout << *col << ",";
				}
			}
			cout << endl;
			createRouteMap(cubes[start-1][j], end);
			possibilities.erase(possibilities.end());
		}
	}
	j=temp;
	
}


int main(int argc, char** argv) {

	counter = 0;
	createRouteMap(1, 15);
	cout<< counter << endl;

		
	system("pause");
	return 0;
}
Last edited on
closed account (E3h7X9L8)
Sorry couldnt answer to your problem but you gained my curiosity with this problem , I was asigned at my school to make some backtracking algorithms and this problem gained my curiosity, I went immediately and coded it.
The problem is solved based on your images with edge positions.
May not be very optimized did my best in a short time.
If you have any questions don't hesitate to ask.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
using std::endl;
using std::cout;
using std::cin;

//2D Array(graph) representing edges of first cube
int cubes[8][3] =
{
	2, 4, 8, //Edge 1 is connected to edge 2, 4, 8
	1, 3, 5, //Edge 2... 
	2, 4, 6, //etc
	1, 3, 7,
	2, 6, 8,
	3, 5, 7,
	4, 6, 8,
	1, 5, 7,
};

int secondCube[9] = { 0, 6, 13, 10, 9, 11, 15, 14, 12 };

//Route array for current traversal calculation
int route[10];
int routes[10][10];
//Routes 2D array to store all routes found

//Keeping count of routes
int routeCounter = 0;

//Magical recursive function
void findSolution(int, int, int);
//Display route using nested for loops and secondCube array
void displayRoute();
//Save current calculated loop into 2D array
void saveRoute();
//Check for edge duplicates
bool noDuplicate(int);

int main()
{
	route[0] = 1;

	findSolution(0, 8, 1);
	displayRoute();

	cin.ignore(255, '\n');
	cin.get();
	return 0;
}


void findSolution(int c, int end, int counter)
{
	if (counter == end && route[counter - 1] == 6)
		//if all cube traversed and last edge is 6
	{
		saveRoute();
		routeCounter++;
	}
	else
	for (int nextPlace = 0; nextPlace < 3; nextPlace++)
		//iterating through all 3 
	{
		if (noDuplicate(cubes[c][nextPlace]) )
			//if edge wasnt traversed
		{
			route[counter] = cubes[c][nextPlace];
			//put next edge in traversal array
			findSolution(cubes[c][nextPlace] - 1, end, counter + 1);
			//proceed with next edge
			route[counter] = 0; 
			//backtracking in case of dead end
		}
	}

}

bool noDuplicate(int x)
{
	for (int i = 0; i < 10; i++)
	{
		if (x == route[i])
			return false;
	}
	return true;
}

void saveRoute()
{
	for (int i = 0; i < 8; i++)
		routes[routeCounter][i] = route[i];
}

void displayRoute() 
{
	//nested for's to display both cubes traversal
	//using the second 1D array with second cube
	//edge positions
	for (int i = 0; i < routeCounter; i++)
	{
		for (int j = 0; j < routeCounter; j++)
		{
			for (int k = 0; k < 8; k++)
				cout << " " << routes[i][k];
			for (int l = 1; l < 8; l++)
				cout << " " << secondCube[routes[j][l]];
			cout << endl;
		}
		cout << endl;
		cout << endl;
	}
}
Last edited on
Topic archived. No new replies allowed.