(算法)Game
2015-10-17 10:31
197 查看
题目:
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[]={-1,0,1}; int cy[]={0,1,0}; 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[0].size(); if(y==n-1 && sum>ans) ans=sum; for(int i=0;i<3;i++){ bool flag=false; int nx=x+cx[i]; if(nx==-1){ nx=m-1; flag=true; } if(nx==m){ nx=0; flag=true; } int ny=y+cy[i]; if(ny==n) continue; if(visited[nx][ny] || grid[nx][ny]==-1) 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>0 && col_num>0){ vector<vector<int> > grid(row_num,vector<int>(col_num)); vector<vector<bool> > visited(row_num,vector<bool>(col_num,false)); for(int i=0;i<row_num;i++){ for(int j=0;j<col_num;j++){ cin>>val; if(val>=-1) grid[i][j]=val; else return 0; } } long long highestScore=0; long long sum=0; for(int i=0;i<row_num;i++){ if(grid[i][0]==-1) continue; visited[i][0]=true; dfs(grid,grid[i][0],i,0,visited,highestScore); visited[i][0]=false; } cout<<highestScore<<endl; } } return 0; }
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>0 && col_num>0){ vector<vector<int> > grid(row_num,vector<int>(col_num)); for(int i=0;i<row_num;i++){ for(int j=0;j<col_num;j++){ cin>>val; if(val>=-1) grid[i][j]=val; else return 0; } } long long highestScore=0; vector<vector<long long> > scores(row_num,vector<long long>(col_num+1,0)); highestScore=getScore(grid,scores); if(highestScore!=0) cout<<highestScore<<endl; else cout<<-1<<endl; } } return 0; } long long getScore(const vector<vector<int> > &grid,vector<vector<long long> > &scores){ int row_num=grid.size(); int col_num=grid[0].size(); long long tmp; int last; long long highestScore=0; for(int j=0;j<col_num;j++){ for(int i=0;i<row_num;i++){ if(grid[i][j]==-1){ scores[i][j+1]=-1; continue; } if(scores[i][j]==-1) continue; // move down last=i; tmp=scores[i][j]+grid[i][j]; scores[i][j+1]=max(tmp,scores[i][j+1]); for(int k=i+1;;k++){ k=(k+row_num)%row_num; if(grid[k][j]==-1 || k==i) break; else{ // transported if(abs(k-last)>1){ scores[k][j+1]=scores[k][j+1]>grid[k][j]?scores[k][j+1]:grid[k][j]; tmp=grid[k][j]; } else{ tmp+=grid[k][j]; if(tmp>scores[k][j+1]) scores[k][j+1]=tmp; } last=k; } } //move up last=i; tmp=scores[i][j]+grid[i][j]; scores[i][j+1]=max(tmp,scores[i][j+1]); for(int k=i-1;;k--){ k=(k+row_num)%row_num; if(grid[k][j]==-1 || k==i) break; else{ if(abs(k-last)>1){ scores[k][j+1]=scores[k][j+1]>grid[k][j]?scores[k][j+1]:grid[k][j]; tmp=grid[k][j]; } else{ tmp+=grid[k][j]; if(tmp>scores[k][j+1]) scores[k][j+1]=tmp; } } last=k; } } } for(int i=0;i<row_num;i++) highestScore=max(highestScore,scores[i][col_num]); return highestScore; }
相关文章推荐
- 从零开始学做机器人——第四章【mega128单片机对IO口的控制】
- HTTPClient
- 匈牙利算法改进 之 队列优化寻找极大增广路径集 —— Hopcroft-Karp算法 【记录】
- vim学习笔记(一)
- Maven手动安装jar包-Oracle Jdbc
- cocos2d-x moveTo的使用
- 在一个窗口中显示多个视频,并在每个子窗口左上角显示系统时间,函数cvShowManyImages是改写的
- 安装淘宝gem出错,报404++安装cocoapods 报ERROR: While executing gem ... (Errno::EPERM) Operation not permit
- java中两个double类型数据的大小
- noi2015获奖通知
- python中的可变与不可变
- 程序员常去的14个顶级开发社区
- Difference between Key, Primary Key, Unique Key and Index in MySQL
- Unity3d之Socket UDP协议
- 解决吐司连续弹出,长时间不消失(简洁)
- noip复赛报名通知
- 非常简单清晰的子网划分知识
- 直接在安卓运行C
- C语言实现简单的ATM机功能
- AndroidManifest.xml文件详解