Submission #74400

# Submission time Handle Problem Language Result Execution time Memory
74400 2018-08-31T15:49:40 Z vvv3334 Mecho (IOI09_mecho) C
100 / 100
249 ms 14496 KB
//baekjoon_5465
#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+1) / 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();
	//
	//int t = BFS2(235);
	//printf("%d\n",BFS2(232));
	//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);
   ^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 452 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 520 KB Output is correct
6 Correct 2 ms 524 KB Output is correct
7 Correct 100 ms 14496 KB Output is correct
8 Correct 2 ms 14496 KB Output is correct
9 Correct 2 ms 14496 KB Output is correct
10 Correct 2 ms 14496 KB Output is correct
11 Correct 2 ms 14496 KB Output is correct
12 Correct 2 ms 14496 KB Output is correct
13 Correct 2 ms 14496 KB Output is correct
14 Correct 2 ms 14496 KB Output is correct
15 Correct 2 ms 14496 KB Output is correct
16 Correct 2 ms 14496 KB Output is correct
17 Correct 2 ms 14496 KB Output is correct
18 Correct 2 ms 14496 KB Output is correct
19 Correct 2 ms 14496 KB Output is correct
20 Correct 2 ms 14496 KB Output is correct
21 Correct 2 ms 14496 KB Output is correct
22 Correct 2 ms 14496 KB Output is correct
23 Correct 2 ms 14496 KB Output is correct
24 Correct 2 ms 14496 KB Output is correct
25 Correct 3 ms 14496 KB Output is correct
26 Correct 3 ms 14496 KB Output is correct
27 Correct 2 ms 14496 KB Output is correct
28 Correct 2 ms 14496 KB Output is correct
29 Correct 2 ms 14496 KB Output is correct
30 Correct 3 ms 14496 KB Output is correct
31 Correct 2 ms 14496 KB Output is correct
32 Correct 3 ms 14496 KB Output is correct
33 Correct 7 ms 14496 KB Output is correct
34 Correct 7 ms 14496 KB Output is correct
35 Correct 26 ms 14496 KB Output is correct
36 Correct 8 ms 14496 KB Output is correct
37 Correct 8 ms 14496 KB Output is correct
38 Correct 30 ms 14496 KB Output is correct
39 Correct 9 ms 14496 KB Output is correct
40 Correct 9 ms 14496 KB Output is correct
41 Correct 41 ms 14496 KB Output is correct
42 Correct 11 ms 14496 KB Output is correct
43 Correct 12 ms 14496 KB Output is correct
44 Correct 48 ms 14496 KB Output is correct
45 Correct 12 ms 14496 KB Output is correct
46 Correct 13 ms 14496 KB Output is correct
47 Correct 60 ms 14496 KB Output is correct
48 Correct 13 ms 14496 KB Output is correct
49 Correct 14 ms 14496 KB Output is correct
50 Correct 80 ms 14496 KB Output is correct
51 Correct 16 ms 14496 KB Output is correct
52 Correct 15 ms 14496 KB Output is correct
53 Correct 90 ms 14496 KB Output is correct
54 Correct 18 ms 14496 KB Output is correct
55 Correct 20 ms 14496 KB Output is correct
56 Correct 122 ms 14496 KB Output is correct
57 Correct 19 ms 14496 KB Output is correct
58 Correct 21 ms 14496 KB Output is correct
59 Correct 151 ms 14496 KB Output is correct
60 Correct 23 ms 14496 KB Output is correct
61 Correct 23 ms 14496 KB Output is correct
62 Correct 167 ms 14496 KB Output is correct
63 Correct 123 ms 14496 KB Output is correct
64 Correct 249 ms 14496 KB Output is correct
65 Correct 181 ms 14496 KB Output is correct
66 Correct 142 ms 14496 KB Output is correct
67 Correct 147 ms 14496 KB Output is correct
68 Correct 53 ms 14496 KB Output is correct
69 Correct 52 ms 14496 KB Output is correct
70 Correct 37 ms 14496 KB Output is correct
71 Correct 44 ms 14496 KB Output is correct
72 Correct 30 ms 14496 KB Output is correct
73 Correct 35 ms 14496 KB Output is correct
74 Correct 65 ms 14496 KB Output is correct
75 Correct 80 ms 14496 KB Output is correct
76 Correct 64 ms 14496 KB Output is correct
77 Correct 67 ms 14496 KB Output is correct
78 Correct 84 ms 14496 KB Output is correct
79 Correct 56 ms 14496 KB Output is correct
80 Correct 68 ms 14496 KB Output is correct
81 Correct 70 ms 14496 KB Output is correct
82 Correct 62 ms 14496 KB Output is correct
83 Correct 83 ms 14496 KB Output is correct
84 Correct 72 ms 14496 KB Output is correct
85 Correct 94 ms 14496 KB Output is correct
86 Correct 82 ms 14496 KB Output is correct
87 Correct 77 ms 14496 KB Output is correct
88 Correct 88 ms 14496 KB Output is correct
89 Correct 95 ms 14496 KB Output is correct
90 Correct 103 ms 14496 KB Output is correct
91 Correct 98 ms 14496 KB Output is correct
92 Correct 97 ms 14496 KB Output is correct