求调:DFS,#3 WA
查看原帖
求调:DFS,#3 WA
692407
_Ralsei_楼主2023/7/6 10:30
#include <iostream>

const int dx[] = { 0, -1, 1, 0, 0, -1, -1, 1, 1 };
const int dy[] = { 0, 0, 0, -1, 1, -1, 1, -1, 1  };

const char word[] = { '0', 'y', 'i', 'z', 'h', 'o', 'n', 'g' };

int n;
char arr[100 + 5][100 + 5];
bool vis[100 + 5][100 + 5];

bool dfs(int x, int y, int cur, int xx, int yy) {
    if (arr[x][y] != word[cur]) { return false; }
    if (cur == 7) { vis[x][y] = true; return true; }

    if (cur == 1) {
        for (int i = 1; i <= 8; i++) {
            if (x + dx[i] >= 1 && x + dx[i] <= n && y + dy[i] >= 1 && y + dy[i] <= n) {
                vis[x][y] |= dfs(x + dx[i], y + dy[i], cur + 1, dx[i], dy[i]);
            }
        }
        return vis[x][y];
    } else {
        if (x + xx < 1 || x + xx > n || y + yy < 1 || y + yy > n) { return false; }
        return vis[x][y] |= dfs(x + xx, y + yy, cur + 1, xx, yy);
    }
}

int main() {
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            std::cin >> arr[i][j];
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            dfs(i, j, 1, 0, 0);
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (!vis[i][j]) {
                std::cout << "*";
            } else {
                std::cout << arr[i][j];
            }
        }
        std::cout << std::endl;
    }

    return 0;
}

2023/7/6 10:30
加载中...