-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
125 lines (98 loc) · 2.21 KB
/
code.c
File metadata and controls
125 lines (98 loc) · 2.21 KB
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
#include <stdio.h>
#include <stdio.h>
#define MAX_NUM 30
int top=-1;
int stack[MAX_NUM];
int isFull(){
if(top==MAX_NUM-1)
return 1;
return 0;
}
int isNull(){
if(top==-1)
return 1;
return 0;
}
void push(int value){
if(isFull()==1)
printf("Over flow");
else
stack[++top]=value;
}
void pop(){
if(isNull()==1)
printf("Underflow");
else
top--;
}
void view(){
int i;
printf("\nStack \n");
if(isNull()==1)
printf("Stack Empty");
else
for(i=0;i<=top;i++)
printf("%d\n",stack[i]);
printf("\n");
}
int matrix[10][10],x,sp;
int isElement(int n){
for(int i = 0;i < top;i++)
if(n == stack[i])
return 1;
return 0;
}
int checkCondition(int ix,int y,int thisx,int thisy){
if(ix == thisx)
if(thisy == y + 1 || thisy == y - 1){
//printf("\n\n%d\n\n",!isElement(matrix[ix][y]));
if(!isElement(matrix[ix][y]))
return 1;
}
if(y == thisy)
if(ix == thisx + 1 || ix == thisx - 1)
if(!isElement(matrix[ix][y]))
return 1;
return 0;
}
void puzzle(int num){
int thisx,thisy;
for(int i = 0;i < x;i++)
for(int j = 0;j < x;j++)
if(matrix[i][j] == num){
printf("\nLoop called\n");
thisx = i;
thisy = j;
push(matrix[i][j]);
break;
}
if(top == x * x - 1)
return;
for(int i = 0;i < x;i++)
for(int j = 0;j < x;j++){
if(top == x * x - 1)
return;
printf("Loop %d %d of %d %d\n",i,j,thisx,thisy);
if(checkCondition(i,j,thisx,thisy)){
view();
puzzle(matrix[i][j]);
}
}
if(top == x * x - 1)
return;
pop();
view();
}
void main(){
int n = 0;
printf("Enter x value\n");
scanf("%d",&x);
printf("Enter starting point\n");
scanf("%d",&sp);
for(int i = 0;i < x;i++)
for(int j = 0;j < x;j++){
matrix[i][j] = n++;
}
puzzle(sp);
view();
}