zoukankan      html  css  js  c++  java
  • UVA 225 Golygons (黄金图形)(回溯)

    题意:平面有k个障碍点。从(0,0)出发,第一次走1个单位,……,第n次走n个单位,恰好回到(0,0),每次必须转弯90°,图形可以自交,但不能经过障碍点。按字典序输出所有移动序列,并输出序列总数。

    分析:

    1、障碍点可能在出发点。

    2、注意拐点不能重复!!!

    3、按字典序输出。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    typedef long long ll;
    typedef unsigned long long llu;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
    const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {1, 0, 0, -1};
    const int dc[] = {0, 1, -1, 0};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const double eps = 1e-8;
    const int MAXN = 500 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int vis[MAXN][MAXN];
    int mark[MAXN][MAXN];
    int ans[30];
    int n;
    int cnt;
    map<int, char> mp;
    void init(){
        string s = "ensw";
        for(int i = 0; i < 4; ++i){
            mp[i] = s[i];
        }
    }
    bool judge(int x, int y, int tx, int ty, int length){
        if((vis[tx][ty] || mark[tx][ty]) && !(tx == 255 && ty == 255 && length == n)) return false;//重复经过某个拐点,但此点不是走了n步后到达的原点
        if(x != tx){
            if(x > tx) swap(x, tx);
            for(int i = x + 1; i < tx; ++i){
                if(vis[i][y]){
                    return false;
                }
            }
            return true;
        }
        if(y != ty){
            if(y > ty) swap(y, ty);
            for(int i = y + 1; i < ty; ++i){
                if(vis[x][i]){
                    return false;
                }
            }
            return true;
        }
    }
    void dfs(int x, int y, int length){
        if(length == n + 1){
            if(x == 255 && y == 255){
                ++cnt;
                for(int i = 1; i <= n; ++i){
                    printf("%c", mp[ans[i]]);
                }
                printf("\n");
            }
        }
        else{
            //必须90°转弯,所以只能向两个方向走
            int dir[3];
            if(ans[length - 1] == 0){//
                dir[0] = 1;//
                dir[1] = 2;//
            }
            else if(ans[length - 1] == 1){//
                dir[0] = 0;
                dir[1] = 3;
            }
            else if(ans[length - 1] == 2){//
                dir[0] = 0;
                dir[1] = 3;
            }
            else if(ans[length - 1] == 3){//西
                dir[0] = 1;
                dir[1] = 2;
            }
            for(int i = 0; i < 2; ++i){
                int tx = x + dr[dir[i]] * length;
                int ty = y + dc[dir[i]] * length;
                if(judge(x, y, tx, ty, length)){
                    ans[length] = dir[i];
                    ++mark[tx][ty];
                    dfs(tx, ty, length + 1);
                    --mark[tx][ty];
                }
            }
        }
    }
    int main(){
        init();
        int T;
        scanf("%d", &T);
        while(T--){
            memset(vis, 0, sizeof vis);
            memset(ans, 0, sizeof ans);
            memset(mark, 0, sizeof mark);
            cnt = 0;
            int k;
            scanf("%d%d", &n, &k);
            while(k--){
                int x, y;
                scanf("%d%d", &x, &y);
                vis[x + 255][y + 255] = 1;
            }
            if(vis[255][255]){
                printf("Found 0 golygon(s).\n\n");
                continue;
            }
            int x = 255;//下标不能为负,所以所有坐标加255,原点在(255,255)。
            int y = 255;
            int length = 1;
            mark[x][y] = 1;
            for(int i = 0; i < 4; ++i){//东,北,南,西
                int tx = x + dr[i] * length;
                int ty = y + dc[i] * length;
                if(judge(x, y, tx, ty, length)){//向此方向走length步无障碍物
                    ans[length] = i;
                    ++mark[tx][ty];
                    dfs(tx, ty, length + 1);
                    --mark[tx][ty];
                }
            }
            printf("Found %d golygon(s).\n\n", cnt);
        }
        return 0;
    }
  • 相关阅读:
    C# Task ContinueWith的实现
    C# Task 是什么?返回值如何实现? Wait如何实现
    C# ExecutionContext 实现
    C# Barrier 实现
    C# CountdownEvent实现
    C# SemaphoreSlim 实现
    C# ManualResetEventSlim 实现
    C# Monitor实现
    C# SpinLock实现
    C# SpinWait 实现
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6292747.html
Copyright © 2011-2022 走看看