after some modification i ended up in this:
I can only take 26 steps [A-Z] and this prorgram should stop at most in 26 steps.
Code:
// Random walk
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
char a[10][10];
void print()
{
int i,j;
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
printf("%c ", a[i][j]);
printf("\n");
}
}
void set()
{
int i,j;
for(i = 0; i < 10; i++)
for(j = 0; j < 10; j++)
a[i][j] = '.';
}
int in_bound(int m, int n)
{
if(m >= 0 && m <= 9 && n >= 0 && n <= 9)
return 1;
else
return 0;
}
int not_used(int m, int n)
{
if(a[m][n] != '.')
return 0;
else
return 1;
}
void gen()
{
int m,n;
int next_move;
m = n = 0;
char c = 'A';
a[m][n] = 'A';
for(c = 'B'; c <= 'Z'; c++){
int ok = 0;
next_move = rand() % 4;
if(next_move <= 0 && in_bound(m, n-1) && not_used(m,n-1)) {
ok = 1;
n--;
a[m][n] = c;
continue;
}
if(next_move <= 1 && in_bound(m, n+1) && not_used(m, n+1)) {
ok = 1;
n++;
a[m][n] = c;
continue;
}
if(next_move <= 2 && in_bound(m-1, n) && not_used(m-1,n)) {
ok = 1;
m--;
a[m][n] = c;
continue;
}
if(next_move <= 3 && in_bound(m+1, n) && not_used(m+1,n)) {
ok = 1;
m++;
a[m][n] = c;
continue;
}if(!ok)
break;
}
}
int main()
{
srand((unsigned)time(NULL));
set();
gen();
print();
return 0;
}
I just quit this problem maybe tomarrow i tackle it again !
thanks for your attention !