习题7-2(uva-225)

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 250, base = 125, Dirs[4][2] = { { 1, 0 }, { 0, 1 }, { 0, -1 }, { -1, 0 } };
const char str[] = "ensw";

int matrix[maxn][maxn], n, k, ret, sx, sy;

bool OverBound(int a){
	if (abs(a) > maxn) return true;
	return false;
}

bool CanMove(int x, int y, int cnt, int d){
	for (int i = 0; i < cnt; i++){
		x += Dirs[d][0];
		y += Dirs[d][1];
		if (OverBound(x) || OverBound(y) || matrix[x][y] == -1) return false;
	}
	return true;
}
void dfs(string &temp, int x, int y, int cnt, int d)
{
	if (OverBound(x) || OverBound(y)) return;

	if (cnt > n){
		if (x == sx && y == sy){
			cout << temp << endl;
			++ret;
		}
		else return;
	}

	int nx, ny;
	for (int i = 0; i < 4; i++){
		if (d == -1 || (d != i && d + i != 3)){
			nx = x + Dirs[i][0] * cnt;
			ny = y + Dirs[i][1] * cnt;
			if (matrix[nx][ny] != 0) continue;
			if (CanMove(x, y, cnt, i)){
				matrix[nx][ny] = 1;
				temp.push_back(str[i]);
				dfs(temp, nx, ny, cnt + 1, i);
				temp.pop_back();
				matrix[nx][ny] = 0;
			}
		}
	}
}
int main()
{
	string temp;
	int t, a, b;
	cin >> t;
	while (t--){
		cin >> n >> k;

		ret = 0;
		memset(matrix, 0, sizeof(matrix));
		for (int i = 0; i < k; i++){
			cin >> a >> b;
			if (OverBound(a + base) || OverBound(b + base)) continue;
			matrix[a + base][b + base] = -1;
		}
		sx = sy = base;
		dfs(temp, sx, sy, 1, -1);
		cout << "Found " << ret << " golygon(s)." << endl << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值