結果

問題 No.367 ナイトの転身
ユーザー machibito2machibito2
提出日時 2016-05-15 00:45:45
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,436 bytes
コンパイル時間 593 ms
コンパイル使用メモリ 66,364 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 21:37:17
合計ジャッジ時間 3,714 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 WA -
testcase_03 AC 2 ms
6,940 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 6 ms
6,940 KB
testcase_13 AC 11 ms
6,944 KB
testcase_14 RE -
testcase_15 WA -
testcase_16 RE -
testcase_17 AC 5 ms
6,940 KB
testcase_18 RE -
testcase_19 AC 5 ms
6,940 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 6 ms
6,944 KB
testcase_22 RE -
testcase_23 RE -
testcase_24 WA -
testcase_25 AC 2 ms
6,940 KB
testcase_26 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void nyuryoku()’:
main.cpp:32:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   32 |                 scanf("%s",in[i]);
      |                 ~~~~~^~~~~~~~~~~~

ソースコード

diff #

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

char in[500][500];
int h,w,masu[500][500] = {};
struct joutai
{
	int x;
	int y;
	int nb;
	int nanho;
};

int check[500][500][2] = {};

queue<joutai> Q;
joutai saisho;
int goalx,goaly;
int ans = -1;
int ndx[8] = {1,2,2,1,-1,-2,-2,-1};
int ndy[8] = {-2,-1,1,2,2,1,-1,-2};
int bdx[4] = {1,1,-1,-1};
int bdy[4] = {-1,1,1,-1};

void nyuryoku()
{
	cin >> h >> w;
	for (int i = 0; i < h; i++)
	{
		scanf("%s",in[i]);
		for (int j = 0; j < w; j++)
		{	
			if(in[i][j] == 'S')
			{
				masu[i][j] = 1;
				saisho.x = i;
				saisho.y = j;
				saisho.nb = 1;
				saisho.nanho = 0;
			}
			else if(in[i][j] == 'G')
			{
				masu[i][j] = 2;
				goalx = i;
				goaly = j;
			}
			else if(in[i][j] == 'R')
			{
				masu[i][j] = 3;
			}
			else
			{
				masu[i][j] = 4;
			}
		}
	}
}

void keisan()
{
	Q.push(saisho);
	while(1)
	{
		if(Q.size() == 0)
		{
			break;
		}
		joutai miru = Q.front();
		Q.pop();	
		if(check[miru.x][miru.y][miru.nb] == 1)
		{
			continue;
		}
		check[miru.x][miru.y][miru.nb] = 1;
		joutai dainyu;
		if(miru.x == goalx && miru.y == goaly)
		{
			ans = miru.nanho;
			break;
		}
		if(miru.nb == 1)
		{
			for (int i = 0; i < 8; i++)
			{
				if(masu[miru.x + ndx[i]][miru.y + ndy[i]] == 4 ||
				masu[miru.x + ndx[i]][miru.y + ndy[i]] == 3 ||
				masu[miru.x + ndx[i]][miru.y + ndy[i]] == 2 ||
				masu[miru.x + ndx[i]][miru.y + ndy[i]] == 1 ||
				(miru.x + ndx[i] < 500) || (miru.y + ndy[i] < 500))
				{
					if(masu[miru.x + ndx[i]][miru.y + ndy[i]] == 3)
					{
						dainyu.nb = 2;
					}
					else
					{
						dainyu.nb = 1;
					}
					dainyu.x = miru.x + ndx[i];
					dainyu.y = miru.y + ndy[i];
					dainyu.nanho = miru.nanho + 1;
					Q.push(dainyu);
				}
			}
		}
		else if(miru.nb == 2)
		{
			for (int i = 0; i < 4; i++)
			{
				if(masu[miru.x + bdx[i]][miru.y + bdy[i]] == 4 ||
				masu[miru.x + bdx[i]][miru.y + bdy[i]] == 3 ||
				masu[miru.x + bdx[i]][miru.y + bdy[i]] == 2 ||
				masu[miru.x + bdx[i]][miru.y + bdy[i]] == 1 ||
				(miru.x + bdx[i] < 500) || (miru.y + bdy[i] < 500))
				{
					if(masu[miru.x + bdx[i]][miru.y + bdy[i]] == 3)
					{
						dainyu.nb = 1;
					}
					else
					{
						dainyu.nb = 2;
					}
					dainyu.x = miru.x + bdx[i];
					dainyu.y = miru.y + bdy[i];
					dainyu.nanho = miru.nanho + 1;
					Q.push(dainyu);
				}
			}
		}
	}
}

int main()
{
	nyuryoku();
	keisan();
	cout << ans << endl;
}
0