# poj2331 Water pipe IDA*

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

n代表管子数量。接下来n个整数分别代表每个管子的长度，在接着n个整数分别代表每个管子的数量。

A*以及IDA*都是启发式搜索，他们都是在一般搜索的基础上增加了启发式函数（估价函数），对于一个启发式搜索，估价函数是非常重要的，估价函数通俗来讲就是一个估算到目的地要花费多少，有了一个好的估价函数，就少了很多不必要的搜索。相反，一个错误估价函数，会将你的搜索引入歧途。既然估价函数对我们这么重要，那我们应该怎么求呢？其实在不同的题目之中有不同的估价函数，比较常见的就是曼哈顿函数，通过计算两点之间绝对值之差来算。

WA的原因

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<set>
#include<queue>
using namespace std;
const int maxn=1001,inf=1e9;
int sx,sy,tx,ty,sum,ans,h1[maxn],h2[maxn],n;
struct node{
int len,num;
};
bool dfs1(node *a,int x,int z){
int i,j;
if(h2[x]==-1 || h2[x]+z>ans) return false;
if(h2[x]==0){
//printf("->h2--%d___%d\n",x,z);
return true;
}
node b[11];
for(i=1;i<=n;i++)
b[i]=a[i];
for(i=1;i<=n;i++)
if(b[i].num>0){
b[i].num--;
if(x-b[i].len>=1)
if(dfs1(b,x-b[i].len,z+1)){
//printf("->h2--%d\n",x);
return true;
}
if(x+b[i].len<=1000)
if(dfs1(b,x+b[i].len,z+1)){
//printf("->h2--%d\n",x);
return true;
}
b[i].num++;
}
return false;
}
bool dfs(node *a,int x,int z){
int i,j;
if(h1[x]==-1 || h1[x]+z>ans) return false;
if(h1[x]==0){
//printf("%d__%d\n",x,z);
//for(i=1;i<=n;i++) printf("%d-----",a[i].num);
return dfs1(a,sy,z);
}
node b[11];
for(i=1;i<=n;i++)
b[i]=a[i];
for(i=1;i<=n;i++)
if(b[i].num>0){
b[i].num--;
if(x-b[i].len>=1)
if(dfs(b,x-b[i].len,z+1)){
//printf("->h1--%d----id:%d\n",x,i);
return true;
}
if(x+b[i].len<=1000)
if(dfs(b,x+b[i].len,z+1)){
//printf("->h1--%d----id:%d=====x+b[i]=%d\n",x,i,x+b[i].len);
return true;
}
b[i].num++;
}
return false;
}
node a[11];
void bfs(int *h,int st){
int i,j;
queue <int> q;
h[st]=0;
q.push(st);
while(!q.empty()){
int x;
x=q.front();
q.pop();
for(i=1;i<=n;i++){
if(x-a[i].len>=1)
if(h[x-a[i].len]==-1){
h[x-a[i].len]=h[x]+1;
q.push(x-a[i].len);
}
if(x+a[i].len<=1000)
if(h[x+a[i].len]==-1){
h[x+a[i].len]=h[x]+1;
q.push(x+a[i].len);
}
}
}
}
void id_astar(){
int i;
if(sx==tx && sy==ty){
puts("0");
return;
}
memset(h1,-1,sizeof(h1));
memset(h2,-1,sizeof(h2));
bfs(h1,tx);
bfs(h2,ty);
/*for(i=1;i<=1000;i++)
if(h1[i]!=-1 || h2[i]!=-1)
printf("h1[%d]=%d    h2[%d]=%d\n",i,h1[i],i,h2[i]);*/
for(ans=1;ans<=sum;ans++){
//if(ans==4)
//system("pause");
if(dfs(a,sx,0))
break;
}
if(ans<=sum)
printf("%d\n",ans);
else puts("-1");
return;
}
int main(){
int i,j,k;
int x1,y1,x2,y2;
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
scanf("%d%d%d%d%d",&sx,&sy,&tx,&ty,&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i].len);
for(i=1;i<=n;i++){
scanf("%d",&a[i].num);
sum+=a[i].num;
}
id_astar();
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120