UVA 225 剪枝+dfs

来源:互联网 发布:linux send 返回值 编辑:程序博客网 时间:2024/05/21 14:00
//http://acm.hust.edu.cn/vjudge/problem/35587#include <stdio.h>#include <string>#include <cstring>#include <queue>#include <algorithm>#include <functional>#include <vector>#include <sstream>#include <iomanip>#include <math.h>#include <iostream>#include <sstream>#include <time.h>#include <stack>#include <set>#include <map>#include <time.h>#include <bitset>using namespace std;const int MAX=300;const int dX[4]= {1,0,0,-1};const int dY[4]= {0,1,-1,0};const char dD[4]= {'e','n','s','w'};int T,N,M,Cnt;char Ans[50];int G[2*MAX+10][2*MAX+10];bool check(int n,int x,int y,int m){    int cx=x,cy=y;    for (int i=1; i<=n; i++)    {        cx+=dX[m],cy+=dY[m];        if (G[cx+MAX][cy+MAX]==-1)            return false;    }    return true;}void dfs (int n,int x,int y,int last){    if (n==N+1&&x==0&&y==0)    {        Cnt+=1;        for (int i=1; i<=N; i++)            cout<<Ans[i];        cout<<'\n';        return;    }    if (n==N+1)        return;    for (int i=0; i<4; i++)    {        if (last==i||last+i==3)            continue;        if (check(n,x,y,i))        {            int nx=x+dX[i]*n,ny=y+dY[i]*n;            if (G[nx+MAX][ny+MAX])                continue;            G[nx+MAX][ny+MAX]=1;            Ans[n]=dD[i];            dfs(n+1,nx,ny,i);            G[nx+MAX][ny+MAX]=0;        }    }}int main(){    cin.sync_with_stdio(false);    cin>>T;    int a,b;    while (T--)    {        Cnt=0;        memset(G,0,sizeof(G));        cin>>N>>M;        for (int i=0; i<M; i++)        {            cin>>a>>b;            if (a<MAX&&a>-MAX&&b<MAX&&b>-MAX)                G[a+MAX][b+MAX]=-1;        }        dfs(1,0,0,-1);        cout<<"Found "<<Cnt<<" golygon(s).\n\n";    }    return 0;}
0 0
原创粉丝点击