-
Notifications
You must be signed in to change notification settings - Fork 0
/
Klaara'sFortress.c
146 lines (124 loc) · 2.72 KB
/
Klaara'sFortress.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
#include <stdio.h>
#include <stdbool.h>
#define MAX_M 20
#define MAX_N 20
typedef struct
{
int x;
int y;
} Point;
typedef struct
{
Point array[MAX_M * MAX_N];
int front;
int rear;
} Queue;
void initQueue(Queue *q)
{
q->front = -1;
q->rear = -1;
}
bool isEmpty(Queue *q)
{
return q->front == -1;
}
void enqueue(Queue *q, Point p)
{
if (isEmpty(q))
{
q->front = 0;
q->rear = 0;
}
else
{
q->rear++;
}
q->array[q->rear] = p;
}
Point dequeue(Queue *q)
{
Point p = q->array[q->front];
if (q->front == q->rear)
{
q->front = -1;
q->rear = -1;
}
else
{
q->front++;
}
return p;
}
int max(int a, int b)
{
return (a > b) ? a : b;
}
int min(int a, int b)
{
return (a < b) ? a : b;
}
int maxThiefTime(int m, int n, int fortress[MAX_M][MAX_N])
{
int directions[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int maxTime = 0;
int bfs(int start_x, int start_y)
{
bool visited[MAX_M][MAX_N] = {false};
int distance[MAX_M][MAX_N] = {0};
Queue q;
initQueue(&q);
Point start = {start_x, start_y};
enqueue(&q, start);
visited[start_x][start_y] = true;
while (!isEmpty(&q))
{
Point p = dequeue(&q);
for (int i = 0; i < 4; i++)
{
int nx = p.x + directions[i][0];
int ny = p.y + directions[i][1];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visited[nx][ny] && fortress[nx][ny] == 0)
{
distance[nx][ny] = distance[p.x][p.y] + 1;
visited[nx][ny] = true;
Point newPoint = {nx, ny};
enqueue(&q, newPoint);
}
}
}
return distance[m - 1][n - 1];
}
int distance = bfs(0, 0);
maxTime = distance;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (fortress[i][j] == 0)
{
fortress[i][j] = 1;
distance = bfs(0, 0);
maxTime = max(maxTime, distance);
fortress[i][j] = 0;
}
}
}
return maxTime + 1;
}
int main()
{
int m, n;
scanf("%d %d", &m, &n);
int fortress[MAX_M][MAX_N];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &fortress[i][j]);
}
}
int result = maxThiefTime(m, n, fortress);
printf("%d", result);
return 0;
getch();
}