-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.c
176 lines (145 loc) · 4.05 KB
/
solver.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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define OK 0
#define NO_INPUT 1
#define TOO_LONG 2
int moveCount = 0;
char **moves;
int k, n;
static int getLine (char *prmpt, char *buff, size_t sz) {
int ch, extra;
// Get line with buffer overrun protection.
if (prmpt != NULL) {
printf ("%s", prmpt);
fflush (stdout);
}
if (fgets (buff, sz, stdin) == NULL)
return NO_INPUT;
// If it was too long, there'll be no newline. In that case, we
// flush to end of line so that excuss doesn't affect the next call.
if(buff[strlen(buff)-1] != '\n') {
extra = 0;
while ((ch=getchar() != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? TOO_LONG : OK;
}
// Otherwise remove newline and give string back to caller.
buff[strlen(buff)-1] = '\0';
return OK;
}
int getIntFromString(char * str, int index){
int ret = 0;
ret = str[index] - '0';
return ret;
}
static void getNandK (){
char *buff = (char*) malloc(100 * sizeof(char));
getLine("Enter N K>", buff, sizeof(buff));
n = getIntFromString(buff, 0);
k = getIntFromString(buff, 2);
free(buff);
}
static void getState (char* name, int* state, int length) {
int i = 0;
int temp = 0;
char *buffer = (char*) malloc( (length * 2) * sizeof(char) );
char prmt[30];
sprintf(prmt, "Enter state %s>", name);
int rc = getLine(prmt, buffer, (sizeof(char) * (length * 2)));
if(rc == TOO_LONG)
printf("input was too long\n");
for(i; i < length; i ++) {
state[i] = getIntFromString(buffer, i*2);
}
free(buffer);
}
int addMove(int from, int to){
moveCount ++;
moves = (char**) realloc(moves, moveCount * sizeof(char *));
moves[moveCount-1] = (char *) malloc(5 * sizeof(char *));
sprintf(moves[moveCount-1], "%d %d", from, to);
}
int moveDisk(int disk, int destination, int*state) {
printf("moving disk %d\n", disk);
int i = 0, j = 0;
int calculatedDestination = 0;
for(j = 0; j < disk; j ++){
// check if the disk your trying to move is on top
printf("disk: %d isn't on top shifting\n", disk);
for(i = disk-1; i >= 0; i --) {
if(state[disk] == state[i]){
calculatedDestination = findDestination(disk, destination, state, i);
moveDisk(i, calculatedDestination, state);
}
}
// ensure that there are no smaller disks where you want to move to
for(i = disk-1; i >= 0; i --) {
if(state[i] == destination) {
printf("disk %d is in the road of disk %d, shifting\n", i, disk);
calculatedDestination = findDestination(disk, destination, state, i);
moveDisk(i, calculatedDestination, state);
}
}
}
addMove(state[disk], destination);
state[disk] = destination;
printf("moved %d -> %d\n", disk+1, destination);
printf("new state:\n");
for(i = 0; i < n; i++){
printf("%d ", state[i]);
}
printf("\n");
}
int isTop(int disk, int * state) {
int i;
for(i = disk-1; i >= 0; i --){
if(state[disk] == state[i]){
return 0;
}
}
return 1;
}
int findDestination(int parentId, int parentDestination, int *state, int disk) {
int l = 1;
while(l <= k){
if(l != parentDestination && diskOnPeg(l, state) == -1 && l != state[disk]) {
//first check for empty peg
return l;
}
l++;
}
// if not empty peg ensure that disk on peg is bigger
for(l = 1; l <= k; l++) {
if(l != parentDestination && l != state[parentId] && l != state[disk])
return l;
}
return -1;
}
int diskOnPeg(int peg, int *state) {
int i = n-1;
for(i; 0 <= i; i--)
if(state[i] == peg)
return i;
return -1;
}
int main() {
int i = 0, j=0, m = 0;
moveCount = 0;
getNandK();
int *currentState = malloc( sizeof(int) * n );
getState("inital", currentState, n);
int *finalState = malloc( sizeof(int) * n );
getState("final", finalState, n);
// loop through each disk starting at the largest i.e n - 1
for(i = (n-1); i >= 0; i--) {
if(currentState[i] != finalState[i]) {
moveDisk(i, finalState[i], currentState);
}
}
printf("%d\n", moveCount);
for(i = 0; i < moveCount; i++){
printf("%s\n", moves[i]);
}
return 0;
}