-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternFinder.c
More file actions
217 lines (193 loc) · 7.79 KB
/
Copy pathPatternFinder.c
File metadata and controls
217 lines (193 loc) · 7.79 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include<stdio.h>
#include<stdbool.h>
#include<windows.h>
int r1,r2,c1,c2,something;
int count=0, cind=0;
bool pattern;
struct Coordinates{//structure for storing coordinates of patterns' locations
int x;
int y;
};
struct reality{ //function for helping in giving background colors to motif
bool yes;
};
void input(int *motif,int p,int q){ // function for taking input elements in motif
printf("\033[1;33mEnter the elements in the motif :\033[1;0m \n");
something=-1;
for(int i=0;i<p;i++){
for(int j=0;j<q;j++){
scanf("%d",((motif+i*q)+j));
if(*((motif+i*q)+j)==0) {printf("\n\033[1;31mYou can't enter 0 here, Read the instructions carefully and RE-RUN the program.\033[1;0m\n\n");
something=10;
break;}
}
if(something>0){break;}
printf("\n");
}
return;
}
void check(int mosaic[r1][c1], int motif[r2][c2], int i, int j){ // function for checking pattern in motif
for(int m=0;m<r1;m++){
for(int n=0;n<c1;n++){
if((i+m>=r2 || j+n>=c2) && mosaic[m][n]==0){
pattern = false;
break;
}
if(mosaic[m][n]==0){
pattern=true;
continue;
}
if(motif[i+m][j+n]==mosaic[m][n] && i+m<r2 && j+n<c2){
pattern=true;
continue;
}
else{pattern=false;
break;
}
}
if(pattern==false){
break;
}
}
return;
}
void check_ele(int num){ // function for giving background color in matrix
if(num==1){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_GREEN);
}
else if(num==2){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_BLUE);
}
else if(num == 3){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_RED);
}
else if(num==4){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_GREEN|BACKGROUND_BLUE);
}
else if(num==5){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_RED|BACKGROUND_BLUE);
}
else if(num==6){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_RED|BACKGROUND_INTENSITY);
}
else if(num==7){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_BLUE|BACKGROUND_RED|BACKGROUND_INTENSITY);
}
else if(num==8){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_GREEN|BACKGROUND_RED);
}
else if(num==9){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_BLUE|BACKGROUND_INTENSITY);
}
else{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
}
return;
}
int main(){
//printing instructions
printf("\n\n\033[1;32m---------------------------------------------------------------------------------------------------\033[1;0m");
printf(" \n\n\033[1;32m**Instructions** :\033[1;0m \n");
printf(" \033[1;32m(i) You can enter any matrix (size : mxn, m,n>0) with elements in range 1-9.\033[1;0m\n");
printf(" \033[1;32m(these numbers are considered as tiles, different element means different tile.)\033[1;0m\n");
printf(" \033[1;32m(ii) Only in pattern matrix you can enter '0' as empty tile.\033[1;0m\n");
printf(" \033[1;32m(empty tile can be considered as any tile in motif.)\033[1;0m\n");
printf(" \033[1;32m(iii) Dimensions of motif matrix should be greater than pattern matrix.\033[1;0m\n");
printf(" \033[1;32m(iv) If you enter any values adjacent to each other, always keep a space between them.\033[1;0m\n");
printf(" \033[1;32m(v) If you enter elements <0 or >9, quality of final result may reduce.\033[1;0m\n\n");
printf("\033[1;32m---------------------------------------------------------------------------------------------------\033[1;0m\n\n");
system("pause");
//taking inputs for pattern
int r,c;
printf("\n\033[1;33mFirst enter number of rows then columns of Pattern : \033[1;0m\n");
scanf("%d %d",&r, &c);
if(r<1 || c<1){ printf("\n\033[1;31mWRONG DIMENSIONS... Read the instructions carefully and RE-RUN the program.\033[1;0m\n");
return 0;}
printf("\033[1;33mEnter the elements in the pattern : \033[1;0m\n");
int PaTTerN[r][c];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
scanf("%d",&PaTTerN[i][j]);
}
printf("\n");
}
//taking inputs for pattern
printf("\033[1;33mNow enter the number of rows then columns for motif in which you want to find above pattern : \033[1;0m\n");
int p,q;
scanf("%d %d",&p,&q);
if(r>p || c>q){ printf("\n\033[1;31mWRONG DIMENSIONS... Read instructions carefully and RE-RUN the program.\033[1;0m\n");
return 0;}
int motif[p][q];
input((int*)motif,p,q);
if(something>0){return 0;}
r1=r;r2=p;c1=c;c2=q;
struct Coordinates coord[p*q];
//checking pattern in motif and taking proper coordinates
for(int i=0;i<p;i++){
for(int j=0;j<q;j++){
if(motif[i][j]==PaTTerN[0][0]||PaTTerN[0][0]==0){
check(PaTTerN,motif,i,j);
if(pattern==true){
count++;
coord[cind].x=i;
coord[cind].y=j;
cind++;
}
}
}
}
printf("\033[1;33mThis is the given Pattern :\033[1;0m\n");
//printing pattern
for(int i=0;i<r1;i++){
for(int j=0;j<c1;j++){
check_ele(PaTTerN[i][j]);
printf("%d ",PaTTerN[i][j]);
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
printf("\n");
}
//printing motif
printf("\n\033[1;33mThis is the given Motif :\033[1;0m\n");
for(int i=0;i<r2;i++){
for(int j=0;j<c2;j++){
check_ele(motif[i][j]);
printf("%d ",motif[i][j]);
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
printf("\n");
}
printf("\n");
system("pause");
struct reality real[r2][c2];
if(count==0) printf("\n\n\033[1;31mNo such pattern found in motif\033[1;0m");
else{
printf("\n\033[1;35mThe number of such patterns in motif is/are : \033[1;36m%d\033[1;0m\n",count);//how many times pattern found
printf("\n\033[1;35mAt following index/indices required pattern is found :\n");
for(int v=0;v<count;v++){
printf("\n\033[1;36m%d] (%d, %d)\033[1;0m\t",v+1,coord[v].x + 1,coord[v].y + 1);//printing location coordinates
for(int l = 0; l<r2;l++){
for(int m=0;m<c2;m++){
real[l][m].yes = false;}
}
for(int o=coord[v].x; o<coord[v].x+r1;o++){
for(int z=coord[v].y;z<coord[v].y+c1;z++){
real[o][z].yes=true;
}
}
for(int i = 0; i<r2;i++){
for(int j=0;j<c2;j++){//showing location of pattern
if(real[i][j].yes == false){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_BLUE|BACKGROUND_RED|BACKGROUND_GREEN);
printf(" \033[1;30m%d \033[1;0m",motif[i][j]);
}
else if (real[i][j].yes == true){
check_ele(motif[i][j]);
printf(" \033[1;37m%d \033[1;0m",motif[i][j]);
}
}
printf("\n \t\t");
}
}
}
return 0;
}