Submission #6437212


Source Code Expand

#include <bits/stdc++.h>
 
using namespace std;
 
#define lli long long int
#define REP(i,s,l) for(lli i=s;i<l;i++)
#define DEBUG 0
#define INF (1LL<<50)
#define MOD 1000000007


lli data[1100][1100]={-1};
lli flag[1100][1100]={-1};

#define L 0
#define U 1
#define R 2
#define D 3

signed main(){

	lli n,m,k;
	cin>>n>>m>>k;

	string d;
	cin>>d;

	/*普通に幅優先探索して、経路の復元を行う*/
	/*その時の経路復元したものを見て、かかる歩数をカウントすることにする*/

	pair<lli,lli> s;
	pair<lli,lli> g;

	REP(i,0,1100)REP(j,0,1100){
		data[i][j]=-1;
		flag[i][j]=-1;
	}

	map<char,vector<lli>> av;
	REP(i,1,n+1)REP(j,1,m+1){
		char tmp;
		cin>>tmp;
		flag[i][j]=0;
		if(tmp=='.')data[i][j]=0;
		else if(tmp=='S'){
			s.first=j;
			s.second=i;
		}
		else if(tmp=='G'){
			g.first=j;
			g.second=i;
		}
		else{
			flag[i][j]=-1;
		}
	}

	REP(i,0,d.size()){
		char nowC = d.at(i);
		av[nowC].push_back(i);
	}

	queue<pair<lli,lli>> q;
	queue<string> qs;
	q.push(s);
	qs.push("");

	lli bflag=0;
	queue<string> qg;
	while(q.size()){
		pair<lli,lli> top = q.front();
		if(top.first == g.first && top.second == g.second){
			qg.push(qs.front());
		}
		string topS = qs.front();
		flag[top.second][top.first]=1;
		q.pop();
		qs.pop();

		lli dx[4]={-1,0,1,0};
		lli dy[4]={0,1,0,-1};
		string ds[4]={"L","D","R","U"};

		for(lli i=0;i<4;i++){
			lli nextX = top.first+dx[i];
			lli nextY = top.second+dy[i];

			if(flag[nextY][nextX]==0){
				q.push(make_pair(nextX,nextY));
				qs.push(topS+ds[i]);
			}
		}
	}

	if(qg.size()==0){
		cout<<-1<<endl;
		return 0;
	}

	string ans=qs.front();
	if(DEBUG)cout<<ans<<endl;

	lli cnt=0;
	lli nowIndex=0;
	lli last=INF;
	while(qg.size()){
		string ans=qg.front();
		qg.pop();

		if(DEBUG)cout<<ans<<endl;

		lli cnt=0;
		lli nowIndex=0;

		for(lli i=0;i<ans.size();i++){
			char nowC = ans.at(i);

			auto itr = lower_bound(av[nowC].begin(),av[nowC].end(),nowIndex);
			if(itr==av[nowC].end()){
				itr = av[nowC].begin();
				cnt += d.size() - nowIndex;
				nowIndex=0;
			}
			if(DEBUG)cout<<"*itr="<<*itr<<endl;
			cnt += (*itr) -nowIndex+1;
			nowIndex = *itr;
			nowIndex++;
			nowIndex %= d.size();
		}
		last=min(last,cnt);
	//cout<<cnt<<endl;
	}
	cout<<last<<endl;

	return 0;
}

Submission Info

Submission Time
Task E - 迷路
User soto800
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2406 Byte
Status RE
Exec Time 2140 ms
Memory 561960 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status AC
TLE × 26
RE × 18
Set Name Test Cases
Sample
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt RE 112 ms 19200 KB
02.txt RE 112 ms 19200 KB
03.txt RE 112 ms 19200 KB
04.txt RE 112 ms 19200 KB
05.txt RE 111 ms 19200 KB
06.txt TLE 2121 ms 248452 KB
07.txt TLE 2125 ms 316704 KB
08.txt TLE 2123 ms 308224 KB
09.txt TLE 2125 ms 317192 KB
10.txt TLE 2121 ms 250516 KB
11.txt TLE 2121 ms 243236 KB
12.txt TLE 2127 ms 331164 KB
13.txt TLE 2123 ms 293532 KB
14.txt TLE 2121 ms 254880 KB
15.txt TLE 2124 ms 294204 KB
16.txt TLE 2123 ms 287760 KB
17.txt TLE 2126 ms 340532 KB
18.txt TLE 2123 ms 281488 KB
19.txt TLE 2121 ms 255672 KB
20.txt TLE 2125 ms 326032 KB
21.txt TLE 2127 ms 343836 KB
22.txt TLE 2119 ms 231480 KB
23.txt TLE 2140 ms 561960 KB
24.txt TLE 2140 ms 537616 KB
25.txt TLE 2127 ms 352660 KB
26.txt TLE 2115 ms 167296 KB
27.txt TLE 2128 ms 355872 KB
28.txt TLE 2120 ms 246816 KB
29.txt TLE 2124 ms 301356 KB
30.txt TLE 2127 ms 344128 KB
31.txt RE 171 ms 20472 KB
32.txt RE 171 ms 20472 KB
33.txt RE 173 ms 20472 KB
34.txt RE 174 ms 20472 KB
35.txt RE 172 ms 20472 KB
36.txt RE 172 ms 20472 KB
37.txt RE 175 ms 20472 KB
38.txt RE 171 ms 20472 KB
39.txt RE 171 ms 20472 KB
40.txt TLE 2105 ms 21152 KB
41.txt RE 112 ms 19200 KB
s1.txt RE 112 ms 19200 KB
s2.txt RE 112 ms 19200 KB
s3.txt RE 112 ms 19200 KB