我这个能AC,但是为啥资源消耗这么大(3s,50M)
查看原帖
我这个能AC,但是为啥资源消耗这么大(3s,50M)
1524532
BDMsx楼主2024/10/26 20:48
#include <bits/stdc++.h>
using namespace std;
struct robot    //定义机器人结构体
{
    long x = 0;
    long y = 0;
    int d = 0;
};
int solution(); 

int main()
{
    int T = 0;  //数据组数
    cin >> T;
    cin.get();
    for (int i = 0; i < T; i++)
    {
        long posNum = solution();
        cout << posNum << endl;
    }
}

int solution()  //定义解决函数
{
    int n,m;
    long k;
    robot robot;
    cin >> n >> m >> k;
    cin.get();
    char map[n][m];   //地图数组,n行,m列
    vector <string> have_been;  //已经去过的位置
    cin >> robot.x >> robot.y >> robot.d;   //获取机器人初始信息
    cin.get();
    for (int x = 0; x < n; x++)  //获取地图
    {
        for (int y = 0; y < m; y++)
        {
            map[x][y] = cin.get();
        }
        cin.get();
    }
    have_been.push_back(to_string(robot.x) + ',' + to_string(robot.y));   //将初始位置加入去过的位置中
    for (long steps = 0; steps < k; steps++) //机器人开始行走
    {
        long next_position[2]{robot.x,robot.y};
        switch (robot.d)    //预测机器人下一步位置
        {
        case 0:
            next_position[1] += 1;
            break;
        case 1:
            next_position[0] += 1;
            break;
        case 2:
            next_position[1] += -1;
            break;
        case 3:
            next_position[0] += -1;
            break;
        default:
            break;
        }
        if (next_position[0] >= 1 && next_position[0] <= n && next_position[1] >= 1 && next_position[1] <= m && 
            map[next_position[0]-1][next_position[1]-1] == '.')   //判断下一步位置合法性,并作出行动,地图坐标转换
        {
            robot.x = next_position[0];
            robot.y = next_position[1];
            string nowpos = to_string(robot.x) +','+ to_string(robot.y);
            have_been.push_back(nowpos);//将新的位置加入行走过的位置中 
        }else{
            robot.d = (robot.d + 1) % 4;
        }
    }//机器人结束行走
    //排除相同的坐标
     sort(have_been.begin(),have_been.end());
     have_been.erase(unique(have_been.begin(),have_been.end()),have_been.end());
    if (have_been.size() == 1) return 1;
    return have_been.size();
}

2024/10/26 20:48
加载中...