阿里在线测评

在一个10*10的棋盘上,每个格子有一个分数值(非负整数)。一个棋子从棋盘上的某一个起始位置移动到某一个终止位置。棋子每次在棋盘上可以朝上下左右4个方向移动,一共最多可以移动n步。每移动到一个格子上,则获得格子上相应分数。初始位置的分数自动获得。请问棋子如何移动,才能获得最多分数。建议使用C++。

#include <vector>
#include <time.h>
#include <iostream>  
using namespace std;


typedef struct{
    int x;
    int y;
    int score;
}Cor;

int solution(int x, int y, int n, int endx, int endy);
int inRange(int x, int y, int minx, int miny, int maxx, int maxy, int endx, int endy, int n, int i);
//10*10的棋盘
int T[10][10] = {
            {6,2,0,9,2,4,8,2,9,4},
            {6,2,0,9,2,4,8,2,9,4},
            {6,2,0,9,2,4,8,2,9,4},
            {6,2,0,9,2,4,8,2,9,4},
            {6,2,0,9,2,4,8,2,9,4},
            {6,2,0,9,2,4,8,2,9,4},
            {6,2,0,9,2,4,8,2,9,4},
            {6,2,0,9,2,4,8,2,9,4},
            {6,2,0,9,2,4,8,2,9,4},
            {6,2,999,9,2,4,8,2,9,4}
            };
int main()
{
    clock_t start,finish;
    double total;
    start=clock();

    int res = solution(1, 2, 11, 9, 2);

    printf("result:%d", res);
    finish=clock();
    total=(double)(finish-start)/CLOCKS_PER_SEC;  
    return 0;
}

/*
 *描述:解决棋盘最多分数问题
 *输入: x:起始位置x坐标
 *     y:起始位置y坐标
 *     n:一共走n步
 *     endx:结束位置x坐标
 *     endy:结束位置y坐标
 * 输出:最大分数值
 */
int solution(int x, int y, int n, int endx, int endy)
{
    vector<Cor> score,score2;
    Cor first;
    first.x = x;
    first.y = y;
    int tt = T[x][y];
    first.score = T[x][y];

    score.push_back(first);

    Cor temp;
    Cor nowCor;
    int i = 1;
    while(i++ <= n) {
        while(!score.empty()) {//取出一个Cor,计算下一步的信息
            nowCor = score.back();
            score.pop_back();

            temp = nowCor;
            temp.x++;
            if(inRange(temp.x, temp.y, 0, 0, 9, 9,endx, endy, n, i)) {
                temp.score += T[temp.x][temp.y];
                score2.push_back(temp);
            }

            temp = nowCor;
            temp.y++;
            if(inRange(temp.x, temp.y, 0, 0, 9, 9,endx, endy, n, i)) {
                temp.score += T[temp.x][temp.y];
                score2.push_back(temp);
            }

            temp = nowCor;
            temp.x--;
            if(inRange(temp.x, temp.y, 0, 0, 9, 9,endx, endy, n, i)) {
                temp.score += T[temp.x][temp.y];
                score2.push_back(temp);
            }

            temp = nowCor;
            temp.y--;
            if(inRange(temp.x, temp.y, 0, 0, 9, 9,endx, endy, n, i)) {
                temp.score += T[temp.x][temp.y];
                score2.push_back(temp);
            }
            score2.push_back(temp);//把下一步的信息放入score2中
        }
        //清空score,score2的内容再放回到score
        score.clear();
        for(vector<Cor>::iterator iter=score2.begin();iter!=score2.end();iter++) {
            score.push_back(*iter);
        }
        score2.clear();
    }
    //在score中找结束位置符合条件的,且分数最大的
    int tempMax = -1;
    for(vector<Cor>::iterator iter=score.begin();iter!=score.end();iter++) {
        if((*iter).x==endx && (*iter).y==endy) {
            tempMax = (*iter).score > tempMax ? (*iter).score : tempMax;
        }
    }
    return tempMax;
}

/*
 *描述:判断输入的坐标是否在棋盘内以及最终是否能达到终点
 *输入: x:位置x坐标
 *     y:位置y坐标
 *     minx:棋盘最小x坐标
 *     miny:棋盘最小y坐标
 *     maxx:棋盘最大x坐标
 *     maxy:棋盘最大y坐标
 *     endx:终点x坐标
 *     endy:终点y坐标
 *     n:一共走n步
 *     i:当前已走了i步
 * 输出:返回判断结果
 */
int inRange(int x, int y, int minx, int miny, int maxx, int maxy, int endx, int endy, int n, int i)
{
    if((abs(x-endx)+abs(y-endy)) > (n-i+1))
        return 0;
    return x<=maxx && x>=minx && y<=maxy && y>=miny;
}
原文地址:https://www.cnblogs.com/season-peng/p/6759517.html