Submission #6437139


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};

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;
	}

	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;
		}
	}

	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);
	
			lli breakCnt=0;
			while(1){
				if(d.at(nowIndex)==nowC)break;
				nowIndex++;
				nowIndex%=d.size();
				breakCnt++;
				if(breakCnt >= k+10){
					cout<<-1<<endl;
					return 0;
				}
			}
			if(DEBUG)cout<<"nowIndex="<<nowIndex<<" breakCnt="<<breakCnt<<endl;
			cnt += breakCnt+1;
			if(DEBUG)cout<<"cnt="<<cnt<<endl;
			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 2312 Byte
Status RE
Exec Time 2139 ms
Memory 568924 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 272 ms 19200 KB
02.txt RE 110 ms 19200 KB
03.txt RE 109 ms 19200 KB
04.txt RE 109 ms 19200 KB
05.txt RE 108 ms 19200 KB
06.txt TLE 2119 ms 245836 KB
07.txt TLE 2123 ms 315880 KB
08.txt TLE 2122 ms 303176 KB
09.txt TLE 2123 ms 316368 KB
10.txt TLE 2120 ms 260956 KB
11.txt TLE 2119 ms 242404 KB
12.txt TLE 2123 ms 331116 KB
13.txt TLE 2122 ms 292816 KB
14.txt TLE 2119 ms 252136 KB
15.txt TLE 2122 ms 289152 KB
16.txt TLE 2120 ms 274664 KB
17.txt TLE 2125 ms 344404 KB
18.txt TLE 2122 ms 287568 KB
19.txt TLE 2119 ms 237316 KB
20.txt TLE 2123 ms 311104 KB
21.txt TLE 2124 ms 342992 KB
22.txt TLE 2118 ms 234900 KB
23.txt TLE 2139 ms 568924 KB
24.txt TLE 2138 ms 538436 KB
25.txt TLE 2125 ms 352860 KB
26.txt TLE 2114 ms 166568 KB
27.txt TLE 2126 ms 355304 KB
28.txt TLE 2120 ms 258016 KB
29.txt TLE 2122 ms 300644 KB
30.txt TLE 2116 ms 208008 KB
31.txt RE 169 ms 19328 KB
32.txt RE 166 ms 19328 KB
33.txt RE 173 ms 19328 KB
34.txt RE 165 ms 19328 KB
35.txt RE 166 ms 19328 KB
36.txt RE 169 ms 19328 KB
37.txt RE 166 ms 19328 KB
38.txt RE 169 ms 19328 KB
39.txt RE 167 ms 19328 KB
40.txt TLE 2105 ms 20532 KB
41.txt RE 110 ms 19200 KB
s1.txt RE 110 ms 19200 KB
s2.txt RE 109 ms 19200 KB
s3.txt RE 108 ms 19200 KB