Golygons
Golygons |
Imagine a country whose cities have all their streets laid out in a regular grid. Now suppose that a tourist with an obsession for geometry is planning expeditions to several such cities.
Starting each expedition from the central cross-roads of a city, the intersection labelled (0,0), our mathematical visitor wants to set off north, south, east or west, travel one block, and view the sights at the intersection (0,1) after going north, (0,-1) after going south, (1,0) after going east or (-1,0) after going west. Feeling ever more enthused by the regularity of the city, our mathematician would like to walk a longer segment before stopping next, going two blocks.
What's more, our visitor doesn't want to carry on in the same direction as before, nor visit the same point twice, nor wishes to double back, so will make a 90 turn either left or right. The next segment should be three blocks, again followed by a right-angle turn, then four, five, and so on with ever-increasing lengths until finally, at the end of the day, our weary traveller returns to the starting point, (0,0).
The possibly self-intersecting figure described by these geometrical travels is called a golygon.
Unfortunately, our traveller will making these visits in the height of summer when road works will disrupt the stark regularity of the cities' grids. At some intersections there will be impassable obstructions. Luckily, however, the country's limited budget means there will never be more than 50 road works blocking the streets of any particular city. In an attempt to gain accountability to its citizens, the city publishes the plans of road works in advance. Our mathematician has obtained a copy of these plans and will ensure that no golygonal trips get mired in molten tar.
Write a program that constructs all possible golygons for a city.
Input
Since our tourist wants to visit several cities, the input file will begin with a line containing an integer specifying the number of cities to be visited.
For each city there will follow a line containing a positive integer not greater than 20 indicating the length of the longest edge of the golygon. That will be the length of the last edge which returns the traveler to (0,0). Following this on a new line will be an integer from 0 to 50 inclusive which indicates how many intersections are blocked. Then there will be this many pairs of integers, one pair per line, each pair indicating the x and y coordinates of one blockage.
Output
For each city in the input, construct all possible golygons. Each golygon must be represented by a sequence of characters from the set {n,s,e,w} on a line of its own, and they should be output in lexicographics order. Following the list of golygons should be a line indicating how many solutions were found. This line should be formatted as shown in the example output. A blank line should appear following the output for each city.
Sample Input
2 8 2 -2 0 6 -2 8 2 2 1 -2 0
Sample Output
wsenenws Found 1 golygon(s). Found 0 golygon(s).
Diagram of the 1st City
![](http://uva.onlinejudge.org/external/2/225img2.gif)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
// freopen("in.txt", "r", stdin);
#endif // ACM
work();
}
/***************************************************/
set<int> ss[23];
void init(int n)
{
ff(i, 23) ss[i].clear();
ss[n+2].insert(0);
ss[n+1].insert(0);
dff(i, n, 1)
{
for(int v : ss[i+2])
ss[i].insert(v+i), ss[i].insert(v-i);
}
}
int maze[201][201];
char path[22];
int dir;
set<string> ans;
void dfs(int x, int y, int now, int end)
{
if(now > end)
{
if(x == 100 && y == 100)
ans.insert(path+1);
return;
}
if(now%2 == dir)
{
if(ss[now+2].count(x+now-100))
{
bool flag = true;
fff(k, 1, now) if(maze[x+k][y]==1) flag = false;
if(flag && maze[x+now][y] != 2)
{
maze[x+now][y] = 2;
path[now] = 'e', dfs(x+now, y, now+1, end);
maze[x+now][y] = 0;
}
}
if(ss[now+2].count(x-now-100))
{
bool flag = true;
fff(k, 1, now) if(maze[x-k][y]==1) flag = false;
if(flag && maze[x-now][y] != 2)
{
maze[x-now][y] = 2;
path[now] = 'w', dfs(x-now, y, now+1, end);
maze[x-now][y] = 0;
}
}
}
else
{
if(ss[now+2].count(y+now-100))
{
bool flag = true;
fff(k, 1, now) if(maze[x][y+k]==1) flag = false;
if(flag && maze[x][y+now] != 2)
{
maze[x][y+now] = 2;
path[now] = 'n', dfs(x, y+now, now+1, end);
maze[x][y+now] = 0;
}
}
if(ss[now+2].count(y-now-100))
{
bool flag = true;
fff(k, 1, now) if(maze[x][y-k]==1) flag = false;
if(flag && maze[x][y-now] != 2)
{
maze[x][y-now] = 2;
path[now] = 's', dfs(x, y-now, now+1, end);
maze[x][y-now] = 0;
}
}
}
}
void work()
{
int T;
scanf("%d", &T);
fff(cas, 1, T)
{
int n, point;
scanf("%d%d", &n, &point);
memset(maze, 0, sizeof(maze));
int x, y;
ff(i, point)
{
scanf("%d%d", &x, &y);
if(abs(x) <= 100 && abs(y) <= 100)
maze[100+x][100+y] = 1;
}
ans.clear();
if(maze[100][100] == 0 && (n==7 || n==8 || n==15 || n==16))
{
init(n);
path[n+1] = 0;
dir = 1;
dfs(100, 100, 1, n);
dir = 0;
dfs(100, 100, 1, n);
for(string str : ans) puts(str.c_str());
}
printf("Found %d golygon(s).\n", ans.size());
puts("");
}
}