acgo题库
  • 首页
  • 题库
  • 学习
  • 天梯
  • 备赛

    竞赛

    • CSP-J/S
    • 蓝桥杯

    考级

    • GESP
    • CPA
    • 电子学会考级
  • 资讯
  • 竞赛
  • 讨论
  • 团队
  • 商城
登录
注册
题目详情提交记录(0)
  • 题解

    #include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; vector<string> g(N); long long sx = 0, sy = 0; for (int i = 0; i < N; i++) { cin >> g[i]; for (int j = 0; j < M; j++) if (g[i][j] == 'S') { sx = i; sy = j; } } vector<vector<long long>> ox(N, vector<long long>(M, LLONG_MAX)); vector<vector<long long>> oy(N, vector<long long>(M, LLONG_MAX)); queue<pair<long long,long long>> q; q.push({sx, sy}); ox[sx][sy] = sx; oy[sx][sy] = sy; int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}; bool yes = false; while (!q.empty()) { auto [x, y] = q.front(); q.pop(); int mx = (int)((x % N + N) % N), my = (int)((y % M + M) % M); for (int d = 0; d < 4; d++) { long long nx = x + dx[d], ny = y + dy[d]; int nmx = (int)((nx % N + N) % N), nmy = (int)((ny % M + M) % M); if (g[nmx][nmy] == '#') continue; if (ox[nmx][nmy] != LLONG_MAX) { // 已访问过该模位置 if (abs(nx - ox[nmx][nmy]) >= N || abs(ny - oy[nmx][nmy]) >= M) { yes = true; break; } } else { ox[nmx][nmy] = nx; oy[nmx][nmy] = ny; q.push({nx, ny}); } } if (yes) break; } cout << (yes ? "Yes" : "No") << endl; return 0; }

    userId_undefined
    未知
    模拟·模拟练习生倔强青铜冒泡宗师→排序元老
    2阅读
    0回复
    0点赞
暂无数据

提交答案之后,这里将显示提交结果~

首页