(算法)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#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法
若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...
随机推荐
- TCP握手与socket通信细节
http://www.jianshu.com/u/5qrPPM http://www.jianshu.com/p/f86512230707
- WebService使用实例
近期刚刚開始学习使用WebService的方法进行server端数据交互,发现网上的资料不是非常全, 眼下就结合收集到的一些资料做了一个小样例和大家分享一下~ 我们在PC机器javaclient中.须 ...
- 在busybox里使用ulimit命令
刚才想使用ulimit修改用户进程的用户栈的大小,发现busybox里没有这个命令,上google搜索得到如下解释: "ulimit" is a shell builtin, me ...
- 在ASP.NET MVC中实现Select多选
我们知道,在ASP.NET MVC中实现多选Select的话,使用Html.ListBoxFor或Html.ListBox方法就可以.在实际应用中,到底该如何设计View Model, 控制器如何接收 ...
- 【Devops】【docker】【CI/CD】docker启动的Jenkins容器 - 系统管理 - 全局工具配置 - 自动安装JDK、Maven、Git、Docker
本篇适用于jenkins是启动的docker容器,自动安装JDK Maven Git Docker等全局工具 ========================================= ...
- 【Elasticsearch】ES中时间查询报错:Caused by: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC 2017] with format [yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis]];
ES中时间查询报错:Caused by: ElasticsearchParseException[failed to parse date field [Sun Dec 31 16:00:00 UTC ...
- fastjson转换对象,属性首字母大小写的问题
请求Json数据的时候,传递过去的String类型转Json数据的时候经常有首字母是大写的情况,例如"LoginAccount":"02:00:00:62:73:74&q ...
- JavaScript中 location.host 与 location.hostname 的区别
JavaScript 中,大多数情况下,我们不会发现 location.host 与 location.hostname 的区别,因为大多数情况下,我们的网页用的是 80 端口. 他们的区别: loc ...
- centos 7 版本升级nginx
1.首先下载对应最新版nginx wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ng ...
- java 模拟发送post请求测试
方法一: HttpClient public void postTest(HttpServletRequest request,Integer type,String phone,String pas ...