結果

問題 No.367 ナイトの転身
ユーザー kotamanegikotamanegi
提出日時 2016-04-29 23:20:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 711 ms / 2,000 ms
コード長 2,463 bytes
コンパイル時間 647 ms
コンパイル使用メモリ 85,384 KB
実行使用メモリ 243,012 KB
最終ジャッジ日時 2024-04-15 05:56:06
合計ジャッジ時間 2,704 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 62 ms
243,012 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 7 ms
6,944 KB
testcase_07 AC 50 ms
236,520 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 230 ms
78,268 KB
testcase_11 AC 711 ms
236,052 KB
testcase_12 AC 18 ms
21,876 KB
testcase_13 AC 22 ms
38,440 KB
testcase_14 AC 62 ms
56,016 KB
testcase_15 AC 7 ms
29,004 KB
testcase_16 AC 64 ms
65,468 KB
testcase_17 AC 8 ms
24,516 KB
testcase_18 AC 12 ms
29,776 KB
testcase_19 AC 9 ms
22,456 KB
testcase_20 AC 5 ms
12,944 KB
testcase_21 AC 12 ms
30,748 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 3 ms
7,764 KB
testcase_24 AC 4 ms
14,012 KB
testcase_25 AC 4 ms
11,920 KB
testcase_26 AC 3 ms
11,792 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:31:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   31 |         scanf("%d%d", &h, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#define  _SCL_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream> 
#include<map>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cstdint>
#define REP(i,n) for(int i = 0;i < n;++i)
using namespace std;
vector<pair<int, int>> red;
bool dp[2][2000][550][550] = {};
int main() {
	int h, w;
	scanf("%d%d", &h, &w);
	pair<int, int> start, end;
	REP(i, h) {
		string hoge;
		cin >> hoge;
		for (int q = 0;q < w;++q) {
			if (hoge[q] == 'S') {
				start = make_pair(i, q);
			}
			else if (hoge[q] == 'G') {
				end = make_pair(i, q);
			}
			else if (hoge[q] == 'R') {
				red.push_back(make_pair(i, q));
			}
		}
	}
	dp[0][0][start.first][start.second] = true;
	for (int i = 1;i < 1200;++i) {
		for (int q = 0;q < h;++q) {
			for (int j = 0;j < w;++j) {
				if (dp[1][i - 1][q][j] == true) {
					//ミニビショップ存在
					if (q + 1 < h&&j+1 < w) {
						dp[1][i][q + 1][j + 1] = true;
					}
					if (q + 1 < h&&j - 1 > -1) {
						dp[1][i][q + 1][j - 1] = true;
					}
					if (q - 1 > -1 && j - 1 > -1) {
						dp[1][i][q - 1][j - 1] = true;
					}
					if (q - 1 > -1 && j + 1 < w) {
						dp[1][i][q - 1][j + 1] = true;
					}
				}
				if (dp[0][i - 1][q][j] != 0) {
					//ナイト存在
					if (q + 2 < h&&j + 1 < w) {
						dp[0][i][q + 2][j + 1] = true;
					}
					if (q + 1 < h&&j + 2 < w) {
						dp[0][i][q + 1][j + 2] = true;
					}
					if (q - 1 > -1&&j + 2 < w) {
						dp[0][i][q - 1][j + 2] = true;
					}
					if (q - 2 > -1&&j + 1 < w) {
						dp[0][i][q - 2][j + 1] = true;
					}
					if (q - 2 > -1 && j - 1 > -1) {
						dp[0][i][q - 2][j - 1] = true;
					}
					if (q - 1 > -1 && j - 2 > -1) {
						dp[0][i][q - 1][j - 2] = true;
					}
					if (q + 1 < h&&j - 2 > -1) {
						dp[0][i][q + 1][j - 2] = true;
					}
					if (q + 2 < h&&j - 1 > -1) {
						dp[0][i][q + 2][j - 1] = true;
					}
				}
			}
		}
		for (int q = 0;q < red.size();++q) {
			swap(dp[0][i][red[q].first][red[q].second], dp[1][i][red[q].first][red[q].second]);
		}
		if (dp[0][i][end.first][end.second] == true || dp[1][i][end.first][end.second] == true) {
			cout << i << endl;
			return 0;
		}
	}
	cout << -1 << endl;
}
0