uva225 回溯剪枝
这道题要剪枝,不然会超时,还有就是每次参加过的城市下次不能再参观,不然会WA。
对于障碍物的坐标我用了两种方法,第一种就是直接用STL里面的set,对于判断是否访问过直接用的count,用时960ms;当我把集合换成数组,200ms通过。
用set的AC代码:
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=300;
int dx[]={1,0,0,-1,5};
int dy[]={0,1,-1,0,5}; //east,north,south,west
char dir[]="ensw";
int vis[maxn][maxn];
int n,cnt;
struct node{
int x,y;
node(int x=0,int y=0):x(x),y(y){}
bool operator < (const node &p) const{
return x>p.x||(x==p.x&&y>p.y);
}
};
set<node>u;
void dfs(int *a,int cur,int d,node pos){
if(cur==n&&pos.x==150&&pos.y==150){
cnt++;
for(int i=0;i<n;++i) printf("%c",dir[a[i]]);
printf("\n");
return;
}
if(cur>=n) return;
if((abs(pos.x-150)+abs(pos.y-150))>((n-cur)*(n+cur+1)/2)) return; //cut
for(int i=0;i<4;++i){
if(dx[i]==dx[d]||dy[i]==dy[d]) continue;
int newx=pos.x+(cur+1)*dx[i],newy=pos.y+(cur+1)*dy[i];
if(vis[newx][newy]) continue; //已经旅游过
int flag=0,c,p;
if(newx==pos.x){
c=min(newy,pos.y),p=max(newy,pos.y);
for(int k=c;k<=p;++k){
node newc(newx,k);
if(u.count(newc)) {flag=1;break;}
}
}
else if(newy==pos.y){
c=min(newx,pos.x),p=max(newx,pos.x);
for(int k=c;k<=p;++k){
node newc(k,newy);
if(u.count(newc)) {flag=1;break;}
}
}
if(flag) continue;
a[cur]=i;
vis[newx][newy]=1;
dfs(a,cur+1,i,node(newx,newy));
vis[newx][newy]=0;
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(vis,0,sizeof(vis));
int k;
scanf("%d%d",&n,&k);
int x,y;
for(int i=0;i<k;++i){
scanf("%d%d",&x,&y);
u.insert(node(x+150,y+150)); //障碍物坐标保存
}
cnt=0;
int a[25];
dfs(a,0,4,node(150,150));
printf("Found %d golygon(s).\n\n",cnt);
u.clear();
}
return 0;
}
用数组的AC代码:
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=300;
int dx[]={1,0,0,-1,5};
int dy[]={0,1,-1,0,5}; //east,north,south,west
char dir[]="ensw";
int vis[maxn][maxn],def[maxn][maxn];
int n,cnt;
struct node{
int x,y;
node(int x=0,int y=0):x(x),y(y){}
bool operator < (const node &p) const{
return x>p.x||(x==p.x&&y>p.y);
}
};
void dfs(int *a,int cur,int d,node pos){
if(cur==n&&pos.x==150&&pos.y==150){
cnt++;
for(int i=0;i<n;++i) printf("%c",dir[a[i]]);
printf("\n");
return;
}
if(cur>=n) return;
if((abs(pos.x-150)+abs(pos.y-150))>((n-cur)*(n+cur+1)/2)) return; //cut
for(int i=0;i<4;++i){
if(dx[i]==dx[d]||dy[i]==dy[d]) continue;
int newx=pos.x+(cur+1)*dx[i],newy=pos.y+(cur+1)*dy[i];
if(vis[newx][newy]) continue; //已经旅游过
int flag=0,c,p;
if(newx==pos.x){
c=min(newy,pos.y),p=max(newy,pos.y);
for(int k=c;k<=p;++k){
node newc(newx,k);
if(def[newx][k]) {flag=1;break;}
}
}
else if(newy==pos.y){
c=min(newx,pos.x),p=max(newx,pos.x);
for(int k=c;k<=p;++k){
node newc(k,newy);
if(def[k][newy]) {flag=1;break;}
}
}
if(flag) continue;
a[cur]=i;
vis[newx][newy]=1;
dfs(a,cur+1,i,node(newx,newy));
vis[newx][newy]=0;
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(vis,0,sizeof(vis));
memset(def,0,sizeof(def));
int k;
scanf("%d%d",&n,&k);
int x,y;
for(int i=0;i<k;++i){
scanf("%d%d",&x,&y);
def[x+150][y+150]=1;
}
cnt=0;
int a[25];
dfs(a,0,4,node(150,150));
printf("Found %d golygon(s).\n\n",cnt);
}
return 0;
}
如有不当之处欢迎指出!
uva225 回溯剪枝的更多相关文章
- 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个数组成一个环,环内的数字不能重复,左右 ...
- 回溯剪枝,dfs,bfs
dfs: 给定一个整数n,将数字1~n排成一排,将会有很多种排列方法. 现在,请你按照字典序将所有的排列方法输出. 输入格式 共一行,包含一个整数n. 输出格式 按字典序输出所有排列方案,每个方案占一 ...
- [算法专题] 深度优先搜索&回溯剪枝
1. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/ Given a string s, ...
- TZOJ 1221 Tempter of the Bone(回溯+剪枝)
描述 The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked i ...
- UVa 225 黄金图形(回溯+剪枝)
https://vjudge.net/problem/UVA-225 题意:平面上有k个障碍点,从(0,0)出发,第一次走1个单位,第二次走2个单位,...第n次走n个单位,最后恰好回到(n,n).每 ...
- Leetcode题目39.组合总和(回溯+剪枝-中等)
题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无 ...
随机推荐
- js_10_dom表单
事件的优先级? 先执行事件,后执行标签内置事件,如果事件返回false不执行后面的事件或标签内置事件 如何通过js提交表单? 任意标签定义onclick事件 函数中写入:document.getEle ...
- linkinFrame--用maven搭项目结构
OK,老早想写一套自己的web框架,然后也一直在看开源的一些框架源码.今天开始正式开始写自己的javaWeb框架,暂时就定义linkinFrame好了. 为什么要写一套自己的框架? 其实这是一个比较矛 ...
- WebSphere--定制配置
本节介绍如何启动和使用 WebSphere应用服务器的管理器(一个图形界面)为 Servlet 活动和 WebSphere应用服务器的组件定制基本设置参数. 1.启动 WebSphere应用服务 ...
- css 块状元素与行内元素(内联元素)的理解
块状元素: 它一般是其他元素的容器元素,可以容纳块状元素和行内元素,它默认是不会和其他元素同一行的,即相当于两个块状元素写一起是垂直布局的.最常用的是div和p 行内元素: 行内元素又称内联元素,它只 ...
- Unity3d 基本设计开发 原则(提高代码可读性)
参考:http://blog.csdn.net/qq_34134078/article/details/51780356 1.单一原则 即:明确类的定义.通俗来讲,让他们只做一件事,而不是多件事. 提 ...
- VNC配置
简介 VNC (Virtual Network Console)是虚拟网络控制台的缩写.它 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的.VNC 是在基于 UNIX ...
- c#扩展方法的使用,实现的几个功能
用扩展类写了一个管理类: using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using Syste ...
- 移动端的弹窗滚动禁止body滚动
前言 最近一个需求是弹窗展示列表,显然是需要一个滚动条的,而滚动到底部就会穿透到body滚动,而阻止默认行为是不行的,这样两个都滑动不了 所以我在点击出现弹窗的时候在body加了以下css让它没有滚动 ...
- Hadoop学习笔记三
一.设置HDFS不进行权限检查 默认的HDFS上的文件类似于Linux中的文件,是有权限的.例如test用户创建的文件,root用户如果没有写权限,则不能进行删除. 有2种办法进行修改,修改文件的权限 ...
- BZOJ 3720: Gty的妹子树 [树上size分块]
传送门 题意: 一棵树,询问子树中权值大于$k$的节点个数,修改点权值,插入新点:强制在线 一开始以为询问多少种不同的权值,那道CF的强制在线带修改版,直接吓哭 然后发现看错了这不一道树上分块水题.. ...