(算法)Game
题目:
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的更多相关文章
- B树——算法导论(25)
B树 1. 简介 在之前我们学习了红黑树,今天再学习一种树--B树.它与红黑树有许多类似的地方,比如都是平衡搜索树,但它们在功能和结构上却有较大的差别. 从功能上看,B树是为磁盘或其他存储设备设计的, ...
- 分布式系列文章——Paxos算法原理与推导
Paxos算法在分布式领域具有非常重要的地位.但是Paxos算法有两个比较明显的缺点:1.难以理解 2.工程实现更难. 网上有很多讲解Paxos算法的文章,但是质量参差不齐.看了很多关于Paxos的资 ...
- 【Machine Learning】KNN算法虹膜图片识别
K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...
- 红黑树——算法导论(15)
1. 什么是红黑树 (1) 简介 上一篇我们介绍了基本动态集合操作时间复杂度均为O(h)的二叉搜索树.但遗憾的是,只有当二叉搜索树高度较低时,这些集合操作才会较快:即当树的高度较高(甚至一种极 ...
- 散列表(hash table)——算法导论(13)
1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...
- 虚拟dom与diff算法 分析
好文集合: 深入浅出React(四):虚拟DOM Diff算法解析 全面理解虚拟DOM,实现虚拟DOM
- 简单有效的kmp算法
以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...
- 神经网络、logistic回归等分类算法简单实现
最近在github上看到一个很有趣的项目,通过文本训练可以让计算机写出特定风格的文章,有人就专门写了一个小项目生成汪峰风格的歌词.看完后有一些自己的小想法,也想做一个玩儿一玩儿.用到的原理是深度学习里 ...
- 46张PPT讲述JVM体系结构、GC算法和调优
本PPT从JVM体系结构概述.GC算法.Hotspot内存管理.Hotspot垃圾回收器.调优和监控工具六大方面进行讲述.(内嵌iframe,建议使用电脑浏览) 好东西当然要分享,PPT已上传可供下载 ...
- 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法
若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...
随机推荐
- 微信图片生成插件,页面截图插件 html2canvas,截图失真 问题的解决方案
html2canvas 是一个相当不错的 JavaScript 类库,它使用了 html5 和 css3 的一些新功能特性,实现了在客户端对网页进行截图的功能.html2canvas 通过获取页面的 ...
- 【Go命令教程】13. go tool cgo
cgo 也是一个 Go 语言自带的特殊工具.一般情况下,我们使用命令 go tool cgo 来运行它.这个工具可以使我们创建能够调用 C 语言代码的 Go 语言源码文件.这使得我们可以使用 Go 语 ...
- 改进架构,实现动态数据源,减少java维护
怎样不用写java代码来完毕开发? 对于大部分的产品和项目来说.页面变化是很头痛的事情.每次小功能上线,新客户到来,都须要进行定制改造,不断的开发维护.每次开发一方面要修改页面,一方面要修改serve ...
- MySQL查询报错 ERROR: No query specified
今天1网友,查询报错ERROR: No query specified,随后它发来截图. root case:查询语法错误 \G后面不能再加分号;,由于\G在功能上等同于;,假设加了分号,那么就是;; ...
- jQuery遍历刚创建的元素
对于刚创建的元素,使用jQuery的each方法,有时候会不起作用.解决方案大致有2种: 1.刚创建完的时候,就使用each方法 $('#btn').on("click", fun ...
- Spring Boot 2中对于CORS跨域访问的快速支持
原文:https://www.jianshu.com/p/840b4f83c3b5 目前的程序开发,大部分都采用前后台分离.这样一来,就都会碰到跨域资源共享CORS的问题.Spring Boot 2 ...
- ASIHTTPRequest学习笔记
1.creating requestsrequest分为同步和异步两种.不同之处在于开始request的函数:[request startSynchronous];[request startAsyn ...
- 在Oracle电子商务套件版本12.2中创建自定义应用程序(文档ID 1577707.1)
在本文档中 本笔记介绍了在Oracle电子商务套件版本12.2中创建自定义应用程序所需的基本步骤.如果您要创建新表单,报告等,则需要自定义应用程序.它们允许您将自定义编写的文件与Oracle电子商务套 ...
- spring源码之—Assert.notNull
org.springframework.util.Assert Assert翻译为中文为"断言".用过JUNIT的应该都知道这个概念了. 就是断定某一个实际的值就为自己预期想得到的 ...
- cocos2d-x CC_SYNTHESIZE_READONLY
//定义一个只读属性Label,在类定义中可以使用this->getLabel来访问 CC_SYNTHESIZE_READONLY(cocos2d::CCLabelTTF*,_label ...