結果

問題 No.367 ナイトの転身
ユーザー FF256grhyFF256grhy
提出日時 2016-04-30 00:25:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,521 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 23,936 KB
実行使用メモリ 11,436 KB
最終ジャッジ日時 2024-04-15 07:54:56
合計ジャッジ時間 1,152 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,836 KB
testcase_01 AC 2 ms
9,836 KB
testcase_02 AC 2 ms
9,708 KB
testcase_03 AC 2 ms
7,788 KB
testcase_04 AC 3 ms
9,704 KB
testcase_05 AC 2 ms
9,832 KB
testcase_06 AC 2 ms
9,948 KB
testcase_07 AC 2 ms
9,836 KB
testcase_08 AC 2 ms
9,832 KB
testcase_09 AC 2 ms
9,832 KB
testcase_10 AC 12 ms
10,720 KB
testcase_11 AC 18 ms
11,436 KB
testcase_12 AC 14 ms
8,456 KB
testcase_13 AC 11 ms
10,488 KB
testcase_14 AC 11 ms
10,496 KB
testcase_15 AC 3 ms
9,960 KB
testcase_16 AC 11 ms
10,472 KB
testcase_17 AC 4 ms
9,888 KB
testcase_18 AC 4 ms
9,768 KB
testcase_19 AC 5 ms
10,024 KB
testcase_20 AC 3 ms
9,904 KB
testcase_21 AC 5 ms
9,960 KB
testcase_22 AC 3 ms
9,836 KB
testcase_23 AC 2 ms
9,848 KB
testcase_24 AC 2 ms
7,804 KB
testcase_25 AC 2 ms
7,684 KB
testcase_26 AC 3 ms
9,840 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:24:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   24 |         scanf("%d%d", &h, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~
main.cpp:25:43: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   25 |         for(int i = 0; i < h; i++) { scanf("%s", str[i]); }
      |                                      ~~~~~^~~~~~~~~~~~~~
main.cpp:69:13: warning: ‘g_j’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   69 |         int b = dp[1][g_i][g_j];
      |             ^
main.cpp:69:13: warning: ‘g_i’ may be used uninitialized in this function [-Wmaybe-uninitialized]
main.cpp:37:22: warning: ‘s_j’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   37 |         queue_j[set] = s_j;
      |         ~~~~~~~~~~~~~^~~~~
main.cpp:36:22: warning: ‘s_i’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   36 |         queue_i[set] = s_i;
      |         ~~~~~~~~~~~~~^~~~~

ソースコード

diff #

#include <stdio.h>

int h, w;
char str[500][501];
int dp[2][500][500];

#define MAX 500 * 500 * 2

int queue_i[MAX];
int queue_j[MAX];
int queue_v[MAX]; // step (+1)
int queue_s[MAX]; // knight : 0, bishop : 1
int set, get;

int next_i[2][8] = { { -2, -2, -1, -1, +1, +1, +2, +2 }, { -1, -1, +1, +1 } };
int next_j[2][8] = { { -1, +1, -2, +2, -2, +2, -1, +1 }, { -1, +1, -1, +1 } };
int next_n[2] = { 8, 4 };

int in(int i, int j) {
	return 0 <= i && i < h && 0 <= j && j < w;
}

int main() {
	scanf("%d%d", &h, &w);
	for(int i = 0; i < h; i++) { scanf("%s", str[i]); }
	
	int s_i, s_j, g_i, g_j;
	for(int i = 0; i < h; i++) {
	for(int j = 0; j < w; j++) {
		if(str[i][j] == 'S') { s_i = i; s_j = j; }
		if(str[i][j] == 'G') { g_i = i; g_j = j; }
	}
	}
	
	dp[0][s_i][s_j] = 1;
	queue_i[set] = s_i;
	queue_j[set] = s_j;
	queue_v[set] = 1;
	queue_s[set] = 0;
	set++;
	
	while(get != set) {
		int i, j, v, s;
		i = queue_i[get];
		j = queue_j[get];
		v = queue_v[get];
		s = queue_s[get];
		
		if(str[i][j] == 'R') { s = (! s); }
		
		for(int k = 0; k < next_n[s]; k++) {
			int ii = i + next_i[s][k];
			int jj = j + next_j[s][k];
			if( in(ii, jj) && dp[s][ii][jj] == 0 ) {
				dp[s][ii][jj] = v + 1;
				queue_i[set] = ii;
				queue_j[set] = jj;
				queue_v[set] = v + 1;
				queue_s[set] = s;
				set++;
			}
		}
		
		get++;
	}
	
	int ans;
	int k = dp[0][g_i][g_j];
	int b = dp[1][g_i][g_j];
	if( k == 0 || b == 0 ) {
		ans = k < b ? b : k;
	} else {
		ans = k < b ? k : b;
	}
	
	printf("%d\n", ans - 1);
	
	return 0;
}
0