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;
}

 

转载于:https://www.cnblogs.com/tyty-Somnuspoppy/p/6292747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值