【习题 7-2 UVA-225】Golygons

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

暴力枚举每次走哪里就好。 用一个二维数组来判重。(数据里,要求不能经过一个点两次->但路径可以相交 然后再用一个flag数组,来判断这个点能不能走。

【代码】

/*
  	1.Shoud it use long long ?
  	2.Have you ever test several sample(at least therr) yourself?
  	3.Can you promise that the solution is right? At least,the main ideal
  	4.use the puts("") or putchar() or printf and such things?
  	5.init the used array or any value?
  	6.use error MAX_VALUE?
  	7.use scanf instead of cin/cout?
  	8.whatch out the detail input require
*/
/*
    一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std;

const int N = 400;
const int O = 200;

const int dx[4] = {1,0,0,-1};
const int dy[4] = {0,1,-1,0};
const char dir[4] = {'e','n','s','w'};

int to[300],a[N],n,ans;
vector<int> g[300];
int flag[N+10][N+10];
int vis[N+10][N+10];

void init(){
    to['e'] = 0;to['n'] = 1;to['s'] = 2;to['w'] = 3;
    g['e'].push_back('n');g['e'].push_back('s');
    g['n'].push_back('e');g['n'].push_back('w');
    g['s'].push_back('e');g['s'].push_back('w');
    g['w'].push_back('n');g['w'].push_back('s');
}

void dfs(int dep,int x,int y,int pre){
    if (vis[O+x][O+y]) return;
    vis[O+x][O+y] = 1;
    if (dep==n){
        if (x==0 && y==0){
            ans++;
            for (int i = 1;i <= dep;i++) cout << dir[a[i]];
            cout << endl;
        }
        vis[O+x][O+y] = 0;
        return;
    }
    for (int i = 0;i < (int) g[pre].size();i++){
        int tx = dx[to[g[pre][i]]],ty = dy[to[g[pre][i]]];
        bool ok = true;
        for (int j = 1;j <= dep+1;j++){
            int nx = x + tx*j,ny = y + ty*j;
            if (flag[O+nx][O+ny]==1){
                ok = false;
                break;
            }
            if (j==(dep+1)) tx = nx,ty = ny;
        }
        if (!ok) continue;
        a[dep+1] = to[g[pre][i]];
        dfs(dep+1,tx,ty,g[pre][i]);
    }
    vis[O+x][O+y] = 0;
}

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
    init();
    int T;
    cin >> T;
    while (T--){
        memset(flag,0,sizeof flag);
        memset(vis,0,sizeof vis);
        ans = 0;
        cin >> n;
        int k;
        cin >> k;
        while (k--){
            int x,y;
            cin >> x >> y;
            flag[O+x][O+y] = 1;
        }

        for (int i = 0;i < 4;i++){
            if (flag[O+0][O+0] || flag[O+dx[i]][O+dy[i]]) continue;

            a[1] = i;
            dfs(1,dx[i],dy[i],dir[i]);

        }
        cout <<"Found "<<ans<<" golygon(s)."<<endl<<endl;
    }
	return 0;

}

posted @ 2017-12-21 11:26  AWCXV  阅读(181)  评论(0编辑  收藏  举报