結果
| 問題 | No.367 ナイトの転身 | 
| コンテスト | |
| ユーザー |  sugim48 | 
| 提出日時 | 2016-04-29 22:38:30 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 54 ms / 2,000 ms | 
| コード長 | 1,795 bytes | 
| コンパイル時間 | 827 ms | 
| コンパイル使用メモリ | 93,428 KB | 
| 実行使用メモリ | 17,408 KB | 
| 最終ジャッジ日時 | 2024-10-04 18:19:23 | 
| 合計ジャッジ時間 | 1,965 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:73:45: warning: ‘tx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   73 |         int ans = min(d[ty][tx][0], d[ty][tx][1]);
      |                                             ^
main.cpp:73:41: warning: ‘ty’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   73 |         int ans = min(d[ty][tx][0], d[ty][tx][1]);
      |                                         ^
main.cpp:40:23: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   40 |         return (y * W + x) * 2 + t;
      |                ~~~~~~~^~~~
main.cpp:46:17: note: ‘sx’ was declared here
   46 |         int sy, sx, ty, tx;
      |                 ^~
main.cpp:40:19: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   40 |         return (y * W + x) * 2 + t;
      |                 ~~^~~
main.cpp:46:13: note: ‘sy’ was declared here
   46 |         int sy, sx, ty, tx;
      |             ^~
            
            ソースコード
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int dy[2][8] = {{-1, -2, -2, -1, 1, 2, 2, 1}, {-1, -1, 1, 1, -1, -1, 1, 1}};
int dx[2][8] = {{-2, -1, 1, 2, 2, 1, -1, -2}, {-1, 1, 1, -1, -1, 1, 1, -1}};
int H, W;
int f(int y, int x, int t) {
	return (y * W + x) * 2 + t;
}
int main() {
	cin >> H >> W;
	vector<string> a(H);
	int sy, sx, ty, tx;
	for (int y = 0; y < H; y++) {
		cin >> a[y];
		for (int x = 0; x < W; x++) {
			if (a[y][x] == 'S') sy = y, sx = x, a[y][x] = '.';
			if (a[y][x] == 'G') ty = y, tx = x, a[y][x] = '.';
		}
	}
	vector<vector<vector<int> > > d(H, vector<vector<int> >(W, vector<int>(2, INT_MAX)));
	d[sy][sx][0] = 0;
	queue<int> q;
	q.push(f(sy, sx, 0));
	while (!q.empty()) {
		int z = q.front(); q.pop();
		int y = z / 2 / W, x = z / 2 % W, t = z % 2;
		for (int k = 0; k < 8; k++) {
			int _y = y + dy[t][k], _x = x + dx[t][k];
			if (_y >= 0 && _y < H && _x >= 0 && _x < W) {
				int _t = t;
				if (a[_y][_x] == 'R') _t ^= 1;
				if (d[_y][_x][_t] > d[y][x][t] + 1) {
					d[_y][_x][_t] = d[y][x][t] + 1;
					q.push(f(_y, _x, _t));
				}
			}
		}
	}
	int ans = min(d[ty][tx][0], d[ty][tx][1]);
	if (ans == INT_MAX) ans = -1;
	cout << ans << endl;
}
            
            
            
        