CP: Break one wall

A maze has open cells ., walls #, a start S and an exit E. Each move goes up, down, left or right. You may walk through at most one wall cell on your way. Find the fewest moves from S to E.

What to build

Run BFS on states (row, column, used), where used is 0 or 1: whether the wall has been broken yet. Entering a wall is only allowed when used is 0 and changes it to 1. Print the smallest distance to E over both values of used, or -1.

Requirements

  • BFS over states with the extra bit used
  • Check the bounds before reading a cell
  • Print -1 if E cannot be reached

Input

The first line has R and C. Each of the next R lines has C characters.

Output

The fewest moves, or -1.

Constraints

1 ≤ R, C ≤ 20.

Example

Input:

3 4
S#.E
.#..
....

Output:

3

Why it works

The same cell can be reached with or without having used the wall, and these are different situations, so each pair (cell, used) gets its own distance. Every move costs 1, so BFS on these states gives the fewest moves. The number of states is only twice the number of cells.

Examples

Example 1 · Input
3 4
S#.E
.#..
....
Expected output
3
Example 2 · Input
3 3
S#.
###
..E
Expected output
4

Hints

Hint 1

Store the state as one number: id = (r * C + c) * 2 + used. dist has R * C * 2 cells set to -1.

Hint 2

From (r, c, u) go to (nr, nc, u) if the cell is open, or to (nr, nc, 1) if it is a wall and u is 0.

Hint 3

The answer is the smaller non-negative of dist at E with used 0 and used 1.

Solution

Show one correct solution (try it yourself first)
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
    int R, C;
    cin >> R >> C;
    vector<string> grid(R);
    for (auto& row : grid) cin >> row;
    int sr = 0, sc = 0, er = 0, ec = 0;
    for (int r = 0; r < R; r++)
        for (int c = 0; c < C; c++) {
            if (grid[r][c] == 'S') sr = r, sc = c;
            if (grid[r][c] == 'E') er = r, ec = c;
        }
    vector<int> dist(R * C * 2, -1);
    int dr[] = {1, -1, 0, 0}, dc[] = {0, 0, 1, -1};
    queue<int> q;
    int start = (sr * C + sc) * 2;
    dist[start] = 0;
    q.push(start);
    while (!q.empty()) {
        int id = q.front();
        q.pop();
        int used = id % 2, cell = id / 2;
        int r = cell / C, c = cell % C;
        for (int k = 0; k < 4; k++) {
            int nr = r + dr[k], nc = c + dc[k];
            if (nr < 0 || nc < 0 || nr >= R || nc >= C) continue;
            int nu = used;
            if (grid[nr][nc] == '#') {
                if (used == 1) continue;
                nu = 1;
            }
            int nid = (nr * C + nc) * 2 + nu;
            if (dist[nid] != -1) continue;
            dist[nid] = dist[id] + 1;
            q.push(nid);
        }
    }
    int a = dist[(er * C + ec) * 2], b = dist[(er * C + ec) * 2 + 1];
    int answer = -1;
    if (a != -1) answer = a;
    if (b != -1 && (answer == -1 || b < answer)) answer = b;
    cout << answer << "\n";
    return 0;
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Graphs with the STL →