P2905 [USACO08OPEN]农场危机Crisis on the Farm
DP
设 f [ i ] [ j ] [ k ] 表示已经走了 i 步,向上走了 j 步,向右走了 k 步时能拯救的最多奶牛数(j,k可以为负,表示反向)
设 g [ i ] [ j ] 表示牛向上走 i 步,向右走 j 步后有多少奶牛恰好在草堆上(同样 i , j 可负)
那么 f [ i ] [ j ] [ k ] = max( f [ i-1 ] [ j -1 ] [ k ] , f [ i-1 ] [ j ] [ k-1 ] , f [ i-1 ] [ j+1 ] [ k ] ,f [ i-1 ] [ j ] [ k+1 ]) +g [ j ] [ k ]
因为数组下标不能为负,所以要把坐标集体加上一个 K
为了方便按字典序输出,我们要倒过来推,并且要按字典序的方向来枚举
即从 f [ k ] 推到 f [ 0 ] 枚举方向为 'E' 'N' 'S' 'W'
好像不太好讲,具体还是看代码吧,代码不难的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=,K=;
int n,m,k;
int xx[]={,,,-},yy[]={,,-,};
char mp[]={'E','N','S','W'};
int f[K<<][N][N],g[K<<][K<<];
int cow[N][],hay[N][];
int main()
{
n=read(); m=read(); k=read();
for(int i=;i<=n;i++) cow[i][]=read(),cow[i][]=read();
for(int i=;i<=m;i++) hay[i][]=read(),hay[i][]=read();
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(abs(hay[j][]-cow[i][])+abs(hay[j][]-cow[i][])<=k)//如果可以到达才计算贡献
g[hay[j][]-cow[i][]+K][hay[j][]-cow[i][]+K]++;//预处理g
for(int i=k;i>=;i--)
for(int x=K-i;x<=K+i;x++)
for(int y=K-i;y<=K+i;y++)//注意大小写K的区别
{
for(int o=;o<;o++) f[i][x][y]=max(f[i][x][y],f[i+][x+xx[o]][y+yy[o]]);
f[i][x][y]+=g[x][y];
}
printf("%d\n",f[][K][K]);
int posx=K,posy=K;//存当前位置
for(int i=;i<k;i++)
{
int o;
for(o=;o<;o++) if(f[i][posx][posy]==f[i+][posx+xx[o]][posy+yy[o]]+g[posx][posy]) break;//如果是从o推过来的就断开
posx+=xx[o]; posy+=yy[o];
printf("%c",mp[o]);
}
return ;
}
P2905 [USACO08OPEN]农场危机Crisis on the Farm的更多相关文章
- bzoj1605 / P2905 [USACO08OPEN]农场危机Crisis on the Farm
P2905 [USACO08OPEN]农场危机Crisis on the Farm 发现总步数$k<=30$,考虑用$k$瞎搞 设$f[u][i][j]$表示已经吹$u$次哨,全体奶牛向右走$i ...
- 洛谷P2905 [USACO08OPEN]农场危机Crisis on the Farm
P2905 [USACO08OPEN]农场危机Crisis on the Farm 题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每 ...
- 洛谷 P2905 [USACO08OPEN]农场危机Crisis on the Farm
题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每一堆里,30只奶牛一只踩在另一只的背上,叠成一座牛塔.牧场 里还有M(1 < ...
- P2905 [USACO08OPEN]农场危机Crisis on the Farm(简单dp+麻烦“回溯”)
惯例,化简题意(看长短决定难度) 一块草坪上有两种点(姑且称为a和b),各有坐标,现在能同时使所有a点向东西南北任意一个方向移动一个单位,若a点与b点重合,则答案增加重合数,求答案的最大值并且求出这个 ...
- bzoj1621 / P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm
P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm 基础dfs,按题意递归即可. #include<iostream> #include< ...
- [USACO08OPEN]农场周围的道路Roads Around The Farm BZOJ 1621 DFS
Farmer John's cows have taken an interest in exploring the territory around the farm. Initially, all ...
- BZOJ1605 [Usaco2008 Open]Crisis on the Farm 牧场危机
标题好长&&我是权限狗,汪汪! 题没看懂的我以为这是一道极难滴题目...然后,然后我就看懂题了. 数据少给了一个条件K <= 30...(没这条件还做个鬼...) f[k, i, ...
- BZOJ 1605 [Usaco2008 Open]Crisis on the Farm 牧场危机:dp【找转移路径】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1605 题意: 平面直角坐标系中,有n个点,m个标记(坐标范围1~1000). 你可以发出口 ...
- BZOJ 1605 [Usaco2008 Open]Crisis on the Farm 牧场危机 DP
题意:链接 方法: DP 解析: 第一眼搜索题,复杂度不同意dfs,并且牛的数量太多不能bfs,迭代更不可能,A*不会估价.可能记忆化? 等等记忆化我还搜个毛线- 直接改成DP就好了. 状态非常好想非 ...
随机推荐
- java判断姓是否合格 百家姓
package util; import java.lang.reflect.Array; public class FirstName { public static boolean ClearNa ...
- Java的JAR包, EAR包 ,WAR包 都是干什么的,有什么区别
JAR包:打成JAR包的代码,一般作为工具类,在项目中,会应用到N多JAR工具包: WAR包:JAVA WEB工程,都是打成WAR包,进行发布,如果我们的服务器选择TOMCAT等轻量级服务器,一般就打 ...
- Python之POST登录测试
不解释,直接上代码: #!/usr/bin/env python # -*- encoding: utf-8 -*- """ @version: v1.0 @author ...
- dubbo-admin打包和zookper安装
1 首选安装Zookper,下载zookeeper-3.5.3-beta版本,在这里我主要演示这个:下载地址:http://mirrors.hust.edu.cn/apache/zookeeper/ ...
- C#简单的图片合成及防止并发的办法
/// <summary> /// 合成图 /// </summary> private string ComposeCarBrandBadImage(AnonAttachme ...
- gitlab 添加ssh秘钥
在创建新的ssh秘钥对之前,要先确认一下电脑中是否已经有了一对秘钥: Git Bash on Windows / GNU/Linux / macOS / PowerShell: cat ~/.ssh/ ...
- (转)C++中使用C代码
昨晚看书的时候碰到一个问题,在C++中如何调用C代码...于是查了一下资料...发现了一个大神写的文章挺好的. -------------------------------------------- ...
- PS中图层混合模式的计算方法
https://zhuanlan.zhihu.com/p/23905865 长久以来一直用中文版本的PS,对于软件中的一些专业名字都是顾名思义,容易误入歧途,但当你真正看到英文版本的名字的时候才有豁然 ...
- GCD 学习(二)dispatch_queue_create创建Dispatch Queue
摘录于: http://zhuyanfeng.com/archives/3042 dispatch_queue_create 用于创建用户线程队列.可以创建Serial/Concurrent Disp ...
- ZROI2018提高day1t3
传送门 分析 考场上想到了先枚举p的长度,在枚举这个长度的所有子串,期望得分40~50pts,但是最终只得了20pts,这是因为我写的代码在验证中总是不断删除s'中的第一个p,而这种方式不能解决形如a ...