题目:

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. 对一个前端AngularJS,后端OData,ASP.NET Web API案例的理解

    依然chsakell,他写了一篇前端AngularJS,后端OData,ASP.NET Web API的Demo,关于OData在ASP.NET Web API中的正删改查没有什么特别之处,但在前端调 ...

  2. ASP.NET MVC异步验证是如何工作的02,异步验证表单元素的创建

    在上一篇"ASP.NET MVC异步验证是如何工作的01,jQuery的验证方式.错误信息提示.validate方法的背后"中,了解了jQuery如何验证,如何显示错误信息,本篇要 ...

  3. 线程中sleep方法和wait方法有什么区别?(转)

    本文转自https://www.cnblogs.com/linkstar/p/6043846.html 线程中sleep方法和wait方法有什么区别?   如果你没有接触过java的多线程,那么多对于 ...

  4. Win7 ArcGIS 10.5 安装错误 A service pack is required on this operatiing system

    前段时间有朋友反馈在Win7专业版下安装ArcGIS 10.5, 无法安装,弹出错误: A service pack is required on this operatiing system,如图: ...

  5. JavaScript 检查是否是数字

    //---------------------------------------------------------- // 功能:检查是否是数字 // 参数: // str // 返回值: // ...

  6. WordPress主题开发实例:get_term_by()获取指定分类链接

    根据名称获取链接 <?php //根据名称获取对应的id $term=get_term_by('name','新闻动态','category'); $term_id=$term->term ...

  7. 犯罪心理第八季/全集Criminal Minds迅雷下载

    本季Criminal Minds Season 8(2012)看点:新季预告一名犯人突发急病,他被送上救护车,救护车在途中发生车祸.犯人对押送他的警官痛下毒手,BAU小组成员再次聚集起来.BAU开始分 ...

  8. Windows下LuaJIT的编译和使用

    1.下载LuaJIT,download>> 2.编译 开始 –> 程序 –> Microsoft Visual Studio xx –> Visual Studio To ...

  9. Xcode修改项目的Build Location

    Xcode默认project通过build输出的目录,存放于目录 /Users/用户名/Library/Developer/Xcode/DerivedData/产品名称-hjwqiconnjhpjle ...

  10. Wireshark的简介

    -------------------------------------------------------------- <Wireshark数据包分析实战>这本书其实还很不错,当时买 ...