[动态规划]飞机炸弹游戏 C++实现

来源:互联网 发布:淘宝店铺装修背景图 编辑:程序博客网 时间:2024/06/05 16:27
/** File name  : .plane.cpp* Function   :  飞机炸弹游戏    C++实现* Created on : 2016年6月16日* Author     : beijiwei@qq.com* Copyright  : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。任何单位和个人不经本人允许不得用于商业用途题目:游戏机最大 12行 X 5列的矩阵,其中 5 X 5 处在屏幕中,其中0 是空格, 1是金币, 2 是敌人初始状态, 飞机处在底部中间位置,飞机每次可以选择 左移一格,右移一格,或者保持不动游戏可以使用一次炸弹,炸弹可使屏幕 5X5区域内 敌人死亡1.遇到金币 +1分.2.遇到敌人 -1分3.如果当前得分为-1分,则 game over屏幕每次下降一格求最终得分,若game over,输出 -1;*/#include <cstdio>#include <iostream>#pragma warning(disable:4996)using namespace std;#define  INFN  -9999int get_current_max(int x, int y);int get_max(int x, int y, int z);int get_map_max(int x);int offset[3][2] = { 1, -1, 1, 0, 1, 1 };int map[12][5] = { 0 };int result[12][5] = { 0 };int main(int argc, char** argv){    int T = 0;    freopen("input.txt", "r", stdin);    cin >> T;    for (int test_case = 1; test_case <= T; test_case++)    {        int N = 0;        int max =INFN;        int cursor = 0;                int ret[12] = {0};        int omap[12][5] = { 0 };           cin >> N;        for (int i = 0; i < N; i++)//读入地图        {            for (int j = 0; j < 5; j++)            {                cin >> map[i][j];                omap[i][j] = map[i][j];            }        }        for (int c = 0; c < 12; c++)//初始化 ret[]        {            ret[c] = INFN;        }        for (int k = 0; k < N - 5 + 1; k++)//扔炸弹        {            for (int i = 0; i < N; i++)//恢复地图            {                for (int j = 0; j < 5; j++)                {                    map[i][j] = omap[i][j];                }            }            for (int i = N-k-5; i < N-k; i++)//整理地图            for (int j = 0; j < 5; j++)            {                if (map[i][j] == 2)                    map[i][j] = 0;            }            ret[cursor++] = get_map_max(N - 1);        }        for (int i = 0; i <= cursor; i++)        {            if (max < ret[i])                max = ret[i];        }        if (max == INFN)            max = -1;        cout << "#" << test_case << "  " << max << endl;    }       return 0;}int get_current_max(int x, int y){    switch (y)    {    case 0:        return x;        break;    case 1:        return x + 1;        break;    case 2:        return x - 1;        break;    default:        break;    }}int get_max(int x, int y, int z){    int max = 0;    if (x >= y)        max = x;    else        max = y;    if (max <= z)        max = z;    return max;}int get_map_max(int x){    int i = 0, j = 0, k = 0,max=INFN;    for (i = 0; i <= x; i++)//初始化 result[]    {        for (j = 0; j < 5; j++)        {            result[i][j] = INFN;        }    }    result[x][1] = get_current_max(0, map[x][1]);    result[x][2] = get_current_max(0, map[x][2]);    result[x][3] = get_current_max(0, map[x][3]);//初始化最底部一行    if (result[x][1] == -1 && result[x][2] == -1 && result[x][3] == -1)    {        return INFN;    }    for (k = x - 1; k >= 0; k--)// 每一行    {        for (i = 0; i < 5; i++)//第k行        {            int tmp[3] = { -9999, -9999, -9999 };            int tx = 0, ty = 0;            for (int b = 0; b < 3; b++)//得到每一行 每一个点的极值            {                tx = k + offset[b][0];                ty = i + offset[b][1];                if (tx >= 0 && tx <= x &&                    ty >= 0 && ty <= 4 &&                    result[tx][ty] != -9999)                {                    tmp[b] = get_current_max(result[tx][ty], map[k][i]);                }            }            result[k][i] = get_max(tmp[0], tmp[1], tmp[2]);            if (result[k][i] <= -1)                result[k][i] = INFN;        }        int count = 0;        for (int c = 0; c < 5; c++)//如果该行都是 -1, game over        {            if (result[k][c] <= -1)                count++;        }        if (count == 5)            return INFN;    }    for (int c = 0; c < 5; c++)    {        if (max < result[0][c])            max = result[0][c];    }    return max;}

0 0
原创粉丝点击