Ghost legs Puzzle -HELP!

Hi,

Having some issue with this problem! 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
        if(i > endPos){
            cout << "Endpos1: "  <<endPos<<endl;
            endPos = endPos + 1;
            cout << "i: " << i << endl;
        }//end if

Are you expecting endPos to change from 0 to 3 when ‘-’ is found?
No, it doesn’t.
It changes from 0 to 1.

    if(line[i] == '-' ){

Besides, i is used for vertical iteration (from 0 to H). Cannot understand your logic to use i in line[i] .

ooooooh I see! I was looking at it as each iteration, i was W for that iteration. and for the previous post, I increased endPos to stand for the columns. So it would be from a to b, which in my interpretation is from 0 to 3.

Hello!

I’m running the same C++ code in my local IDE (Visual Studio) and in the browser, yet the output for one of the test shows an incorrect solution in the browser. I don’t see why the solutions would differ, and it won’t pass all the test because of it. The test is the third one, “6 lanes”:

Input:
16 14
F E D C B A
| |–| | | |
|–| |–| |–|
| |–| |–| |
| | | | |–|
| |–| |–| |
| | |–| | |
| | |–| |–|
|–| | |–| |
| | |–| | |
|–| | | |–|
| |–| | | |
| | |–| | |
0 1 2 3 4 5

Output from browser:

Standard Output Stream:

A4
E1
F3
D0
C2
B5

Output from local run:
F3
E1
D0
C2
B5
A4

I’m using an unordered_map to store and print the values. It stores the labels as they come and should print them as such.

in cpp, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values. You can assume data are in random order, not following the sequence you put data in.
So your matching is correct but wrong in ordering.

1 Like

Alright, thanks a lot for the insight! So I basically got it in the right order locally just by chance?

Yes, unordered_map is by design “unordered”.

The final ordering is determined by hashing, which can be affected by different machines, different compilers (and version and config), different memory usage at the particular moment, etc.