結果

問題 No.367 ナイトの転身
ユーザー PulmnPulmn
提出日時 2018-07-12 23:34:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 1,890 bytes
コンパイル時間 1,340 ms
コンパイル使用メモリ 171,264 KB
実行使用メモリ 49,128 KB
最終ジャッジ日時 2024-04-15 04:10:22
合計ジャッジ時間 3,235 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 192 ms
49,116 KB
testcase_11 AC 272 ms
49,128 KB
testcase_12 AC 116 ms
20,776 KB
testcase_13 AC 104 ms
20,976 KB
testcase_14 AC 106 ms
20,092 KB
testcase_15 AC 5 ms
6,944 KB
testcase_16 AC 103 ms
18,240 KB
testcase_17 AC 11 ms
6,944 KB
testcase_18 AC 15 ms
6,940 KB
testcase_19 AC 29 ms
7,936 KB
testcase_20 AC 12 ms
6,944 KB
testcase_21 AC 29 ms
7,680 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 3 ms
6,940 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
//const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

class Graph{
	private:
	int n;
	vvp g;
	public:
	int DIJ(int s,int t){
		priority_queue<P> q;
		vi d(n,inf);
		d[s]=0;
		q.push({0,s});
		while(!q.empty()){
			P p=q.top();
			q.pop();
			int v=p.second;
			if(d[v]<-p.first) continue;
			for(auto i:g[v]){
				int u=i.first,D=d[v]+i.second;
				if(d[u]>D){
					d[u]=D;
					q.push({-D,u});
				}
			}
		}
		return d[t];
	}
	Graph(int v){
		n=v;
		g=vvp(v);
	}
	void add_edge(int s,int t,int c=1){
		g[s].push_back({t,c});
	}
};

const int dx[8]={2,1,-1,-2,-2,-1,1,2},dy[8]={1,2,2,1,-1,-2,-2,-1};
const int DX[4]={1,1,-1,-1},DY[4]={1,-1,1,-1};

int h,w,I,J;
vvi a;

int main(){
	cin>>h>>w;
	a=vvi(h,vi(w));
	for(int i=0;i<h;i++) for(int j=0;j<w;j++){
		char c;
		cin>>c;
		if(c=='S') I=i*w+j;
		if(c=='G') J=i*w+j;
		if(c=='R') a[i][j]=-1;
	}
	Graph g(2*h*w);
	for(int i=0;i<h;i++) for(int j=0;j<w;j++) for(int k=0;k<8;k++){
		int x=i+dx[k],y=j+dy[k];
		if(x>=0&&x<h&&y>=0&&y<w) g.add_edge(i*w+j,x*w+y+(a[x][y]<0?h*w:0));
	}
	for(int i=0;i<h;i++) for(int j=0;j<w;j++) for(int k=0;k<4;k++){
		int x=i+DX[k],y=j+DY[k];
		if(x>=0&&x<h&&y>=0&&y<w) g.add_edge(i*w+j+h*w,x*w+y+(a[x][y]<0?0:h*w));
	}
	int D=min(g.DIJ(I,J),g.DIJ(I,J+h*w));
	cout<<(D==inf?-1:D)<<endl;
}
0