#A14. 深搜模板-红与黑-填空-无偏移数组

深搜模板-红与黑-填空-无偏移数组

    #include
    using namespace std;

    int w, h, ans;
    char a[30][30];

    void dfs(int x, int y) {
        if (a[x][y] == {{ input(1) }}) return; // 填空1:判断是否为障碍物
        if (x < {{ input(2) }} || x > h || y < {{ input(3) }} || y > w) return; // 填空2-4:判断是否越界
        a[x][y] = {{ input(5) }}; // 填空5:标记已访问
        ans{{ input(6) }}; // 填空6:更新答案
        dfs(x - 1, y); // 向上搜索
        dfs(x + 1, y); // 向下搜索
        dfs(x, y - 1); // 向左搜索
        dfs(x, y + 1); // 向右搜索
    }

    int main() {
        while (1) {
            int x, y;
            cin >> w >> h;
            if (w == 0 && h == 0) {
                return 0;
            }
            for (int i = 1; i <= h; i++) {
                for (int j = 1; j <= w; j++) {
                    cin >> a[i][j];
                    if (a[i][j] == {{ input(7) }}) { // 填空7:判断起始点
                        x = i, y = j;
                    }
                }
            }
            ans = {{ input(8) }}; // 填空8:初始化答案
            dfs(x, y);
            cout << ans << endl;
            ans = 0;
        }
        return 0;
    }