Sounds like Hill Climbing to me!
This was coded in Turbo C, but it can easily be altered. Shows visual hill climbing in an array, with step constraints of one value, maximum.
Code:
/* an example of basic Hill Climbing, constrained by a step maximum
Adak, June 16, 2010
status: OK
*/
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define ROWS 8
struct point {
int row;
int col;
int value;
}p1;
void info(void);
int main() {
int i, j, step, max, last_value, new_value;
int x, y, x1, y1;
int hill[ROWS][ROWS] = { //correct local maxima is at hill[0][7]
{ 0, 1, 0, 0, 0, 0, 0, 17 },
{ 3, 1, 2, 0, 0, 0, 16, 0 },
{ 3, 3, 0, 8, 20, 15, 0, 0 },
{ 0, 4, 5, 9, 0, 8, 14, 0 },
{ 0, 0, 6, 7, 9, 13, 8, 0 },
{ 0, 0, 8, 0, 12, 8, 8, 0 },
{ 0, 9, 8, 11, 0, 0, 0, 0 },
{ 0, 0, 10, 0, 0, 0, 0, 0 }
};
struct point p2;
step=1;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
info();
p2 = p1;
gotoxy(1,5);
textcolor(14); //affects only cprintf() "direct video" output
printf("\n 0 1 2 3 4 5 6 7");
printf("\n =============================\n");
for(i=0;i<ROWS;i++) {
printf("%d: ", i);
for(j=0;j<ROWS;j++) {
if(i==0 && j==0) {
printf("%2d",hill[i][j]);
cprintf("* ");
}
else
printf("%2d ",hill[i][j]);
}
putchar('\n');
}
printf("\n From row 0, column 0, climb to:");
do {
p1.value = hill[p1.row][p1.col];
last_value = p1.value;
max = p1.value + step;
for(i=0; i<8; i++) {
if(i==0 && p1.row > 0) { //12 o'clock
p2.row = p1.row -1;
}
else if(i==1 && p1.row > 0) { //1:30 o'clock
p2.row = p1.row -1;
p2.col = p1.col +1;
}
else if(i==2 && p1.col < ROWS -1) //3 o'clock
p2.col = p1.col +1;
else if(i==3 && p1.row < ROWS -1 && p1.col < ROWS -1) { //4:30 o'clock
p2.row = p1.row +1;
p2.col = p1.col +1;
}
else if(i==4 && p1.row < ROWS -1) //6 o'clock
p2.row = p1.row +1;
else if(i==5 && p1.row < ROWS -1 && p1.col > 0) { //7:30 o'clock
p2.row = p1.row +1;
p2.col = p1.col -1;
}
else if(i==6 && p1.col > 0) //9 o'clock
p2.col = p1.col -1;
else if(i==7 && p1.row > 0 && p1.col > 0) { //10:30 o'clock
p2.row = p1.row -1;
p2.col = p1.col -1;
}
p2.value = hill[p2.row][p2.col];
if(p2.value > p1.value && p2.value <= max) {
p1 = p2;
break;
}
else
p2 = p1;
}
new_value = p1.value;
if(new_value > last_value) {
x1 = wherex();
y1 = wherey();
x=6+(p1.col*4);
y=8+p1.row;
gotoxy(x,y);
cprintf("*");
gotoxy(x1,y1);
if(new_value % 2)
putchar('\n');
printf(" >> row: %d, col: %d, value: %2d", p1.row,p1.col,p1.value);
}
sleep(1); //pause for 1 second
}while(new_value > last_value);
printf("\n\n\t\t\t press enter when ready");
(void) getchar();
return 0;
}
void info(void) {
int i;
printf(" An example of Hill Climbing, in an array. From the start at\n");
printf(" the top left row and column, each step must be:\n\n");
printf(" * to a higher value, and \n * no more than the current value + 1\n");
printf("\n This algorithm finds the local maximal value, which may or may not\n\
be the greatest value in the array.");
printf(" Here, it misses the highest\n value of 20, near the center of the array.\n");
printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t press enter when ready\n");
(void) getchar();
for(i=0;i<4;i++)
printf("\n\n\n\n\n\n\n\n\n\n\n\n");
}
The executable (runs in Windows XP, at least), can be d/l'd from:
http://en.swoopshare.com/file/0088f549c ... MB&lang=en