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

Compiler report

input/code.cpp:1:1: error: stray '\250' in program
    1 | ¨//#include "bits/stdc++.h";
      | ^
input/code.cpp: In function 'int findRobot(std::string)':
input/code.cpp:8:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 |         for (int i = 0; i < map.size(); i++)
      |                         ~~^~~~~~~~~~~~
input/code.cpp: In function 'int findClosestCoin(std::string, int)':
input/code.cpp:24:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |         for (int i = 1; i < map.size(); i++)
      |                         ~~^~~~~~~~~~~~
input/code.cpp:29:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |...

Code

¨//#include "bits/stdc++.h";
#include <bits/stdc++.h>

using namespace std;

int findRobot(string map)
{
	for (int i = 0; i < map.size(); i++)
	{
		if (map[i] == 'R')
		{
			return i;
		}
	}
	return 0;
}
void moveRobot(string* map, int from, int to)
{
	map->at(from) = '.';
	map->at(to) = 'R';
}
int findClosestCoin(string map, int robot)
{
	for (int i = 1; i < map.size(); i++)
	{
		bool right = false;
		bool left = false;
		// Onko oikealla kolikko?
		if (robot + i < map.size())
		{
			right = map[robot + i] == '*';
		}
		// Vasen
		if (robot - i > 0)
		{
			left = map[robot - i] == '*';
		}

		if (right && left)
		{
			// Kahden kolikon välissä
			return -1;
		}
		if (right)
		{
			return robot + i;
		}
		if (left)
		{
			return robot - i;
		}
	}
	return -1;
}


int main()
{
	int rooms;
	string map;
	cin >> rooms;
	cin >> map;

	int steps = 0;
	int coins = 0;

	int robotPos = findRobot(map);
	int closestCoin = findClosestCoin(map, robotPos);

	while (closestCoin != -1)
	{
		steps += abs(closestCoin - robotPos);
		coins++;

		moveRobot(&map, robotPos, closestCoin);

		robotPos = findRobot(map);
		closestCoin = findClosestCoin(map, robotPos);
	}

	cout << steps << " " << coins;
}