社区讨论

c++大佬求调!必关!!

P1234小 A 的口头禅参与者 6已保存回复 7

讨论操作

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

当前回复
7 条
当前快照
1 份
快照标识符
@m5uht9ji
此快照首次捕获于
2025/01/13 11:37
去年
此快照最后确认于
2025/11/04 11:41
4 个月前
查看原帖
为什么RE了?
CPP
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1010;
struct stu{
    int x, y;
};
bool vis[N][N];
char a[N][N], k[110];
string step[N][N];
int n, m, ans, dxy[][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
bool check(int x, int y, string s){
    int id = s[s.size() - 1] - 'a';
    if(x >= 1 && x <= n && y >= 1 && y <= m && !vis[x][y] && k[id] == a[x][y])
        return 1;
    return 0;
}
void bfs(int sx, int sy){
    queue <stu> q;
    step[sx][sy] = "h";
    q.push({sx, sy});
    vis[sx][sy] = 1;
    while(!q.empty()){
        int x = q.front().x, y = q.front().y; q.pop();
        if(step[x][y].size() == 4){
            ans ++;
            continue;
        }
        for(int i = 0; i < 4; i++){
            int nx = x + dxy[i][0], ny = y + dxy[i][1];
            if(check(nx, ny, step[x][y])){
                vis[nx][ny] = 1;
                q.push({nx, ny});
                step[nx][ny] = step[x][y] + a[nx][ny];
            }
        }
    }
}

int main(){
    k['h' - 'a'] = 'e', k['e' - 'a'] = 'h';
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++) 
            cin >> a[i][j];
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(a[i][j] == 'h'){
                memset(vis, 0, sizeof vis);
                memset(step, 0, sizeof step);
                bfs(i, j);
            }
    cout << ans << "\n";
    return 0;
}

回复

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

正在加载回复...