Submission #74398

# Submission time Handle Problem Language Result Execution time Memory
74398 2018-08-31T15:36:12 Z vvv3334 Mecho (IOI09_mecho) C
95 / 100
259 ms 14452 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();
	
	printf("%d\n",BFS2(2));
	outputVISIT();*/

	//return;

	left = 0;
	right = 2 * 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 444 KB Output is correct
3 Correct 2 ms 444 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 560 KB Output is correct
7 Correct 120 ms 14360 KB Output is correct
8 Correct 2 ms 14360 KB Output is correct
9 Correct 2 ms 14360 KB Output is correct
10 Correct 2 ms 14360 KB Output is correct
11 Correct 2 ms 14360 KB Output is correct
12 Incorrect 2 ms 14360 KB Output isn't correct
13 Correct 2 ms 14360 KB Output is correct
14 Correct 2 ms 14360 KB Output is correct
15 Correct 2 ms 14360 KB Output is correct
16 Correct 2 ms 14360 KB Output is correct
17 Correct 2 ms 14360 KB Output is correct
18 Correct 2 ms 14360 KB Output is correct
19 Correct 2 ms 14360 KB Output is correct
20 Correct 2 ms 14360 KB Output is correct
21 Correct 2 ms 14360 KB Output is correct
22 Correct 2 ms 14360 KB Output is correct
23 Correct 2 ms 14360 KB Output is correct
24 Correct 2 ms 14360 KB Output is correct
25 Correct 2 ms 14360 KB Output is correct
26 Correct 2 ms 14360 KB Output is correct
27 Correct 2 ms 14360 KB Output is correct
28 Correct 2 ms 14360 KB Output is correct
29 Correct 2 ms 14360 KB Output is correct
30 Correct 2 ms 14360 KB Output is correct
31 Correct 2 ms 14360 KB Output is correct
32 Correct 2 ms 14360 KB Output is correct
33 Correct 7 ms 14360 KB Output is correct
34 Correct 8 ms 14360 KB Output is correct
35 Correct 24 ms 14360 KB Output is correct
36 Correct 8 ms 14360 KB Output is correct
37 Correct 9 ms 14360 KB Output is correct
38 Correct 33 ms 14360 KB Output is correct
39 Correct 10 ms 14360 KB Output is correct
40 Correct 11 ms 14360 KB Output is correct
41 Correct 43 ms 14360 KB Output is correct
42 Correct 13 ms 14360 KB Output is correct
43 Correct 11 ms 14360 KB Output is correct
44 Correct 56 ms 14360 KB Output is correct
45 Correct 13 ms 14360 KB Output is correct
46 Correct 13 ms 14360 KB Output is correct
47 Correct 63 ms 14360 KB Output is correct
48 Correct 15 ms 14360 KB Output is correct
49 Correct 19 ms 14360 KB Output is correct
50 Correct 74 ms 14360 KB Output is correct
51 Correct 17 ms 14360 KB Output is correct
52 Correct 20 ms 14360 KB Output is correct
53 Correct 109 ms 14360 KB Output is correct
54 Correct 20 ms 14360 KB Output is correct
55 Correct 20 ms 14360 KB Output is correct
56 Correct 123 ms 14360 KB Output is correct
57 Correct 21 ms 14360 KB Output is correct
58 Correct 22 ms 14360 KB Output is correct
59 Correct 143 ms 14360 KB Output is correct
60 Correct 26 ms 14360 KB Output is correct
61 Correct 24 ms 14360 KB Output is correct
62 Correct 188 ms 14360 KB Output is correct
63 Correct 125 ms 14360 KB Output is correct
64 Correct 259 ms 14360 KB Output is correct
65 Correct 191 ms 14360 KB Output is correct
66 Correct 141 ms 14360 KB Output is correct
67 Correct 141 ms 14360 KB Output is correct
68 Correct 60 ms 14360 KB Output is correct
69 Correct 60 ms 14360 KB Output is correct
70 Correct 42 ms 14360 KB Output is correct
71 Correct 47 ms 14360 KB Output is correct
72 Correct 34 ms 14360 KB Output is correct
73 Correct 47 ms 14360 KB Output is correct
74 Correct 72 ms 14360 KB Output is correct
75 Correct 85 ms 14360 KB Output is correct
76 Correct 75 ms 14360 KB Output is correct
77 Correct 89 ms 14360 KB Output is correct
78 Correct 90 ms 14452 KB Output is correct
79 Correct 57 ms 14452 KB Output is correct
80 Correct 68 ms 14452 KB Output is correct
81 Correct 77 ms 14452 KB Output is correct
82 Correct 76 ms 14452 KB Output is correct
83 Correct 88 ms 14452 KB Output is correct
84 Correct 80 ms 14452 KB Output is correct
85 Correct 92 ms 14452 KB Output is correct
86 Correct 89 ms 14452 KB Output is correct
87 Correct 93 ms 14452 KB Output is correct
88 Correct 100 ms 14452 KB Output is correct
89 Correct 103 ms 14452 KB Output is correct
90 Correct 103 ms 14452 KB Output is correct
91 Correct 120 ms 14452 KB Output is correct
92 Correct 115 ms 14452 KB Output is correct