結果

問題 No.367 ナイトの転身
ユーザー 🐬hec
提出日時 2016-04-29 22:43:08
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 2,124 bytes
コンパイル時間 1,744 ms
コンパイル使用メモリ 176,796 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2024-10-11 00:16:40
合計ジャッジ時間 2,785 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/functional:54,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:71,
                 from main.cpp:1:
In constructor 'constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = int&; long unsigned int _Idx = 1; _Head = int]',
    inlined from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(_UHead&&, _UTail&& ...) [with _UHead = int&; _UTail = {int}; <template-parameter-2-3> = void; long unsigned int _Idx = 1; _Head = int; _Tail = {int}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/tuple:292:38,
    inlined from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(_UHead&&, _UTail&& ...) [with _UHead = int&; _UTail = {int&, int}; <template-parameter-2-3> = void; long unsigned int _Idx = 0; _Head = int; _Tail = {int, int}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/tuple:292:38,
    inlined from 'constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int&, int&, int}; bool _Valid = true; typename std::enable_if<_TCC<_Valid>::__is_implicitly_constructible<_UElements ...>(), bool>::type <anonymous> = true; _Elements = {int, int, int}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/tuple:744:54,
    inlined from 'int main()' at main.cpp:45:9:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/tuple:200:11: warning: 'sw' may be used uninitialized [-Wmaybe-uninitialized]
  200 |         : _M_head_impl(std::forward<_UHead>(__h)) { }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:37:16: note: 'sw' was declared here
   37 |         int sh,sw,gh,gw;
      |                ^~
In constructor 'constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = int&; long unsigned int _Idx = 

ソースコード

diff #

#include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using namespace std;


string board[510];
int dist[510][510][2];

using state=tuple<int,int,int>;

int main(void){
	int h,w;
	cin >> h >> w;
	rep(i,h) cin >> board[i];

	clr(dist,-1);

	int sh,sw,gh,gw;
	rep(i,h)rep(j,w){
		if(board[i][j]=='S') sh=i,sw=j;
		if(board[i][j]=='G') gh=i,gw=j;
	}

	dist[sh][sw][0]=0;
	queue<state> q;
	q.push(state(sh,sw,0));

	while(!q.empty()){
		int ch,cw,type;
		tie(ch,cw,type)=q.front();q.pop();
		if(type==0){
			const int dh[8]={-2,-2,-1,-1,1,1,2,2};
			const int dw[8]={-1,1,-2,2,-2,2,-1,1};
			rep(i,8){
				int nh=ch+dh[i],nw=cw+dw[i],ntype=type;
				if(nh<0||h<=nh) continue;
				if(nw<0||w<=nw) continue;
				if(board[nh][nw]=='R') ntype^=1;
				if(dist[nh][nw][ntype]==-1){
					dist[nh][nw][ntype]=dist[ch][cw][type]+1;
					q.push(state(nh,nw,ntype));
				}
			}
		}else{
			const int dh[4]={-1,-1,1,1};
			const int dw[4]={-1,1,-1,1};
			rep(i,4){
				int nh=ch+dh[i],nw=cw+dw[i],ntype=type;
				if(nh<0||h<=nh) continue;
				if(nw<0||w<=nw) continue;
				if(board[nh][nw]=='R') ntype^=1;
				if(dist[nh][nw][ntype]==-1){
					dist[nh][nw][ntype]=dist[ch][cw][type]+1;
					q.push(state(nh,nw,ntype));
				}
			}
		}
	}

	int ans=1<<20;
	rep(i,2) if(dist[gh][gw][i]!=-1) chmin(ans,dist[gh][gw][i]);
	if(ans==(1<<20)) ans=-1;
	cout << ans << endl;
	return 0;
}
0