[poj 2331] Water pipe ID A*迭代加深搜索(dfs)
来源:互联网 发布:sql注入的防护 编辑:程序博客网 时间:2024/06/05 00:16
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
题目链接: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)
- 迭代加深ID-DFS搜索算法
- 【DFS迭代加深搜索+剪枝】POJ_1011_Sticks
- poj 2286 The Rotation Game (迭代加深搜索)
- POJ 2248 迭代加深搜索
- [UVa 11212] Editing a Book (迭代加深搜索)
- 迭代加深搜索
- 迭代加深搜索
- 搜索-迭代加深
- uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索
- 埃及分数(迭代加深搜索)
- 埃及分数(迭代加深搜索)
- 埃及分数(迭代加深搜索)
- 迭代加深搜索(埃及分数)
- uva12558 (迭代加深搜索)
- 棋盘染色(迭代加深搜索)
- 剪格子(迭代加深搜索)
- 埃及分数(迭代加深搜索)
- STM32启动后系统初始化SystemInit()
- 【完全背包】HDU1963Investment【数据压缩】
- 二维码
- Spring事务Transaction配置的五种注入方式详解
- 史上最全的maven pom.xml文件教程详解
- [poj 2331] Water pipe ID A*迭代加深搜索(dfs)
- RabbitMq、ActiveMq、ZeroMq、kafka之间的比较,资料汇总
- Oracle ORA-01940 无法删除当前已连接用户
- java中两种随机数的产生方式
- lightoj 1246 - Colorful Board DP+组合数
- PHP 登录【完整代码】
- ubunto开启root账户并开启SSH远程访问
- iOS 9.3 升级后无法真机测试 Could not find Developer Disk Image
- HDU 1181 变形课