結果

問題 No.367 ナイトの転身
コンテスト
ユーザー treeone
提出日時 2016-04-29 23:41:34
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,750 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,586 ms
コンパイル使用メモリ 181,804 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2026-04-20 13:06:37
合計ジャッジ時間 2,022 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/allocator.h:46,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/string:45,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bitset:54,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:54,
                 from main.cpp:1:
In member function 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = data; _Args = {data}; _Tp = data]',
    inlined from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = data; _Args = {data}; _Tp = data]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/alloc_traits.h:674:17,
    inlined from 'void std::deque<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {data}; _Tp = data; _Alloc = std::allocator<data>]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/deque.tcc:170:30,
    inlined from 'void std::deque<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = data; _Alloc = std::allocator<data>]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_deque.h:1621:21,
    inlined from 'void std::queue<_Tp, _Sequence>::push(value_type&&) [with _Tp = data; _Sequence = std::deque<data, std::allocator<data> >]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_queue.h:318:20,
    inlined from 'int main()' at main.cpp:49:8:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/new_allocator.h:191:11: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mai

ソースコード

diff #
raw source code

#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