CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:rottis
Submission time:2024-10-28 10:43:03 +0200
Language:C++ (C++20)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:31:14: error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'char*')
   31 |     std::cin >> board;
      |     ~~~~~~~~ ^~ ~~~~~
      |          |      |
      |          |      char*
      |          std::istream {aka std::basic_istream<char>}
In file included from /usr/include/c++/11/iostream:40,
                 from input/code.cpp:4:
/usr/include/c++/11/istream:168:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]' (near match)
  168 |       operator>>(bool& __n)
      |       ^~~~~~~~
/usr/include/c++/11/istream:168:7: note:   conversion of argument 1 would be ill-formed:
input/code.cpp:31:17: error: cannot bind non-const lvalue reference of type 'bool&' to a val...

Code

#define LEFT -1
#define RIGHT 1

#include <iostream>

char* step(char* board, long n, char* robot, int dir) {
    if (robot == nullptr) {
        throw "you fucked it up idiot!!!";
    }
    while (robot >= board && robot < board + n*sizeof(char)) {
        robot += dir*sizeof(char);
        if (*robot == '*') {
            return robot;
        }
    }
    return nullptr;
}

void print_board(char* board, long n) {
    for (int i = 0; i < n; i++) {
        std::cout << board[i];
    }
}

int main() {
    long n;
    std::cin >> n;
    
    char *board = (char*) malloc((1+n) * sizeof(char));

    std::cin >> board;
    
    long long steps = 0;
    long coins_collected = 0;
    
    char *robot_ptr;
    for (long i = 0; i < n; i++) {
        if (board[i] == 'R') {
            robot_ptr = board + i;
            break;
        }
    }
    char *left_ptr = step(board, n, robot_ptr, LEFT);
    char *right_ptr = step(board, n, robot_ptr, RIGHT);
    
    while (
        !(left_ptr == nullptr && right_ptr == nullptr) && (
            ( // either but not both is null
                left_ptr == nullptr || right_ptr == nullptr
            ) ||
            ( // both are not null and not in middle
                (long)(right_ptr - board) + (long)(left_ptr - board)
                != 2*(long)(robot_ptr - board)
            )
        )
        ) {
        std::cout << (long)(left_ptr - board) << ' ' << (long)(robot_ptr - board) << ' ' << (long)(right_ptr - board) << '\n';
        // closer to left?
        if (
            (robot_ptr - left_ptr < right_ptr - robot_ptr) && left_ptr != nullptr
            ) {
            // go left
            steps += robot_ptr - left_ptr;
            robot_ptr = left_ptr;
            left_ptr = step(board, n, left_ptr, LEFT);
        } else {
            // go right
            steps += right_ptr - robot_ptr;
            robot_ptr = right_ptr;
            right_ptr = step(board, n, right_ptr, RIGHT);
        }
        coins_collected++;
    }
    
    std::cout << steps << ' ' << coins_collected << std::endl;
    
    return 0;
}