[poj 2331] Water pipe ID A*迭代加深搜索(dfs)
Water pipe
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 2265 Accepted: 602
Description
The Eastowner city is perpetually haunted with water supply shortages, so in order to remedy this problem a new water-pipe has been built. Builders started the pipe from both ends simultaneously, and after some hard work both halves were connected. Well, almost. First half of pipe ended at a point (x1, y1), and the second half – at (x2, y2). Unfortunately only few pipe segments of different length were left. Moreover, due to the peculiarities of local technology the pipes can only be put in either north-south or east-west direction, and be connected to form a straight line or 90 degree turn. You program must, given L1, L2, … Lk – lengths of pipe segments available and C1, C2, … Ck – number of segments of each length, construct a water pipe connecting given points, or declare that it is impossible. Program must output the minimum required number of segments.
Constraints
1 <= k <= 4, 1 <= xi, yi, Li <= 1000, 1 <= Ci <= 10
Input
Input contains integers x1 y1 x2 y2 k followed by 2k integers L1 L2 … Lk C1 C2 … Ck
Output
Output must contain a single integer – the number of required segments, or −1 if the connection is impossible.
Sample Input
20 10 60 50 2 70 30 2 2
Sample Output
4
Source
Northeastern Europe 2003, Far-Eastern Subregion
题目链接: id=2331">http://poj.org/problem?id=2331
题意:
在二维网格上给你起点,终点,与(n《=10)的管子(长度与数量)用最少的管子数完毕路径;
思路:由于管子不能切断。所以“盲目”dfs 长路漫漫。。。
仅仅好迭代加深!
——————–分开计算x。y轴————————–
1.预处理*h数组。计算i状态到终点(单维)的最最短路(估价系统)
for(ans=1;ans<=tot;ans++){if(dfs(a,sx,0,0)) break;}
2.“盲目”dfs(+剪枝)到终点(单维)
剪*
if(hv==-1||hv+dep>ans) return 0;
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node{
int l,num;
}a[11];
int n,h1[1010],h2[1010],ans;
int tx,ty,sx,sy;
int tot;
void cal(int *h,int pos)
{
queue<int> q;
h[pos]=0;
q.push(pos);
while(!q.empty())
{
pos=q.front();
q.pop();
for(int i=1;i<=n;i++)
{
int nex=pos-a[i].l;
if(nex>0&&h[nex]==-1)
{
h[nex]=h[pos]+1;
q.push(nex);
}
nex+=2*a[i].l;
if(nex<=1000&&h[nex]==-1)
{
h[nex]=h[pos]+1;
q.push(nex);
}
}
}
}
bool dfs(node *a,int x,int dep,int id)
{
int hv;
if(id==0) hv=h1[x];else hv=h2[x];
if(hv==-1||hv+dep>ans) return 0;
if(hv==0)
{
if(id==0) return dfs(a,sy,dep,1);
else return 1;
}
node tmp[10];
for(int i=1;i<=n;i++) tmp[i]=a[i];
for(int i=1;i<=n;i++)
if(tmp[i].num)
{
tmp[i].num--;
int now=x-tmp[i].l;
if(now>0) if(dfs(tmp,now,dep+1,id)) return 1;
now+=2*tmp[i].l;
if(now<=1000) if(dfs(tmp,now,dep+1,id)) return 1;
tmp[i].num++;
}
return 0;
}
void id_a_star()
{
memset(h1,-1,sizeof(h1));
memset(h2,-1,sizeof(h2));
cal(h1,tx);
cal(h2,ty);
for(ans=1;ans<=tot;ans++)
{
if(dfs(a,sx,0,0)) break;
}
if(ans<=tot)
printf("%d\n",ans);
else printf("-1\n");
}
int main()
{
scanf("%d%d%d%d%d",&sx,&sy,&tx,&ty,&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i].l);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].num);
tot+=a[i].num;
}
if(sx==tx&&sy==ty) printf("0\n");
else id_a_star();
}
[poj 2331] Water pipe ID A*迭代加深搜索(dfs)的更多相关文章
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
- BZOJ 1085 骑士精神 迭代加深搜索+A*
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1085 题目大意: 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个 ...
- vijos1308 埃及分数(迭代加深搜索)
题目链接:点击打开链接 题目描写叙述: 在古埃及.人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数.如:2/3=1/2+1/6,但不同意2/3=1/3+1/3,由于加数中有同样的.对于 ...
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
随机推荐
- uboot的readme
## (C) Copyright 2000 - 2008# Wolfgang Denk, DENX Software Engineering, wd@denx.de.## See file CREDI ...
- ZZULIoj 1913: 小火山的计算能力
Description 别人说小火山的计算能力不行,小火山很生气,于是他想证明自己,现在有一个表达式,他想计算出来. Input 首先是一个t(1<=20)表示测试组数.然后一个表达式,表达式长 ...
- python中split()函数讲解
本文讲述的是string.split(s[, sep[, maxsplit]]),针对string类型的split()函数.它主要是切割字符串,结果返回由字符串元素组成的一个列表,具体怎么使用看下面的 ...
- Knockout v3.4.0 中文版教程-1-入门和安装
英文原版教程:http://knockoutjs.com/documentation/introduction.html 注:此教程根据英文原版翻译,仅作练习,如有不足或错误,请指正 说明: 对原文中 ...
- 【01】如何在XMind中排列自由主题
如何在XMind中一招排列自由主题 在XMind思维导图软件中,用户可以随心所欲的添加自由主题,但由于自由主题的灵活性,造成了它的不整齐性,相对需要操持界面排列有序的用户来说,会造成一定的困扰. 第一 ...
- 动态修改字节码以替换用反射调用get set方法的形式
1. 起因 在前两天,为了解决websphere和JDK8上部署的应用发起webservice调用(框架用的cxf)时报错的问题,跟了一些代码,最终发现可以通过加上参数-Dcom.sun.xml.bi ...
- NYOJ 745 蚂蚁的难题(二)
蚂蚁的难题(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 下雨了,下雨了,蚂蚁搬家了. 已知有n种食材需要搬走,这些食材从1到n依次排成了一个圈.小蚂蚁对每种 ...
- 【Luogu】P2522Problemb(莫比乌斯反演)
题目链接 同Zip—Queries,但是用到容斥原理 设f(n,m)是(x,y)的对数,其中1<=x<=n,1<=y<=m 则有f(n,m)-f(a-1,n)-f(b-1,m) ...
- BZOJ 1226 [SDOI2009]学校食堂Dining ——状压DP
看到B<=8,直接状态压缩即可. dp[i][j][k]表示当前相对位置是关于i的,并且i以前的已经就餐完毕,j表示i和之后的就餐情况,k表示上一个就餐的人的相对位置. 然后Dp即可 #incl ...
- BZOJ 1861 [Zjoi2006]Book 书架 ——Splay
[题目分析] 模板题目. 首尾两个虚拟结点,十分方便操作. [代码] #include <cstdio> #include <cstring> #include <cma ...