提出 #61382370


ソースコード 拡げる

#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <cstring>

using namespace std;

const int dx[2][4] = {{-1, 1, 0, 0}, {0, 0, -1, 1}}; // Direction: vertical, horizontal
const int dy[2][4] = {{0, 0, -1, 1}, {-1, 1, 0, 0}}; // Alternating directions
const int INF = 1e9;

int H, W;
vector<string> grid;

int bfs(int startX, int startY, int goalX, int goalY) {
    // Queue for BFS: (x, y, moves, last direction)
    queue<tuple<int, int, int, int>> q;
    bool visited[1000][1000][2]; // Visited array for (x, y, direction)
    memset(visited, false, sizeof(visited));

    // Push the start position
    q.emplace(startX, startY, 0, 0); // Vertical start
    q.emplace(startX, startY, 0, 1); // Horizontal start
    visited[startX][startY][0] = true;
    visited[startX][startY][1] = true;

    while (!q.empty()) {
        auto [x, y, moves, dir] = q.front();
        q.pop();

        // If we reach the goal, return the moves
        if (x == goalX && y == goalY) {
            return moves;
        }

        // Alternate direction for the next move
        int nextDir = 1 - dir;

        // Explore neighbors in the allowed direction
        for (int i = 0; i < 4; ++i) {
            int nx = x + dx[nextDir][i];
            int ny = y + dy[nextDir][i];

            // Check bounds and obstacles
            if (nx >= 0 && nx < H && ny >= 0 && ny < W && grid[nx][ny] != '#') {
                if (!visited[nx][ny][nextDir]) {
                    visited[nx][ny][nextDir] = true;
                    q.emplace(nx, ny, moves + 1, nextDir);
                }
            }
        }
    }

    // If the goal is not reachable
    return -1;
}

int main() {
    // Input
    cin >> H >> W;
    grid.resize(H);

    int startX = -1, startY = -1, goalX = -1, goalY = -1;

    for (int i = 0; i < H; ++i) {
        cin >> grid[i];
        for (int j = 0; j < W; ++j) {
            if (grid[i][j] == 'S') {
                startX = i;
                startY = j;
            } else if (grid[i][j] == 'G') {
                goalX = i;
                goalY = j;
            }
        }
    }

    // Perform BFS
    int result = bfs(startX, startY, goalX, goalY);

    // Output the result
    cout << result << endl;

    return 0;
}

提出情報

提出日時
問題 D - Snaky Walk
ユーザ Darkerkiller
言語 C++ 20 (gcc 12.2)
得点 0
コード長 2352 Byte
結果 WA
実行時間 68 ms
メモリ 7560 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 400
結果
WA × 3
AC × 8
WA × 49
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 04_random4_00.txt, 04_random4_01.txt, 04_random4_02.txt, 04_random4_03.txt, 05_handmade_00.txt, 05_handmade_01.txt, 05_handmade_02.txt, 05_handmade_03.txt, 05_handmade_04.txt, 05_handmade_05.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt WA 2 ms 5416 KiB
00_sample_01.txt WA 2 ms 5456 KiB
00_sample_02.txt WA 2 ms 5608 KiB
01_random_00.txt WA 8 ms 5912 KiB
01_random_01.txt WA 3 ms 5440 KiB
01_random_02.txt AC 12 ms 6432 KiB
01_random_03.txt AC 5 ms 5840 KiB
01_random_04.txt AC 10 ms 6316 KiB
01_random_05.txt AC 2 ms 5460 KiB
01_random_06.txt WA 2 ms 5412 KiB
01_random_07.txt AC 2 ms 5420 KiB
01_random_08.txt WA 26 ms 7396 KiB
01_random_09.txt WA 38 ms 7368 KiB
01_random_10.txt WA 51 ms 7368 KiB
01_random_11.txt WA 21 ms 7364 KiB
01_random_12.txt WA 30 ms 7428 KiB
01_random_13.txt WA 40 ms 7388 KiB
01_random_14.txt WA 28 ms 7336 KiB
01_random_15.txt WA 68 ms 7372 KiB
01_random_16.txt WA 62 ms 7272 KiB
01_random_17.txt WA 47 ms 7428 KiB
01_random_18.txt WA 54 ms 7404 KiB
01_random_19.txt WA 22 ms 7420 KiB
02_random2_00.txt WA 14 ms 7308 KiB
02_random2_01.txt WA 14 ms 7344 KiB
02_random2_02.txt WA 14 ms 7528 KiB
02_random2_03.txt WA 14 ms 7372 KiB
02_random2_04.txt WA 15 ms 7344 KiB
02_random2_05.txt WA 14 ms 7316 KiB
02_random2_06.txt WA 14 ms 7344 KiB
02_random2_07.txt WA 15 ms 7320 KiB
02_random2_08.txt WA 14 ms 7444 KiB
02_random2_09.txt WA 14 ms 7348 KiB
02_random2_10.txt WA 14 ms 7376 KiB
02_random2_11.txt WA 14 ms 7532 KiB
02_random2_12.txt WA 14 ms 7308 KiB
02_random2_13.txt WA 15 ms 7372 KiB
02_random2_14.txt WA 14 ms 7336 KiB
02_random2_15.txt WA 15 ms 7324 KiB
02_random2_16.txt WA 14 ms 7308 KiB
02_random2_17.txt WA 14 ms 7448 KiB
02_random2_18.txt AC 15 ms 7244 KiB
02_random2_19.txt WA 14 ms 7380 KiB
03_random3_00.txt WA 36 ms 7352 KiB
03_random3_01.txt WA 35 ms 7412 KiB
03_random3_02.txt WA 36 ms 7544 KiB
03_random3_03.txt WA 35 ms 7320 KiB
04_random4_00.txt WA 39 ms 7348 KiB
04_random4_01.txt WA 39 ms 7412 KiB
04_random4_02.txt WA 38 ms 7380 KiB
04_random4_03.txt WA 38 ms 7340 KiB
05_handmade_00.txt WA 41 ms 7560 KiB
05_handmade_01.txt AC 41 ms 7392 KiB
05_handmade_02.txt AC 2 ms 5456 KiB
05_handmade_03.txt WA 2 ms 5448 KiB
05_handmade_04.txt WA 2 ms 5612 KiB
05_handmade_05.txt WA 33 ms 7404 KiB