1171. Lost in Space
http://acm.timus.ru/problem.aspx?space=1&num=1171
一天的时间,WA了N遍,居然是因为数组开小了呀,我勒个去!鄙视自己。。。。。。
我是从第 1 层往上更新的
dp[level][x][y][day] 表示到第level层的(x,y)位置(第level层已经用完,该去第level+1层)且用了 day 天时的最多食物量
d[level][x][y][x1][y1][day] 表示在第level层 从(x,y)到(x1,y1) 且用day天 最多获取事物量
d[level][x][y][x1][y1][day]的求取过程可以爆搜
dp[level][x][y][day]=max(dp[level][x][y][day] , dp[level-1][x1][y1][day1]+d1[level][x][y][x1][y1][day-day1])
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector> using namespace std; const double eps=1e-9;
const int INF=0x3f3f3f3f;
const int N=17;
int dp[N][4][4][16*N];
int fx[N][4][4][16*N],fy[N][4][4][16*N],fd[N][4][4][16*N];
int d1[N][4][4][4][4][N];
int d[N][4][4][4][4][N];
int food[N][4][4];
int down[N][4][4];
int X[]={0,1,0,-1};
int Y[]={1,0,-1,0};
char dir[]={'E','S','W','N'};
bool visited[4][4];
int level,sx,sy;
void dfs(int x,int y,int f,int ln,int path)
{
visited[x][y]=true;
f+=food[level][x][y];
++ln;
if(d1[level][sx][sy][x][y][ln]<f)
{
d1[level][sx][sy][x][y][ln]=f;
d[level][sx][sy][x][y][ln]=path;
}
for(int i=0;i<4;++i)
{
int x1=x+X[i];
int y1=y+Y[i];
if(x1>=0&&x1<4&&y1>=0&&y1<4&&!visited[x1][y1])
{
int path1=(path|(i<<(2*(ln-1))));
dfs(x1,y1,f,ln,path1);
}
}
visited[x][y]=false; }
int main()
{
//freopen("data.in","r",stdin);
int n,ex,ey;
scanf("%d",&n);
int days=n*16;
for(int i=n;i>=1;--i)
{
for(int x=0;x<4;++x)
for(int y=0;y<4;++y)
scanf("%d",&food[i][x][y]);
for(int x=0;x<4;++x)
for(int y=0;y<4;++y)
scanf("%d",&down[i][x][y]);
}
scanf("%d %d",&ex,&ey);--ex;--ey;
for(int i=1;i<=n;++i)
for(int x=0;x<4;++x)
for(int y=0;y<4;++y)
{
for(int x1=0;x1<4;++x1)
for(int y1=0;y1<4;++y1)
for(int l=0;l<=days;++l)
d1[i][x][y][x1][y1][l]=-1;
memset(visited,false,sizeof(visited));
level=i;sx=x;sy=y;
dfs(x,y,0,0,0);
} memset(dp,-1,sizeof(dp)); for(int x=0;x<4;++x)
for(int y=0;y<4;++y)
dp[0][x][y][0]=0; for(int i=0;i<n;++i)
for(int x=0;x<4;++x)
for(int y=0;y<4;++y)
for(int l=0;l<=days;++l)
if(dp[i][x][y][l]>=0&&(down[i+1][x][y]||i==0))
{
for(int x1=0;x1<4;++x1)
for(int y1=0;y1<4;++y1)
for(int r=1;r<=16;++r)
if(d1[i+1][x1][y1][x][y][r]!=-1)
{
if(dp[i+1][x1][y1][l+r]<dp[i][x][y][l]+d1[i+1][x1][y1][x][y][r])
{
add[i+1][x1][y1][l+r]=r;
fx[i+1][x1][y1][l+r]=x;
fy[i+1][x1][y1][l+r]=y;
fd[i+1][x1][y1][l+r]=l;
}
}
}
int k=1;
for(int l=1;l<=days;++l)
if(dp[n][ex][ey][l]*k>dp[n][ex][ey][k]*l)
k=l;
printf("%.4f\n",1.0*dp[n][ex][ey][k]/k);
vector<char>ans;
int deep=n;
while(deep>0)
{
int x1=fx[deep][ex][ey][k],y1=fy[deep][ex][ey][k],k1=fd[deep][ex][ey][k];
int path=d[deep][ex][ey][x1][y1][k-k1];
for(int i=1;i<k-k1;++i)
{
ans.push_back(dir[path&0x3]);
path=path>>2;
}
ex=x1;ey=y1;k=k1; if(deep>1)
{ans.push_back('D');}
--deep;
}
printf("%d\n",ans.size());
for(unsigned int i=0;i<ans.size();++i)
printf("%c",ans[i]);
if(ans.size()>0)
printf("\n"); return 0;
}
1171. Lost in Space的更多相关文章
- java head space/ java.lang.OutOfMemoryError: Java heap space内存溢出
上一篇JMX/JConsole调试本地还可以在centos6.5 服务器上进行监控有个问题端口只开放22那么设置的9998端口 你怎么都连不上怎么监控?(如果大神知道还望指点,个人见解) 线上项目出现 ...
- Eclipse中启动tomcat报错java.lang.OutOfMemoryError: PermGen space的解决方法
有的项目引用了太多的jar包,或者反射生成了太多的类,异或有太多的常量池,就有可能会报java.lang.OutOfMemoryError: PermGen space的错误, 我们知道可以通过jvm ...
- myeclipse 内存不够用报错PermGen space 和 An internal error has occurred.
最近项目中又增加了新的模块,项目的代码又多了不少.运行的时候总是报如下错误 Exception in thread "http-apr-80-exec-6" java.lang.O ...
- java.lang.OutOfMemoryError: PermGen space及其解决方法
PermGen space的全称是Permanent Generation space,是指内存的永久保存区域OutOfMemoryError: PermGen space从表面上看就是内存益出,解决 ...
- User space 与 Kernel space
学习 Linux 时,经常可以看到两个词:User space(用户空间)和 Kernel space(内核空间). 简单说,Kernel space 是 Linux 内核的运行空间,User spa ...
- java.lang.OutOfMemoryError: PermGen space错误解决方法
1. MyEclipse 中报 PermGen space window--> preferences-->Myclipse-->Servers-->Tomcat- ...
- Git使用出错:Couldn‘t reserve space for cygwin‘s heap, Win32
今天使用Git在命令行下更新代码遇到了问题,起初觉得是自己安装某软件导致冲突,从网上搜索了一下找到类似问题,成功解决问题. 错误信息如下: E:\storm-sql>git pull origi ...
- 修改windows自带的Ctrl+Space输入法切换快捷键
使用场景: 多为我等码农使用一些编辑器时,编辑器的默认代码提示热键为 ctrl+space ,但这个热键被系统的输入法开关占用.如果遇到可以设置快捷键的编辑器还好,要是不能设置的话(比如火狐浏览器的代 ...
- Disk Space Usage 术语理解:unallocated, unused and reserved
通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...
随机推荐
- hive的使用01
1.安装mysql数据库 1.1 查看本机是否安装了mysql数据库(rpm -qa | grep mysql)
- ubantu14下vim的配置...
日出江花红胜火,春来江水绿如蓝.---
- .NET HttpWebRequest/WebClient网络请求第一次很慢解决方案
不使用代理: <?xml version="1.0" encoding="utf-8" ?> <configuration> <s ...
- CentOS 7 Rescure
之前从来没想过会在Linux系统中使用这个东西-- 今天系统无法启动了!!! 一.开机进度条卡住了.查看一下字符卡在哪里了? Starting MySQL Community Server... 就是 ...
- Freemarker中日期时间格式出错
今天遇到一个奇怪的问题.同事访问我电脑发布的程序页面,freemarker日期格式报错.而其他电脑访问则没有问题. 先贴出错误信息. FreeMarker template error The str ...
- 回调函数及数组中sort()方法实现排序的原理
1.回调函数:把一个方法A当一个参数值传递到另外一个函数B中,在B执行的过程当中我们随时根据需求让A方法执行: 什么是回调 :它是异步编程基本的方法,需要异步处理的时候一般采用后续传递的方式,将后 ...
- iOS - Phone 电话
1.调用电话 1.1 拨打系统电话 调用系统自带的打电话程序,要跳转到打电话程序,打完电话自动跳转回来. 在 iOS9.0 + 系统隐私控制里禁止查询设备中已安装的 App,所以在 iOS9.0 + ...
- stm32串口输出丢失第一个字符的问题及原因
因为TC和TXE 标志位在复位的时候会被置1,导致第一次没有发送. 所以在初始化串口的时候可以增加一句 USARTx->SR=0;(如 USART1->SR=0;)
- 规则“Windows Server 2003 FILESTREAM 修补程序检查” 失败。
近期在客户环境搭建SQL故障转移群集,操作系统为SQL Server 2012R2,数据库版本为SQL Server2008 R2,在安装过程中遇到问题:没有安装Windows Server 2003 ...
- 附加类型的实体失败,因为相同类型的其他实体已具有相同的主键值。在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 时如果图形中的任何实体具有冲突键值
var list= DAL.LoadEntities(x => x.OrderCode == orderCode).AsNoTracking().ToList().FirstOrDefault( ...