我这个能AC,但是为啥资源消耗这么大(3s,50M)

P11228 [CSP-J 2024] 地图探险

BDMsx @ 2024-10-26 20:48:38

#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();
}

by Lawrenceling @ 2024-10-26 21:00:30

咳咳,你这代码。。。

建议紫衫。


by _luogu_huowenshuo_ @ 2024-10-26 21:13:47

@BDMsx 一眼机器人写的


by BDMsx @ 2024-10-26 21:23:04

@Lawrence2011 为什么啊QAQ


by BDMsx @ 2024-10-26 21:23:18

@_luoguhuowenshuo 包不是的


by _luogu_huowenshuo_ @ 2024-10-26 21:26:43

@BDMsx C++中long这玩意挺费物,在实际的使用中,long与int几乎没有区别


by Lawrenceling @ 2024-10-26 21:34:38

@BDMsx 能过的代码尽量不要贴,况且你这代码确实很AI


by BDMsx @ 2024-10-26 21:40:24

@Lawrence2011 为啥AI啊哥,给指正指正呗,刚学C++两周


|