Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CODE IN C++ BELOW IS SAMPLE INPUT AND SOLUTION TO CHECK INPUT: Your program shou

ID: 3715008 • Letter: C

Question

CODE IN C++ BELOW IS SAMPLE INPUT AND SOLUTION TO CHECK

INPUT:

Your program should read its input from the file input.txt, in the following format. The input begins with a two positive integers on a line indicating the number of rows r and columns c of the maze, respectively. The next r lines contain the color and directional information for each arrow in the maze. Each line has c values, where each value represents the color of the arrow in the corresponding row and column in the maze (R or B), followed by a hyphen (-), followed by the direction of the arrow (N, E, S, W, NE, SE, SW, or NW). The color codes R and B represent “red” and “blue,” respectively, while the direction codes N, E, S, W, NE, SE, SW, and NW represent “north,” “east,” “south,” “west,” “northeast,” “southeast,” “southwest,” and “northwest,” respectively. The goal square (bulls-eye) is represented by a letter O. You may assume that the bulls-eye will always be in the bottom-right corner of the maze. For the “Apollo and Diana” maze, the input is:

8 8
R-E R-SE B-S B-SW R-S R-SW R-S R-S
B-E R-S B-SE R-E B-SE B-S B-W R-SW
R-N B-W B-SW R-SE R-NE B-SW B-W R-W
R-SE R-SE B-SW R-SE R-S B-NW R-E B-NW
B-NE R-W R-S B-S B-E B-NE B-NW R-NW
R-S B-SE R-SE R-SE R-NW R-NE B-E R-W
R-NE B-W B-SE R-E R-E B-E B-NW R-SW
B-NE R-E B-N R-NE B-NE B-N B-NW O

OUTPUT:

Your program should write its output to the file output.txt, in the following format. The output should consist of a path from the top left square to the bottom right square (bulls-eye). It should be a single line consisting of a sequence of moves, separated by spaces. Each move should be represented by the number of spaces to move and the direction, with no space in between. The direction should be represented using N, E, S, W, NE, SE, SW, and NW, as in the input. Of course, the sequence of moves described in the file must solve the maze described in the input.

For example, if your first three moves take you 3 spaces east, 3 spaces southwest, and 4 spaces southeast, your output should begin as follows:

3E 3SW 4SE

You are welcome to try figuring out the solution to the “Apollo and Diana” puzzle on your own, but that won’t get you any points. Your assignment is to model the maze as a graph and to solve the problem using an appropriate graph algorithm

SAMPLE SOLUTION:

2E 4S 2S 1SE 3NE 3NW 3E 5W 1S 1W 1N 7E 1SW 3W 2SE 2NE 5W 1SW 4SE 7N 3SW

2SW 2S 7NE 7S

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdio>


using namespace std;

using namespace std;


string getColor(const string &s);
string getDirect(const string &s);
int getOptionCode(const string &s);

int main()
{
    ifstream file { "input.txt" };
    if (!file.is_open())
        return -1;

    int m,n;

    file >> m;
    file >> n;

    string maze [m][n] {};

    for (int i{}; i != m; ++i)
    {
        for (int j{}; j != n; ++j)
        {
            file >> maze[i][j];
        }
    }

    string color,direct,prev;
    vector<string> track;
    int i=0,j=0;


     for (int i{}; i != m; ++i)
    {
        for (int j{}; j != n; ++j)
        {
            cout<< maze[i][j]<<" ";
        }
        cout<<endl;
    }


    int count=0;
    prev = getColor(maze[0][0]);
    direct=getDirect(maze[0][0]);
    int c=0;

    while(c<20)//maze[i][j] != "O")
    {   c++;
        count++;
        color=getColor(maze[i][j]);
        cout <<maze[i][j]<<' '<<i<<' '<<j<<endl;
        track.push_back(maze[i][j]);

        if(color== "R" && prev == "B")
        {
            prev= "R";
            count=0;
            direct=getDirect(maze[i][j]);
        }

        else if( color== "B" && prev == "R")
        {
            prev= "B";
            count=0;
            direct=getDirect(maze[i][j]);
        }


        int option = getOptionCode(direct);
        switch(option)
        {

        case 1:
        {
            string temp =color;
            while(i>0){
                if(temp != color)
                    break;
                temp=getColor(maze[i][j]);
                i--;
            }
            if(i>0)
                i--;
            break;
        }
        case 2:
        {
            string temp =color;

            while(j< n-1){
                if(temp != color)
                    break;
                temp=getColor(maze[i][j]);
                j++;
            }

            if(j< n-1)
                j++;
            break;
        }
        case 3:
        {
            if(j>0)
                j--;
            break;

        }
        case 4:
        {
             if(i< m-1)
                i++;
            break;

        }

        case 5:
        {
            if(i > 0 && j < n-1)
            {
                i--;
                j++;
            }
            break;
        }
        case 6:
        {
            if(i > 0 && j > 0)
            {
                i--;
                j--;
            }
            break;
        }
        case 7:
        {
            if(i < m-1 && j < n-1)
            {
                i++;
                j++;
            }
            break;
        }
        case 8:
        {
            if(i < m-1 && j >0)
            {
                i++;
                j--;
            }
            break;
        }

        default:
        {
            cout<<"! INVALID directions given..."<<endl;

        }

        }
    }

    cout <<"--------Printing Directions--------"<<endl;
    cout <<track.size()<<endl;
    /**for (vector<string>::const_iterator i = track.begin(); i != track.end(); ++i)
        std::cout << *i << ' ';*/

    return 0;
}

string getColor(const string &s)
{
    vector<string> result;
    stringstream ss(s);
    string item;
    while (getline(ss, item, '-'))
    {
        result.push_back(item);
    }
    return result.at(0);

}

string getDirect(const string &s)
{
    vector<string> result;
    stringstream ss(s);
    string item;
    while (getline(ss, item, '-'))
    {
        result.push_back(item);
    }
    return result.at(1);

}

int getOptionCode(const string &s)
{
    int code=0;

    if(s=="N")
        code=1;

    else if(s=="E")
        code=2;

    else if(s=="W")
        code=3;

    else if(s=="S")
        code=4;

    else if(s=="NE")
        code=5;

    else if(s=="NW")
        code=6;

    else if(s=="SE")
        code=7;

    else if(s=="SW")
        code=8;

    return code;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote