当前位置: 首页 > news >正文

(算法)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[]={-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;
}

 

转载于:https://www.cnblogs.com/AndyJee/p/4887055.html

相关文章:

  • Java Web项目的发布
  • 学习git遇到的问题的提出与总结
  • 鸡蛋的硬度
  • 百度基础技术测试部一面2015/10/15 实习生
  • 在C#中使用官方驱动操作MongoDB
  • Android 百度推送服务
  • ISG2015
  • Oracle常见SQL语句
  • Python模块Scrapy导入出错:ImportError: cannot import name xmlrpc_client
  • php的lareval框架配置出错
  • 九度OJ 1160:放苹果 (DFS)
  • 58同城技术委员会执行主席沈剑:好的架构是进化来的,不是设计来的
  • php 下载doc文档
  • Sterling B2B Integrator与SAP交互 - 01 简介
  • 若烟火云朵只给你一人
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • Facebook AccountKit 接入的坑点
  • git 常用命令
  • Java 最常见的 200+ 面试题:面试必备
  • javascript 哈希表
  • javascript数组去重/查找/插入/删除
  • Linux后台研发超实用命令总结
  • Map集合、散列表、红黑树介绍
  • node入门
  • PV统计优化设计
  • RxJS: 简单入门
  • 测试开发系类之接口自动化测试
  • 创建一种深思熟虑的文化
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 缓存与缓冲
  • 坑!为什么View.startAnimation不起作用?
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 为什么要用IPython/Jupyter?
  • 用Python写一份独特的元宵节祝福
  • ​MPV,汽车产品里一个特殊品类的进化过程
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • ​什么是bug?bug的源头在哪里?
  • #define用法
  • #QT项目实战(天气预报)
  • (09)Hive——CTE 公共表达式
  • (23)Linux的软硬连接
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (vue)页面文件上传获取:action地址
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (非本人原创)史记·柴静列传(r4笔记第65天)
  • (附源码)SSM环卫人员管理平台 计算机毕设36412
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (生成器)yield与(迭代器)generator
  • (四)图像的%2线性拉伸
  • (转)Linux整合apache和tomcat构建Web服务器
  • (转)大型网站的系统架构
  • (转)清华学霸演讲稿:永远不要说你已经尽力了
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版