E. Pac Man for your New Phone

You are writing an app for your friend’s new Phone, the newPhone. Since you grew up on Pac Man, you want to write a simplified version of the game. In this game, the board is a rectangular grid and Pac Man starts at the upper left-hand corner. His goal is to get to the lower right-hand corner. He always moves one square to the right or one square down. Each square he goes to has a “goody” that’s worth a particular amount of points. Your score is simply the sum of the scores of the goodies in each square you have visited.

For example, if the game board looks like this (P indicates Pac Man’s starting location, and E indicates his ending location):
image.png
then Pac Man’s optimal strategy would be to move right, down, right, right again, then down to yield a score of 3 + 4 + 9 + 3 = 19.

The Problem:
Given a game board, determine the maximum possible score for Pac Man.

The Input:
There will be multiple game boards in the input file. The first input line contains a positive integer n, indicating the number of game boards to be processed. The first line of each game board will contain two positive integers, r (0 < r < 100) and c (1 < c < 100), representing the number of rows and columns for this game board. (The example above has three rows and four columns.) Each of the following r input lines will contain c tokens, representing the contents of that row. The first item on the first of these lines will be the character ‘P’, representing Pac Man’s original location and the last item on the last line will be the character ‘E’, representing Pac Man’s goal location. The rest of the items will be positive integers less than 1000. Items will be separated by a single space on each line.

The Output:
At the beginning of each test case, output “Game Board #g:”, where g is the input board number (starting from 1). For each game board, simply print out the maximum possible score for the game.

Leave a blank line after the output for each test case. Follow the format illustrated in Sample Output.

样例输入复制
2
3 4
P 3 2 8
1 4 9 3
6 2 2 E
2 2
P 5
401 E
样例输出复制
Game Board #1: 19

Game Board #2: 401

感谢队友

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,n,m;
    cin>>t;
    int k=0;
    int a[101][101];
    while(t--){
        k++;
        char c;
        cin>>n>>m;
        if(n==1&&m==2){
            cin>>c;
            cin>>c;
            cout<<"Game Board #"<<k<<": "<<0<<endl;
            cout<<endl;
        }
        memset(a,0,sizeof a);
        cin>>c;
        for(int i=2;i<=m;i++)cin>>a[1][i];
        for(int i=2;i<n;i++)
        for(int j=1;j<=m;j++)cin>>a[i][j];
        for(int i=1;i<m;i++)cin>>a[n][i];
        cin>>c;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        a[i][j]+=max(a[i-1][j],a[i][j-1]);
        cout<<"Game Board #"<<k<<": "<<a[n][m]<<endl;
        cout<<endl;
    }
    return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值