結果
| 問題 | No.367 ナイトの転身 | 
| コンテスト | |
| ユーザー |  FF256grhy | 
| 提出日時 | 2016-04-30 00:25:04 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 16 ms / 2,000 ms | 
| コード長 | 1,521 bytes | 
| コンパイル時間 | 189 ms | 
| コンパイル使用メモリ | 23,936 KB | 
| 実行使用メモリ | 11,484 KB | 
| 最終ジャッジ日時 | 2024-10-04 23:34:23 | 
| 合計ジャッジ時間 | 951 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
コンパイルメッセージ
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;
      |         ~~~~~~~~~~~~~^~~~~
            
            ソースコード
#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;
}
            
            
            
        