-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject0.c
128 lines (119 loc) · 1.83 KB
/
Project0.c
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
#include<stdio.h>
//function declarations
void InitBoard(int xMax, int yMax);
int IsOnBoard(int x, int y);
void Visit(int x, int y);
void UnVisit(int x, int y);
void CalculatePaths(int x, int y);
void PrintPath(void);
int numVisited;
int numSolutions;
int board[5][5];
int visitedStackX[25];
int visitedStackY[25];
int main(int argc, char *argv[])
{
int xMax = 5, yMax = 5;
numVisited = 0;
numSolutions = 0;
InitBoard(xMax, yMax);
int x, y;
for(x=0;x<xMax;x++)
{
for(y=0; y<yMax; y++)
{
if(numVisited!=0)
{
numVisited = 0;
}
CalculatePaths(x, y);
}
}
printf("Solutions found: %d\n", numSolutions);
return 0;
}
void InitBoard(int xMax, int yMax)
{
int x;
int y;
for(x=0; x<xMax; x++)
{
for(y=0;y<yMax;y++)
{
board[x][y] = 1;
}
}
}
int IsOnBoard(int x, int y)
{
if(x<0||x>4||y<0||y>4)
{
return 0;
}
return board[x][y];
}
void CalculatePaths(int x, int y)
{
Visit(x, y);
if(numVisited>=25)
{
PrintPath();
UnVisit(x, y);
return;
}
if(IsOnBoard(x+2,y+1)==1)
{
CalculatePaths(x+2, y+1);
}
if(IsOnBoard(x+2,y-1)==1)
{
CalculatePaths(x+2, y-1);
}
if(IsOnBoard(x-2,y+1)==1)
{
CalculatePaths(x-2,y+1);
}
if(IsOnBoard(x-2,y-1)==1)
{
CalculatePaths(x-2, y-1);
}
if(IsOnBoard(x+1,y+2)==1)
{
CalculatePaths(x+1, y+2);
}
if(IsOnBoard(x+1,y-2)==1)
{
CalculatePaths(x+1, y-2);
}
if(IsOnBoard(x-1,y+2)==1)
{
CalculatePaths(x-1, y+2);
}
if(IsOnBoard(x-1,y-2)==1)
{
CalculatePaths(x-1, y-2);
}
UnVisit(x,y);
}
void Visit(int x, int y)
{
visitedStackY[numVisited]=y;
visitedStackX[numVisited]=x;
board[x][y] = 0;
numVisited++;
}
void UnVisit(int x, int y)
{
board[x][y] = 1;
numVisited--;
}
void PrintPath()
{
numSolutions++;
int i;
for(i=0;i<24;i++)
{
printf("%d, ", 5*visitedStackX[i]+visitedStackY[i]);
}
printf("%d\n", 5*visitedStackX[24]+visitedStackY[24]);
}