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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include "counthsr.h"
#include <iostream>
#include <vector>
/*
is the recursion function. Checks if the vector is in range and moves the x and y position
*/
int SpiderCount::countHSR_recurse(std::vector<int> ary,
int dim_x, int dim_y,
int finish_x, int finish_y,
int pos_x, int pos_y,
int _squaresLeft)
{
/*
BASE CASE RETURNS IF SQUARES LEFT = 0 AND ON THE FINISH LINE INCREMENT TOTAL
*/
if ((_squaresLeft == 0) && (pos_x == finish_x) && (pos_y == finish_y))
{
std::cout << "Increminting the total";
total++;
}
/*
MOVE RIGHT
*/
if (isCell(pos_x + 1, pos_y, dim_x, dim_y))
{
if (ary[(pos_x + 1)*(dim_y) + pos_y] == 0) // means we havent been there
{
--_squaresLeft; // decrement the squares left
(pos_x) += 1; // move our position
ary[(pos_x)*(dim_y) + (pos_y)] = 1; // set that position to visited
countHSR_recurse(ary, dim_x, dim_y, finish_x, finish_y, pos_x, pos_y, _squaresLeft); // recursion
ary[(pos_x)*(dim_y) + (pos_y)] = 0; // backtracking set our position to 0
++_squaresLeft; // increment our squares left
pos_x -= 1; // move back
}
}
// MOVE UP
if (isCell(pos_x, pos_y + 1, dim_x, dim_y))
{
if (ary[(pos_x)*(dim_y) + (pos_y + 1)] == 0)
{
--_squaresLeft;
pos_y += 1;
ary[(pos_x)*(dim_y) + (pos_y)] = 1;
countHSR_recurse(ary, dim_x, dim_y, finish_x, finish_y, pos_x, pos_y, _squaresLeft);
ary[(pos_x)*(dim_y) + (pos_y)] = 0;
++_squaresLeft;
pos_y -= 1;
}
}
// MOVE LEFT
if (isCell(pos_x - 1, pos_y, dim_x, dim_y))
{
if (ary[(pos_x)*(dim_y) + pos_y ] == 0)
{
--_squaresLeft;
pos_x += -1;
ary[(pos_x)*(dim_y) + (pos_y)] = 1;
countHSR_recurse(ary, dim_x, dim_y, finish_x, finish_y, pos_x, pos_y, _squaresLeft);
ary[(pos_x)*(dim_y) + (pos_y)] = 0;
++_squaresLeft;
pos_x += 1;
}
}
// MOVE DOWN
if (isCell(pos_x, pos_y - 1, dim_x, dim_y))
{
if (ary[(pos_x)*(dim_y) + (pos_y - 1)] == 0)
{
--_squaresLeft;
pos_y += -1;
ary[(pos_x)*(dim_y) + (pos_y)] = 1;
countHSR_recurse(ary, dim_x, dim_y, finish_x, finish_y, pos_x, pos_y, _squaresLeft);
ary[(pos_x)*(dim_y) + (pos_y)] = 0;
++_squaresLeft;
pos_y += 1;
}
}
//MOVE LEFT/DOWN
if (isCell(pos_x - 1, pos_y - 1, dim_x, dim_y))
{
if (ary[(pos_x-1)*(dim_y)+(pos_y - 1)] == 0)
{
--_squaresLeft;
pos_y += -1;
pos_x += -1;
ary[(pos_x)*(dim_y)+(pos_y)] = 1;
countHSR_recurse(ary, dim_x, dim_y, finish_x, finish_y, pos_x, pos_y, _squaresLeft);
ary[(pos_x)*(dim_y)+(pos_y)] = 0;
++_squaresLeft;
pos_y += 1;
pos_x += 1;
}
}
// MOVE RIGHT/DOWN
if (isCell(pos_x + 1, pos_y - 1, dim_x, dim_y))
{
if (ary[(pos_x+1)*(dim_y)+(pos_y - 1)] == 0)
{
--_squaresLeft;
pos_y += -1;
pos_x += 1;
ary[(pos_x)*(dim_y)+(pos_y)] = 1;
countHSR_recurse(ary, dim_x, dim_y, finish_x, finish_y, pos_x, pos_y, _squaresLeft);
ary[(pos_x)*(dim_y)+(pos_y)] = 0;
++_squaresLeft;
pos_y += -1;
pos_x += 1;
}
}
//MOVE RIGHT/UP
if (isCell(pos_x + 1, pos_y + 1, dim_x, dim_y))
{
if (ary[(pos_x + 1)*(dim_y)+(pos_y + 1)] == 0)
{
--_squaresLeft;
pos_y += 1;
pos_x += 1;
ary[(pos_x)*(dim_y)+(pos_y)] = 1;
countHSR_recurse(ary, dim_x, dim_y, finish_x, finish_y, pos_x, pos_y, _squaresLeft);
ary[(pos_x)*(dim_y)+(pos_y)] = 0;
++_squaresLeft;
pos_y += -1;
pos_x += -1;
}
}
//MOVE LEFT/UP
if (isCell(pos_x - 1, pos_y + 1, dim_x, dim_y))
{
if (ary[(pos_x - 1)*(dim_y)+(pos_y + 1)] == 0)
{
--_squaresLeft;
pos_y += +1;
pos_x += -1;
ary[(pos_x)*(dim_y)+(pos_y)] = 1;
countHSR_recurse(ary, dim_x, dim_y, finish_x, finish_y, pos_x, pos_y, _squaresLeft);
ary[(pos_x)*(dim_y)+(pos_y)] = 0;
++_squaresLeft;
pos_y += -1;
pos_x += 1;
}
}
return total;
}
/*
Creates the board and calls our recursion function to to print out the count. This is our wrapper function
*/
int SpiderCount::countHSR(int dim_x, int dim_y, int hole_x, int hole_y, int start_x, int start_y, int finish_x, int finish_y)
{
int pos_x = start_x; //create are start x
int pos_y = start_y; // create are start y
int _squaresleft = (dim_y*dim_x)-2; // because we start off with two squares taken away
size = dim_x*dim_y; // size of the array
std::vector<int> ary(dim_x*dim_y); // make the board
ary[hole_x*dim_y + hole_y] = 1; // set our obstacle
ary[start_x*dim_y + start_y] = 1; // set our start path as visited.
return countHSR_recurse(ary, dim_x, dim_y, finish_x, finish_y, pos_x, pos_y, _squaresleft);
}
// checks if the vector is in range returns false if its not in range.
bool SpiderCount::isCell(int pos_x, int pos_y, int dim_x, int dim_y)
{
if ((pos_x >= dim_x) || (pos_y >= dim_y)||(pos_x*dim_y + pos_y < 0))
return false;
return true;
}
int main()
{
// run a test
SpiderCount test;
int count = test.countHSR(3, 2, 2, 1, 1, 1, 2, 0); // should return 2 but returns 5.
std::cout << count;
}
|