求小范围 hack。
查看原帖
求小范围 hack。
852144
Loser_Syx楼主2023/11/21 20:03
#include <ctime>
#include <iostream>
#include <cassert>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
#define debug puts("qwq")
#define open(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define close fclose(stdin);fclose(stdout);
#define aint(x) x.begin(), x.end()
namespace FastIO {
	template <typename T = int>
	inline T read() {
		T s = 0, w = 1;
		char c = getchar();
		while (!isdigit(c)) {
			if (c == '-') w = -1;
			c = getchar();
		}
		while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
		return s * w;
	}
	template <typename T>
	inline void read(T &s) {
		s = 0;
		int w = 1;
		char c = getchar();
		while (!isdigit(c)) {
			if (c == '-') w = -1;
			c = getchar();
		}
		while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
		s = s * w;
	}
	template <typename T, typename... Args> inline void read(T &x, Args &...args) {
		read(x), read(args...);
	}
	template <typename T>
	inline void write(T x, char ch) {
		if (x < 0) x = -x, putchar('-');
		static char stk[25];
		int top = 0;
		do {
			stk[top++] = x % 10 + '0', x /= 10;
		} while (x);
		while (top) putchar(stk[--top]);
		putchar(ch);
		return;
	}
} using namespace FastIO;
char Getchar() {
	char c = getchar();
	while (c != '.' && c != '#' && c != 'S' && c != 'C') c = getchar();
	return c;
}
char a[1010][1010];
struct node {int x, y, step;};
struct pii {int x, y;};
pii to[1010][1010][4];
bool vis[1010][1010];
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
signed main() {
	int r = read(), c = read(), sx, sy;
	for (int i = 1; i <= r; ++i) {
		for (int j = 1; j <= c; ++j) {
			a[i][j] = Getchar();
			if (a[i][j] == 'S') sx = i, sy = j;
		}
	}
	for (int i = 1; i <= r; ++i) {
		int nx = 0, ny = 0;
		for (int j = 1; j <= c; ++j) {
			if (j == 1 || a[i][j-1] == '#') {
				nx = i, ny = j;
			}
			if (j == c || a[i][j+1] == '#') {
				to[i][j][0] = {nx, ny};
			}
		}
		for (int j = c; j; --j) {
			if (j == c || a[i][j+1] == '#') {
				nx = i, ny = j;
			}
			if (j == 1 || a[i][j-1] == '#') {
				to[i][j][1] = {nx, ny};
			}
		}
	}
	for (int i = 1; i <= c; ++i) {
		int nx = 0, ny = 0;
		for (int j = 1; j <= r; ++j) {
			if (j == 1 || a[j-1][i] == '#') {
				nx = j, ny = i;
			}
			if (j == r || a[j+1][i] == '#') {
				to[j][i][2] = {nx, ny};
			}
		}
		for (int j = r; j; --j) {
			if (j == r || a[j+1][i] == '#') {
				nx = j, ny = i;
			}
			if (j == 1 || a[j-1][i] == '#') {
				to[j][i][3] = {nx, ny};
			}
		}
	}
	auto in = [&](int x, int y) -> bool {
		return x && y && x <= r && y <= c && a[x][y] != '#' && vis[x][y] == 0;
	};
	queue<node> q;
	q.push({sx, sy, 0});
	vis[sx][sy] = 1;
	while (!q.empty()) {
		node u = q.front();
		//cout << u.x << ' ' << u.y << ' ' << u.step << '\n';
		if (a[u.x][u.y] == 'C') return write(u.step, '\n'), 0;
		q.pop();
		for (int i = 0; i < 4; ++i) {
			node _new = {to[u.x][u.y][i].x, to[u.x][u.y][i].y, u.step + 1};
			if (in(_new.x, _new.y)) {
				vis[_new.x][_new.y] = 1;
				q.push(_new);
			}
			_new = {u.x + dir[i][0], u.y + dir[i][1], u.step + 1};
			if (in(_new.x, _new.y)) {
				vis[_new.x][_new.y] = 1;
				q.push(_new);
			}
		}
	}
	return 0;
}
2023/11/21 20:03
加载中...