Hi,
Having some issue with this problem (https://www.codingame.com/training/easy/ghost-legs)! Can someone take a look at my code and see what I’m wrong?
Thanks!
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
int main()
{
int W;
int H;
string topLine;
string botLine;
int startPos = 0;
int endPos = 0;
cin >> W >> H; cin.ignore();
//getting the input
cout << "H: "<<H<<endl;
cout << "W: " <<W<<endl;
for (int i = 0; i < H; i++) {
string line;
getline(cin, line);
//if the first row, copy the input and get rid of the spaces.
if(i == 0){
topLine=line;
//this is a way to remove spaces in the string after the copy
topLine.erase(std::remove_if(topLine.begin(), topLine.end(), ::isspace), topLine.end());
} //end if
//else if the last row, copy the input and get rid of the spaces
else if (i==(H-1)){
botLine = line;
botLine.erase(std::remove_if(botLine.begin(), botLine.end(), ::isspace), botLine.end());
}//end else if
/* Issue area: Cannot figure out why this if statment is having trouble identifying the "-"*/
/* in the string in each iteration. */
if(line[i] == '-' ){
cout<<"Bridge!"<<endl;
cout << "endPos: " <<endPos <<endl;
cout << "i after bridge: " << i <<endl;
//if the bridge or "-" is to the right of the current position, move to the right column
if(i > endPos){
cout << "Endpos1: " <<endPos<<endl;
endPos = endPos + 1;
cout << "i: " << i << endl;
}//end if
//if the bridge or "-" is to the left of the current position, move to the left column
else if(i<endPos){
cout << "Endpos2: " <<endPos<<endl;
endPos = endPos - 1;
cout << "i: " << i << endl;
}//end else if
//if the current position is in the same iteration, find the next "-"
/*else if (i == endPos){
cout << "Hello!" <<endl;
if (line[i+1]=='-'){
endPos = endPos + 1;
}
else if(line[i-1]=='-') {
endPos = endPos - 1;
}
}//end else if*/
}//end if
//cout <<line<<endl;
}//end for
//
if(botLine[0]=='0'){
cout << "endPos1: " << endPos <<endl;
//endPos = (endPos/2);
cout << "endPos1: " <<endPos <<endl;
cout<<"Bot1: "<<botLine[endPos+1]<<endl;
cout<<botLine<<endl;
}//end if
else{
cout << "endPos2: " << endPos <<endl;
//endPos = ((endPos/2));
cout<<"Bot2: "<<botLine[endPos-1]<<endl;
cout<<botLine<<endl;
}//end else
cout <<endPos<<endl;
cout << topLine[startPos]<< botLine[endPos] << endl;
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
}//end main