結果

問題 No.367 ナイトの転身
ユーザー treeonetreeone
提出日時 2016-04-29 23:41:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,750 bytes
コンパイル時間 1,828 ms
コンパイル使用メモリ 163,816 KB
実行使用メモリ 7,648 KB
最終ジャッジ日時 2024-04-15 06:07:38
合計ジャッジ時間 2,556 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 31 ms
7,584 KB
testcase_11 AC 36 ms
7,648 KB
testcase_12 AC 10 ms
6,944 KB
testcase_13 AC 11 ms
6,944 KB
testcase_14 AC 14 ms
6,944 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 14 ms
6,944 KB
testcase_17 AC 4 ms
6,944 KB
testcase_18 AC 4 ms
6,944 KB
testcase_19 AC 5 ms
6,944 KB
testcase_20 AC 4 ms
6,944 KB
testcase_21 AC 5 ms
6,944 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:50:21: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   50 |         d[sy][sx][1]=0;
      |         ~~~~~~~~~~~~^~
main.cpp:50:21: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

const int INF=1e7;
int kdy[8]={2,2,1,1,-1,-1,-2,-2};
int kdx[8]={-1,1,-2,2,-2,2,-1,1};
int bdy[4]={1,1,-1,-1};
int bdx[4]={-1,1,-1,1};

struct data{
	int y,x;
	bool k;
	data(int y, int x,int k):y(y),x(x),k(k){}
};

char c[510][510]={};
int d[510][510][2];


signed main(){
	int h,w;
	cin>>h>>w;
	int sy,sx,gy,gx;
	rep(i,0,h){
		rep(j,0,w){
			cin>>c[i][j];
			if(c[i][j]=='S'){
				sy=i;
				sx=j;
			}else if(c[i][j]=='G'){
				gy=i;
				gx=j;
			}
		}
	}
	int f=1;
	rep(i,0,h) rep(j,0,w) rep(k,0,2) d[i][j][k]=INF;
	queue<data> q;
	q.push(data(sy,sx,f));
	d[sy][sx][1]=0;
	while(q.size()){
		data p=q.front(); q.pop();
		if(p.y==gy && p.x==gx) break;
		if(p.k){
			rep(i,0,8){
				int ny=p.y+kdy[i];
				int nx=p.x+kdx[i];
				if(0<=ny && ny<h && 0<=nx && nx<w){
					int tmp=1;
					if(c[ny][nx]=='R') tmp=0;
					if(d[ny][nx][tmp]!=INF) continue;
					q.push(data(ny,nx,tmp));
					d[ny][nx][tmp]=d[p.y][p.x][1]+1;
					//cout<<p.k<<" "<<ny<<" "<<nx<<" "<<d[ny][nx]<<endl;
				}
			}
		}else{
			rep(i,0,4){
				int ny=p.y+bdy[i];
				int nx=p.x+bdx[i];
				if(0<=ny && ny<h && 0<=nx && nx<w){
					int tmp=0;
					if(c[ny][nx]=='R') tmp=1;
					if(d[ny][nx][tmp]!=INF) continue;
					else q.push(data(ny,nx,tmp));
					d[ny][nx][tmp]=d[p.y][p.x][0]+1;
					//cout<<p.k<<" "<<ny<<" "<<nx<<" "<<d[ny][nx]<<endl;
				}
			}
		}
	}
	int ans=min(d[gy][gx][0],d[gy][gx][1]);
	if(ans==INF) cout<<-1<<endl;
	else cout<<ans<<endl;
}
0