poj2331 (IDA*)
题意:给你k种管道,然后是每种的长度,每种的数量,求(x1,y1)到(x2,y2)所用管道的最少数量
思路:
最开始考虑的是直接bfs,但是没有成功。
然后发现可以先找x轴x1 到 x2 ,再找y轴y1 到 y2。两个的和便是最终答案。
先用bfs处理出两条轴上的估计函数(即每个地方到x2或y2的距离),然后枚举深度搜索。
Orz;
1.最开始思路的方向就错了,没想到可以x,y轴分开来考虑- -,果然脑子转不过来
2.而且最后忘了判断是否有答案,贡献了个TL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
typedef long long ll;
using namespace std; int len[5];
int num[5];
int have1[1005];
int have2[1005];
int x1,x2,y2,y1,k; void bfs(int *a,int to) //估价函数,到to的最小步数
{
queue<int >q;
q.push(to);
a[to] = 0;
while(!q.empty())
{
int cur = q.front();
q.pop(); for(int i = 0; i < k; i++)
{
if(cur-len[i] > 0 && a[cur-len[i]] == -1)
{
a[cur-len[i]] = a[cur] + 1;
q.push(cur-len[i]);
} if(cur+len[i]<= 1000 && a[cur+len[i]] == -1)
{
a[cur+len[i]] = a[cur]+1;
q.push(cur+len[i]);
}
}
}
} bool IDA(int cur,int now,int ci,int flag)
{
if(!flag)
{
if(cur + have1[now] > ci)
return false;
if(now == x2) //当x轴已经达到目标点x2,去搜索y轴
{
if(IDA(0,y1,ci-cur,1))
return true;
}
} if(flag)
{
if(cur+have2[now] > ci)
return false;
if(now == y2) //y轴到达y2点
return true;
} for(int i = 0; i < k; i++)
{
if(num[i] > 0) //还能添加i
{
num[i]--;
if(flag == 0)
{
if(now + len[i] <= 1000) if(IDA(cur+1,now+len[i],ci,flag)) return true;
if(now - len[i] > 0) if(IDA(cur+1,now-len[i],ci,flag)) return true;
}
else
{
if(now + len[i] <= 1000) if(IDA(cur+1,now+len[i],ci,flag)) return true;
if(now - len[i] > 0) if(IDA(cur+1,now-len[i],ci,flag)) return true;
}
num[i]++;
}
}
return false;
} int main()
{
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2) != EOF)
{
int tmax = 0,ans;
scanf("%d",&k);
for(int i = 0; i < k; i++)
scanf("%d",len+i); for(int i = 0; i < k; i++)
{
scanf("%d",num+i);
tmax += num[i];
}
if(x1 == x2 && y1 == y2)
{
printf("0\n");
continue;
} memset(have1,-1,sizeof(have1));
memset(have2,-1,sizeof(have2));
bfs(have1,x2);
bfs(have2,y2);
for(int i = 0;; i++)
{
if(IDA(0,x1,i,0))
{
ans = i;
break;
}
if(i > tmax){ //找不到答案
ans = 10000;
break;
}
}
if(ans <= tmax)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}
poj2331 (IDA*)的更多相关文章
- Booksort POJ - 3460 (IDA*)
Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...
- UVA - 10384 The Wall Pusher(推门游戏)(IDA*)
题意:从起点出发,可向东南西北4个方向走,如果前面没有墙则可走:如果前面只有一堵墙,则可将墙向前推一格,其余情况不可推动,且不能推动游戏区域边界上的墙.问走出迷宫的最少步数,输出任意一个移动序列. 分 ...
- 人类即将进入互联网梦境时代(IDA)
在电影<盗梦空间>中,男主角科布和妻子在梦境中生活了50年,从楼宇.商铺.到河流浅滩.一草一木.这两位造梦师用意念建造了属于自己的梦境空间.你或许并不会想到,在不久未来,这看似科幻的情节将 ...
- POJ2286 The Rotation Game(IDA*)
The Rotation Game Time Limit: 15000MS Memory Limit: 150000K Total Submissions: 5691 Accepted: 19 ...
- UVA-10384 The Wall Pushers (IDA*)
题目大意:走迷宫,遇到墙时可以推着墙走,但墙后还是墙时便不能推.求出一条任意的最短路径. 题目分析:这道题出的比较人性,输入的时候便是将四周的墙用二进制数表示好了,其实这样减轻了做题人的负担.IDA* ...
- POJ - 2286 - The Rotation Game (IDA*)
IDA*算法,即迭代加深的A*算法.实际上就是迭代加深+DFS+估价函数 题目传送:The Rotation Game AC代码: #include <map> #include < ...
- 骑士精神(IDA*)
题目描述 输入格式 第一行有一个正整数T(T<=10),表示一共有N组数据.接下来有T个5×5的矩阵,0表示白色骑士,1表示黑色骑士,*表示空位.两组数据之间没有空行. 输出格式 对于每组数据都 ...
- BZOJ 1085(IDA*)
题面 传送门 分析 首先,直接搜索肯定会TLE 很容易想到用迭代加深的方法,限定搜索深度 但是,这样仍然不够,需要用启发式的方法优化 我们设计一个估价函数f(x)=g(x)+h(x)f(x)=g(x) ...
- 【wikioi】2495 水叮当的舞步(IDA*)
http://wikioi.com/problem/2495/ 这题我还是看题解啊囧.(搜索实在太弱.完全没想到A*,还有看题的时候想错了,.,- -) 好吧,估价还是那么的简单,判断颜色不同的数目即 ...
随机推荐
- Python之旅.第四章.模块与包 4.02
一.模块的使用之import 1 什么是模块?模块就一系统功能的集合体,在python中,一个py文件就是一个模块,比如module.py,其中模块名module2 使用模块2.1 import 导入 ...
- Aache的虚拟主机配置虚拟目录
3. 打开 httpd.conf 文件, 添加如下代码: # Virtual hosts Include conf/extra/httpd-vhosts.conf 如果已存在,将Include前面的# ...
- Python内置函数(37)——sorted
英文文档: sorted(iterable[, key][, reverse]) Return a new sorted list from the items in iterable. Has tw ...
- Spring AOP AspectJ
本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某 ...
- apigw鉴权分析(1-3)百度 AI - 鉴权方式分析
http://ai.baidu.com/docs#/Begin/top 一.访问入口 二.鉴权方式分析 1.鉴权认证方式一 - access_token - 针对HTTP API调用者 2.鉴权认证方 ...
- Python学习之中文注释问题
简单写个输入.输出,并注释 # 输入 print'100+200=',100+200 # 输入 name = raw_input() 报错了: SyntaxError: Non-ASCII chara ...
- python/*args和**kwargs
*args和**kwargs #coding=utf8 __author__ = 'Administrator' # 当函数的参数不确定时,可以使用*args和**kwargs.*args没有key值 ...
- 前段 format方法
a.为字符串创建format方法,用于字符串格式化 String.prototype.format=function (arg) { //console.log(this,arg); //this,当 ...
- 使用net.sf.cssbox实现网页截图
需要引用包,在pom.xml中添加引用: <dependency> <groupId>net.sf.cssbox</groupId> <artifactId& ...
- java字符串类型常量拼接与变量拼接的区别
前言 首先看下下面代码结果是什么? package cn.demo_01; public class StringDemo02 { public static void main(String[] a ...