結果

問題 No.367 ナイトの転身
ユーザー YazatenYazaten
提出日時 2016-04-29 22:46:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 1,958 bytes
コンパイル時間 1,691 ms
コンパイル使用メモリ 174,768 KB
実行使用メモリ 60,672 KB
最終ジャッジ日時 2024-04-19 07:37:43
合計ジャッジ時間 3,432 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
17,280 KB
testcase_01 AC 10 ms
17,280 KB
testcase_02 AC 11 ms
17,152 KB
testcase_03 AC 11 ms
17,152 KB
testcase_04 AC 12 ms
17,152 KB
testcase_05 AC 10 ms
17,152 KB
testcase_06 AC 11 ms
17,152 KB
testcase_07 AC 12 ms
17,152 KB
testcase_08 AC 11 ms
17,152 KB
testcase_09 AC 12 ms
17,152 KB
testcase_10 AC 154 ms
60,672 KB
testcase_11 AC 186 ms
60,672 KB
testcase_12 AC 86 ms
33,792 KB
testcase_13 AC 80 ms
33,920 KB
testcase_14 AC 76 ms
33,280 KB
testcase_15 AC 13 ms
17,792 KB
testcase_16 AC 76 ms
31,232 KB
testcase_17 AC 19 ms
18,816 KB
testcase_18 AC 23 ms
19,712 KB
testcase_19 AC 32 ms
21,504 KB
testcase_20 AC 20 ms
19,200 KB
testcase_21 AC 27 ms
21,632 KB
testcase_22 AC 10 ms
17,408 KB
testcase_23 AC 11 ms
17,408 KB
testcase_24 AC 11 ms
17,408 KB
testcase_25 AC 11 ms
17,280 KB
testcase_26 AC 11 ms
17,152 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:90:24: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
   90 |         dijkstra(pii(sy*w+sx,0));
      |                      ~~^~
main.cpp:83:13: note: 'sy' was declared here
   83 |         int sy,sx,gy,gx;
      |             ^~
main.cpp:90:26: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
   90 |         dijkstra(pii(sy*w+sx,0));
      |                      ~~~~^~~
main.cpp:83:16: note: 'sx' was declared here
   83 |         int sy,sx,gy,gx;
      |                ^~
main.cpp:92:41: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized]
   92 |         int res = min(d[gy*w+gx][0],d[gy*w+gx][1]);
      |                                       ~~^~
main.cpp:83:19: note: 'gy' was declared here
   83 |         int sy,sx,gy,gx;
      |                   ^~
main.cpp:92:43: warning: 'gx' may be used uninitialized [-Wmaybe-uninitialized]
   92 |         int res = min(d[gy*w+gx][0],d[gy*w+gx][1]);
      |                                       ~~~~^~~
main.cpp:83:22: note: 'gx' was declared here
   83 |         int sy,sx,gy,gx;
      |                      ^~

ソースコード

diff #

#include "bits/stdc++.h"
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
//#define INF (1LL<<59)


#define MAX_V (500*500)
#define MAX_B 2

struct edge{pii to;int cost;};
vector<edge> G[MAX_V][MAX_B];
int d[MAX_V][MAX_B];

void dijkstra(pii s){
	priority_queue< pair<int,pii>,vector<pair<int,pii>>,greater<pair<int,pii>> > que;
	rep(i,MAX_V)rep(j,MAX_B)d[i][j]=INF;
	d[s.first][s.second]=0;
	que.push(make_pair(0,s));
	
	while(que.size()){
		pair<int,pii> p=que.top();
		que.pop();
		
		int v=p.second.first;
		int b=p.second.second;
		if(d[v][b]<p.first)continue;
		
		rep(i,G[v][b].size()){
			edge e=G[v][b][i];
			if(d[e.to.first][e.to.second]>d[v][b]+e.cost){
				d[e.to.first][e.to.second]=d[v][b]+e.cost;
				que.push(make_pair(d[e.to.first][e.to.second],e.to));
			}
		}
	}
}



int main(){
	int h,w;
	cin>>h>>w;
	vector<string> data(h);
	rep(i,h)cin>>data[i];
	
	int dy[]={1,-1,1,-1};
	int dx[]={1,1,-1,-1};
	
	rep(i,h){
		rep(j,w){
			rep(l,4){
				int ddy = i+dy[l];
				int ddx = j+dx[l];
				int k=1;
				if( ddx>=0 && ddy>=0 && ddx<w && ddy<h && data[ddy][ddx]=='R')k=0;
				if( ddx>=0 && ddy>=0 && ddx<w && ddy<h )G[i*w+j][1].pb(edge{pii(ddy*w+ddx,k),1});
			}
		}
	}
	
	int dy2[]={-1,-2,-2,-1,1,2,2,1};
	int dx2[]={-2,-1,1,2,2,1,-1,-2};
	
	rep(i,h){
		rep(j,w){
			rep(l,8){
				int ddy = i+dy2[l];
				int ddx = j+dx2[l];
				int k = 0;
				if( ddx>=0 && ddy>=0 && ddx<w && ddy<h && data[ddy][ddx]=='R')k=1;
				if( ddx>=0 && ddy>=0 && ddx<w && ddy<h )G[i*w+j][0].pb(edge{pii(ddy*w+ddx,k),1});
			}
		}
	}
	
	int sy,sx,gy,gx;
	rep(i,h){
		rep(j,w){
			if(data[i][j]=='G')gy=i,gx=j;
			if(data[i][j]=='S')sy=i,sx=j;
		}
	}
	dijkstra(pii(sy*w+sx,0));
	
	int res = min(d[gy*w+gx][0],d[gy*w+gx][1]);
	
	if(res==INF)cout<<"-1"<<endl;
	else cout<<res<<endl;
}
0