【面试笔试算法】Program 5 : 推箱子 (网易游戏笔试题)

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

推箱子是一款经典游戏。如图所示,灰色格子代表不能通过区域,蓝色方格是箱子,黑色圆形代表玩家,含有圆点的格子代表目标点。

【面试笔试算法】Program 5 : 推箱子 (网易游戏笔试题)

规定以下规则:

1、一局游戏中只会有一个箱子,一个玩家和一个目标点。

2、通过方向键控制玩家移动。

3、图中的灰色格子代表墙壁,玩家与箱子都不能通过。

4、推到墙壁的箱子,就无法再将箱子推离墙壁,因为玩家无法到达箱子靠墙壁的一侧去推箱子。也就是说箱子只能以“被推”的方式被移动,不是以“被拉”的方式被移动。但如果玩家将箱子推至墙壁后,垂直墙壁的两侧没有阻碍物,则玩家可以朝这两个不同的方向推移箱子。如果箱子进入角落,就没有办法再推动这个箱子了。

5、玩家是不能走出场景的。玩家推着箱子到达场景边缘,如果继续点击使玩家和箱子向墙壁前进的方向键,箱子和人都会保持不动。玩家的前进方向上如果有墙壁,也是不能前进的。但是这些点击都视为合理的输入。

6、箱子一旦到达目标点,就不能再移动了。但这时,玩家仍然可以在场景内*行动。如果继续尝试推箱子,那么玩家将会和箱子一起保持在原地不动。

现在,给出一种方向键的点击方案,请判断,这种方案是否能使箱子最终停在目标点上。为了方便表示,我们以0代表空白格子,以4代表不能通过区域,以1代表玩家,以3代表箱子,以2代表目标点。

输入

第一行数据包含三个整数,N,M,S。其中,N(0 < N <= 100)代表格子的宽度,M(0 < M <= 100)代表格子的高度,S(0 < S <= 200)代表测试点的个数。

接下来的M行,每行都会有N个字符,描述当前的盘面。

接下来的S行,每行都代表一个测试点。每行都以一个整数T(0 < T <= 10000)开头,接下来是一个空格和T个字符。这T个字符仅由d,u,l,r这四个字母组成,分别代表了敲击向下,向上,向左,向右的方向键。

输出

对于每个测试点,输出最后箱子是否在目标点上。如果是,输出YES,如果不是,则输出NO。

样例输入
5 4 3
00000
13000
00200
00000
4 rurd
6 urdldr
6 rrrurd
样例输出
YES

YES

NO

代码:
#include "stdafx.h"
#include <string>
#include <stdio.h>
#include <iostream>

using namespace std;

char Board[ 100][100 ]; // 棋盘
char Boardbak[100][100 ];

int main()
{
    int n, m , s;
    string temp (100, '0');
    int player_x = 0 , player_y = 0;
    int player_x_bak = 0 , player_y_bak = 0;
    bool havaPlayer = false ;
    cin >> n >> m >> s;
    for (int x = 0; x < m; ++x)
    {
        cin >> temp;
         for ( int y = 0 ; y < n; ++y )
         {
             if (! havaPlayer && temp [y] == '1')
             {
                player_x_bak = x;
                player_y_bak = y;
                havaPlayer = true ;
             }
            Boardbak [x][ y] = temp [y];
         }
    }

    while (s--)
    {
         bool isSuccess = false;
         int count = 0;
        string path (10000, '0');
        cin >> count >> path;

         //初始化棋盘
         for ( int i = 0 ; i < m; ++i )
         {
             for ( int j = 0 ; j < n; ++j )
             {
                Board [i][ j] = Boardbak [i][ j];
             }
         }
         //初始化玩家位置
        player_x = player_x_bak;
        player_y = player_y_bak;

         for ( int z = 0 ; z < count;++z )
         {
             if ( path[z ] == 'd' )//向下
             {
                 if ( player_x + 2 < m) // 判断下面没有越界
                 {
                     if (! isSuccess && Board [player_x + 1][ player_y] == '3')// 判断下面有箱子
                     {
                         if ( Board[player_x + 2 ][player_y] == '0')// 判断箱子下面是空格
                         {
                             //更新棋盘
                            Board [player_x][player_y] = '0' ;
                            Board [player_x + 1][ player_y] = '1' ;
                            Board [player_x + 2][ player_y] = '3' ;
                             //更新玩家位置
                            player_x = player_x + 1;
                         }
                         else if (Board[player_x + 2 ][player_y] == '2') //箱子下面是目标点
                         {
                            isSuccess = true ;
                         }
                     }
                     else  // 判断没有箱子,就只有玩家动
                     {
                         if ( Board[player_x + 1 ][player_y] == '0')// 玩家下面是空格
                         {
                             //更新棋盘
                            Board [player_x][player_y] = '0' ;
                            Board [player_x + 1][ player_y] = '1' ;
                             //更新玩家位置
                            player_x = player_x + 1;
                         }
                     }
                 }
                 else
                 {
                     if ( Board[player_x + 1 ][player_y] == '0')  //下面越界,玩家下面是空格
                     {
                         //更新棋盘
                        Board [player_x][player_y] = '0' ;
                        Board [player_x + 1][ player_y] = '1' ;
                         //更新玩家位置
                        player_x = player_x + 1;
                     }
                 }

             }

             else if (path[z ] == 'u' )//向上
             {
                 if ( player_x - 2 >=0) // 判断上面没有越界
                 {
                     if (! isSuccess && Board [player_x - 1][ player_y] == '3')// 判断上面有箱子
                     {
                         if ( Board[player_x - 2 ][player_y] == '0')// 判断箱子上面是空格
                         {
                             //更新棋盘
                            Board [player_x][player_y] = '0' ;
                            Board [player_x - 1][ player_y] = '1' ;
                            Board [player_x - 2][ player_y] = '3' ;
                             //更新玩家位置
                            player_x = player_x - 1;
                         }
                         else if (Board[player_x - 2 ][player_y] == '2') //箱子上面是目标点
                         {
                            isSuccess = true ;
                         }
                     }
                     else  // 判断没有箱子,就只有玩家动
                     {
                         if ( Board[player_x - 1 ][player_y] == '0')// 玩家上面空格
                         {
                             //更新棋盘
                            Board [player_x][player_y] = '0' ;
                            Board [player_x - 1][ player_y] = '1' ;
                             //更新玩家位置
                            player_x = player_x - 1;
                         }
                     }
                 }
                 else
                 {
                     if ( Board[player_x - 1 ][player_y] == '0')  //上面越界,玩家上面是空格
                     {
                         //更新棋盘
                        Board [player_x][player_y] = '0' ;
                        Board [player_x - 1][ player_y] = '1' ;
                         //更新玩家位置
                        player_x = player_x - 1;
                     }
                 }
             }
             else if (path[z ] == 'l' )//左
             {
                 if ( player_y - 2 >= 0 ) // 判断左边没有越界
                 {
                     if (! isSuccess && Board [player_x][player_y -1 ] == '3')// 判断左边有箱子
                     {
                         if ( Board[player_x ][player_y - 2] == '0') //判断箱子左边是空格
                         {
                             //更新棋盘
                            Board [player_x][player_y] = '0' ;
                            Board [player_x][player_y - 1] = '1' ;
                            Board [player_x][player_y - 2] = '3' ;
                             //更新玩家位置
                            player_y = player_y - 1;
                         }
                         else if (Board[player_x ][player_y - 2] == '2') //箱子左边是目标点
                         {
                            isSuccess = true ;
                         }
                     }
                     else  // 判断没有箱子,就只有玩家动
                     {
                         if ( Board[player_x ][player_y - 1] == '0') //玩家左边是空格
                         {
                             //更新棋盘
                            Board [player_x][player_y] = '0' ;
                            Board [player_x][player_y -1 ] = '1' ;
                             //更新玩家位置
                            player_y = player_y - 1;
                         }
                     }
                 }
                 else
                 {
                     if ( Board[player_x ][player_y - 1] == '0')   //左边越界,玩家左边是空格
                     {
                         //更新棋盘
                        Board [player_x][player_y] = '0' ;
                        Board [player_x][player_y - 1] = '1' ;
                         //更新玩家位置
                        player_y = player_y - 1;
                     }
                 }
             }
             else if (path[z ] == 'r' )//向右
             {
                 if ( player_y + 2 < n) // 判断右边没有越界
                 {
                     if (! isSuccess && Board [player_x][player_y + 1] == '3')// 判断右边有箱子
                     {
                         if ( Board[player_x ][player_y + 2] == '0') //判断箱子右边是空格
                         {
                             //更新棋盘
                            Board [player_x][player_y] = '0' ;
                            Board [player_x][player_y + 1] = '1' ;
                            Board [player_x][player_y + 2] = '3' ;
                             //更新玩家位置
                            player_y = player_y + 1;
                         }
                         else if (Board[player_x ][player_y + 2] == '2') //箱子右边是目标点
                         {
                            isSuccess = true ;
                         }
                     }
                     else  // 判断没有箱子,就只有玩家动
                     {
                         if ( Board[player_x ][player_y + 1] == '0') //玩家右边是空格
                         {
                             //更新棋盘
                            Board [player_x][player_y] = '0' ;
                            Board [player_x][player_y + 1] = '1' ;
                             //更新玩家位置
                            player_y = player_y + 1;
                         }
                     }
                 }
                 else
                 {
                     if ( Board[player_x ][player_y + 1] == '0')   //右边越界,玩家右边是空格
                     {
                         //更新棋盘
                        Board [player_x][player_y] = '0' ;
                        Board [player_x][player_y + 1] = '1' ;
                         //更新玩家位置
                        player_y = player_y + 1;
                     }
                 }
             }
         }

         if ( isSuccess)
         {
            cout << "YES" << endl;
         }
         else
            cout << "NO" << endl;
    }

    return 0;
}

上一篇:JAVA_SE基础——8.基本数据类型


下一篇:EF core2.1+MySQL报错'Void Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBuilder..ctor(Microsoft.EntityFrameworkCore.Storage.IRelationalTypeMapper)