+ 2
Why don't my two codes pass all the test cases for the 2D Map code coach?
Here's the Cpp version (passes tests 1, 2 and 6): https://sololearn.com/compiler-playground/c7H2KAOM22BM/?ref=app And the Py code (just passes tests 1 and 2): https://sololearn.com/compiler-playground/cAJJ4KDyEbaL/?ref=app
5 ответов
+ 2
Calvin Jude in the C++ code, its coordinate conversion is flawed. Hint: The comma makes 6 characters per row, not 5.
In the Python code, you need to revise the if condition where it tests for the edge case when both Ps are in the same row.
Both codes will pass after these minor corrections.
+ 2
//Calvin Jude, for the c++ solution, if you wanted to use %5 and /5 instead of %6 and /6, you would have to first remove the ','. It will cost a few additional steps, though.
#include <iostream>
using namespace std;
int main() {
string s;
cin>>s;
// erase the ","
size_t i = s.find(",");
while(i!=string::npos){
s.erase(i,1);
i = s.find(",",i);
}
int a = s.find('P'),
b = s.rfind('P');
cout << abs(a%5 - b%5) + abs(a/5 - b/5);
}
/*
Sample input:
XPXXX,XXXXX,XXXXX,XXXPX,XXXXX
Expected output:
5
*/
+ 1
2D MAP
---------------
You're given a representation of a 5x5 2D map, and if you can only move left, right, up, or down, determine how many moves you would have to make to get between two points on the map.
Task:
Determine the total number of moves that are needed between two points on a map. The points that you move between are marked with a P and the spaces in between are marked with X.
Input Format:
A string that represents the 2D space starting at the top left. Each level from top to bottom is separated by a comma.
Output Format:
An integer that represents the total number of moves that you had to make.
Sample Input:
XPXXX,XXXXX,XXXXX,XXXPX,XXXXX
Sample Output:
5
0
Calvin Jude
use enumerate to simplify your Python code
https://sololearn.com/compiler-playground/c8gy1vaGNE9y/?ref=app
0
Calvin Jude
for your C++, you could use string.find and string.rfind to simplify the code.
https://sololearn.com/compiler-playground/cXS98Am2d2KZ/?ref=app