【BFS】 P10864 [HBCPC2024] Genshin Impact Startup Forbidden II|普及+

发布于:2025-07-08 ⋅ 阅读:(21) ⋅ 点赞:(0)

本文涉及知识点

C++BFS算法

P10864 [HBCPC2024] Genshin Impact Startup Forbidden II

题目描述

弹窗内容

LeavingZ:你被禁止玩《原神》。

蓝边铅球因LeavingZ的禁止而无法玩《原神》,所以她转向了围棋。

围棋游戏由两名玩家进行,一方使用黑子,另一方使用白子。两名玩家轮流下子,黑子先行。围棋棋盘由 19 × 19 19\times 19 19×19的交叉点组成,我们用 ( x , y ) (x,y) (x,y)表示第 x x x行第 y y y列的交叉点。棋子放置在交叉点上。左上角为 ( 1 , 1 ) (1,1) (1,1),右下角为 ( 19 , 19 ) (19,19) (19,19)

如果 ∣ x 1 − x 2 ∣ + ∣ y 1 − y 2 ∣ = 1 |x_1-x_2| + |y_1-y_2| = 1 x1x2+y1y2=1,那么交叉点 ( x 1 , y 1 ) (x_1,y_1) (x1,y1) ( x 2 , y 2 ) (x_2,y_2) (x2,y2)是相邻的。相邻的交叉点上放置相同颜色的棋子属于同一组棋子。一个棋子的“气”数等于该棋子所在交叉点的相邻交叉点上没有棋子的个数。一组棋子的“气”数等于该组棋子中所有棋子的“气”数之和。一组棋子如果“气”数为零,则被视为“死棋”并且必须从棋盘上移除。

注意,在黑子落子后,优先移除任何死掉的白子,然后重新计算黑子的“气”数。这是因为可能出现这样的情况:黑子落子后,黑白两方的棋子都没有“气”,但移除死掉的白子会增加黑子的“气”。白子落子的处理方式类似。在白子落子后,优先移除任何死掉的黑子,然后重新计算白子的“气”数。

现在有一局围棋,从空棋盘开始,总共进行了 m m m步。给定每步棋子的放置位置,请输出每步棋子落子后,分别有多少颗黑子和白子被移除。显然,黑子在奇数步落子,白子在偶数步落子。保证棋子放置在空的交叉点上。注意,棋子可以放置在 任意 \textbf{任意} 任意当前没有棋子的交叉点上,无论是否违反了现实中的围棋规则 ( 1 ) ^{(1)} (1)

注释:

  • (2):译者补充

输入格式

输入包含 m m m 行( 1 ≤ m ≤ 5 × 1 0 5 1 \le m \le 5\times 10^5 1m5×105),第 i i i 行包含两个整数 x i , y i x_i, y_i xi,yi 1 ≤ x i , y i ≤ 19 1 \le x_i, y_i \le 19 1xi,yi19),表示第 i i i 步在 ( x i , y i ) (x_i, y_i) (xi,yi) 位置放置棋子。

保证棋子放置在当前没有棋子的交叉点上。

输出格式

输出包含 m m m 行,每行包含两个整数。第 i i i 行的第一个整数表示第 i i i 步后被移除的黑子数量,第二个整数表示被移除的白子数量。

翻译者:Immunoglobules

输入输出样例 #1

输入 #1

8
2 1
1 1
1 2
2 2
1 1
1 3
2 3
3 1

输出 #1

0 0
0 0
0 1
0 0
0 0
0 0
0 0
3 0

BFS

性质一:某组棋没死,在(x,y)处下棋后变死棋。说明:a,下棋之前,至少相邻一个空位。b,下棋之后没空位了。故(x,y)就是相邻的棋子。
结论一:以下黑子为例。grid[x][y]=1后,分别枚举grid[x1][y1]为-1的相邻单格是否是死棋,如果是死棋移除。判断grid[x][y]所在连通区域是否是死棋,如果是移除。
时间复杂度:O ( 19 × 19 × M ) ≈ 2 × 1 0 8 (19\times 19 \times M) \approx 2\times 10^8 (19×19×M)2×108,本题时间限制4s。
注意:判断grid[x][y]所在连通区域是否是死棋,必须BFS完。背景色没有棋子,蓝色和绿色是白棋,黑色是黑棋。如果蓝色格子BFS到左边,就提前结束。那绿色就BFS不到蓝色格子(已访问)。
![![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/eaa205e54f974bce87eecbf8e9e508a3.png](https://i-blog.csdnimg.cn/direct/97f52fbbf81e411db4ca2517ae5f3802.png

代码

核心代码

#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>

#include <bitset>
using namespace std;

template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
	in >> pr.first >> pr.second;
	return in;
}

template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
	in >> get<0>(t) >> get<1>(t) >> get<2>(t);
	return in;
}

template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
	in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
	return in;
}

template<class T = int>
vector<T> Read() {
	int n;
	cin >> n;
	vector<T> ret(n);
	for (int i = 0; i < n; i++) {
		cin >> ret[i];
	}
	return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
	vector<T> ret;
	T tmp;
	while (cin >> tmp) {
		ret.emplace_back(tmp);
		if ('\n' == cin.get()) { break; }
	}
	return ret;
}

template<class T = int>
vector<T> Read(int n) {
	vector<T> ret(n);
	for (int i = 0; i < n; i++) {
		cin >> ret[i];
	}
	return ret;
}

template<int N = 1'000'000>
class COutBuff
{
public:
	COutBuff() {
		m_p = puffer;
	}
	template<class T>
	void write(T x) {
		int num[28], sp = 0;
		if (x < 0)
			*m_p++ = '-', x = -x;

		if (!x)
			*m_p++ = 48;

		while (x)
			num[++sp] = x % 10, x /= 10;

		while (sp)
			*m_p++ = num[sp--] + 48;
		AuotToFile();
	}
	void writestr(const char* sz) {
		strcpy(m_p, sz);
		m_p += strlen(sz);
		AuotToFile();
	}
	inline void write(char ch)
	{
		*m_p++ = ch;
		AuotToFile();
	}
	inline void ToFile() {
		fwrite(puffer, 1, m_p - puffer, stdout);
		m_p = puffer;
	}
	~COutBuff() {
		ToFile();
	}
private:
	inline void AuotToFile() {
		if (m_p - puffer > N - 100) {
			ToFile();
		}
	}
	char  puffer[N], * m_p;
};

template<int N = 1'000'000>
class CInBuff
{
public:
	inline CInBuff() {}
	inline CInBuff<N>& operator>>(char& ch) {
		FileToBuf();
		ch = *S++;
		return *this;
	}
	inline CInBuff<N>& operator>>(int& val) {
		FileToBuf();
		int x(0), f(0);
		while (!isdigit(*S))
			f |= (*S++ == '-');
		while (isdigit(*S))
			x = (x << 1) + (x << 3) + (*S++ ^ 48);
		val = f ? -x : x; S++;//忽略空格换行		
		return *this;
	}
	inline CInBuff& operator>>(long long& val) {
		FileToBuf();
		long long x(0); int f(0);
		while (!isdigit(*S))
			f |= (*S++ == '-');
		while (isdigit(*S))
			x = (x << 1) + (x << 3) + (*S++ ^ 48);
		val = f ? -x : x; S++;//忽略空格换行
		return *this;
	}
	template<class T1, class T2>
	inline CInBuff& operator>>(pair<T1, T2>& val) {
		*this >> val.first >> val.second;
		return *this;
	}
	template<class T1, class T2, class T3>
	inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {
		*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
		return *this;
	}
	template<class T1, class T2, class T3, class T4>
	inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {
		*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
		return *this;
	}
	template<class T = int>
	inline CInBuff& operator>>(vector<T>& val) {
		int n;
		*this >> n;
		val.resize(n);
		for (int i = 0; i < n; i++) {
			*this >> val[i];
		}
		return *this;
	}
	template<class T = int>
	vector<T> Read(int n) {
		vector<T> ret(n);
		for (int i = 0; i < n; i++) {
			*this >> ret[i];
		}
		return ret;
	}
	template<class T = int>
	vector<T> Read() {
		vector<T> ret;
		*this >> ret;
		return ret;
	}
private:
	inline void FileToBuf() {
		const int canRead = m_iWritePos - (S - buffer);
		if (canRead >= 100) { return; }
		if (m_bFinish) { return; }
		for (int i = 0; i < canRead; i++)
		{
			buffer[i] = S[i];//memcpy出错			
		}
		m_iWritePos = canRead;
		buffer[m_iWritePos] = 0;
		S = buffer;
		int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
		if (readCnt <= 0) { m_bFinish = true; return; }
		m_iWritePos += readCnt;
		buffer[m_iWritePos] = 0;
		S = buffer;
	}
	int m_iWritePos = 0; bool m_bFinish = false;
	char buffer[N + 10], * S = buffer;
};


class CGrid {
public:
	CGrid(int rCount, int cCount) :m_r(rCount), m_c(cCount) {}
	template<class Fun1, class Fun2>
	vector<vector<pair<int, int>>> NeiBo(Fun1 funVilidCur, Fun2 funVilidNext, int iConnect = 4)
	{
		vector<vector<pair<int, int>>> vNeiBo(m_r * m_c);
		for (int r = 0; r < m_r; r++)
		{
			for (int c = 0; c < m_c; c++)
			{
				if (!funVilidCur(r, c))continue;
				auto& v = vNeiBo[Mask(r, c)];
				if ((r > 0) && funVilidNext(r - 1, c)) {
					v.emplace_back(r - 1, c);
				}
				if ((c > 0) && funVilidNext(r, c - 1)) {
					v.emplace_back(r, c - 1);
				}
				if ((r + 1 < m_r) && funVilidNext(r + 1, c)) {
					v.emplace_back(r + 1, c);
				}
				if ((c + 1 < m_c) && funVilidNext(r, c + 1)) {
					v.emplace_back(r, c + 1);
				}
			}
		}
		return vNeiBo;
	}
	vector<vector<int>> Dis(vector<pair<int, int>> start, std::function<bool(int, int)> funVilidCur, std::function<bool(int, int)> funVilidNext, const int& iConnect = 4) {
		static short dir[8][2] = { {0, 1}, {1, 0}, {-1, 0},{ 0, -1},{1,1},{1,-1},{-1,1},{-1,-1} };
		vector<vector<int>> vDis(m_r, vector<int>(m_c, -1));
		for (const auto& [r, c] : start) {
			vDis[r][c] = 0;
		}
		for (int i = 0; i < start.size(); i++) {
			const auto [r, c] = start[i];
			if (!funVilidCur(r, c)) { continue; }
			for (int k = 0; k < iConnect; k++) {
				const int r1 = r + dir[k][0];
				const int c1 = c + dir[k][1];
				if ((r1 < 0) || (r1 >= m_r) || (c1 < 0) || (c1 >= m_c)) { continue; }
				if (funVilidNext(r1, c1) && (-1 == vDis[r1][c1])) {
					start.emplace_back(r1, c1);
					vDis[r1][c1] = vDis[r][c] + 1;
				}
			}
		}
		return vDis;
	}
	void EnumGrid(std::function<void(int, int)> call)
	{
		for (int r = 0; r < m_r; r++)
		{
			for (int c = 0; c < m_c; c++)
			{
				call(r, c);
			}
		}
	}
	vector<pair<int, int>> GetPos(std::function<bool(int, int)> fun) {
		vector<pair<int, int>> ret;
		for (int r = 0; r < m_r; r++)
		{
			for (int c = 0; c < m_c; c++)
			{
				if (fun(r, c)) {
					ret.emplace_back(r, c);
				}
			}
		}
		return ret;
	}
	inline int Mask(int r, int c) { return  m_c * r + c; }
	const int m_r, m_c;
	const inline static int s_Moves[][2] = { {1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1} };
};

class Solution {
		public:
			vector<pair<int,int>> Ans(vector<pair<int,int>>& ope) {
				CGrid ng(19, 19);
				auto Vilid = [&](int r, int c) {return true; };
				auto neiBo = ng.NeiBo(Vilid, Vilid);
				int grid[19][19] ;//黑子0,白子1,无子-1
				memset(grid, -1, sizeof(grid));
				int vis[19][19] = { 0 };
				auto BFS = [&](int r, int c, const int val) {
					vector<pair<int, int>> v;
					auto Add = [&](int r, int c, const int val)
					{
						if (val != grid[r][c]) { return; }
						if (vis[r][c]) { return; }
						vis[r][c] = 1;
						v.emplace_back(r, c);
					};
					Add(r, c, val);
					bool bDead = true;
					for (int i = 0; i < v.size(); i++) {
						for (auto [r1, c1] : neiBo[ng.Mask(v[i].first, v[i].second)]) {
							if (-1 == grid[r1][c1]) { bDead = false;; }//至少一气
							Add(r1, c1, val);
						}
					}
					if (!bDead) { return 0; }
					for (const auto& [r, c] : v) {
						grid[r][c] = -1;
					}
					return (int)v.size();
				};	
				vector<pair<int, int>> ans;
				for (int i = 0; i < ope.size();i++) {
					auto [r, c] = ope[i];
					r--, c--;
					const int cur = i&1;
					const int other = cur^1;
					grid[r][c] = cur;
					int res[2] = { 0 };
					memset(vis, 0, sizeof(vis));
					for (const auto& [r1, c1] : neiBo[ng.Mask(r, c)]) {
						res[other] += BFS(r1, c1, other);
					}
					memset(vis, 0, sizeof(vis));
					res[cur] += BFS(r, c, cur);
					ans.emplace_back(res[0], res[1]);
				}
				return ans;
			}
			
		};

int main() {
#ifdef _DEBUG
	freopen("a.in", "r", stdin);
#endif // DEBUG
	ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);
	auto ope = Read<pair<int, int>>();
#ifdef _DEBUG
	Out(ope, "ope");
#endif // DEBUG
	auto res = Solution().Ans(ope);
	for (const auto& [i1, i2] : res) {
		cout << i1 << " " << i2 <<  "\n";
	}
	return 0;
}

单元测试

vector < pair<int, int>> ope;
		TEST_METHOD(TestMethod11)
		{
			ope = { {2,1},{1,1},{1,2},{2,2},{1,1},{1,3},{2,3},{3,1} };
			auto res = Solution().Ans(ope);
			AssertV({ {0,0},{0,0},{0,1}, {0,0},{0,0}, {0,0},{0,0},{3,0} }, res);
		}

扩展阅读

我想对大家说的话
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛
失败+反思=成功 成功+反思=成功

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。


网站公告

今日签到

点亮在社区的每一天
去签到