当前位置: 首页 > news >正文

POJ 2331 Water pipe IDA*

预处理估价函数。其他的套模板就可以了。

不过有一个地方没住意WA了2小时。

#include<stdio.h> #include<string.h> #include<queue> using namespace std; int sx,sy,tx,ty,n; int len[5],num[5]; int bound,total; int h[2]={1,-1}; int hx[1010],hy[1010]; bool ans; struct point{ int x; int step; }; int Min(int a,int b){ return a>b?b:a; } void bfs(int v,int * hx){ int i,j,u,xx; queue<struct point>que; struct point sta; sta.step=0; sta.x=v; que.push(sta); hx[v]=0; while(!que.empty()){ struct point tem; tem=que.front(); que.pop(); u=tem.x; for(i=0;i<2;i++){ for(j=1;j<=n;j++){ xx=u+len[j]*h[i]; if(xx<=0 || xx>1000 || hx[xx]!=-1) continue; if(hx[u]+1>total) continue; hx[xx]=hx[u]+1; struct point cur; cur.step=hx[xx]; cur.x=xx; que.push(cur); } } } } int dfs(int dep,int x,int *nu,int type){ int i,j,xx,next_bound; if(type==0){ if(dep+hx[x]+hy[sy]>bound) return dep+hx[x]+hy[sy]; if(hx[x]==0) return dfs(dep,sy,nu,1); } else if(type==1){ if(dep+hy[x]>bound) return dep+hy[x]; if(hy[x]==0){ ans=true; return dep; } } int tmp[5]; for(i=1;i<=n;i++) tmp[i]= nu[i]; int new_bound=1e7; for(i=0;i<2;i++){ for(j=1;j<=n;j++) if(tmp[j]){ tmp[j]--; xx=x+h[i]*len[j]; if(xx<=0 || xx>1000 ){ tmp[j]++; //一开始我这里忘了去恢复现场,WA了好久 continue; } if(type==0){ if(hx[xx]==-1){ tmp[j]++; continue; //一开始我这里忘了去恢复现场,WA了好久 } } if(type==1){ if(hy[xx]==-1){ tmp[j]++; continue; //一开始我这里忘了去恢复现场,WA了好久 } } next_bound = dfs(dep+1,xx,tmp,type); if(!ans) new_bound=Min(new_bound,next_bound); else return next_bound; tmp[j]++; } } return new_bound; } void IDA_STAR(){ memset(hx,-1,sizeof(hx)); memset(hy,-1,sizeof(hy)); bfs(tx,hx); bfs(ty,hy); if(hx[sx]==-1 || hy[sy]==-1){ printf("-1\n"); return; } bound=hx[sx]+hy[sy]; while(bound<=total && !ans){ bound=dfs(0,sx,num,0); } if(ans) printf("%d\n",bound); else printf("-1\n"); } int main(){ int i,j; scanf("%d%d%d%d",&sx,&sy,&tx,&ty); scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&len[i]); total=0; for(i=1;i<=n;i++){ scanf("%d",&num[i]); total+=num[i]; } ans=false; IDA_STAR(); }


相关文章:

  • 软件问题
  • POJ 3460 Booksort IDA*
  • SpringCloud系列八:自定义Ribbon配置
  • 结构之法算法之道blog最新博文集锦第6期CHM文件0积分下载
  • BZOJ4318: OSU!
  • MSBuild使用初步
  • python库--pandas--写入文本文件
  • WPF程序编译(从命令行到Visual Studio)
  • Hibernate学习(1)- 初识
  • C#下.NET配置文件使用(一)
  • JS运行机制(进程与线程的区分)
  • C#下.NET配置文件使用(二)
  • MAC为Apache2服务器配置多个虚拟主机
  • WPF下的布局(Layout、Panel)小记
  • let 与 const 的用法
  • echarts的各种常用效果展示
  • golang中接口赋值与方法集
  • Java超时控制的实现
  • leetcode388. Longest Absolute File Path
  • Linux下的乱码问题
  • Python实现BT种子转化为磁力链接【实战】
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • Xmanager 远程桌面 CentOS 7
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 给github项目添加CI badge
  • 观察者模式实现非直接耦合
  • 基于HAProxy的高性能缓存服务器nuster
  • 开源地图数据可视化库——mapnik
  • 类orAPI - 收藏集 - 掘金
  • 排序算法学习笔记
  • 前端攻城师
  • 入门到放弃node系列之Hello Word篇
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 提醒我喝水chrome插件开发指南
  • 原生 js 实现移动端 Touch 滑动反弹
  • Java总结 - String - 这篇请使劲喷我
  • #Java第九次作业--输入输出流和文件操作
  • #调用传感器数据_Flink使用函数之监控传感器温度上升提醒
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (C++17) std算法之执行策略 execution
  • (C语言)fgets与fputs函数详解
  • (Java)【深基9.例1】选举学生会
  • (LeetCode 49)Anagrams
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (剑指Offer)面试题34:丑数
  • (一) springboot详细介绍
  • (一) storm的集群安装与配置
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (转) Face-Resources
  • (转)用.Net的File控件上传文件的解决方案
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .java 指数平滑_转载:二次指数平滑法求预测值的Java代码