#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;
}
}
01-09
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交