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 中的数字可以无 ...
随机推荐
- 爬虫_vs_反爬虫
爬虫中有哪些专业术语? 爬虫:自动获取网站数据的程序,关键是批量的获取 反爬虫:使用技术手段防止爬虫程序的方法 误伤:反爬虫技术将普通用户识别为爬虫,效果再好也不能用(禁止ip) 成本:反爬虫需要人力 ...
- docker之NGINX镜像构建
Nginx是一个高性能的Web和反向代理服务器,它具有很多非常优越的特性:1.作为Web服务器.2.作为负载均衡服务器.3.作为邮件代理服务器.4.安装及配置简单.接下来我们介绍在docker构建ng ...
- js中的语句
java语句与js的语句一样.1.判断if else/switch <script type=text/javascript> function fun1(){ var num = 10; ...
- Java常用代码总结
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/7004474.html 1.日期与字符串之间的转换 public static void m ...
- vue中多个input绑定enter按键事件
默认再每个input上加 keyup.enter 是可以绑定事件的 可是多个input一起绑定同一个事件就比较麻烦 所以就在vue dom里面遍历查找并绑定事件 let handleEnter = ( ...
- java 虚拟机--新生代与老年代GC [转]
原文链接:http://www.360doc.com/content/12/1023/16/9615799_243296263.shtml 1. Java堆中各代分布: 图1:Java堆中各代分布 Y ...
- BZOJ 3239: Discrete Logging [BGSG]
裸题 求\(ind_{n,a}b\),也就是\(a^x \equiv b \pmod n\) 注意这里开根不能直接下取整 这个题少了一些特判也可以过... #include <iostream& ...
- BZOJ 1937: [Shoi2004]Mst 最小生成树 [二分图最大权匹配]
传送门 题意: 给一张无向图和一棵生成树,改变一些边的权值使生成树为最小生成树,代价为改变权值和的绝对值,求最小代价 线性规划的形式: $Min\quad \sum\limits_{i=1}^{m} ...
- BZOJ 3173: [Tjoi2013]最长上升子序列 [splay DP]
3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1613 Solved: 839[Submit][St ...
- 使用websocket实现在线聊天功能
很早以前为了快速达到效果,使用轮询实现了在线聊天功能,后来无意接触了socket,关于socket我的理解是进程间通信,首先要有服务器跟客户端,服务的启动监听某ip端口定位该进程,客户端开启socke ...