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 191 192 193 194 195 196 197 198 199 200 201 202 203
|
#include <iostream>
#include <utility>
#include <ctime>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <cassert>
#define NDEBUG
//#undef NDEBUG
///array limit
constexpr std::size_t limit=25;
template<std::size_t N,std::size_t M>
void print_peak_finder(int(&)[N][M]);
/********************for MAP objects*******************************/
struct MyMap
{
int Map[limit][limit]
{
{4,4,4,2,5,2,2,2,7,6,6,9,9,2,2,2,9,7,7,7,6,4,2,2,5},
{6,4,2,2,5,4,2,5,7,5,6,6,5,4,4,4,9,5,6,6,7,9,2,2,6},
{7,9,2,2,6,6,0,5,4,5,3,3,0,0,7,6,2,5,0,3,6,7,3,2,3},
{0,9,1,4,1,7,0,2,0,4,0,3,3,8,7,6,2,7,9,9,4,3,1,3,1},
{1,8,1,6,3,9,0,0,0,8,0,0,0,9,0,0,5,4,5,0,3,3,3,3,2},
{0,6,0,7,5,9,0,3,1,9,7,1,0,9,9,0,6,0,5,0,4,3,1,3,1},
{7,7,1,9,3,7,1,3,1,9,0,1,4,4,9,3,6,1,0,0,4,3,3,7,0},
{9,9,0,9,9,3,3,8,5,8,6,5,0,7,8,4,9,6,4,0,7,0,1,7,4},
{7,0,1,7,4,0,4,8,0,8,1,6,0,1,0,6,9,1,2,0,7,9,0,7,3},
{7,0,0,9,3,7,7,9,1,2,3,8,9,1,2,5,7,0,2,9,4,4,0,5,0},
{3,7,0,9,0,8,0,9,0,3,1,7,9,0,3,6,0,1,2,6,5,8,4,6,0},
{6,7,3,4,3,8,2,7,1,6,0,7,8,2,3,7,1,0,3,6,1,8,1,6,9},
{5,8,4,6,0,3,3,0,7,9,0,0,0,7,0,8,0,1,0,5,9,9,0,9,9},
{7,9,6,7,1,3,5,2,6,9,6,1,3,9,8,8,0,3,0,5,7,7,1,9,3},
{7,9,0,7,0,0,7,0,0,1,8,0,0,9,4,0,5,6,0,0,7,0,0,9,3},
{7,2,4,8,1,1,8,3,6,0,8,8,6,9,4,0,2,7,3,5,0,7,1,4,4},
{0,3,3,9,0,3,9,0,7,1,4,0,6,2,1,1,1,8,3,7,3,7,0,9,0},
{3,3,3,9,2,2,9,3,7,9,7,2,7,0,9,6,7,8,3,7,7,0,0,9,3},
{4,4,0,5,0,5,0,2,6,9,3,2,8,7,9,6,3,4,3,7,4,4,4,2,3},
{4,3,1,3,1,5,0,3,3,2,2,2,8,7,4,2,5,5,3,3,7,2,4,8,3},
{7,7,0,7,0,0,7,0,0,1,8,0,0,9,4,0,5,6,0,0,7,0,0,90,3},
{7,2,4,8,1,1,8,3,6,0,8,8,6,9,4,0,2,7,3,5,0,9,1,4,1},
{0,3,3,9,0,3,9,0,7,1,4,0,6,2,1,1,1,8,3,7,3,7,0,9,0},
{3,3,3,9,2,2,9,3,7,9,7,2,7,0,9,6,7,8,3,7,7,0,0,9,3},
{4,4,0,5,0,5,0,2,6,9,3,2,8,7,9,6,3,4,3,7,4,4,4,2,5},
};
};
class peak_finder
{
template<std::size_t N,std::size_t M>
friend void print_peak_finder(int(&)[N][M]);
private:
MyMap peakMap;
std::map<std::pair<std::size_t,std::size_t>,std::size_t> coord_data; /*******for DEBUG*******/
void print_coord_track() const; /*******for DEBUG*******/
std::size_t recursive_call_stacks=0; /*******for DEBUG*******/
std::size_t Xindex=rand()%limit,Yindex=rand()%limit;
std::size_t locate_next_XCORD() const;
std::size_t locate_next_YCORD() const;
std::pair<std::size_t,std::size_t> next_indices();
bool peak_found() const;
public:
void locate_peak();
};
int main()
{
srand(time(nullptr));
peak_finder peak;
peak.locate_peak();
}
/**scan horizontally across the current row and locate the*
**largest value , return the x- coord of the value */
std::size_t peak_finder::locate_next_XCORD() const
{
std::size_t x=0,value=peakMap.Map[0][Yindex];
for(std::size_t loc=0;loc<limit;loc++)
{
if(value<peakMap.Map[loc][Yindex])
{value=peakMap.Map[loc][Yindex]; x=loc;}
}
if(x==Xindex){x=rand()%(limit);}
return x;
}
/**the value returned from above is immediately set as the current*
**X coord i.e the member Xindex, this object have new coordinates*
**on the current position scan vertically(y-direction) and locate*
**the largest value and return its y coordinate */
std::size_t peak_finder::locate_next_YCORD() const
{
std::size_t y=0,value=peakMap.Map[Xindex][0];
for(std::size_t loc=0;loc<limit;loc++)
{
if(value<peakMap.Map[Xindex][loc])
{value=peakMap.Map[Xindex][loc]; y=loc;}
}
if(y==Yindex){y=rand()%(limit);}
return y;
}
/**wrapper for the above functions returns a pair of new {x,y}*
**coordinates */
std::pair<std::size_t,std::size_t> peak_finder::next_indices()
{
auto x=locate_next_XCORD(); Xindex=x;
auto y=locate_next_YCORD();
return {x,y};
}
/**checks if this object current coordinates {Xindex,Yindex} are a true peak*/
bool peak_finder::peak_found() const
{
std::size_t value=peakMap.Map[Xindex][Yindex];
///check if all four neighbour{+X,+Y,-X,-Y: directions} values are less than current co-ordinate values on the map
if((Xindex>0&&Yindex>0)&&(Xindex<(limit-1)&&Yindex<(limit-1)))
{
if(value>peakMap.Map[Xindex][Yindex-1]&&value>peakMap.Map[Xindex][Yindex+1]&&value>peakMap.Map[Xindex-1][Yindex]&&value>peakMap.Map[Xindex+1][Yindex])
return true;
else
return false;
}
///check for three neighbours on the left edge
else if(Xindex==0&&Yindex>0&&Yindex<(limit-1))
{
if(value>peakMap.Map[Xindex+1][Yindex]&&value>peakMap.Map[Xindex][Yindex-1]&&value>peakMap.Map[Xindex][Yindex+1])
return true;
else
return false;
}
///check for three neighbours on the right edge
else if(Xindex==(limit-1)&&Yindex>0&&Yindex<(limit-1))
{
if(value>peakMap.Map[Xindex-1][Yindex]&&value>peakMap.Map[Xindex][Yindex-1]&&value>peakMap.Map[Xindex][Yindex+1])
return true;
else
return false;
}
///check for three neighbours on the top edge
else if(Yindex==0&&Xindex>0&&Xindex<(limit-1))
{
if(value>peakMap.Map[Xindex-1][Yindex]&&value>peakMap.Map[Xindex+1][Yindex]&&value>peakMap.Map[Xindex][Yindex+1])
return true;
else return false;
}
///check for three neighbours on the bottom edge
else if(Yindex==(limit-1)&&Xindex>0&&Xindex<(limit-1))
{
if(value>peakMap.Map[Xindex-1][Yindex]&&value>peakMap.Map[Xindex+1][Yindex]&&value>peakMap.Map[Xindex][Yindex-1])
return true;
else return false;
}
///check for two neighbours at the top left corner
else if(Xindex==0&&Yindex==0)
{
if(value>peakMap.Map[Xindex+1][Yindex]&&value>peakMap.Map[Xindex][Yindex+1])
return true;
else
return false;
}
///check for two neighbours at the bottom left corner
else if(Xindex==0&&Yindex==(limit-1))
{
if(value>peakMap.Map[Xindex+1][Yindex]&&value>peakMap.Map[Xindex][Yindex-1])
return true;
else
return false;
}
///check for two neighbours at the top right corner
else if(Xindex==(limit-1)&&Yindex==0)
{
if(value>peakMap.Map[Xindex-1][Yindex]&&value>peakMap.Map[Xindex][Yindex+1])
return true;
else
return false;
}
///check for two neighbours at the bottom right corner
else if(Xindex==(limit-1)&&Yindex==(limit-1))
{
if(value>peakMap.Map[Xindex][Yindex-1]&&value>peakMap.Map[Xindex-1][Yindex])
return true;
else return false;
}
}
|