-
Notifications
You must be signed in to change notification settings - Fork 34
/
1066-Gathering-Food.cpp
154 lines (101 loc) · 1.83 KB
/
1066-Gathering-Food.cpp
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
#include <iostream>
#include <stdio.h>
#include <queue>
#include <limits.h>
#include <string.h>
using namespace std;
struct node{
int i;
int j;
int count;
};
int n;
int k;
char a[20][20];
int temp;
node find_distance(char target, node start)
{
queue <node> q;
node temp;
int b[4] = {+1, -1, 0, 0};
int c[4] = {0, 0, +1, -1};
int i;
int j;
int ii;
int jj;
int vis[20][20];
memset(vis, 0, sizeof(vis));
q.push(start);
vis[start.i][start.j] = 1;
while(!q.empty()) {
temp = q.front();
q.pop();
i = temp.i;
j = temp.j;
if(a[i][j] == target) {
return temp;
}
for (int cs = 0; cs < 4; cs++) {
if((i + b[cs] < n and i + b[cs] >= 0) and (j + c[cs] < n and j + c[cs] >= 0)) {
node epic;
ii = i + b[cs];
jj = j + c[cs];
if((a[ii][jj] == '.' or a[ii][jj] == target) and vis[ii][jj] == 0) {
epic.i = i + b[cs];
epic.j = j + c[cs];
epic.count = temp.count + 1;
vis[ii][jj] = 1;
q.push(epic);
}
}
}
}
temp.count = INT_MAX;
return temp;
}
int main()
{
int t;
int sum;
int starti;
int startj;
char target;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
scanf("%d", &n);
k = -999;
sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
temp = a[i][j] - 'A' + 1;
k = max(k, temp);
if(a[i][j] == 'A') {
starti = i;
startj = j;
}
}
}
node temp;
temp.i = starti;
temp.j = startj;
temp.count = 0;
int i;
for (i = 0; i < k; i++) {
target = 'A' + i;
temp.count = 0;
temp = find_distance(target, temp);
if(temp.count == INT_MAX) {
break;
}
a[temp.i][temp.j] = '.';
sum += temp.count;
}
if(i == k) {
printf("Case %d: %d\n", cs, sum);
}
else {
printf("Case %d: Impossible\n", cs);
}
}
}