P2905 [USACO08OPEN]农场危机Crisis on the Farm

题目描述

约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每一堆里,30只奶牛一只踩在另一只的背上,叠成一座牛塔.牧场 里还有M(1 < M < 1000)个高高的草垛.

作为出色的指挥家,约翰可以通过口哨指挥奶牛们移动.他的口哨有四个音,分别能使所有 的牛塔向东南西北四个方向移动一格.

每一次,当一个牛塔到达了一个草垛所在的格子,牛塔最上方的奶牛就会跳到草垛上,而且 不再下来,而其他奶牛仍然呈塔状站在草垛所在的格子里.当牛塔只剩一只奶牛时,这只奶牛也 会跳到草垛上.

突然,约翰大惊失色:原来邻家的奶缸爆炸了!滚滚而下的牛奶正朝着约翰的牧场冲来,不久就要将牧场淹没.约翰必须马上行动,用口哨声挽救奶牛们的生命.他要指挥奶牛尽量多地跳 上草操,草操上的奶牛将不会被淹死.

约翰还有K次吹口哨的机会.那他最多还能救多少奶牛呢?请计算最多能挽救的奶牛数,以及 达到这个数目约翰需要吹的口哨调子序列.序列用E,W,S,N表示东西南北.如果有多种序列能达到 要求,输出作为字符串最小的.

输入输出格式

输入格式:

  • Line 1: Three space-separated integers: N, M, and K

  • Lines 2..N+1: Line i+1 describes the X,Y location of a stack of 30 cows using two space-separated integers: X_i and Y_i

  • Lines N+2..N+M+1: Line i+N+1 describes the X,Y location of a haystack using two space-separated integers: X_i and Y_i

输出格式:

  • Line 1: A single integer that is the most number of cows that can be saved.

  • Line 2: K characters, the lexicographically least sequence of commands FJ should issue to maximize the number of cows saved.

输入输出样例

输入样例#1:

3 6 3
3 4
6 2
5 7
8 2
9 2
6 4
5 4
6 7
8 7
输出样例#1:

6
EEE

说明

Use the 'east' whistle three times, at which point the milk floods the area. Each haystack ends up saving 1 cow.

对于100%的数据,1\le K\le 30,1\le N,M,X_i,Y_i\le 10001≤K≤30,1≤N,M,X​i​​,Y​i​​≤1000

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 1500
using namespace std;
int n,m,K,ans;
int dx[]={,,,-};
int dy[]={,,-,};
char step[][][];
char C[]={'W','S','N','E'};
int cnt[][],f[][][]; //f[k][i][j]记录走k步,纵向移动了i-31步,横向移动了j-31步,所能拯救的最多的牛的数量。
int cawx[MAXN],cawy[MAXN],grassx[MAXN],grassy[MAXN];//记录牛和草垛的横纵坐标.
int main(){
scanf("%d%d%d",&n,&m,&K);
for(int i=;i<=n;i++)
scanf("%d%d",&cawx[i],&cawy[i]);
for(int i=;i<=m;i++)
scanf("%d%d",&grassx[i],&grassy[i]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
int cx=cawx[i]-grassx[j]; //第i头牛移动到第j个草垛,纵向最少所走的距离.
int cy=cawy[i]-grassy[j]; //第i头牛移动到第j个草垛,横向最少所走的距离.
if(abs(cx)<=&&abs(cy)<=)//因为k<=30,所以当有一个方向的距离大于30,就不用考虑了,因为一定不可能走到.
cnt[cx+][cy+]++; //否则cnt记录走纵向i步横向走j步所能拯救的牛的数量.
}
for(int k=;k<=K;k++)
for(int i=;i<=;i++)
for(int j=;j<=;j++){
f[k][i][j]=-0x3f3f3f3f;
step[k][i][j]='Z';
}
f[][][]=cnt[][]; //赋初值,最开始时所能拯救的牛的数量为0.
//这里要理解,因为他可以向上下左右走,为了防止负坐标的出现,我们把一开始时的原点坐标当做(31,31).
for(int k=;k<=K;k++)
for(int i=;i<=;i++)
for(int j=;j<=;j++)
f[k][i][j]=cnt[i][j]+max(max(f[k-][i-][j],f[k-][i+][j]),max(f[k-][i][j-],f[k-][i][j+]));
//这个状态转移方程就不用解释了,还是很容易理解的.
for(int i=;i<=;i++)
for(int j=;j<=;j++)
ans=max(ans,f[K][i][j]);
for(int i=;i<=;i++)
for(int j=;j<=;j++)
if(ans==f[K][i][j])
step[K][i][j]='A'; //如果为纵向走i步横向走j步是一种可行的走法,记录以方便求字典序最小.
for(int k=K-;k>=;k--)
for(int i=;i<=;i++)
for(int j=;j<=;j++)
for(int l=;l<;l++)
if(f[k][i][j]+cnt[i+dx[l]][j+dy[l]]==f[k+][i+dx[l]][j+dy[l]]&&step[k+][i+dx[l]][j+dy[l]]<'Z')
step[k][i][j]=C[l]; //倒序找出所有可能的走法.
cout<<ans<<endl;
int i=,j=;
for(int k=;k<K;k++){
cout<<step[k][i][j];
if(step[k][i][j]=='E') i--; //找字典序最小的.
else if(step[k][i][j]=='W') i++;
else if(step[k][i][j]=='S') j++;
else if(step[k][i][j]=='N') j--;
}
}

洛谷P2905 [USACO08OPEN]农场危机Crisis on the Farm的更多相关文章

  1. 洛谷 P2905 [USACO08OPEN]农场危机Crisis on the Farm

    题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每一堆里,30只奶牛一只踩在另一只的背上,叠成一座牛塔.牧场 里还有M(1 < ...

  2. bzoj1605 / P2905 [USACO08OPEN]农场危机Crisis on the Farm

    P2905 [USACO08OPEN]农场危机Crisis on the Farm 发现总步数$k<=30$,考虑用$k$瞎搞 设$f[u][i][j]$表示已经吹$u$次哨,全体奶牛向右走$i ...

  3. P2905 [USACO08OPEN]农场危机Crisis on the Farm

    传送门 DP 设 f [ i ] [ j ] [ k ] 表示已经走了 i 步,向上走了 j 步,向右走了 k 步时能拯救的最多奶牛数(j,k可以为负,表示反向) 设 g [ i ] [ j ] 表示 ...

  4. P2905 [USACO08OPEN]农场危机Crisis on the Farm(简单dp+麻烦“回溯”)

    惯例,化简题意(看长短决定难度) 一块草坪上有两种点(姑且称为a和b),各有坐标,现在能同时使所有a点向东西南北任意一个方向移动一个单位,若a点与b点重合,则答案增加重合数,求答案的最大值并且求出这个 ...

  5. 洛谷——P2908 [USACO08OPEN]文字的力量Word Power

    P2908 [USACO08OPEN]文字的力量Word Power 题目描述 Farmer John wants to evaluate the quality of the names of hi ...

  6. 洛谷——P2910 [USACO08OPEN]寻宝之路Clear And Present Danger

    P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 题目描述 Farmer John is on a boat seeking fabled treasur ...

  7. 洛谷 P3079 [USACO13MAR]农场的画Farm Painting

    P3079 [USACO13MAR]农场的画Farm Painting 题目描述 After several harsh winters, Farmer John has decided it is ...

  8. 洛谷 P2908 [USACO08OPEN]文字的力量Word Power

    P2908 [USACO08OPEN]文字的力量Word Power 题目描述 Farmer John wants to evaluate the quality of the names of hi ...

  9. 洛谷 P2906 [USACO08OPEN]牛的街区Cow Neighborhoods | Set+并查集

    题目: https://www.luogu.org/problemnew/show/P2906 题解: 垃圾水题 #include<cstdio> #include<algorith ...

随机推荐

  1. Java for LeetCode 110 Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  2. cetons 怎么强制卸载 PHP

    查看php版本命令: #php -v 这个命令是删除不干净的 #yum remove php 因为使用这个命令以后再用 #php -v 还是会看到有版本信息的..... 必须强制删除 #rpm -qa ...

  3. git设置只允许特定类型的文件

    git设置只允许特定类型的文件 # 忽略所有文件 * # 不忽略目录 !*/ # 不忽略文件.gitignore和*.foo !.gitignore !*.foo

  4. BZOJ 3990 [SDOI2015]排序

    题解: 首先很容易看出各个操作是互不影响的,即对于一个合法的操作序列,我们可以任意交换两个操作的位置而不影响合法性. 因此我们可以忽略操作先后的影响,只考虑这个操作是否会出现在操作序列中. 如果用2n ...

  5. c# XML-Object对象 序列化-反序列化

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  6. zabbix性能优化等

    摘自: http://blog.sina.com.cn/s/blog_4cbf97060101fcfw.html 非常好的一篇,值得有用

  7. 使用 HTML5 的 IndexedDB API

    1. [代码]判断是否支持 IndexedDB     var indexedDB = window.indexedDB || window.webkitIndexedDB || window.moz ...

  8. TP框架控制器和对应方法创建

    控制器和对应方法创建 控制器是MVC模式中的核心,TP默认有一个控制器:   Index控制器里面有一个操作方法:Index   我们在访问http://localhost:8080/Thinkphp ...

  9. JavaScript中函数的无限次运算问题

    开博客有一段时间了,一直没动笔,原因是确实没看到什么值得写的内容.直到今天在司徒正美的博客里看到一个问题. http://www.cnblogs.com/rubylouvre/archive/2012 ...

  10. 团队作业第5周——测试与发布(Alpha版本)

    1.发现的bug a.同时按下和蛇前进方向相反的键和垂直方向的任意一个键贪吃蛇会死亡(比如贪吃蛇向右行走,同时按左上或左下都会game over) b.刷新的苹果会在蛇身上出现 暂时还没能力修复,以后 ...