【紫书】UVA225

题目提交点

UVA225

代码

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define _for(i, a, b) for (int i = (a); i < (b); ++i)

struct Point {
	int x, y;
	Point(int x = 0, int y = 0) : x(x), y(y) {}

	Point operator+ (const Point& t) {
		return { x + t.x, y + t.y };
	}

	Point operator* (const int& t) {
		return { x * t, y * t };
	}

	bool operator== (const Point& t) {
		return x == t.x && y == t.y;
	}
};

const int N = 512;
unordered_map<int, vector<int>> blocks_x, blocks_y;
const char DIR[] = "nesw";
Point go[] = { {0,1}, {1, 0}, {0, -1}, {-1, 0} };
int vis[N][N];
int T, n, k;

ostream& operator << (ostream & os, vector<char>&path) {
	for (const auto p : path) os << p;
	return os;
}

bool inRange(int x, int l, int r) {
	if (l > r) return inRange(x, r, l);
	return l <= x && x <= r;
}

bool is_valid(Point& pf, Point& pt) {
	if (pf.x == pt.x) {
		if (!blocks_x.count(pt.x)) return true;
		for (const auto& p : blocks_x[pt.x]) if (inRange(p, pf.y, pt.y)) return false;
	}
	else{
		if (!blocks_y.count(pt.y)) return true;
		for (const auto& p : blocks_y[pt.y]) if (inRange(p, pf.x, pt.x)) return false;
	}
	return true;
}

void dfs(Point p, vector<char>& path, set<string>& paths) {
	int sz = path.size();
	if (sz == n) {
		if (!p.x && !p.y) {
			stringstream ss;
			ss << path;
			paths.insert(ss.str());
		}
		return;
	}

	if (((n - sz) * (sz + 1 + n) / 2) < (abs(p.x) + abs(p.y))) return;
	int dir = strchr(DIR, path.back()) - DIR;

	for (int i = 1; i <= 3; i += 2) {
		int d = (dir + i) % 4;
		Point dot = p + (go[d] * (sz + 1));
		int x = dot.x + 256;
		int y = dot.y + 256;

		if (is_valid(p, dot) && !vis[x][y]) {
			path.push_back(DIR[d]);
			vis[x][y] = true;
			dfs(dot, path, paths);
			vis[x][y] = false;
			path.pop_back();
		}
	}

}

int main() {
#ifdef LOCAL
	freopen("data.in", "r", stdin);
#endif // LOCAL

	scanf("%d", &T); // 输入翻转次数
	_for (j, 0, T) {

		scanf("%d%d", &n, &k); 
		_for(i, 0, k) {
			int x, y;
			scanf("%d%d", &x, &y);
			blocks_x[x].push_back(y);
			blocks_y[y].push_back(x);
		}

		vector<char> path;
		set<string> paths;
		
		_for(i, 0, 4) {
			if (is_valid(go[i], go[i])) {
				path.push_back(DIR[i]);
				dfs(go[i], path, paths);
				path.pop_back();
			}
		}

		for (const auto& p : paths) puts(p.c_str());
		printf("Found %zd golygon(s).\n\n", paths.size());

		blocks_x.clear();
		blocks_y.clear();
		memset(vis, 0, sizeof vis);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值