跳到正文

个人记录

MADE BY FKXR

fkxr约 12 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 12 分钟

版本

  1. v1当前
C++
//Do not hack it
#include <bits/stdc++.h>
#include <conio.h>
#define endl cerr<<"------------------I Love Sqrt Decomposition------------------\n";
using namespace std;
#ifdef __linux__
#define Gc getchar_unlocked
#define Pc putchar_unlocked
#endif

#ifndef __linux__
#define Gc getchar
#define Pc putchar
#endif

#define ds(x) (x=='\r'||x=='\n'||x==' ')
namespace g {
	template<typename T> inline void r(T &a) {
		a = 0;
		char ch = Gc();
		bool ok = 0;
		for (; ch < '0' || ch > '9';) {
			ok ^= (ch == '-');
			ch = Gc();
		}
		for (; ch >= '0' && ch <= '9';) {
			a = (a << 1) + (a << 3) + (ch ^ 48);
			ch = Gc();
		}
		if (ok)
			a = -a;
	}
	template<typename T> inline void w(T a) {
		if (a == 0) {
			Pc('0');
			return;
		}
		static char ch[20];
		int till = 0;
		if (a < 0) {
			Pc('-');
			for (; a;) {
				ch[till++] = -(a % 10);
				a /= 10;
			}
		} else {
			for (; a;) {
				ch[till++] = a % 10;
				a /= 10;
			}
		}
		for (; till;)
			Pc(ch[--till] ^ 48);
	}
	struct Srr {
		Srr operator>>(int &a) {
			r(a);
			return{};
		}
		Srr operator>>(char &ch) {
			ch = Gc();
			for (; ds(ch);) {
				ch = Gc();
			}
			return{};
		}
		Srr operator>>(string &s) {
			s = "";
			char ch = Gc();
			for (; ds(ch);) {
				ch = Gc();
			}
			for (; !(ds(ch) || ch == EOF);) {
				s.push_back(ch);
				ch = Gc();
			}
			return {};
		}
		template<typename T> Srr operator<<(T &a) {
			r(a);
			return{};
		}
		void is(string &s, int T) {
			s = "";
			char ch = Gc();
			for (; ds(ch);) {
				ch = Gc();
			}
			for (; T--;) {
				s.push_back(ch);
				ch = Gc();
			}
		}
	} in;
	struct Sww {
		Sww operator<<(const int a) {
			w(a);
			return{};
		}
		Sww operator<<(const char ch) {
			Pc(ch);
			return{};
		}
		Sww operator<<(const string s) {
			for (int i = 0; i < s.size(); i++) {
				Pc(s[i]);
			}
			return{};
		}
		template<typename T> Sww operator>>(const T a) {
			w(a);
			return{};
		}
	} out;
	namespace __STL {
		const bool __is_P[] = { 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 };
		inline bool IP(const int a) {
			if (a <= 29) {
				return __is_P[a];
			}
			if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0) {
				return 0;
			}
			for (int i = 6;; i += 6) {
				if (((i + 1) * (i + 1)) > a) {
					return 1;
				}
				if (a % (i + 1) == 0) {
					return 0;
				}
				if (((i + 5) * (i + 5)) > a) {
					return 1;
				}
				if (a % (i + 5) == 0) {
					return 0;
				}
			}
		}
		inline int power(int a, int b, const int mod = -1) {
			int ans = 1;
			if (mod == -1) {
				for (; b;) {
					if (b & 1) {
						ans *= a;
					}
					b >>= 1;
					a *= a;
				}
				return ans;
			}
			for (; b;) {
				if (b & 1) {
					ans = ans * a % mod;
				}
				b >>= 1;
				a = a * a % mod;
			}
			return ans;
		}
	}
} using g::in;
using g::out;
using namespace g::__STL;
#define eout cerr
#define g getch()
int a[15][15];
int l[15][15];
bool vis[15][15];
bool bj[15][15];
int n;
int sd;

void init(int z) {
	memset(l, 0, sizeof(l));
	memset(vis, 0, sizeof(vis));
	memset(bj, 0, sizeof(bj));
	memset(a, 0, sizeof(a));
	mt19937 rd(sd);
	sd = rd();
	int cnt = 0;
	for (; cnt < z;) {
		int x = rd() % n + 1;
		int y = rd() % n + 1;
		if (!l[x][y]) {
			l[x][y] = 1;
			++cnt;
		}
	}
	for (int x = 1; x <= n; x++) {
		for (int y = 1; y <= n; y++) {
			a[x][y] =
			    l[x  ][y + 1] + l[x  ][y] + l[x  ][y - 1]
			    + l[x - 1][y + 1] + l[x - 1][y] + l[x - 1][y - 1]
			    + l[x + 1][y + 1] + l[x + 1][y] + l[x + 1][y - 1];
		}
	}
}

int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};

int dy[] = {1, 0, -1, -1, 1, 1, 0, -1};

void dfs(int x, int y, bool c) {
	if (x < 1 || y < 1 || x > n || y > n || vis[x][y]) {
		return;
	}
	if (l[x][y]) {
		return;
	}
	vis[x][y] = 1;
	if (a[x][y]) {
		return;
	}/*
	if(a[x][y]&&c){
		return;
	}*/
	for (int i = 0; i < 8; i++) {
		dfs(x + dx[i], y + dy[i], 0);
	}
}

int x = 1, y = 1;
/*
1234567890
         |
 -  -  - |
         |

*/
void print() {
	//out<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
	system("cls");
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (i == x && j == y) {
				out << "---";
			} else {
				out << "   ";
			}
		}
		out << "\n";
		for (int j = 1; j <= n; j++) {
			if (i == x && j == y) {
				if (vis[i][j]) {
					out << "|" << a[i][j] << "|";
				} else {
					out << "|" << (bj[i][j] ? "*" : "@") << "|";
				}
			} else {
				if (vis[i][j]) {
					out << " " << a[i][j] << " ";
				} else {
					out << " " << (bj[i][j] ? "*" : "@") << " ";
				}
			}
		}
		out << "\n";
		for (int j = 1; j <= n; j++) {
			if (i == x && j == y) {
				out << "---";
			} else {
				out << "   ";
			}
		}
		out << "\n";
	}
}


signed main() {
	sd = time(0);
	int k;
	in >> n >> k;
	init(k);
	bool ok = 0, bk = 0;
	print();
	for (;;) {
		if (bk) {
			if ((!ok) && a[x][y] != 0) {
				bk = 1;
				init(k);
				continue;
			} else {
				bk = 0;
				ok = 1;
				if (l[x][y]) {
					for (int i = 1; i <= n; i++) {
						for (int j = 1; j <= n; j++) {
							if (l[i][j]) {
								out << "!";
							} else {
								out << " ";
							}
						}
						cout << '\n';
					}
					return 0;
				} else {
					dfs(x, y, 1);
				}
			}
			continue;
		}
		char ch = g;
		if (ch == 'q') { //点
			if ((!ok) && a[x][y] != 0) {
				bk = 1;
				init(k);
				continue;
			} else {
				bk = 0;
				ok = 1;
				if (l[x][y]) {
					for (int i = 1; i <= n; i++) {
						for (int j = 1; j <= n; j++) {
							if (l[i][j]) {
								out << "!";
							} else {
								out << a[i][j];
							}
						}
						cout << '\n';
					}
					return 0;
				} else {
					dfs(x, y, 1);
				}
			}
		} else if (ch == 'e') { //右键
			bj[x][y] ^= 1;
		} else if (ch == 'a') {
			y--;
		} else if (ch == 'd') {
			y++;
		} else if (ch == 'w') {
			x--;
		} else {
			x++;
		}
		x = min(x, n);
		x = max(x, 1);
		y = min(y, n);
		y = max(y, 1);
		print();
	}
	return 0;
}
原文