UVa 225 黄金图形(回溯+剪枝)
https://vjudge.net/problem/UVA-225
题意:
平面上有k个障碍点,从(0,0)出发,第一次走1个单位,第二次走2个单位,...第n次走n个单位,最后恰好回到(n,n)。每次必须转弯90°。
思路:
首先要注意的是坐标有可能是负的,所以在这里我们可以障碍点的坐标值+105变成正值,原点也就变成了(105,105),为什么是加105呢?因为我们最多走20步,最远是310,所以如果大于了105,之后肯定就回不来了,所以只需要加上105就可以了。
然后就是各种剪枝了,比较重要的就是当前距离大于剩余可走距离时,之后不管怎么走,肯定是回不到原点了,此时剪枝。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std; const int maxn = ;
int n, k, cnt;
const int dir[][] = { { , }, { , }, { , - }, { -, } };
const char dict[] = "ensw";
int G[*maxn][*maxn];
int vis[*maxn][*maxn];
int path[maxn];
int sum[]; void init()
{
//计算步数
sum[] = ;
for (int i = ; i <= ; i++)
sum[i] = sum[i - ] + i;
} bool judge(int x, int y,int d)
{
int dis = abs(x - ) + abs(y - );
if (dis > sum[n] - sum[d]) return false;
return true;
} void dfs(int f, int d, int x, int y)
{
if (d == n)
{
if (x!= || y!=) return;
for (int i = ; i < n; i++)
printf("%c", dict[path[i]]);
printf("\n");
cnt++;
return;
} for (int k = ; k < ; k++)
{
if (k == f || k + f == ) continue;
int dx = x;
int dy = y;
int flag = ;
for (int i = ; i <= d + ; i++)
{
dx = dx + dir[k][];
dy = dy + dir[k][];
if (G[dx][dy]||!judge(dx,dy,d)) { flag = ; break; }
}
if (flag && !vis[dx][dy])
{
vis[dx][dy] = ;
path[d] = k;
dfs(k, d + , dx, dy);
vis[dx][dy] = ;
}
}
return;
} int main()
{
//ios::sync_with_stdio(false);
//freopen("D:\\txt.txt", "r", stdin);
int T;
init();
scanf("%d", &T);
while (T--)
{
scanf("%d%d",&n, &k);
memset(G, , sizeof(G));
memset(vis, , sizeof(vis));
for (int i = ; i < k; i++)
{
int a, b;
scanf("%d%d", &a, &b);
a += ;
b += ;
if (a>= && b>=) G[a][b] = ;
}
cnt = ;
dfs(-, , , );
printf("Found %d golygon(s).\n\n", cnt);
}
}
UVa 225 黄金图形(回溯+剪枝)的更多相关文章
- 回溯剪枝,dfs,bfs
dfs: 给定一个整数n,将数字1~n排成一排,将会有很多种排列方法. 现在,请你按照字典序将所有的排列方法输出. 输入格式 共一行,包含一个整数n. 输出格式 按字典序输出所有排列方案,每个方案占一 ...
- HDU 5113 Black And White 回溯+剪枝
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...
- HDU 2553 N皇后问题(回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398797 题意: 在N*N(N <= 10)的方格棋盘放置了N个皇后,使得它们不相互攻击(即 ...
- HDU1010 Tempter of the Bone(回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...
- HDU1016 Prime Ring Problem (回溯 + 剪枝)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...
- UVA - 225 Golygons (黄金图形)(回溯)
题意:平面有k个障碍点.从(0,0)出发,第一次走1个单位,……,第n次走n个单位,恰好回到(0,0),每次必须转弯90°,图形可以自交,但不能经过障碍点.按字典序输出所有移动序列,并输出序列总数. ...
- UVa 208 - Firetruck 回溯+剪枝 数据
题意:构造出一张图,给出一个点,字典序输出所有从1到该点的路径. 裸搜会超时的题目,其实题目的数据特地设计得让图稠密但起点和终点却不相连,所以直接搜索过去会超时. 只要判断下起点和终点能不能相连就行了 ...
- UVA225 Golygons 黄金图形(dfs+回溯)
剪枝1:在同一个维度上的点具有相同的奇偶性,如果奇数数量只有奇数个那么一定不能返回原点. 剪枝2:当前位置怎么也走不回去. 3:沿途判断障碍即可. 在oj上提交0.347s,最快的0.012s,应该有 ...
- [算法专题] 深度优先搜索&回溯剪枝
1. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/ Given a string s, ...
随机推荐
- 2.搭建cassandra时遇到没有公网网卡的问题
阿里云服务器有两种网络,一种是经典网络,一种是专用网络,经典网络是公网网卡的,但是专用网络是没有公网网卡的. 如图: 经典网络,公网ip是139.129.31.108: 专用网络,公网ip是 问题: ...
- matlab常用方法
1:matlab进行符号的虚数运算 直接使用符号 a+b*j运算,结果是一个角度值,不是复数. 可以使用 a+b*(1j) 进行运算. 如下 position(index,)=radius; ...
- [py][mx]django通过邮箱找回密码
忘记密码处理流程 注意: 这个文章里的找回密码页面截取有问题. 找回密码页应该是让输入邮箱 直接上代码 class ActiveView(View): # 主要功能是修改user_profile里的i ...
- [LeetCode] 785. Is Graph Bipartite?_Medium tag: DFS, BFS
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- PNG格式图片常见转换方法
前言 最近碰到一个需要将图片由原始的PNG转化为JPG的需求,由于PNG图片本身质量等原因,导致转化为JPG之后,存在失真的问题,后来一个同事分享了下述的HighQualityPNGToJPG方法解决 ...
- 集成树模型使用自动搜索模块GridSearchCV,stacking
一. GridSearchCV参数介绍 导入模块: from sklearn.model_selection import GridSearchCV GridSearchCV 称为网格搜索交叉验证调参 ...
- matlab 以excel格式将字符串数组写入TXT文件
[m, n] = size(FFoutpu);fp = fopen('FFoutpu.txt','wt');fprintf(fp, 'name CODE ROTC EBIT_EV SHIZHI ROT ...
- js中数组的字符串表示
<html> <head> <title>数组的字符串表示</title> <script type="text/javascript& ...
- mysql性能优化2
sql语句优化 性能不理想的系统中除了一部分是因为应用程序的负载确实超过了服务器的实际处理能力外,更多的是因为系统存在大量的SQL语句需要优化. 为了获得稳定的执行性能,SQL语句越简单越好.对复杂的 ...
- mysql count group by统计条数方法
mysql count group by统计条数方法 mysql 分组之后如何统计记录条数? gourp by 之后的 count,把group by查询结果当成一个表再count一次select c ...