結果

問題 No.367 ナイトの転身
ユーザー 🐬hec🐬hec
提出日時 2016-04-29 22:43:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 2,124 bytes
コンパイル時間 1,644 ms
コンパイル使用メモリ 175,844 KB
実行使用メモリ 5,908 KB
最終ジャッジ日時 2023-08-01 08:07:17
合計ジャッジ時間 2,821 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,388 KB
testcase_01 AC 3 ms
5,328 KB
testcase_02 AC 3 ms
5,404 KB
testcase_03 AC 3 ms
5,340 KB
testcase_04 AC 3 ms
5,336 KB
testcase_05 AC 3 ms
5,328 KB
testcase_06 AC 3 ms
5,448 KB
testcase_07 AC 3 ms
5,460 KB
testcase_08 AC 3 ms
5,324 KB
testcase_09 AC 3 ms
5,444 KB
testcase_10 AC 18 ms
5,836 KB
testcase_11 AC 24 ms
5,908 KB
testcase_12 AC 17 ms
5,524 KB
testcase_13 AC 13 ms
5,636 KB
testcase_14 AC 14 ms
5,528 KB
testcase_15 AC 3 ms
5,336 KB
testcase_16 AC 14 ms
5,488 KB
testcase_17 AC 5 ms
5,624 KB
testcase_18 AC 5 ms
5,484 KB
testcase_19 AC 6 ms
5,384 KB
testcase_20 AC 4 ms
5,364 KB
testcase_21 AC 7 ms
5,500 KB
testcase_22 AC 3 ms
5,460 KB
testcase_23 AC 3 ms
5,380 KB
testcase_24 AC 3 ms
5,328 KB
testcase_25 AC 4 ms
5,348 KB
testcase_26 AC 3 ms
5,448 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
次のファイルから読み込み:  /usr/local/gcc7/include/c++/12.2.0/functional:54,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/x86_64-pc-linux-gnu/bits/stdc++.h:71,
         次から読み込み:  main.cpp:1:
コンストラクタ ‘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 /usr/local/gcc7/include/c++/12.2.0/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 /usr/local/gcc7/include/c++/12.2.0/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 <無名> = true; _Elements = {int, int, int}]’ at /usr/local/gcc7/include/c++/12.2.0/tuple:744:54,
    inlined from ‘int main()’ at main.cpp:45:9:
/usr/local/gcc7/include/c++/12.2.0/tuple:200:11: 警告: ‘sw’ may be used uninitialized [-Wmaybe-uninitialized]
  200 |         : _M_head_impl(std::forward<_UHead>(__h)) { }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: 関数 ‘int main()’ 内:
main.cpp:37:16: 備考: ‘sw’ はここで定義されています
   37 |         int sh,sw,gh,gw;
      |                ^~
コンストラクタ ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = int&; long unsigned int _Idx = 0; _Head = int]’ 内,
    inlined from ‘constexpr std::_T

ソースコード

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