Submission #257302

# Submission time Handle Problem Language Result Execution time Memory
257302 2020-08-04T05:20:46 Z 송준혁(#5061) None (JOI16_skating) C++17
100 / 100
458 ms 18836 KB
#include <bits/stdc++.h>
#define pb push_back
#define lb lower_bound
#define fi first
#define se second
#define mup(a,x) a=min(a,x)
#define Mup(a,x) a=max(a,x)
#define INF 1234567890
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

LL ans;
int N, M, num;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
int L[1010][1010], R[1010][1010];
int U[1010][1010], D[1010][1010];
bool chk[1010][1010];

struct Data{
	int x, y, w;
	bool operator<(const Data &r)const{return w > r.w;}
};
priority_queue<Data> PQ;

int main(){
	int x1, y1, x2, y2;
	scanf("%d %d", &N, &M);
	for (int i=1; i<=N; i++){
		for (int j=1; j<=M; j++){
			char ch;
			scanf(" %c", &ch);
			chk[i][j] = (ch=='#');
		}
	}
	for (int i=2; i<N; i++) for (int j=2; j<M; j++){
		if (chk[i][j]) continue;
		L[i][j] = chk[i][j-1]?j:L[i][j-1];
		U[i][j] = chk[i-1][j]?i:U[i-1][j];
	}
	for (int i=N-1; i>1; i--) for (int j=M-1; j>1; j--){
		if (chk[i][j]) continue;
		R[i][j] = chk[i][j+1]?j:R[i][j+1];
		D[i][j] = chk[i+1][j]?i:D[i+1][j];
	}
	scanf("%d %d", &x1, &y1);
	scanf("%d %d", &x2, &y2);
	PQ.push((Data){x1, y1, 0});
	while (!PQ.empty()){
		Data t = PQ.top();
		PQ.pop();
		if (chk[t.x][t.y]) continue;
		chk[t.x][t.y] = true;
		if (t.x == x2 && t.y == y2){
			printf("%d\n", t.w);
			return 0;
		}
		if (!chk[t.x][L[t.x][t.y]]) PQ.push((Data){t.x, L[t.x][t.y], t.w+1});
		if (!chk[t.x][R[t.x][t.y]]) PQ.push((Data){t.x, R[t.x][t.y], t.w+1});
		if (!chk[U[t.x][t.y]][t.y]) PQ.push((Data){U[t.x][t.y], t.y, t.w+1});
		if (!chk[D[t.x][t.y]][t.y]) PQ.push((Data){D[t.x][t.y], t.y, t.w+1});
		for (int i=0; i<4; i++) if (!chk[t.x+dx[i]][t.y+dy[i]]) PQ.push((Data){t.x+dx[i], t.y+dy[i], t.w+2});
	}
	puts("-1");
	return 0;
}

Compilation message

skating.cpp: In function 'int main()':
skating.cpp:28:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~~
skating.cpp:32:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf(" %c", &ch);
    ~~~~~^~~~~~~~~~~~
skating.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &x1, &y1);
  ~~~~~^~~~~~~~~~~~~~~~~~~
skating.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &x2, &y2);
  ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 0 ms 512 KB Output is correct
8 Correct 0 ms 512 KB Output is correct
9 Correct 0 ms 512 KB Output is correct
10 Correct 0 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 0 ms 512 KB Output is correct
8 Correct 0 ms 512 KB Output is correct
9 Correct 0 ms 512 KB Output is correct
10 Correct 0 ms 512 KB Output is correct
11 Correct 9 ms 3712 KB Output is correct
12 Correct 11 ms 3840 KB Output is correct
13 Correct 7 ms 3968 KB Output is correct
14 Correct 9 ms 3712 KB Output is correct
15 Correct 5 ms 3712 KB Output is correct
16 Correct 15 ms 4032 KB Output is correct
17 Correct 12 ms 3712 KB Output is correct
18 Correct 9 ms 3712 KB Output is correct
19 Correct 12 ms 3712 KB Output is correct
20 Correct 6 ms 3456 KB Output is correct
21 Correct 7 ms 3712 KB Output is correct
22 Correct 5 ms 3584 KB Output is correct
23 Correct 12 ms 3712 KB Output is correct
24 Correct 12 ms 3712 KB Output is correct
25 Correct 9 ms 3712 KB Output is correct
26 Correct 10 ms 3712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 0 ms 512 KB Output is correct
8 Correct 0 ms 512 KB Output is correct
9 Correct 0 ms 512 KB Output is correct
10 Correct 0 ms 512 KB Output is correct
11 Correct 9 ms 3712 KB Output is correct
12 Correct 11 ms 3840 KB Output is correct
13 Correct 7 ms 3968 KB Output is correct
14 Correct 9 ms 3712 KB Output is correct
15 Correct 5 ms 3712 KB Output is correct
16 Correct 15 ms 4032 KB Output is correct
17 Correct 12 ms 3712 KB Output is correct
18 Correct 9 ms 3712 KB Output is correct
19 Correct 12 ms 3712 KB Output is correct
20 Correct 6 ms 3456 KB Output is correct
21 Correct 7 ms 3712 KB Output is correct
22 Correct 5 ms 3584 KB Output is correct
23 Correct 12 ms 3712 KB Output is correct
24 Correct 12 ms 3712 KB Output is correct
25 Correct 9 ms 3712 KB Output is correct
26 Correct 10 ms 3712 KB Output is correct
27 Correct 253 ms 17592 KB Output is correct
28 Correct 382 ms 18804 KB Output is correct
29 Correct 73 ms 17720 KB Output is correct
30 Correct 181 ms 18836 KB Output is correct
31 Correct 88 ms 17148 KB Output is correct
32 Correct 307 ms 17248 KB Output is correct
33 Correct 60 ms 8824 KB Output is correct
34 Correct 150 ms 17272 KB Output is correct
35 Correct 316 ms 17596 KB Output is correct
36 Correct 251 ms 17592 KB Output is correct
37 Correct 120 ms 17144 KB Output is correct
38 Correct 458 ms 18800 KB Output is correct
39 Correct 114 ms 17144 KB Output is correct
40 Correct 79 ms 17144 KB Output is correct