感觉思路是对的,但样例过不了
查看原帖
感觉思路是对的,但样例过不了
668891
isbfpoiaewfohqaw楼主2022/10/3 18:39
#include <bits/stdc++.h>
using namespace std;
const char stdname[] = "yizhong";
char c[105][105], d[105][105];
int n;
int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy[] = {1, 0, -1, 1, -1, 1, 0, -1};
struct pa {
    int x, y, z;
    pa(int a, int b, int c) {
        x = a, y = b, z = c;
    }
    pa() {}
    int &operator[](int a) {
        if (a == 0) return x;
        if (a == 1) return y;
        if (a == 2) return z;
        return x;
    }
};
queue<pa> q;
void fill() {
    int cs = 0;
    while (!q.empty()) {
        pa t = q.front();
        q.pop();
        d[t[0]][t[1]] = stdname[t[2]];
        cs++;
    }
    for (int i = 0; i < cs; i++)
        q.push(pa(0, 0, 0));
};
char get(int x, int y) {
    if (x < 0 || y < 0 || x > n || y > n)
        return '\000';
    return c[x][y];
}
void dfs(int x, int y, int where, int ways) {
//    if (ways == 6){
//        if (where == 6){
//            fill();
//            d[x][y] = 'g';
//            return;
//        }
//    }
    if (where == 8) {
        fill();
        return;
    }
    if (ways == -1) {
        for (int fromWays = 0; fromWays < 8; fromWays++) {
            if (get(x + dx[fromWays], y + dy[fromWays]) == stdname[where]) {
                q.push(pa(x, y, where - 1));
                dfs(x + dx[fromWays], y + dy[fromWays], where + 1, fromWays);
                q.pop();
            }
        }
    } else {
        if (get(x + dx[ways], y + dy[ways]) == stdname[where]) {
            q.push(pa(x, y, where - 1));
            dfs(x + dx[ways], y + dy[ways], where + 1, ways);
            q.pop();
        }
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            d[i][j] = '*';
    for (int i = 0; i < n; i++)
        cin >> c[i];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            if (c[i][j] == 'y')
                dfs(i, j, 1, -1);
        }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << d[i][j];
        cout << endl;
    }
    return 0;
}
2022/10/3 18:39
加载中...