CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:TNT
Submission time:2024-10-30 21:49:53 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:23:95: error: a function-definition is not allowed here before '{' token
   23 |         std::vector<int> CollectCoins(int Rooms, std::string Layout, std::vector<int> Output) {
      |                                                                                               ^
input/code.cpp:52:18: error: 'CollectCoins' was not declared in this scope
   52 |         Output = CollectCoins(Rooms, Layout, Output);
      |                  ^~~~~~~~~~~~

Code

#include <cstddef>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>


int main () {
	// Input:
	// n rooms
	// Building layout
	// 	e.g.
	// 	20
	// 	**.*......*.R*...*..
	// * = coin; . = empty; R = robot
	int Rooms;
	std::string Layout;
	std::cin >> Rooms >> Layout;

	// GOAL: Find the max number of coins the robot can collect by always going to
	// the closest coin
	std::vector<int> Output = {0,0};
	std::vector<int> CollectCoins(int Rooms, std::string Layout, std::vector<int> Output) {
		std::size_t CurRobot = Layout.find("R");

		// Distance to the first coin left
		std::size_t CoinLeft = Layout.find_last_of("*", CurRobot);
		std::size_t DistanceLeft = CurRobot - CoinLeft;

		// Distance to the first coin right
		std::size_t CoinRight = Layout.find_first_of("*", CurRobot);
		std::size_t DistanceRight = CoinRight - CurRobot;

		if (DistanceLeft==DistanceRight || ((CoinLeft == (size_t)-1) && (CoinRight == (size_t)-1))) {
			return Output;
		}

		Layout[CurRobot] = '.';
		Output[1]++;

		if (DistanceLeft<DistanceRight) {
			Output[0] += DistanceLeft;
			Layout[CoinLeft] = 'R';
		} else {
			Output[0] += DistanceRight;
			Layout[CoinRight] = 'R';
		}
		//std::cout << Layout << "\n";

		return CollectCoins(Rooms, Layout, Output);
	}
	Output = CollectCoins(Rooms, Layout, Output);
	int Steps = Output[0];
	int Coins = Output[1];

	std::cout << Steps << " " << Coins << "\n";
}