题目:

Jeff loves playing games, Gluttonous snake( an old game in NOKIA era ) is one of his favourites. However, after playing gluttonous snake so many times, he finally got bored with the original rules.In order to bring new challenge to this old game, Jeff introduced new rules :
1.The ground is a grid, with n rows and m columns(1 <= n, m <= 500).

2.Each cell contains a value v (-1<=vi<=99999), if v is -1, then this cell is blocked, and the snakecan not go through, otherwise, after the snake visited this cell, you can get v point.

3.The snake can start from any cell along the left border of this ground and travel until it finally stops at one cell in the right border.

4.During this trip, the snake can only go up/down/right, and can visit each cell only once.Special cases :
  a. Even in the left border and right border, the snake can go up and down. 
  b. When the snake is at the top cell of one column, it can still go up, which demands the player to  pay  all current points , then the snake will be teleported to the bottom cell of this column and vice  versa.

After creating such a new game, Jeff is confused how to get the highest score. Please help him to write a program to solve this problem.
Input
  The first line contains two integers n (rows) andm (columns), (1 <= n, m <= 500), separated by a      single space.
  Next n lines describe the grid. Each line contains m integers vi (-1<=vi<=99999) vi = -1 means the cell is blocked.
Output
  Output the highest score you can get. If the snake can not reach the right side, output -1.Limits

Sample Test
Input
4 4

-1 4 5 1

2 -1 2 4

3 3 -1 3

4 2 1 2
output
23
Path is as shown below

Input
4 4

-1 4 5 1

2 -1 2 4

3 3 -1 -1

4 2 1 2
output
16

Path is as shown below

思路:

1、回溯法

2、动态规划

代码:

1、回溯法

#include<iostream>
#include<vector> using namespace std; int cx[]={-,,};
int cy[]={,,}; void dfs(const vector<vector<int> > &grid,long long sum,int x,int y,vector<vector<bool> > &visited,long long &ans){
int m=grid.size();
int n=grid[].size(); if(y==n- && sum>ans)
ans=sum; for(int i=;i<;i++){
bool flag=false;
int nx=x+cx[i];
if(nx==-){
nx=m-;
flag=true;
}
if(nx==m){
nx=;
flag=true;
}
int ny=y+cy[i];
if(ny==n)
continue;
if(visited[nx][ny] || grid[nx][ny]==-)
continue;
visited[nx][ny]=true;
if(flag)
dfs(grid,grid[nx][ny],nx,ny,visited,ans);
else
dfs(grid,sum+grid[nx][ny],nx,ny,visited,ans);
visited[nx][ny]=false;
}
} int main(){
int val;
int row_num,col_num;
while(cin>>row_num>>col_num){
if(row_num> && col_num>){
vector<vector<int> > grid(row_num,vector<int>(col_num));
vector<vector<bool> > visited(row_num,vector<bool>(col_num,false));
for(int i=;i<row_num;i++){
for(int j=;j<col_num;j++){
cin>>val;
if(val>=-)
grid[i][j]=val;
else
return ;
}
} long long highestScore=;
long long sum=; for(int i=;i<row_num;i++){
if(grid[i][]==-)
continue;
visited[i][]=true;
dfs(grid,grid[i][],i,,visited,highestScore);
visited[i][]=false;
}
cout<<highestScore<<endl;
}
}
return ;
}

2、动态规划

#include<iostream>
#include<vector>
#include<stdlib.h> using namespace std; //int row_num,col_num; long long getScore(const vector<vector<int> > &grid,vector<vector<long long> > &scores); int main(){
int val;
int row_num,col_num;
while(cin>>row_num>>col_num){
if(row_num> && col_num>){
vector<vector<int> > grid(row_num,vector<int>(col_num));
for(int i=;i<row_num;i++){
for(int j=;j<col_num;j++){
cin>>val;
if(val>=-)
grid[i][j]=val;
else
return ;
}
} long long highestScore=;
vector<vector<long long> > scores(row_num,vector<long long>(col_num+,)); highestScore=getScore(grid,scores); if(highestScore!=)
cout<<highestScore<<endl;
else
cout<<-<<endl;
}
}
return ;
} long long getScore(const vector<vector<int> > &grid,vector<vector<long long> > &scores){
int row_num=grid.size();
int col_num=grid[].size();
long long tmp;
int last;
long long highestScore=; for(int j=;j<col_num;j++){
for(int i=;i<row_num;i++){
if(grid[i][j]==-){
scores[i][j+]=-;
continue;
} if(scores[i][j]==-)
continue; // move down
last=i;
tmp=scores[i][j]+grid[i][j];
scores[i][j+]=max(tmp,scores[i][j+]); for(int k=i+;;k++){
k=(k+row_num)%row_num;
if(grid[k][j]==- || k==i)
break;
else{
// transported
if(abs(k-last)>){
scores[k][j+]=scores[k][j+]>grid[k][j]?scores[k][j+]:grid[k][j];
tmp=grid[k][j];
}
else{
tmp+=grid[k][j];
if(tmp>scores[k][j+])
scores[k][j+]=tmp;
}
last=k;
}
} //move up
last=i;
tmp=scores[i][j]+grid[i][j];
scores[i][j+]=max(tmp,scores[i][j+]); for(int k=i-;;k--){
k=(k+row_num)%row_num;
if(grid[k][j]==- || k==i)
break;
else{
if(abs(k-last)>){
scores[k][j+]=scores[k][j+]>grid[k][j]?scores[k][j+]:grid[k][j];
tmp=grid[k][j];
}
else{
tmp+=grid[k][j];
if(tmp>scores[k][j+])
scores[k][j+]=tmp;
}
}
last=k;
}
}
} for(int i=;i<row_num;i++)
highestScore=max(highestScore,scores[i][col_num]); return highestScore;
}

(算法)Game的更多相关文章

  1. B树——算法导论(25)

    B树 1. 简介 在之前我们学习了红黑树,今天再学习一种树--B树.它与红黑树有许多类似的地方,比如都是平衡搜索树,但它们在功能和结构上却有较大的差别. 从功能上看,B树是为磁盘或其他存储设备设计的, ...

  2. 分布式系列文章——Paxos算法原理与推导

    Paxos算法在分布式领域具有非常重要的地位.但是Paxos算法有两个比较明显的缺点:1.难以理解 2.工程实现更难. 网上有很多讲解Paxos算法的文章,但是质量参差不齐.看了很多关于Paxos的资 ...

  3. 【Machine Learning】KNN算法虹膜图片识别

    K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  4. 红黑树——算法导论(15)

    1. 什么是红黑树 (1) 简介     上一篇我们介绍了基本动态集合操作时间复杂度均为O(h)的二叉搜索树.但遗憾的是,只有当二叉搜索树高度较低时,这些集合操作才会较快:即当树的高度较高(甚至一种极 ...

  5. 散列表(hash table)——算法导论(13)

    1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...

  6. 虚拟dom与diff算法 分析

    好文集合: 深入浅出React(四):虚拟DOM Diff算法解析 全面理解虚拟DOM,实现虚拟DOM

  7. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  8. 神经网络、logistic回归等分类算法简单实现

    最近在github上看到一个很有趣的项目,通过文本训练可以让计算机写出特定风格的文章,有人就专门写了一个小项目生成汪峰风格的歌词.看完后有一些自己的小想法,也想做一个玩儿一玩儿.用到的原理是深度学习里 ...

  9. 46张PPT讲述JVM体系结构、GC算法和调优

    本PPT从JVM体系结构概述.GC算法.Hotspot内存管理.Hotspot垃圾回收器.调优和监控工具六大方面进行讲述.(内嵌iframe,建议使用电脑浏览) 好东西当然要分享,PPT已上传可供下载 ...

  10. 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法

    若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...

随机推荐

  1. [Go] Http包 使用简介

    请求的结构 HTTP 的交互以请求和响应的应答模式.Go 的请求我们早就见过了,handler 函数的第二个参数 http.Requests.其结构为: type Request struct { M ...

  2. Parallel Programming--perfbook

    https://www.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html

  3. 安装APK的错误码(PackageManager.java)

    安装APK的错误码,定义在android源码中的这个文件中:frameworks\base\core\java\android\content\pm\PackageManager.java /** * ...

  4. 使用jQuery实现input数值的增量和减量

    在很多电商网站中,在购物车所在页面,涉及到商品数量的时候,都会提供一个+号按钮和-号按钮来实现增1和减1,并且只允许input中输入数值.Bootstrap TouchSpin这款插件就是针对此需求而 ...

  5. AutoMapper在MVC中的运用04-string映射各种类型、一个属性映射多个属性等

    本篇AutoMapper使用场景: ※ 类型转换,源string类型分别转换成int, DateTime,Type ※ 源和目标都包含复杂类型属性 ※ 把源中的一个属性映射到目标中的多个属性 类型转换 ...

  6. Client Dataset Basics

    文章出处:  http://www.informit.com/articles/article.aspx?p=24094 In the preceding two chapters, I discus ...

  7. Delphi把一张PNG横向分割成N张透明通道的图片

    Delphi新版本虽然集成了PngImage但是分割复制什么的却非常难用.稍微封装了一下.可以把一张PNG横向分割成N张.透明通道什么的都可以保持不变.typeTPngArray = array of ...

  8. android:activity活动的生命周期

    掌握活动的生命周期对任何 Android 开发者来说都非常重要,当你深入理解活动的生命 周期之后,就可以写出更加连贯流畅的程序,并在如何合理管理应用资源方面,你会发挥的 游刃有余.你的应用程序将会拥有 ...

  9. Jenkins CI CD

    原文:https://www.sunjianhua.cn/archives/jenkins-ci-cd.html 1.安装git 以下为简单应用,适合无gitlab服务器用户. #在git服务器(19 ...

  10. 对ORM的支持 之 8.4 集成JPA ——跟我学spring3

    8.4  集成JPA JPA全称为Java持久性API(Java Persistence API),JPA是Java EE 5标准之一,是一个ORM规范,由厂商来实现该规范,目前有Hibernate. ...