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
|
#include <iostream>
#include <vector>
using namespace std;
// Declare the dimensions of the array with in an 'enum'
enum {ROW = 7, COLUMN = 8};
// Declare a global variable 'totalGold'
// Declare a vector to record the path of numbers taken
int totalGold = 0;
vector<int> path;
// struct used to store position in the array
struct position
{
int x;
int y;
position(){}
position(int a, int b):x(a), y(b) {}
void setPosition(position);
void setPosition(int, int);
};
void position::setPosition(position sample){
x = sample.x;
y = sample.y;
}
void position::setPosition(int a, int b){
x = a;
y = b;
}
// ========================================= //
// ** Declaration of functions used in program ** //
// Check if position co-ordinates are out of bound
int indexOutOfBound(position);
// Compare the immideately above and right values and return the
// position of the bigger value
void compare(position&, int matrix[][COLUMN]);
// When position is at the very right column
void addAllAlongColumn(position, int matrix[][COLUMN]);
// When position is at the very top row
void addAllAlongRow(position, int matrix[][COLUMN]);
// Display vector
void show(vector<int>);
// ========================================= //
int main(){
position checker(6, 0);
int maze[ROW][COLUMN] = {
{5, 1, 0, 4, 1, 1, 2, 0},
{0, 3, 2, 1, 0, 3, 0, 1},
{4, 3, 0, 6, 5, 0, 1, 0},
{3, 1, 0, 3, 4, 0, 1, 3},
{0, 5, 2, 0, 1, 1, 5, 1},
{2, 1, 6, 1, 6, 0, 2, 1},
{0, 0, 4, 3, 2, 3, 0, 2}
};
while(true){
if(indexOutOfBound(checker) == checker.x + checker.y)
break;
else if(indexOutOfBound(checker) == checker.x){
addAllAlongColumn(checker, maze);
break;
}
else if (indexOutOfBound(checker) == checker.y){
addAllAlongRow(checker, maze);
break;
}
else
compare(checker, maze);
}
show(path);
cout << endl << "Total gold coins = " << totalGold << endl;
return 0;
}
// Function Declarations
int indexOutOfBound(position sample){
// Beware that sample.x only goes upwards hence decreases
if((sample.x < 0) && (sample.y >= COLUMN))
return sample.x + sample.y;
else if (sample.x < 0)
return sample.y;
else if(sample.y >= COLUMN)
return sample.x;
else
return -2;
}
// =============================================================
void compare(position &sample, int matrix[][COLUMN]){
// Number above is greater
if(matrix[sample.x - 1][sample.y] > matrix[sample.x][sample.y + 1]){
totalGold += matrix[sample.x - 1][sample.y];
sample.setPosition(sample.x - 1, sample.y);
path.push_back(matrix[sample.x][sample.y]);
}
// Number to the right is greater
else if (matrix[sample.x][sample.y + 1] > matrix[sample.x - 1][sample.y]){
totalGold += matrix[sample.x][sample.y + 1];
sample.setPosition(sample.x, sample.y + 1);
path.push_back(matrix[sample.x][sample.y]);
}
// Both numbers are equal
// Restructure the next 'else if' condidition in a way that gives the 'offset' problem a solution
// The 'offset' problem is listed on the very end of this page
else if (matrix[sample.x - 1][sample.y] == matrix[sample.x][sample.y + 1]){
if(matrix[sample.x - 2][sample.y] > matrix[sample.x][sample.y + 2]){
totalGold += matrix[sample.x - 1][sample.y];
sample.setPosition(sample.x - 1, sample.y);
path.push_back(matrix[sample.x][sample.y]);
}
else if (matrix[sample.x][sample.y + 2] > matrix[sample.x - 2][sample.y]){
totalGold += matrix[sample.x][sample.y + 1];
sample.setPosition(sample.x, sample.y + 1);
path.push_back(matrix[sample.x][sample.y]);
}
}
}
// =============================================================
void addAllAlongColumn(position sample, int matrix[][COLUMN]){
for(int i = sample.x; i > -1; --i){
totalGold += matrix[i][sample.y - 1];
path.push_back(matrix[i][sample.y - 1]);
}
}
// =============================================================
void addAllAlongRow(position sample, int matrix[][COLUMN]){
for(int i = sample.y; i < COLUMN; ++i){
totalGold += matrix[sample.x - 1][i];
path.push_back(matrix[sample.x - 1][i]);
}
}
// =============================================================
void show(vector<int> sample){
for(unsigned int i = 0; i < sample.size(); ++i)
cout << "[" << sample[i] << "]" << " ";
}
/* ===================================================
OFFSET PROBLEM:
Assume the conditions where current position is at
3 rows down and 1 column from the right (the cell
that contains 3):
int maze[3][4] = {
{1, 2, 4, 5},
{4, 5, 2, 0},
{87, 5, 3, 2}
};
After comparing 2 and 2 the program finds out that
it needs to do further comparisons to figure out
which the best path is. But it can't do a comparison
between 4 and a non-existent number. What's the solution here?
================================================== */
| |