Getting distance between two elements in a 2D map - not quite correct -solved by Brian | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Getting distance between two elements in a 2D map - not quite correct -solved by Brian

I am working on the 2D Map challenge, so careful spoiler alert... Anyways, somehow my solution is flawed but I do not know where the mistake is. Any help or hints would be appreciated :) Here is the code: #include<iostream> #include<string> #include<vector> #include<sstream> #include<algorithm> int main() { std::string str = "PXXXX,XXXXX,XXXXP,XXXXX,XXXXX"; //std::string str; //std::getline(std::cin, str); std::vector<std::string> vec; std::stringstream ss(str); std::string strTemp; std::vector<int> nums; std::vector<int> block; std::vector<int> option; int result = 0; //first tokenize strings and create vector of strings while(std::getline(ss, strTemp, ',')) { vec.push_back(strTemp); } //find index of 'P' in each string for(auto &val : vec) { std::cout << val << std::endl; std::size_t found = val.find('P'); if (found != std::string::npos) { nums.push_back(found); } } //find instance of 'P' in string for(size_t i = 0; i < vec.size(); i++) { if (std::find(vec[i].begin(), vec[i].end(), 'P') != vec[i].end()) { block.push_back(i); } } //if two Ps are found in the same string... if(block.size()<2) { for(unsigned int i = 0; i< vec[block[0]].size(); i++) { if(vec[block[0]][i] == 'P') { option.push_back(i); } } result = option[1] - option[0]; std::cout << result; } //subtract position of indices and position in blocks and add result else { result = (nums[1]-nums[0]) + (block[1]-block[0]); std::cout << result; } }

26th Dec 2022, 10:55 PM
The_Fox
The_Fox - avatar
8 Answers
+ 2
The_Fox I found the problem. Yes, abs() is the solution. Consider what happens when the 2nd P is in a lower row and to the left of the first P.
27th Dec 2022, 8:41 PM
Brian
Brian - avatar
+ 3
The_Fox the distance between two points is the length of their hypotenuse: sqrt(sqr(x2-x1)+sqr(y2-y1)) Conveniently, there is a standard library function for that. #include <cmath> result = hypot(x2-x1, y2-y1);
26th Dec 2022, 11:55 PM
Brian
Brian - avatar
+ 3
The_Fox now I understand this is a Code Coach task. I will take another look.
27th Dec 2022, 7:53 PM
Brian
Brian - avatar
+ 2
You are welcome. The_Fox, I wish the same for you!
27th Dec 2022, 9:38 PM
Brian
Brian - avatar
+ 1
Brian I guess I looked too long so I did not see it ;) Thanks a lot!! Happy New Year to you and your loved ones. Until next time.
27th Dec 2022, 9:30 PM
The_Fox
The_Fox - avatar
0
Brian Thank you. Not sure if that will help me in my specific case but I will try! Happy holidays :)
27th Dec 2022, 8:15 AM
The_Fox
The_Fox - avatar
0
@Brian The hypotenuse does not help me here because I need to 'walk' the distance through the different x-fields (if that makes any sense). e.g. XPXX XXXX XXXP Distance = 4 -> 2 down, 2 to the right (cannot walk diagonally).
27th Dec 2022, 8:26 AM
The_Fox
The_Fox - avatar
0
Exactly… it might have something to do with one of the values. Not sure. Someone mentioned using absolute numbers only…
27th Dec 2022, 8:14 PM
The_Fox
The_Fox - avatar