题目:

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. .NET开源分布式日志框架ExceptionLess实战演练(公开版)

    一.课程介绍 在以前,我们做日志收集大多使用 Log4net,Nlog 等框架,在应用程序变得复杂并且集群的时候,可能传统的方式已经不是很好的适用了,因为收集各个日志并且分析他们将变得麻烦而且浪费时间 ...

  2. Asp.net core使用IIS在windows上进行托管

    摘要 最近项目中,尝试使用asp.net core开发,在部署的时候,考虑现有硬件,只能部署在windows上,linux服务器暂时没有. 部署注意事项 代码中启用iis和Kestrel public ...

  3. SQL Server 2000 绿色精简版gsql适用于xp/win7/win8/win10

    老的程序员肯定都用过sql2000数据库,我在2006-2010年之间,做的不少网站也都是sql2000数据库的,但是后来随着mysql的兴起,就逐渐不再使用sql数据库了.但是最近有个客户的网站要修 ...

  4. GetBuiltProjectOutputRecursive error running Xamarin Forms iOS on Visual Studio

    Seems like I get this weird problem while running Xamarin.iOS on Visual studio. This happened after ...

  5. 关于pcie的备忘

    总线驱动:深度优先统计资源,深度滞后分配资源 资源包括Bus id和内存(prefectable和non-prefectable内存) 设备驱动:包括设备驱动层和消息通信 主要是四个部分: (1)中断 ...

  6. UITabBar 详解

    1.push时,将tabar隐藏,方法1,在push之前,加入如下代码: -(IBAction)btnOnClicked:(id)sender { SQVideoListViewController ...

  7. lemon OA 我长时间经历的第一个开源项目

    对于原作者来说, 他长时间运营了一个项目,lemon OA .目前,八百多star.在运营这个项目的过程中,我想说,他成了activiti 目前国内比较牛逼的几个人.还有 spring securit ...

  8. SharePoint JavaScript API 根据文件路径删除文件

    最近,有这么个需求,然后写了几行代码,记录一下.有需要的可以参考一下. 有几个需要注意的地方,就是文件URL要传相对地址,使用网站对象之前要Load一下. 当然,如果你的网站不在根路径下,还可以用oW ...

  9. Logcat多tag过滤

    当Android设备通过usb连接成功后,在logcat中能看到很多log信息,但太多了很容易将我们关注的日志给淹没掉,所以我们需要过滤.如果接入了不同的SDK,那么log的tag可能会不同,所以有时 ...

  10. docker 删除无用的镜像文件的命令小计

    df -h  查看当前服务器的内存情况 docker system prune  删除无用镜像文件命令 执行ok之后,再次查看内存情况.