社区讨论

竞选本题最长代码。

P1363幻象迷宫参与者 4已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@mjlld6p2
此快照首次捕获于
2025/12/25 23:22
2 个月前
此快照最后确认于
2025/12/26 06:30
2 个月前
查看原帖
我也竞选本题最长代码。这是孩子刷的第一道绿题,发现绿题好像都是一些奇技淫巧,自己费劲脑子想出来的代码,还是下了两个样例,补上了一些情况的处理,才过的。以至于我对自己写出来的代码的正确性已经不自信了:( 开始畏惧绿难了啊啊啊啊
CPP
#include<iostream>
#include<vector>
#include<deque>
#include<map>
using namespace std;

void paint(vector<vector<char>>& s, vector<vector<bool>>& v, vector<pair<int, int>>& dian, map<pair<int, int>, bool>& shi, int x, int y, int n, int m)
{
	vector<vector<int>>dir = { {1,0},{-1,0},{0,1},{0,-1} };
	deque<pair<int, int>>d;
	v[x][y] = 1;
	d.push_back({ x,y });
	if (x == 0 || x == n - 1 || y == 0 || y == m - 1)
	{
		dian.push_back({ x,y });
		shi[{x, y}] = 1;
	}
	while (!d.empty())
	{
		pair<int, int>p = d.front();
		d.pop_front();

		for (int i = 0; i < 4; i++)
		{
			int a = (p.first + dir[i][0] + n) % n;
			int b = (p.second + dir[i][1] + m) % m;
			if (s[a][b] == '.')
			{
				if (!v[a][b])
				{
					v[a][b] = 1;
					d.push_back({ a,b });
					if (a == 0 || a == n - 1 || b == 0 || b == m - 1)
					{
						dian.push_back({ a,b });
						shi[{a, b}] = 1;
					}
				}
			}
		}
	}
}

bool find(vector<vector<char>>& s, map<pair<int, int>, bool>& shi, deque<vector<int>>& bian, int x, int y, int nowx, int nowy, int dstx, int dsty, int n, int m)
{
	if (nowx == 0 && nowy == 0)
		return false;
	vector<vector<bool>>v(n, vector<bool>(m, 0));
	vector<vector<int>>dir = { {1,0},{-1,0},{0,1},{0,-1} };
	deque<pair<int, int>>d;
	v[x][y] = 1;
	d.push_back({ x,y });
	if (x == dstx && y == dsty)
	{
		if (nowx != 0 || nowy != 0)
		{
			return true;
		}
	}
	else
	{
		if (x == 0)
		{
			if (s[n - 1][y] == '.')
			{
				if (n - 1 == dstx && y == dsty)
				{
					if (nowx - 1 != 0 || nowy != 0)
					{
						return true;
					}
				}
				else
				{
					if (shi[{n - 1, y}] == 1)
					{
						shi[{n - 1, y}] = 0;
						bian.push_back({ n - 1,y,nowx - 1,nowy });
					}
				}
			}
		}
		if (x == n - 1)
		{
			if (s[0][y] == '.')
			{
				if (0 == dstx && y == dsty)
				{
					if (nowx + 1 != 0 || nowy != 0)
					{
						return true;
					}
				}
				else
				{
					if (shi[{0, y}] == 1)
					{
						shi[{0, y}] = 0;
						bian.push_back({ 0,y,nowx + 1,nowy });
					}
				}
			}
		}
		if (y == 0)
		{
			if (x == dstx && m - 1 == dsty)
			{
				if (nowx != 0 || nowy - 1 != 0)
				{
					return true;
				}
			}
			else
			{
				if (s[x][m - 1] == '.')
				{
					if (shi[{x, m - 1}] == 1)
					{
						shi[{x, m - 1}] = 0;
						bian.push_back({ x,m - 1,nowx,nowy - 1 });
					}
				}
			}
		}
		if (y == m - 1)
		{
			if (x == dstx && 0 == dsty)
			{
				if (nowx != 0 || nowy + 1 != 0)
				{
					return true;
				}
			}
			else
			{
				if (s[x][0] == '.')
				{
					if (shi[{x, 0}] == 1)
					{
						shi[{x, 0}] = 0;
						bian.push_back({ x,0,nowx,nowy + 1 });
					}
				}
			}
		}
	}
	while (!d.empty())
	{
		pair<int, int>p = d.front();
		d.pop_front();

		for (int i = 0; i < 4; i++)
		{
			int a = (p.first + dir[i][0]);
			int b = (p.second + dir[i][1]);
			if (a >= 0 && a < n && b >= 0 && b < m)
			{
				if (s[a][b] == '.')
				{
					if (!v[a][b])
					{
						v[a][b] = 1;
						d.push_back({ a,b });
						if (a == 0 || a == n - 1 || b == 0 || b == m - 1)
						{
							if (a == dstx && b == dsty)
							{
								if (nowx != 0 || nowy != 0)
								{
									return true;
								}
							}
							else
							{
								if (shi[{a, b}] == 1)
								{
									shi[{a, b}] = 0;
									if (a == 0)
									{
										if (s[n - 1][b] == '.')
										{
											if (shi[{n - 1, b}] == 1)
											{
												shi[{n - 1, b}] = 0;
												bian.push_back({ n - 1,b,nowx - 1,nowy });
											}
										}
									}
									if (a == n - 1)
									{
										if (s[0][b] == '.')
										{
											if (shi[{0, b}] == 1)
											{
												shi[{0, b}] = 0;
												bian.push_back({ 0,b,nowx + 1,nowy });
											}
										}
									}
									if (b == 0)
									{
										if (s[a][m - 1] == '.')
										{
											if (shi[{a, m - 1}] == 1)
											{
												shi[{a, m - 1}] = 0;
												bian.push_back({ a,m - 1,nowx,nowy - 1 });
											}
										}
									}
									if (b == m - 1)
									{
										if (s[a][0] == '.')
										{
											if (shi[{a, 0}] == 1)
											{
												shi[{a, 0}] = 0;
												bian.push_back({ a,0,nowx,nowy + 1 });
											}
										}
									}

								}
							}
						}
					}
				}
			}
		}
	}
	return false;
}

bool way(vector<vector<char>>& s, vector<pair<int, int>>& dian, map<pair<int, int>, bool>& si, int n, int m)
{
	for (int i = 0; i < dian.size(); i++)
	{
		map<pair<int, int>, bool> shi(si);
		int x = dian[i].first;
		int y = dian[i].second;
		deque<vector<int>>bian;
		if (x == 0)
		{
			if (s[n - 1][y] == '.')
			{
				bian.push_back({ n - 1,y,-1,0 });
				shi[{n - 1, y}] = 0;
			}
		}
		if (x == n - 1)
		{
			if (s[0][y] == '.')
			{
				bian.push_back({ 0,y ,1,0 });
				shi[{0, y}] = 0;
			}
		}
		if (y == 0)
		{
			if (s[x][m - 1] == '.')
			{
				bian.push_back({ x,m - 1 ,0,-1 });
				shi[{x, m - 1}] = 0;
			}
		}
		if (y == m - 1)
		{
			if (s[x][0] == '.')
			{
				bian.push_back({ x,0 ,0,1 });
				shi[{x, 0}] = 0;
			}
		}
		while (!bian.empty())
		{
			vector<int>temp = bian.front();
			bian.pop_front();
			int x_ = temp[0];
			int y_ = temp[1];
			int nowx = temp[2];
			int nowy = temp[3];
			if (find(s, shi, bian, x_, y_, nowx, nowy, x, y, n, m))
			{
				return true;
			}
		}
	}
	return false;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n, m;
	while (cin >> n >> m)
	{
		vector<vector<char>>s(n, vector<char>(m));
		int x, y;

		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				char p;
				cin >> p;
				if (p == 'S')
				{
					x = i;
					y = j;
					p = '.';
				}
				s[i][j] = p;
			}
		}

		vector<vector<bool>>v(n, vector<bool>(m, 0));
		vector<pair<int, int>> dian;
		map<pair<int, int>, bool>shi;
		paint(s, v, dian, shi, x, y, n, m);
		if (way(s, dian, shi, n, m))
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
}

回复

5 条回复,欢迎继续交流。

正在加载回复...