CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:osmukka
Submission time:2024-11-06 10:04:46 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int distance_left(char*, int)':
input/code.cpp:9:17: error: 'l_coin_index' was not declared in this scope
    9 |     for(int i = l_coin_index-1; i >= 0; i--)
      |                 ^~~~~~~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:58:9: error: 'd_left' was not declared in this scope
   58 |         d_left  = distance_left(rooms, robot_index);
      |         ^~~~~~
input/code.cpp:59:9: error: 'd_right' was not declared in this scope
   59 |         d_right = distance_right(rooms, robot_index, amount);
      |         ^~~~~~~

Code

#include <iostream>
#include <vector>
#include <string>


int distance_left(char* array, int robot_index)
{

    for(int i = l_coin_index-1; i >= 0; i--)
    {
        if(array[i] == '*')
        {
            return robot_index - i;
        }
    }
    // If no coins exist to the left. Distance between two indices can't be 0.
    return 0;
}


int distance_right(char* array, int robot_index, const int array_len)
{
    for(int i = array_len-robot_index; i <= array_len; i--)
    {
        if(array[i] == '*')
        {
            return i - robot_index;
        }
    }
    // If no coins exist to the left.
    return 0;
}


int main()
{
    int amount = 0;
    char rooms[300000];
    int robot_index = 0;
    int steps = 0;
    int coins = 0;
 
    std::cin >> amount;
    std::cin >> rooms;

    for(int i = 0; i < amount; i++)
    {
        if(rooms[i] == 'R')
        {
            robot_index = i;
            break;
        }
    }


    while(1)
    {
        d_left  = distance_left(rooms, robot_index);
        d_right = distance_right(rooms, robot_index, amount);
 
        if(d_left < d_right)
        {
            steps += d_left;
            rooms[robot_index] = '.';
            robot_index--;
            rooms[robot_index] = 'R';
            coins++;
        }
        else if(d_right < d_left)
        {
            steps += d_right;
            rooms[robot_index] = '.';
            robot_index++;
            rooms[robot_index] = 'R';
            coins++;
        }
        else if(d_left == d_right)
        {
            break;
        }
    }
 
    std::cout << steps << " " << coins << "\n";
    return 0;
}