답안 #787053

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
787053 2023-07-18T17:25:55 Z Bula Awesome Arrowland Adventure (eJOI19_adventure) C++14
100 / 100
93 ms 28100 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int ll
const ll mod=1e9+7;
int n,m;
char c[501][501];

bool check(int x, int y){
	if(x<1 || y<1 || x>n || y>m ) return false;
	if(x==n && y==m) return true;
	if(c[x][y]=='X') return false;
	return true;
}

main(){
	cin>>n>>m;
	vector< pair<int,int> > v[n*m+1];
	
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>c[i][j];
		}
	}
	vector<pair<int,int> > p={{-1,0},{0,1},{1,0},{0,-1}};
	vector<int> t={-m,1,m,-1};
	map<char,int> mp;
	mp['N']=0;
	mp['E']=1;
	mp['S']=2;
	mp['W']=3;
	int x=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			x++;
			if(c[i][j]=='X') continue;
			int l=mp[c[i][j]];
			for(int k=0;k<4;k++){
				int a=i+p[k].first;
				int b=j+p[k].second;
				bool ok = check(a,b);
				if(ok==false) continue;
				int dist;
				if(k<l) dist=(k+4)-l;
				else dist=k-l;
				v[x].pb({x+t[k],dist});
			}
		}
	}
//	for(int i=1;i<=n*m;i++){
//		for(int j=0;j<v[i].size();j++){
//			cout<<i<<" "<<v[i][j].first<<" "<<v[i][j].second<<endl;
//		}
//	}
	vector<int> d(n*m+1,INT_MAX);
	d[1]=0;
	set< pair<int,int> > q;
	q.insert({0,1});
	while(!q.empty()){
		//pair<int,int> pr=q.begin();
		int u=q.begin()->second;
		q.erase(q.begin());
		
		for(auto edge : v[u]){
			int to = edge.first;
			int len = edge.second;
		
			if(d[u] + len < d[to]){
				q.erase({d[to],to});
				d[to]=d[u]+len;
				q.insert({d[to],to});
			}
		}
	}
//	for(int i=1;i<=n*m;i++) cout<<i<<" "<<d[i]<<endl;
	if(d[n*m] == INT_MAX) cout<<-1<<endl;
	else cout<<d[n*m]<<endl;
}

Compilation message

adventure.cpp:20:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   20 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 304 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 308 KB Output is correct
20 Correct 1 ms 308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 304 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 308 KB Output is correct
20 Correct 1 ms 308 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 304 KB Output is correct
35 Correct 7 ms 2388 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 8 ms 2772 KB Output is correct
38 Correct 6 ms 4052 KB Output is correct
39 Correct 80 ms 23140 KB Output is correct
40 Correct 82 ms 23064 KB Output is correct
41 Correct 39 ms 22920 KB Output is correct
42 Correct 93 ms 22992 KB Output is correct
43 Correct 88 ms 28100 KB Output is correct
44 Correct 38 ms 22984 KB Output is correct