CF540 C BFS 水
阿新 • • 發佈:2017-08-29
turn eof pty return its win spa pop 初始
‘.‘->‘X‘
前者走後變成後者,後者除了是終點不能再走。初始位置是X很傻的以為這樣從初始點走出去後初始位置就變成不能走了,實際上是還能走一次的。
其他就是BFS,路上記得把路變成X就好了
太傻了,特記一下
/** @Date : 2017-08-27 19:38:53 * @FileName: C BFS.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h> #define LL long long #define PII pair<int ,int> #define MP(x, y) make_pair((x),(y)) #define fi first #define se second #define PB(x) push_back((x)) #define MMG(x) memset((x), -1,sizeof(x)) #define MMF(x) memset((x),0,sizeof(x)) #define MMI(x) memset((x), INF, sizeof(x)) using namespace std; const int INF = 0x3f3f3f3f; const int N = 1e5+20; const double eps = 1e-8; int n, m; char mp[600][600]; int vis[600][600]; int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; int sx, sy, tx, ty; int bfs(int sx, int sy, int tx, int ty) { queue<PII >q; q.push(MP(sx, sy)); //vis[sx][sy] = 2;//題目自己說起始點是X了 那麽不是不能再走了麽?怎麽又變成說是走完後是X了 while(!q.empty()) { PII nw = q.front(); q.pop(); for(int i = 0; i < 4; i++) { int nx = nw.fi + dir[i][0]; int ny = nw.se + dir[i][1]; if(nx == tx && ny == ty && vis[tx][ty] == 1) return 1; if(!vis[nx][ny] && nx > 0 && ny > 0 && nx <= n && ny <= m) { q.push(MP(nx, ny)); vis[nx][ny]++; } } } return 0; } int main() { while(cin >> n >> m) { MMF(vis); for(int i = 1; i <= n; i++) { scanf("%s", &mp[i][1]); for(int j = 1; j <= m; j++) if(mp[i][j] == ‘X‘) vis[i][j] = 1; } scanf("%d%d", &sx, &sy); scanf("%d%d", &tx, &ty); int ans = bfs(sx, sy, tx, ty); printf("%s\n", ans?"YES":"NO"); } return 0; }
CF540 C BFS 水