码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2331:Water pipe(IDA*)

时间:2014-07-26 17:18:12      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:搜索   poj   

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. 
bubuko.com,布布扣
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

思路:先用bfs将x轴和y轴都搜一次,得到所有的h值,然后再dfs找到答案


#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;

int sx,sy,ex,ey,n,hx[1005],hy[1005],sum,ans;
struct node
{
    int l,c;
} a[10];

void bfs(int *h,int pos)
{
    int i;
    queue<int> Q;
    h[pos] = 0;
    Q.push(pos);
    while(!Q.empty())
    {
        pos = Q.front();
        Q.pop();
        for(i = 0; i<n; i++)
        {

            if(pos-a[i].l>=1 && h[pos-a[i].l]==-1)
            {
                h[pos-a[i].l] = h[pos]+1;
                Q.push(pos-a[i].l);
            }
            if(pos+a[i].l<=1000 && h[pos+a[i].l]==-1)
            {
                h[pos+a[i].l] = h[pos]+1;
                Q.push(pos+a[i].l);
            }
        }
    }
}

int IDA(node *a,int x,int deep,int kind)
{
    int i,hv;
    node tem[10];
    hv = kind?hy[x]:hx[x];
    if(hv == -1 || hv+deep>ans)
        return 0;
    if(hv == 0)
    {
        if(kind == 0) return IDA(a,sy,deep,1);
        else return 1;
    }
    for(i = 0; i<n; i++)
        tem[i] = a[i];
    for(i = 0; i<n; i++)
    {
        if(tem[i].c<=0)
            continue;
        tem[i].c--;
        if(x-tem[i].l>=1) if(IDA(tem,x-tem[i].l,deep+1,kind)) return 1;
        if(x+tem[i].l<=1000) if(IDA(tem,x+tem[i].l,deep+1,kind)) return 1;
        tem[i].c++;
    }
    return 0;
}

void solve()
{
    memset(hx,-1,sizeof(hx));
    memset(hy,-1,sizeof(hy));
    bfs(hx,ex);
    bfs(hy,ey);
    for(ans = 1; ans<=sum; ans++)
        if(IDA(a,sx,0,0))
            break;
    if(ans<=sum)
        printf("%d\n",ans);
    else
        printf("-1\n");
}

int main()
{
    int i,j;
    sum = 0;
    scanf("%d%d%d%d%d",&sx,&sy,&ex,&ey,&n);
    for(i = 0; i<n; i++)
        scanf("%d",&a[i].l);
    for(i = 0; i<n; i++)
    {
        scanf("%d",&a[i].c);
        sum+=a[i].c;
    }
    if(sx == ex && sy == ey)
        printf("0\n");
    else
        solve();

    return 0;
}


POJ2331:Water pipe(IDA*),布布扣,bubuko.com

POJ2331:Water pipe(IDA*)

标签:搜索   poj   

原文地址:http://blog.csdn.net/libin56842/article/details/38146245

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!