Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the following C++ code, replace memset (within the functionbfs) with an alternative operation that will initialize the arrayvisited in the same way. #include #include

In the following C++ code, replace memset (within the functionbfs) with an alternative operation that will initialize the array"visited" in the same way.

#include
#include
#include
#include
#include

using namespace std;

int visited[51][51][51];
char arr[51][51];

struct Vertex
{
int a;
int b;
int c;
int t;

//adding default vals to the struct
Vertex(int p1,int p2,int p3,int d)
{
a = p1;
b = p2;
c = p3;
t = d;
}
};

void bfs (int n, int p1, int p2, int p3)
{
queue nodes;
memset(visited,-1,sizeof(visited));
//int visited[51][51][51] = { -1 };
nodes.push(Vertex(p1,p2,p3,0));
visited[p1][p2][p3] = 0;
//queue traversal
while(!nodes.empty())
{
//dequeue a vertex fromqueue
int a =nodes.front().a;
int b =nodes.front().b;
int c =nodes.front().c;
int t =nodes.front().t;
nodes.pop();
//now look at allneighbors
for(int i = 1; i <= n;i++)
{
//if we'relooking at the node or the edge we're traveling on isn't the sameas color connecting p2 and p3
if(i == a|| arr[a][i] != arr[b][c])
{
continue;
}
//if pointhas been looked at
if(visited[i][b][c] != -1)
{
continue;
}
//if ithasn't been processed yet do that now
nodes.push(Vertex(i,b,c,t+1));
//cout<< "p1:t+1=" << t+1 << endl;
visited[i][b][c] = t+1;
}
for(int i = 1; i <= n;i++)
{
if(i == b|| arr[b][i] != arr[a][c])
{
continue;
}
if(visited[a][i][c] !=-1)
{
continue;
}
nodes.push(Vertex(a,i,c,t+1));
visited[a][i][c] =t+1;
}
for(int i = 1; i <= n; i++)
{
if(i == c || arr[c][i] !=arr[a][b])
{
continue;
}
if(visited[a][b][i] !=-1)
{
continue;
}
nodes.push(Vertex(a,b,i,t+1));
visited[a][b][i] = t+1;
}
}
}

int main ( )
{
char s;
int n,p1,p2,p3;
while (cin >> n) //readin number of nodes
{
if(n == 0)
{
break;
}
cin >> p1 >> p2>> p3;
//read in the array
for ( int i = 1; i <= n ;i++ )
{
for ( intj = 1 ; j <= n ; j++ )
{
cin >> s;
arr[i][j] = s;
//cout << arr[i][j] << "";
}
//cout<< endl;
}

int soln;
bool beginCondition =true;
bfs(n,p1,p2,p3);
//if the node isn't equal toinfinity, set soln to visited node val and iterate.
for(int i = 1; i <= n ;i++)
{
if(visited[i][i][i] != -1)
{
//cout << "Visited!" < //cout << "Soln:" << soln<< endl;
if(beginCondition)
{
soln =visited[i][i][i];
beginCondition =false;
}
else
{
soln =min(soln,visited[i][i][i]);
}
}
}

if(beginCondition)
{
cout<< "impossible" << endl;
}
else
{
cout<< soln << endl;
}
}
}

-----------------------------------------------------------------------------------------------------------------

Example Input (each run is one line of integers and an array ofa size that matches the first int val):

3 1 2 3r b rb b br b r2 1 2 2y gg y1 1 1 1a2 1 1 1a bb d2 1 2 2a bb a2 1 2 2a bb b2 1 2 2b bb b2 1 2 2b bb a3 1 2 3a a aa a aa a a3 1 2 3a b ab a ba b a3 1 2 3a b ab b ba b a

Example Output (solution is number or "impossible"):

2impossible00impossible1122impossible2

Step by Step Solution

3.39 Rating (149 Votes )

There are 3 Steps involved in it

Step: 1

Answer replace memsetvisited1sizeofvisited with forint i0i 51i forint j0j ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Electrical Engineering questions

Question

What is the name of the program?

Answered: 1 week ago