#include <stdio.h>
int rear=1, front=1, n, k, array[1001][1001], Queuex[1000001], Queuey[1000001], Time[1000001], Dirx[4]= {1, -1, 0, 0}, Diry[4]= {0, 0, 1, -1}, Visit[1001][1001];
void push(int a, int b, int c)
{
Queuex[rear] = a;
Time[rear] = c;
Queuey[rear++] = b;
}
int popx()
{
return Queuex[front];
}
int popy()
{
return Queuey[front++];
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int s=0, i, j, check=0, m=0, res=0, checking=0, tmp=0, tle;
scanf("%d %d", &n, &k);
tle=n*k;
tmp = n;
n=k;
k=tmp;
for (i=1; i<=n; i++)
{
for (j=1; j<=k; j++)
{
scanf("%d", &array[i][j]);
if (array[i][j] != 0)
{
check++;
tle--;
}
if (array[i][j] == 1)
{
push(i, j, 0);
Visit[i][j] = 1;
}
}
}
if (check == n*k)
{
printf("0");
return 0;
}
check=0;
while (front != rear)
{
s=popx();
m=popy();
res++;
for (i=0; i<4; i++)
{
if (array[s+Dirx[i]][m+Diry[i]] == 0 && s+Dirx[i] > 0 && m+Diry[i] > 0 && s+Dirx[i] <= n && m+Diry[i] <= k && Visit[s+Dirx[i]][m+Diry[i]] == 0)
{
tle--;
array[s+Dirx[i]][m+Diry[i]] = 1;
push(s+Dirx[i], m+Diry[i], Time[res]+1);
Visit[s+Dirx[i]][m+Diry[i]] = 1;
}
}
if (tle == 0) break;
}
for (i=1; i<=n; i++)
{
for (j=1; j<=k; j++)
{
if (array[i][j] == 1 || array[i][j] == -1)
{
//printf("%d ", array[i][j]);
checking++;
}
}
//printf("\n");
}
if (checking == n*k) printf("%d", Time[rear-1]);
else printf("-1");
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
20632 KB |
Output is correct |
2 |
Correct |
0 ms |
20632 KB |
Output is correct |
3 |
Correct |
0 ms |
20632 KB |
Output is correct |
4 |
Correct |
0 ms |
20632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
20632 KB |
Output is correct |
2 |
Correct |
0 ms |
20632 KB |
Output is correct |
3 |
Correct |
0 ms |
20632 KB |
Output is correct |
4 |
Correct |
0 ms |
20632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
20632 KB |
Output is correct |
2 |
Correct |
0 ms |
20632 KB |
Output is correct |
3 |
Correct |
0 ms |
20632 KB |
Output is correct |
4 |
Correct |
0 ms |
20632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20632 KB |
Output is correct |
2 |
Correct |
4 ms |
20632 KB |
Output is correct |
3 |
Correct |
0 ms |
20632 KB |
Output is correct |
4 |
Correct |
0 ms |
20632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20632 KB |
Output is correct |
2 |
Correct |
0 ms |
20632 KB |
Output is correct |
3 |
Correct |
8 ms |
20632 KB |
Output is correct |
4 |
Correct |
0 ms |
20632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
20632 KB |
Output is correct |
2 |
Correct |
4 ms |
20632 KB |
Output is correct |
3 |
Correct |
0 ms |
20632 KB |
Output is correct |
4 |
Correct |
4 ms |
20632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
20632 KB |
Output is correct |
2 |
Correct |
24 ms |
20632 KB |
Output is correct |
3 |
Correct |
16 ms |
20632 KB |
Output is correct |
4 |
Correct |
12 ms |
20632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
20632 KB |
Output is correct |
2 |
Correct |
56 ms |
20632 KB |
Output is correct |
3 |
Correct |
4 ms |
20632 KB |
Output is correct |
4 |
Correct |
8 ms |
20632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
20632 KB |
Output is correct |
2 |
Correct |
104 ms |
20632 KB |
Output is correct |
3 |
Correct |
8 ms |
20632 KB |
Output is correct |
4 |
Correct |
68 ms |
20632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
20632 KB |
Output is correct |
2 |
Correct |
140 ms |
20632 KB |
Output is correct |
3 |
Correct |
116 ms |
20632 KB |
Output is correct |
4 |
Correct |
104 ms |
20632 KB |
Output is correct |