-
Notifications
You must be signed in to change notification settings - Fork 2
/
1008. Image Encoding.c
238 lines (194 loc) · 4.69 KB
/
1008. Image Encoding.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <stdio.h>
/* Queue used for BFS */
struct coord
{
int x, y;
};
typedef struct queueNode
{
struct coord idx;
struct queueNode* next;
} QueueNode;
typedef struct queue
{
QueueNode* Head;
QueueNode* Tail;
} Queue;
int isEmpty(Queue Q)
{
return (Q.Head == NULL);
}
void enqueue(Queue* Q, struct coord idx)
{
QueueNode* newNode = (QueueNode*) malloc(sizeof(QueueNode));
newNode->idx = idx;
newNode->next = NULL;
if (Q->Tail == NULL)
{
Q->Tail = newNode;
Q->Head = Q->Tail;
}
else
{
Q->Tail->next = newNode;
Q->Tail = newNode;
}
}
struct coord dequeue(Queue* Q)
{
struct coord temp;
if (isEmpty(*Q))
{
printf("Queue is empty.\n");
return temp;
}
QueueNode* oldHead = Q->Head;
struct coord idx = oldHead->idx;
Q->Head = Q->Head->next;
free(oldHead);
if (Q->Head == NULL)
Q->Tail = NULL;
return idx;
}
/********************/
#define WHITE 0
#define BLACK 1
#define VISITED 2
#define N 10
int G[11][11];
int rep; /* representation */
int main(void)
{
Queue Q = {NULL, NULL};
int i, j, h;
int NrNodes = 1;
struct coord st;
char s[20];
for (i = 1; i <= N; i++)
for (j = 1; j <= N; j++)
G[i][j] = WHITE;
gets(s);
rep = sscanf(s, "%d %d", &st.x, &st.y);
if (rep == 1)
{
int x, y;
for (i = 0; i < st.x; i++)
{
scanf("%d %d", &x, &y);
G[x][y] = BLACK;
}
}
else /* rep == 2 */
{
gets(s);
enqueue(&Q, st);
G[st.x][st.y] = BLACK;
while (s[0] != '.') {
st = dequeue(&Q);
NrNodes++;
for (i = 0; i < strlen(s) - 1; i++)
{
struct coord tmp;
switch (s[i])
{
case 'R' :
tmp.x = st.x + 1; tmp.y = st.y;
enqueue(&Q, tmp);
G[tmp.x][tmp.y] = BLACK;
break;
case 'T' :
tmp.x = st.x; tmp.y = st.y + 1;
enqueue(&Q, tmp);
G[tmp.x][tmp.y] = BLACK;
break;
case 'L' :
tmp.x = st.x - 1; tmp.y = st.y;
enqueue(&Q, tmp);
G[tmp.x][tmp.y] = BLACK;
break;
case 'B' :
tmp.x = st.x; tmp.y = st.y - 1;
enqueue(&Q, tmp);
G[tmp.x][tmp.y] = BLACK;
break;
}
}
gets(s);
}
}
/*
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++)
printf("%3d ", G[i][j]);
printf("\n");
}
*/
if (rep == 1)
{
/* Show second representation */
/* (1) - find left and lower */
for (i = 1; i <= N; i++)
for (j = 1; j <= N; j++)
if (G[i][j] != 0)
{
st.x = i;
st.y = j;
goto end_loop;
}
end_loop:
/* (2) - run a BFS */
enqueue(&Q, st);
printf("%d %d\n", st.x, st.y);
while (!isEmpty(Q))
{
struct coord c;
st = dequeue(&Q);
G[st.x][st.y] = VISITED;
/* Right */
c.x = st.x + 1; c.y = st.y;
if (G[c.x][c.y] == BLACK)
{
enqueue(&Q, c);
G[c.x][c.y] = VISITED;
printf("R");
}
/* Top */
c.x = st.x; c.y = st.y + 1;
if (G[c.x][c.y] == BLACK)
{
enqueue(&Q, c);
G[c.x][c.y] = VISITED;
printf("T");
}
/* Left */
c.x = st.x - 1; c.y = st.y;
if (G[c.x][c.y] == BLACK)
{
enqueue(&Q, c);
G[c.x][c.y] = VISITED;
printf("L");
}
/* Bottom */
c.x = st.x; c.y = st.y - 1;
if (G[c.x][c.y] == BLACK)
{
enqueue(&Q, c);
G[c.x][c.y] = VISITED;
printf("B");
}
if (isEmpty(Q))
printf(".\n");
else
printf(",\n");
}
}
else
{
printf("%d\n", NrNodes);
for (i = 1; i <= N; i++)
for (j = 1; j <= N; j++)
if (G[i][j] != 0)
printf("%d %d\n", i, j);
}
return 0;
}