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

思路:先用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

时间: 2024-10-22 10:54:56

POJ2331:Water pipe(IDA*)的相关文章

[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 s

ZOJ 2655 Water Pipe bfs 带方向状态

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2655 Water Pipe Time Limit: 5 Seconds      Memory Limit: 32768 KB Two waterworks want to connect to each other with water pipes. Just as the map shows, the waterworks sit on two corners

Java-NIO(九):管道 (Pipe)

Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 代码使用示例: 1 @Test 2 public void testPipe() throws IOException { 3 // 1.获取通道 4 Pipe pipe = Pipe.open(); 5 6 // 2.获取sink管道,用来传送数据 7 Pipe.SinkChannel sinkChannel = pipe.sink(); 8

linux C/C++:进程间通信-pipe

进程间通信-pipe 进程间通信 每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用户空间拷到内核缓冲区,进程2再从内核缓冲区把数据读走,内核提供的这种机制称为进程间通信(IPC,InterProcess Communication). pipe 管道(pipe)就是一项基本的进程间通信的方法. #include <unistd.h> int pipe(int pipefd[2]); 使用

TNS-12518 &amp; Linux Error:32:Broken pipe

最近一周,有一台ORACLE数据库服务器的监听服务在凌晨2点过几分的时间点突然崩溃,以前从没有出现过此类情况,但是最近一周出现了两次这种情况,检查时发现了如下一些信息: $ lsnrctl services   LSNRCTL for Linux: Version 10.2.0.4.0 - Production on 12-DEC-2014 08:22:34   Copyright (c) 1991, 2007, Oracle.  All rights reserved.   Connectin

洛谷 P1029 最大公约数和最小公倍数问题 Label:Water&amp;&amp;非学习区警告

题目描述 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件: 1.P,Q是正整数 2.要求P,Q以x0为最大公约数,以y0为最小公倍数. 试求:满足条件的所有可能的两个正整数的个数. 输入输出格式 输入格式: 二个正整数x0,y0 输出格式: 一个数,表示求出满足条件的P,Q的个数 输入输出样例 输入样例#1: 3 60 输出样例#1: 4 说明 P,Q有4种 3 60 15 12 12 15 60 3 代码

洛谷 P1546 最短网络 Agri-Net Label:Water最小生成树

题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其他农场.为了用最小的消费,他想铺设最短的光纤去连接所有的农场. 你将得到一份各农场之间连接费用的列表,你必须找出能连接所有农场并所用光纤最短的方案.每两个农场间的距离不会超过100000 输入输出格式 输入格式: 第一行: 农场的个数,N(3<=N<=100). 第二行..结尾: 后来的行包含了一

洛谷 P2726 阶乘 Factorials Label:Water

题目背景 N的阶乘写作N!,表示小于等于N的所有正整数的乘积. 题目描述 阶乘会变大得很快,如13!就必须用32位整数类型来存储,到了70!即使用浮点数也存不下了. 你的任务是找到阶乘最前面的非零位.举个例子: 5!=1*2*3*4*5=120,所以5!的最靠前的非零位是1. 7!=1*2*3*4*5*6*7=5040,所以最靠前的非零位是5. 输入输出格式 输入格式: 共一行,一个不大于4,220的正整数N 输出格式: 共一行,输出N!最靠后的非零位. 输入输出样例 输入样例#1: 7 输出样

Ionic2学习笔记(3):Pipe

作者:Grey 原文地址: http://www.cnblogs.com/greyzeng/p/5538630.html ? ? ? ? ? ? Pipe类似过滤器,比如,在一个字符串要展现在页面之前, 我们需要对这个字符串进行一些操作,比如:将字符串转化为大写,加一个前缀/后缀-- Pipe的作用就是来实现类似的需求: 模型如下: ? ? ? 假设一个字符串: "hello", 我们在展示这个字符串的时候,需要先转换为大写, 然后加一个后缀 " WORLD", 最