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
|
#include<iostream>
#include<cmath>
using namespace std;
// creating the lattice
bool lattice[7][7]=
{1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1
} ;
int Num=0; // Variable counting the walks
int Lenght=0; // Variable keeping track of the length
int way (int x, int y)
{
//Reasons for saying a self-avoiding walk is invalid
if(Lenght > 3 || x < 0 || y < 0 || x > 6|| y > 6 || lattice[x][y] != 1)
return 0;
// Preventing a walk going to the same place twice
lattice[x][y] = 0;
Lenght++;
if(Lenght == 3)
{
Num++;
Lenght = 0;
}
//Going in the four possible directions
return way(x+1, y) ||
way(x, y+1) ||
way(x-1, y) ||
way(x, y-1);
}
int main()
{//Starting from the middle of the array
way(3,3);
cout<<Num<<endl;
return 0;
}
|