20 pts,求助,悬关 !!!
查看原帖
20 pts,求助,悬关 !!!
842408
pangyuchen75楼主2024/2/29 17:45

我的提交记录

Code:Code:

#include <cstdio>
#include <map>
#include <queue>
#define N 10005
using namespace std;

const int xx[4] = {-1, 1, 0, 0};
const int yy[4] = {0, 0, -1, 1};

int n, m;
char ch;
int be_x, be_y;
bool can[N][N];

struct Node {
	int x, y;
	
	Node(int _x, int _y) {
		x = _x;
		y = _y;
	}
};

queue <Node> q;

bool bfs(int be_x, int be_y) {
	map <int, map <int, bool> > vis;
	
	while(!q.empty())
		q.pop();
	
	vis[be_x][be_y] = true;
	q.push(Node(be_x, be_y));
	
	while(!q.empty()) {
		Node top = q.front();
		q.pop();
		
		if(top.x < -n || top.x > 2 * n - 2 || top.y < -m || top.y > 2 * m - 2)
			return true;
		
		for(int i = 0; i < 4; ++i) {
			int x1 = top.x + xx[i];
			int y1 = top.y + yy[i];
			
			if(vis[x1][y1])
				continue;
			
			if(!can[x1 % n][y1 % m])
				continue;
			
			vis[x1][y1] = true;
			q.push(Node(x1, y1));
		}
	}
	
	return false;
}

int main() {
	while(scanf("%d%d", &n, &m) != EOF) {
		for(int i = 0; i < n; ++i) {
			scanf("\n");
			for(int j = 0; j < m; ++j) {
				scanf("%c", &ch);
				if(ch == '.')
					can[i][j] = true;
				else if(ch == '#')
					can[i][j] = false;
				else {
					can[i][j] = true;
					be_x = i, be_y = j;
				}
			}
		}
		
		puts(bfs(be_x, be_y) ? "Yes" : "No");
	}
	
	return 0;
}
2024/2/29 17:45
加载中...