社区讨论

为什么我的i还是*,看来好多遍了!

P1101单词方阵参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lq8x7146
此快照首次捕获于
2023/12/17 11:21
2 年前
此快照最后确认于
2023/12/17 14:08
2 年前
查看原帖
CPP
#include<iostream>
#include<vector>
#include<unordered_map>

using namespace std;
const int N = 110;
char a[N][N];
int n;
int dx[8] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[8] = {1, -1, 0, 0, 1, -1, -1, 1};
struct node {
    int x;
    int y;
};
vector<node> q;
bool st[N][N];
unordered_map<char, char> p;

void dfs(int x, int y, int k, char now, int fx, int fy) {
    if (now == 'g') {
        for (int i = 0; i < q.size(); i++) {
            auto t = q[i];
            int nx = t.x;
            int ny = t.y;
            st[nx][ny] = true;
        }
        return;
    }
    if (k == 0) {
        for (int i = 0; i < 8; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && a[nx][ny] == 'i') {
                node temp;
                temp.x = nx;
                temp.y == ny;
                q.push_back(temp);
                dfs(nx, ny, 1, 'i', dx[i], dy[i]);
                q.pop_back();
            }
        }
    } else {
        int nx = x + fx;
        int ny = y + fy;
        if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && a[nx][ny] == p[now]) {
            node temp;
            temp.x = nx;
            temp.y = ny;
            q.push_back(temp);
            dfs(nx, ny, 1, a[nx][ny], fx, fy);
            q.pop_back();
        }
    }

}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            cin >> a[i][j];
    p['y'] = 'i';
    p['i'] = 'z';
    p['z'] = 'h';
    p['h'] = 'o';
    p['o'] = 'n';
    p['n'] = 'g';
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
            if (a[i][j] == 'y') {
                node temp;
                temp.x = i;
                temp.y = j;
                q.push_back(temp);
                dfs(i, j, 0, 'y', 0, 0);
                q.pop_back();
            }
        }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (!st[i][j])
                a[i][j] = '*';
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
            cout << a[i][j];
        cout << endl;
    }


    return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...