CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:Collectseal120
Submission time:2024-10-29 16:08:18 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int moveRight(const std::vector<char>&, int)':
input/code.cpp:8:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 |     for (int i = robotPosition + 1; i < charPath.size(); i++) {
      |                                     ~~^~~~~~~~~~~~~~~~~
input/code.cpp:13:12: error: 'INT_MAX' was not declared in this scope
   13 |     return INT_MAX;
      |            ^~~~~~~
input/code.cpp:4:1: note: 'INT_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
    3 | #include <vector>
  +++ |+#include <climits>
    4 | 
input/code.cpp: In function 'int moveLeft(const std::vector<char>&, int)':
input/code.cpp:22:12: error: 'INT_MAX' was not declared in this scope
   22 |     return INT_MAX;
      |            ^~~~~~~
input/code.cpp:22:12: note: 'INT_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?

Code

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int moveRight(const vector<char>& charPath, int robotPosition) {
for (int i = robotPosition + 1; i < charPath.size(); i++) {
if (charPath[i] == '*') {
return (i - robotPosition);
}
}
return INT_MAX;
}
int moveLeft(const vector<char>& charPath, int robotPosition) {
for (int i = robotPosition - 1; i >= 0; i--) {
if (charPath[i] == '*') {
return (robotPosition - i);
}
}
return INT_MAX;
}
int main() {
int room;
cin >> room;
string path;
cin >> path;
vector<char> charPath(path.begin(), path.end());
int robot_position = path.find('R');
int stepsTaken = 0;
int coinsCollected = 0;
while (true) {
int leftDist = moveLeft(charPath, robot_position);
int rightDist = moveRight(charPath, robot_position);
if (leftDist == rightDist) {
break;
} else if (leftDist < rightDist) {
charPath[robot_position] = '.';
robot_position -= leftDist;
charPath[robot_position] = 'R';
stepsTaken += leftDist;
coinsCollected += 1;
} else if (rightDist < leftDist) {
charPath[robot_position] = '.';
robot_position += rightDist;
charPath[robot_position] = 'R';
stepsTaken += rightDist;
coinsCollected += 1;
}
}
cout << stepsTaken << " " << coinsCollected << endl;
}