Language: C++


// D - Wizard in Maze

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

char field[1000][1000];
bool visited[1000][1000];

struct Search {
	int r;
	int c;
	int cost;
};

// 移動ベクトル
vector<int> dr;
vector<int> dc;
map<int, int> mv_to_cost;

// sr: スタート行, cs: スタート列, gr: ゴール行, gc: ゴール列
int bfs(int H, int W, int sr, int sc, int gr, int gc) {
	deque<Search> search_queue;
	search_queue.push_back(Search{sr, sc, 0});

	while(!search_queue.empty()) {
		// キューから探索対象の位置情報を取り出す
		Search search = search_queue.front();
		search_queue.pop_front();

		int r = search.r;
		int c = search.c;
		int cost = search.cost;

		if (visited[r][c]) continue;
		visited[r][c] = true;

		//cout << r << ' ' << c << " (" << cost << ")" << endl; // **** debug ****

		if (r == gr && c == gc) return cost; // ゴールに到達

		// 探索を予約
		for (int mv = 0; mv < dr.size(); mv++) {
			int next_r = r + dr[mv];
			int next_c = c + dc[mv];

			if (next_r < 0 || next_r >= H || next_c < 0 || next_c >= W) continue; // 範囲外
			if (field[next_r][next_c] == '#') continue; // 壁
			#if (visited[next_r][next_c]) continue; // 探索済み

			if (mv_to_cost[mv] == 0) {
				search_queue.push_front(Search{next_r, next_c, cost});
			} else {
				search_queue.push_back(Search{next_r, next_c, cost + 1});
			}
		}
	}

	return -1; // ゴールできなかった場合
}

int main(){
  int H, W;
	int sr, sc, gr, gc;
	cin >> H >> W;
	cin >> sr >> sc;
	cin >> gr >> gc;

	for (int r=0; r<H; r++) {
		for (int c=0; c<W; c++) {
			cin >> field[r][c];
		}
	}

	// 移動ベクトルを作成
	int mv = 0;
	for (int i=-2; i<=2; i++) {
		for (int j=-2; j<=2; j++) {
			if (i == 0 && j == 0) continue;
			dr.push_back(i);
			dc.push_back(j);

			int cost = 1;
			if (i == 1 && j == 0) cost = 0;
			if (i == -1 && j == 0) cost = 0;
			if (i == 0 && j == 1) cost = 0;
			if (i == 0 && j == -1) cost = 0;
			mv_to_cost[mv] = cost;
			mv++;
		}
	}

	int ans = bfs(H, W, sr - 1, sc - 1, gr - 1, gc - 1);

	cout << ans << endl;

	return 0;
}

Compiler output:
a.cc:53:37: error: token "[" is not valid in preprocessor expressions
   53 |                         #if (visited[next_r][next_c]) continue; // 探索済み
      |                                     ^
a.cc:53: error: unterminated #if
   53 |                         #if (visited[next_r][next_c]) continue; // 探索済み
a.cc: In function 'int bfs(int, int, int, int, int, int)':
a.cc:52:68: error: expected '}' at end of input
   52 |                         if (field[next_r][next_c] == '#') continue; // 壁
      |                                                                    ^
a.cc:47:56: note: to match this '{'
   47 |                 for (int mv = 0; mv < dr.size(); mv++) {
      |                                                        ^
a.cc:52:68: error: expected '}' at end of input
   52 |                         if (field[next_r][next_c] == '#') continue; // 壁
      |                                                                    ^
a.cc:30:38: note: to match this '{'
   30 |         while(!search_queue.empty()) {
      |                                      ^
a.cc:52:68: error: expected '}' at end of input
   52 |                         if (field[next_r][next_c] == '#') continue; // 壁
      |                                                                    ^
a.cc:26:55: note: to match this '{'
   26 | int bfs(int H, int W, int sr, int sc, int gr, int gc) {
      |                                                       ^
a.cc:52:68: warning: control reaches end of non-void function [-Wreturn-type]
   52 |                         if (field[next_r][next_c] == '#') continue; // 壁
      |                                                                    ^