結果

問題 No.367 ナイトの転身
ユーザー mMqrMruYrNII4TdmMqrMruYrNII4Td
提出日時 2017-06-23 15:18:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,586 bytes
コンパイル時間 1,039 ms
コンパイル使用メモリ 68,216 KB
実行使用メモリ 8,560 KB
最終ジャッジ日時 2024-04-14 06:56:55
合計ジャッジ時間 2,832 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,240 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 3 ms
6,976 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 3 ms
6,948 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
7,112 KB
testcase_10 AC 33 ms
7,876 KB
testcase_11 AC 41 ms
8,388 KB
testcase_12 AC 27 ms
8,560 KB
testcase_13 AC 20 ms
8,400 KB
testcase_14 AC 24 ms
8,004 KB
testcase_15 AC 4 ms
8,312 KB
testcase_16 AC 25 ms
8,316 KB
testcase_17 AC 6 ms
7,112 KB
testcase_18 AC 8 ms
8,052 KB
testcase_19 AC 9 ms
7,368 KB
testcase_20 AC 5 ms
8,228 KB
testcase_21 AC 10 ms
8,172 KB
testcase_22 AC 3 ms
7,108 KB
testcase_23 AC 3 ms
7,792 KB
testcase_24 AC 3 ms
7,496 KB
testcase_25 AC 4 ms
6,980 KB
testcase_26 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:43:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   43 |                         scanf(" %c", &c);
      |                         ~~~~~^~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
 
using namespace std;

#define FOR(i,a,b) for (ll i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)

typedef long long ll;

const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS = 1e-10;

int d[501][501][2];
int dx[2][8] = {{-2,-2,-1,-1,1,1,2,2},{-1,-1,1,1}};
int dy[2][8] = {{-1,1,-2,2,-2,2,-1,1},{-1,1,-1,1}};
int map[501][501];
int f[501][501][2];
int f_f[2] = {8,4};

int main(){
	REP(i,501){
		REP(j,501){
			REP(k,2){
				d[i][j][k] = 100000;
			}
		}
	}
	
	queue<int> q;
	int h, w;
	
	cin >> h >> w;
	
	int g_x, g_y;
	
	REP(i,h){
		REP(j,w){
			char c;
			scanf(" %c", &c);
			
			if (c == 'S'){ d[i][j][0] = 0; d[i][j][1] = 0; f[i][j][0] = 1; f[i][j][1] = 1; q.push(j); q.push(i);}
			if (c == 'G'){ g_x = j; g_y = i;}
			if (c == 'R'){ map[i][j] = 1;}
		}
	}
	
	q.push(0);
	
	while (!q.empty()){
		int x = q.front(); q.pop();
		int y = q.front(); q.pop();
		int flag = q.front(); q.pop();
		int nf;
		
		REP(i,f_f[flag]){
			int x1 = x+dx[flag][i];
			int y1 = y+dy[flag][i];
			
			if (x1 >= 0 && x1 < w && y1 >= 0 && y1 < h){
				if (map[y1][x1] == 1) nf = !flag;
				else nf = flag;
				
				if (f[y1][x1][nf] == 0){
					q.push(x1); q.push(y1);
					
					if (nf) q.push(1);
					else q.push(0);
					
					d[y1][x1][nf] = d[y][x][flag]+1;
					f[y1][x1][nf] = 1;
				}
			}
		}
	}
	
	REP(i,h){
		REP(j,w){
			if (d[i][j][0] == 100000 && d[i][j][1] == 100000){
				d[i][j][0] = -1; d[i][j][1] = -1;
			}
		}
	}
	
	cout << min(d[g_y][g_x][0], d[g_y][g_x][1]) << endl;
	
	return 0;
}
0