题意:平面有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[] = {, , , -};
const int dc[] = {, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int vis[MAXN][MAXN];
int mark[MAXN][MAXN];
int ans[];
int n;
int cnt;
map<int, char> mp;
void init(){
string s = "ensw";
for(int i = ; i < ; ++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 == && ty == && length == n)) return false;//重复经过某个拐点,但此点不是走了n步后到达的原点
if(x != tx){
if(x > tx) swap(x, tx);
for(int i = x + ; i < tx; ++i){
if(vis[i][y]){
return false;
}
}
return true;
}
if(y != ty){
if(y > ty) swap(y, ty);
for(int i = y + ; i < ty; ++i){
if(vis[x][i]){
return false;
}
}
return true;
}
}
void dfs(int x, int y, int length){
if(length == n + ){
if(x == && y == ){
++cnt;
for(int i = ; i <= n; ++i){
printf("%c", mp[ans[i]]);
}
printf("\n");
}
}
else{
//必须90°转弯,所以只能向两个方向走
int dir[];
if(ans[length - ] == ){//东
dir[] = ;//北
dir[] = ;//南
}
else if(ans[length - ] == ){//北
dir[] = ;
dir[] = ;
}
else if(ans[length - ] == ){//南
dir[] = ;
dir[] = ;
}
else if(ans[length - ] == ){//西
dir[] = ;
dir[] = ;
}
for(int i = ; i < ; ++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 + );
--mark[tx][ty];
}
}
}
}
int main(){
init();
int T;
scanf("%d", &T);
while(T--){
memset(vis, , sizeof vis);
memset(ans, , sizeof ans);
memset(mark, , sizeof mark);
cnt = ;
int k;
scanf("%d%d", &n, &k);
while(k--){
int x, y;
scanf("%d%d", &x, &y);
vis[x + ][y + ] = ;
}
if(vis[][]){
printf("Found 0 golygon(s).\n\n");
continue;
}
int x = ;//下标不能为负,所以所有坐标加255,原点在(255,255)。
int y = ;
int length = ;
mark[x][y] = ;
for(int i = ; i < ; ++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 + );
--mark[tx][ty];
}
}
printf("Found %d golygon(s).\n\n", cnt);
}
return ;
}

UVA - 225 Golygons (黄金图形)(回溯)的更多相关文章

  1. UVA225 Golygons 黄金图形(dfs+回溯)

    剪枝1:在同一个维度上的点具有相同的奇偶性,如果奇数数量只有奇数个那么一定不能返回原点. 剪枝2:当前位置怎么也走不回去. 3:沿途判断障碍即可. 在oj上提交0.347s,最快的0.012s,应该有 ...

  2. Uva 225 Golygons

    这道题如果直接用Dfs,运气好的话是可以直接过的. 但如果要在Dfs的基础上加快速度,剪枝是必不可少的. 我的剪枝策略: 1.当前点(x,y)回到出发点至少需要 |x| +| y| 步,如果剩余的步数 ...

  3. UVa 225 黄金图形(回溯+剪枝)

    https://vjudge.net/problem/UVA-225 题意:平面上有k个障碍点,从(0,0)出发,第一次走1个单位,第二次走2个单位,...第n次走n个单位,最后恰好回到(n,n).每 ...

  4. UVa 129 Krypton Factor【回溯】

    学习的紫书的回溯,理解起来还是好困难的说啊= = #include<iostream> #include<cstdio> #include<cstring> #in ...

  5. UVa 1602 网格动物(回溯)

    https://vjudge.net/problem/UVA-1602 题意:计算n连通块不同形态的个数. 思路: 实在是不知道该怎么做好,感觉判重实在是太麻烦了. 判重就是判断所有格子位置是否都相同 ...

  6. UVa 129 Krypton Factor (DFS && 回溯)

    题意 : 如果一个字符串包含两个相邻的重复子串,则称它是“容易的串”,其他串称为“困难的 串”.例如,BB.ABCDACABCAB.ABCDABCD都是容易的串,而D.DC.ABDAB. CBABCB ...

  7. uva 387 A Puzzling Problem (回溯)

     A Puzzling Problem  The goal of this problem is to write a program which will take from 1 to 5 puzz ...

  8. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  9. UVa 167(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索

    UVa 167 题意:八行八列的棋盘每行每列都要有一个皇后,每个对角线上最多放一个皇后,让你放八个,使摆放位置上的数字加起来最大. 参考:https://blog.csdn.net/xiaoxiede ...

随机推荐

  1. 调用百度汇率api 获取各国的汇率值

    设置一个定时任务,每天更新汇率java代码如下 package com.thinkgem.jeesite.modules.huiLvApi.service; import java.io.Buffer ...

  2. 「Luogu P2468 [SDOI2010]粟粟的书架」

    这道题分为两个部分 Part1 前置芝士 前缀和(后缀和,二维前缀和):可以预处理一下数据. 二分查找:可以在较短的时间内找出答案. 具体做法 可以发现\(R,C\)不大,只有\(200\),于是可以 ...

  3. java中如何修改事务的隔离级别

    事务的特性: 原子性(Atomicity)原子性是指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. (多条语句要么都成功,要么都失败.) 一致性(Consistency)事务前后 ...

  4. 莫烦 - Pytorch学习笔记 [ 一 ]

    1. Numpy VS Torch #相互转换 np_data = torch_data.numpy() torch_data = torch.from_numpy(np_data) #abs dat ...

  5. [CMake笔记] CMake向解决方案添加源文件兼头文件

    回顾 在上一篇笔记里总结的时候说到,aux_source_directory这个函数在添加源码文件时,是不会把头文件添加进去的,这里就介经一下另外一个方法,也是我一直使用的. 添加文件*.cpp与*. ...

  6. 144、Java链表之定义一个Node类并输出

    01.代码如下: package TIANPAN; class Node { // 每一个链表实际上就是由多个节点组成的 private String data; // 要保存的数据 private ...

  7. 四 动态sql 标签的使用(if&where&sql片段&foreach)

    if标签的使用: userMapper.xml  userMapper.java junit: where标签: 注意:写了where标签就不用手动写where语句 sql片段的设置和调用: forr ...

  8. HashMap ( Java 8)

    HashTable是早起java提供的基于hash表的实现,不允许存放null键和值,是同步的,影响开销,不太被推荐. HashMap行为上和HashTable差不多,不是同步的,允许键和值为null ...

  9. 六、java基础-单例模式_继承_覆盖_多态

    1.单例模式: 1)提出原因 是由gof 也就是四人组提出来的.为了保证jvm中某一类型的java对象永远只有一个,同时也是为了节省内存的开销.因为外面程序可以通过new的方法直接调用类里面的构造方法 ...

  10. VUE 动态切换列表active样式

    参考VUE官方文档样式绑定 https://cn.vuejs.org/v2/guide/class-and-style.html 需求是动态加载出来了所有菜单列表,点击其中一个li元素改变这个元素的背 ...