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的更多相关文章

  1. java head space/ java.lang.OutOfMemoryError: Java heap space内存溢出

    上一篇JMX/JConsole调试本地还可以在centos6.5 服务器上进行监控有个问题端口只开放22那么设置的9998端口 你怎么都连不上怎么监控?(如果大神知道还望指点,个人见解) 线上项目出现 ...

  2. Eclipse中启动tomcat报错java.lang.OutOfMemoryError: PermGen space的解决方法

    有的项目引用了太多的jar包,或者反射生成了太多的类,异或有太多的常量池,就有可能会报java.lang.OutOfMemoryError: PermGen space的错误, 我们知道可以通过jvm ...

  3. myeclipse 内存不够用报错PermGen space 和 An internal error has occurred.

    最近项目中又增加了新的模块,项目的代码又多了不少.运行的时候总是报如下错误 Exception in thread "http-apr-80-exec-6" java.lang.O ...

  4. java.lang.OutOfMemoryError: PermGen space及其解决方法

    PermGen space的全称是Permanent Generation space,是指内存的永久保存区域OutOfMemoryError: PermGen space从表面上看就是内存益出,解决 ...

  5. User space 与 Kernel space

    学习 Linux 时,经常可以看到两个词:User space(用户空间)和 Kernel space(内核空间). 简单说,Kernel space 是 Linux 内核的运行空间,User spa ...

  6. java.lang.OutOfMemoryError: PermGen space错误解决方法

    1. MyEclipse 中报 PermGen space       window--> preferences-->Myclipse-->Servers-->Tomcat- ...

  7. Git使用出错:Couldn‘t reserve space for cygwin‘s heap, Win32

    今天使用Git在命令行下更新代码遇到了问题,起初觉得是自己安装某软件导致冲突,从网上搜索了一下找到类似问题,成功解决问题. 错误信息如下: E:\storm-sql>git pull origi ...

  8. 修改windows自带的Ctrl+Space输入法切换快捷键

    使用场景: 多为我等码农使用一些编辑器时,编辑器的默认代码提示热键为 ctrl+space ,但这个热键被系统的输入法开关占用.如果遇到可以设置快捷键的编辑器还好,要是不能设置的话(比如火狐浏览器的代 ...

  9. Disk Space Usage 术语理解:unallocated, unused and reserved

    通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...

随机推荐

  1. alert效果

    <!DOCTYPE html> <head> <meta charset="utf-8" /> <title>拉伸效果</ti ...

  2. json接口

    http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?apikey=7waqfqbprs7pajbz2 ...

  3. \r与\n的区别

    \r : return 到当前行的最左边. \n: newline 向下移动一行,并不移动左右. Linux中\n表示回车+换行: Windows中\r\n表示回车+换行. Mac中\r表示回车+换行 ...

  4. elasticsearch 初学 笔记(1)

    使用 curl进行数据的的格式, 1.下载curl,配置路径 格式 C:\Users\Administrator>curl -XPUT http://localhost:9200/dept/em ...

  5. MySQL索引原理及慢查询优化 转载

    原文地址: http://tech.meituan.com/mysql-index.html MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能 ...

  6. Git 使用教程

    Git 使用教程 更详细请参考:廖雪峰的官方网站 - Git教程 1. 安装Git客户端软件 Git for Windows http://msysgit.github.io/ 2. 创建版本库 两点 ...

  7. 给博客添加Flash时钟

    依稀记得在cnblogs很多大牛的博客里见到过大牛的新闻公告栏里那种动感十足的Flash时钟控件,先上图: 作为一名刚的接触博客菜鸟,自然免不了一番好奇.博客设置选项里翻来覆去找(以为是cnblogs ...

  8. 看懂UML类图

    这里不会将UML的各种元素都提到,我只想讲讲类图中各个类之间的关系: 能看懂类图中各个类之间的线条.箭头代表什么意思后,也就足够应对 日常的工作和交流: 同时,我们应该能将类图所表达的含义和最终的代码 ...

  9. js和jquery获取当前对象的子元素

    开发中经常遇到需要获取ul下的il对象,个人总结了js和jquery的方法. HTML片断: <ul class="box"> <li>子元素1</l ...

  10. Linux中的shell

    shell的含义: 首先shell的英文含义是"壳": 它是相对于内核来说的,因为它是建议在核的基础上,面向于用户的一种表现形式,比如我们看到一个球,见到的是它的壳,而非核. Li ...