CF540 C BFS 水

'.'->'X'

前者走后变成后者,后者除了是终点不能再走。初始位置是X很傻的以为这样从初始点走出去后初始位置就变成不能走了,实际上是还能走一次的。

其他就是BFS,路上记得把路变成X就好了

太傻了,特记一下

/** @Date    : 2017-08-27 19:38:53
  * @FileName: C BFS.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @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
", ans?"YES":"NO");
	}
    return 0;
}
原文地址:https://www.cnblogs.com/Yumesenya/p/7449976.html