結果
| 問題 | No.367 ナイトの転身 | 
| コンテスト | |
| ユーザー |  Pulmn | 
| 提出日時 | 2018-07-12 23:34:28 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 250 ms / 2,000 ms | 
| コード長 | 1,890 bytes | 
| コンパイル時間 | 1,331 ms | 
| コンパイル使用メモリ | 171,040 KB | 
| 実行使用メモリ | 49,104 KB | 
| 最終ジャッジ日時 | 2024-10-04 15:01:48 | 
| 合計ジャッジ時間 | 3,274 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
ソースコード
#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;
}
            
            
            
        