//baekjoon_10216
#pragma warning(disable : 4996)
#include <stdio.h>
#include <stdlib.h>
int N, S;
int MAP[1000][1000];
char str[810];
int CHANGE['Z'+1];
int sr, sc, er, ec;
int VISIT[1000][1000];
typedef struct st
{
int r;
int c;
int time;
}QUEUE;
QUEUE Que[810 * 810];
int WP, RP;
void input()
{
int i, k, tmp;
CHANGE['T'] = -1;
CHANGE['G'] = 0;
CHANGE['H'] = 1;
CHANGE['M'] = 0;
CHANGE['D'] = 0x7fff0000;
scanf("%d %d", &N, &S);
for (i = 1; i <= N;i++)
{
scanf("%s", str + 1);
for (k = 1; k <= N;k++)
{
if (str[k] == 'M')
{
sr = i;
sc = k;
}
else if(str[k] == 'D')
{
er = i;
ec = k;
}
MAP[i][k] = CHANGE[str[k]];
}
}
//printf("%d %d %d %d\n", sr, sc, er, ec);
}
void output()
{
int i, k;
for (i = 1; i <= N;i++)
{
for (k = 1; k <= N;k++)
printf("%2d ", MAP[i][k]);
putchar('\n');
}
putchar('\n');
}
void outputVISIT()
{
int i, k;
for (i = 1; i <= N;i++)
{
for (k = 1; k <= N;k++)
printf("%2d ", VISIT[i][k]);
putchar('\n');
}
putchar('\n');
}
int dr[] = { 0,1,0,-1 };
int dc[] = { 1,0,-1,0 };
void BFS()
{
int i, nr, nc;
QUEUE out;
while (WP > RP)
{
out = Que[RP++];
for (i = 0; i < 4;i++)
{
nr = out.r + dr[i];
nc = out.c + dc[i];
if (nr < 1 || nc < 1 || nr > N || nc > N) continue;
if (MAP[nr][nc] == 0)
{
Que[WP].r = nr;
Que[WP++].c = nc;
MAP[nr][nc] = MAP[out.r][out.c] + 1;
}
}
}
}
int BFS2(int mid)
{
int i, k, nr, nc;
QUEUE out;
if (mid == MAP[sr][sc]-1 ) return 0;
WP = RP = 0;
for (i = 1; i <= N;i++)
for (k = 1; k <= N;k++)
VISIT[i][k] = 0;
Que[WP].r = sr;
Que[WP].c = sc;
Que[WP++].time = mid;
VISIT[sr][sc] = 1;
while (WP > RP)
{
out = Que[RP++];
if (out.r == er && out.c == ec) return 1;
for (i = 0; i < 4;i++)
{
nr = out.r + dr[i];
nc = out.c + dc[i];
if (nr < 1 || nc < 1 || nr > N || nc > N) continue;
if (MAP[nr][nc] == -1 || VISIT[nr][nc] != 0) continue;
if ((mid + (out.time-mid) / S) >= (MAP[nr][nc] - 1)) continue;
Que[WP].r = nr;
Que[WP].c = nc;
Que[WP++].time = out.time + 1;
VISIT[nr][nc] = (out.time - mid) / S + 1;
}
//outputVISIT();
}
return 0;
}
int main()
{
int i, k;
int right, left, mid;
int tmp, ans;
input();
//output();
for (i = 1; i <= N;i++)
{
for (k = 1; k <= N;k++)
{
if (MAP[i][k] == 1)
{
Que[WP].r = i;
Que[WP++].c = k;
}
}
}
BFS();
/*output();
printf("%d\n",BFS2(5));
outputVISIT();*/
//return;
left = 0;
right = N * N;
ans = -1;
while (left <= right)
{
mid = (left + right) / 2;
tmp = BFS2(mid);
//printf("%d %d\n", tmp, mid);
if (tmp)
{
ans = mid;
left = mid + 1;
}
else
right = mid - 1;
}
printf("%d\n", ans);
return 0;
}
Compilation message
mecho.c:2:0: warning: ignoring #pragma warning [-Wunknown-pragmas]
#pragma warning(disable : 4996)
mecho.c: In function 'input':
mecho.c:52:22: warning: array subscript has type 'char' [-Wchar-subscripts]
MAP[i][k] = CHANGE[str[k]];
^
mecho.c:26:12: warning: unused variable 'tmp' [-Wunused-variable]
int i, k, tmp;
^~~
mecho.c:34:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &S);
^~~~~~~~~~~~~~~~~~~~~~
mecho.c:38:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", str + 1);
^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
592 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
592 KB |
Output isn't correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
132 ms |
14920 KB |
Output is correct |
8 |
Incorrect |
2 ms |
14920 KB |
Output isn't correct |
9 |
Correct |
2 ms |
14920 KB |
Output is correct |
10 |
Correct |
2 ms |
14920 KB |
Output is correct |
11 |
Correct |
2 ms |
14920 KB |
Output is correct |
12 |
Incorrect |
2 ms |
14920 KB |
Output isn't correct |
13 |
Correct |
2 ms |
14920 KB |
Output is correct |
14 |
Correct |
2 ms |
14920 KB |
Output is correct |
15 |
Incorrect |
2 ms |
14920 KB |
Output isn't correct |
16 |
Correct |
2 ms |
14920 KB |
Output is correct |
17 |
Incorrect |
2 ms |
14920 KB |
Output isn't correct |
18 |
Correct |
2 ms |
14920 KB |
Output is correct |
19 |
Incorrect |
2 ms |
14920 KB |
Output isn't correct |
20 |
Correct |
3 ms |
14920 KB |
Output is correct |
21 |
Incorrect |
2 ms |
14920 KB |
Output isn't correct |
22 |
Correct |
2 ms |
14920 KB |
Output is correct |
23 |
Incorrect |
2 ms |
14920 KB |
Output isn't correct |
24 |
Correct |
2 ms |
14920 KB |
Output is correct |
25 |
Incorrect |
2 ms |
14920 KB |
Output isn't correct |
26 |
Correct |
2 ms |
14920 KB |
Output is correct |
27 |
Incorrect |
3 ms |
14920 KB |
Output isn't correct |
28 |
Correct |
2 ms |
14920 KB |
Output is correct |
29 |
Incorrect |
3 ms |
14920 KB |
Output isn't correct |
30 |
Correct |
2 ms |
14920 KB |
Output is correct |
31 |
Incorrect |
2 ms |
14920 KB |
Output isn't correct |
32 |
Correct |
2 ms |
14920 KB |
Output is correct |
33 |
Incorrect |
8 ms |
14920 KB |
Output isn't correct |
34 |
Correct |
8 ms |
14920 KB |
Output is correct |
35 |
Correct |
23 ms |
14920 KB |
Output is correct |
36 |
Incorrect |
8 ms |
14920 KB |
Output isn't correct |
37 |
Correct |
9 ms |
14920 KB |
Output is correct |
38 |
Correct |
28 ms |
14920 KB |
Output is correct |
39 |
Incorrect |
10 ms |
14920 KB |
Output isn't correct |
40 |
Correct |
13 ms |
14920 KB |
Output is correct |
41 |
Correct |
41 ms |
14920 KB |
Output is correct |
42 |
Incorrect |
11 ms |
14920 KB |
Output isn't correct |
43 |
Correct |
11 ms |
14920 KB |
Output is correct |
44 |
Correct |
48 ms |
14920 KB |
Output is correct |
45 |
Incorrect |
13 ms |
14920 KB |
Output isn't correct |
46 |
Correct |
14 ms |
14920 KB |
Output is correct |
47 |
Correct |
64 ms |
14920 KB |
Output is correct |
48 |
Incorrect |
16 ms |
14920 KB |
Output isn't correct |
49 |
Correct |
15 ms |
14920 KB |
Output is correct |
50 |
Correct |
77 ms |
14920 KB |
Output is correct |
51 |
Incorrect |
18 ms |
14920 KB |
Output isn't correct |
52 |
Correct |
17 ms |
14920 KB |
Output is correct |
53 |
Correct |
99 ms |
15660 KB |
Output is correct |
54 |
Incorrect |
19 ms |
15696 KB |
Output isn't correct |
55 |
Correct |
19 ms |
16176 KB |
Output is correct |
56 |
Correct |
108 ms |
18192 KB |
Output is correct |
57 |
Incorrect |
23 ms |
18192 KB |
Output isn't correct |
58 |
Correct |
21 ms |
18584 KB |
Output is correct |
59 |
Correct |
135 ms |
20764 KB |
Output is correct |
60 |
Incorrect |
24 ms |
20764 KB |
Output isn't correct |
61 |
Correct |
24 ms |
21212 KB |
Output is correct |
62 |
Correct |
152 ms |
23632 KB |
Output is correct |
63 |
Correct |
121 ms |
23632 KB |
Output is correct |
64 |
Correct |
233 ms |
23752 KB |
Output is correct |
65 |
Correct |
172 ms |
24408 KB |
Output is correct |
66 |
Incorrect |
150 ms |
25056 KB |
Output isn't correct |
67 |
Correct |
132 ms |
25732 KB |
Output is correct |
68 |
Correct |
56 ms |
26264 KB |
Output is correct |
69 |
Correct |
55 ms |
26892 KB |
Output is correct |
70 |
Correct |
42 ms |
27636 KB |
Output is correct |
71 |
Correct |
47 ms |
28156 KB |
Output is correct |
72 |
Correct |
35 ms |
28792 KB |
Output is correct |
73 |
Correct |
46 ms |
32364 KB |
Output is correct |
74 |
Correct |
68 ms |
32992 KB |
Output is correct |
75 |
Correct |
85 ms |
33748 KB |
Output is correct |
76 |
Correct |
74 ms |
34264 KB |
Output is correct |
77 |
Correct |
66 ms |
34892 KB |
Output is correct |
78 |
Correct |
90 ms |
35648 KB |
Output is correct |
79 |
Correct |
57 ms |
36148 KB |
Output is correct |
80 |
Correct |
65 ms |
36888 KB |
Output is correct |
81 |
Correct |
73 ms |
37532 KB |
Output is correct |
82 |
Correct |
64 ms |
38068 KB |
Output is correct |
83 |
Correct |
83 ms |
38660 KB |
Output is correct |
84 |
Correct |
76 ms |
39352 KB |
Output is correct |
85 |
Correct |
95 ms |
39916 KB |
Output is correct |
86 |
Correct |
85 ms |
40544 KB |
Output is correct |
87 |
Correct |
84 ms |
41172 KB |
Output is correct |
88 |
Correct |
87 ms |
41928 KB |
Output is correct |
89 |
Correct |
93 ms |
42444 KB |
Output is correct |
90 |
Correct |
88 ms |
43200 KB |
Output is correct |
91 |
Correct |
92 ms |
43844 KB |
Output is correct |
92 |
Correct |
97 ms |
44364 KB |
Output is correct |