#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define debu
using namespace std;
const int maxn=220;
const int dx[4]= {1,0,0,-1};
const int dy[4]= {0,1,-1,0};
const char dir[]= {'e','n','s','w'};
int t,n,k,ans;
int path[25];
int sum[25];
int v[maxn<<1][maxn<<1];
int block[maxn<<1][maxn<<1];
void init()
{
memset(v,0,sizeof(v));
memset(block,0,sizeof(block));
memset(path,0,sizeof(path));
sum[1]=1;
for(int i=2; i<=k; i++) sum[i]=sum[i-1]+i;
}
int check(int x,int y,int xx,int yy)
{
if(x==xx)
{
for(int i=min(y,yy); i<=max(y,yy); i++)
if(block[x][i]) return 0;
}
else
{
for(int i=min(x,xx); i<=max(x,xx); i++)
if(block[i][y]) return 0;
}
return 1;
}
void solve(int x,int y,int dep,int last)
{
if(dep==k+1)
{
if(x==maxn&&y==maxn)
{
ans++;
for(int i=1; i<dep; i++) printf("%c",dir[path[i]]);
printf("\n");
}
return ;
}
int left=sum[k]-sum[dep-1];
int tmp=abs(x-maxn)+abs(y-maxn);
if(left<tmp) return ;
for(int i=0; i<4; i++)
{
if(i!=last&&((i+last)!=3))
{
int xx=x+dx[i]*dep;
int yy=y+dy[i]*dep;
if(!v[xx][yy]&&check(x,y,xx,yy))
{
v[xx][yy]=1;
path[dep]=i;
solve(xx,yy,dep+1,i);
v[xx][yy]=0;
}
}
}
}
int main()
{
#ifdef debug
freopen("in.in","r",stdin);
#endif // debug
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&k,&n);
init();
ans=0;
for(int i=0; i<n; i++)
{
int x,y;
scanf("%d%d",&x,&y);
block[x+maxn][y+maxn]=1;
}
solve(maxn,maxn,1,-10);
printf("Found %d golygon(s).\n\n",ans);
}
return 0;
}
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=161
09-04