Robot on the Board 1 思维,模拟

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
题意 :

  • 给一二维地图,再给出一串指令,如果走到边界就break,问从哪个点出发能走最多指令

思路 :

  • 默认初始位于 ( 1 , 1 ) (1, 1) (1,1)
  • 遍历指令,根据当前已执行的指令动态维护行动范围的矩阵空间
  • 当长/宽超过地图范围已经无法继续执行,得到最终答案
  • 用最左和最上的扫描线更新答案 a x = 1 − m a x l e f t ax = 1 - maxleft ax=1maxleft, a y = 1 − m a x r i g h t ay = 1 - maxright ay=1maxright
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int _;
    cin >> _;

    while (_ -- )
    {
    
    
        int n, m;
        string str;
        cin >> n >> m >> str;

        int nx = 0, ny = 0;
        int ml = 0, mr = 0, mu = 0, md = 0;
        int ax = 1, ay = 1;

        for (int i = 0; i < str.size(); i ++ )
        {
    
    
            if (str[i] == 'L') -- nx;
            if (str[i] == 'R') ++ nx;
            if (str[i] == 'U') -- ny;
            if (str[i] == 'D') ++ ny;
            ml = min(ml, nx), mr = max(mr, nx);
            mu = min(mu, ny), md = max(md, ny);
            if (mr - ml + 1 > m) break;
            if (md - mu + 1 > n) break;
            ax = 1 - mu, ay = 1 - ml;
        }
        cout << ax << ' ' << ay << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_51448653/article/details/121212708