RE on #14,好奇自己什么地方写挂的。
码风可能不太美好。
#include<iostream>
#include<string>
#include<queue>
const int N = 5e2 + 5;
int n, m, s_x, s_y, e_x, e_y;
std::string a[N]; bool vis[N][N];
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
struct Node {int x, y; };
inline bool check1(int x, int y) {return x >= 1 && x <= n && y >= 1 && y <= m; }
inline bool check2(int x, int y) {return check1(x, y) && a[x][y] != '#' && !vis[x][y]; }
inline void push(std::queue<Node>& q, int x, int y) {
q.push((Node){x, y}), vis[x][y] = true;
}
bool solve() {
std::queue<Node> q; push(q, s_x, s_y);
while(!q.empty()) {
Node now = q.front(); q.pop();
for(int i = 0; i < 4; ++i) {
int now_x = now.x + dx[i], now_y = now.y + dy[i];
if(!check2(now_x, now_y)) continue;
if(now_x == e_x && now_y == e_y) return true;
push(q, now_x, now_y);
}
}
return false;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0), std::cout.tie(0);
std::cin >> n >> m;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
std::cin >> a[i][j];
if(a[i][j] == 's') s_x = i, s_y = j;
if(a[i][j] == 'g') e_x = i, e_y = j;
}
}
std::cout << (solve() ? "Yes" : "No");
return 0;
}