結果

問題 No.367 ナイトの転身
ユーザー たこしたこし
提出日時 2016-06-07 00:09:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 2,214 bytes
コンパイル時間 893 ms
コンパイル使用メモリ 95,376 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2024-04-17 05:25:48
合計ジャッジ時間 1,811 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,376 KB
testcase_01 AC 3 ms
5,504 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,888 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,504 KB
testcase_10 AC 29 ms
5,888 KB
testcase_11 AC 37 ms
5,760 KB
testcase_12 AC 10 ms
5,504 KB
testcase_13 AC 10 ms
5,632 KB
testcase_14 AC 15 ms
5,632 KB
testcase_15 AC 3 ms
5,504 KB
testcase_16 AC 13 ms
5,504 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 4 ms
5,632 KB
testcase_21 AC 6 ms
5,504 KB
testcase_22 AC 4 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>
#include <complex>
  
#define INF 100000000
#define mod7 10000000007
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL_MAX 9223372036854775807
#define INF_LL 9223372036854775
#define EPS 1e-10
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double 
 
using namespace std;

#define MAX_H 505
#define MAX_W 505

int H, W;
int memo[MAX_H][MAX_W][2];

char field[MAX_H][MAX_W];
int Sx, Sy;

void input(){
  cin >> H >> W;
  for(int i = 0; i < H ; i++){
    for(int j = 0; j < W; j++){
      cin >> field[i][j];
      if(field[i][j] == 'S'){
	Sx = j; Sy = i;
      }
    }
  }
}

struct sStatus{
  int job;
  int x,y;
  sStatus(int job, int x, int y) : job(job), x(x), y(y){}
};

bool isInField(int x, int y){
  return 0 <= x && x < W && 0 <= y && y < H;
}

int dx0[] = {-2,-1,1,2,-2,-1,1,2};
int dy0[] = {-1,-2,-2,-1,1,2,2,1};

int dx1[] = {-1,1,-1,1};
int dy1[] = {-1,-1,1,1};

int solve(){

  fill((int*)memo, (int*)memo + MAX_H*MAX_W*2, INF);
  queue<sStatus> que;
  que.push(sStatus(0, Sx, Sy));
  memo[Sy][Sx][0] = 0;
  
  while(que.size()){

    sStatus st = que.front();
    que.pop();

    if(field[st.y][st.x] == 'G')
      return memo[st.y][st.x][st.job];

    for(int d = 0; d < (st.job == 0 ? 8 : 4); d++){
      int nx, ny;
      if(st.job == 0){
	nx = st.x + dx0[d];
	ny = st.y + dy0[d];
      }
      else{
	nx = st.x + dx1[d];
	ny = st.y + dy1[d];
      }
      if(isInField(nx,ny)){
	int nextJob = st.job;
	if(field[ny][nx] == 'R'){
	  nextJob = (nextJob+1)%2;
	}
	if(memo[ny][nx][nextJob] == INF){
	  memo[ny][nx][nextJob] = memo[st.y][st.x][st.job] + 1;
	  que.push(sStatus(nextJob, nx, ny));
	}
      }
    }
    
  }

  return -1;

}

int main(){

  input();

  cout << solve() << endl;

  return 0;

}
0