Question: Programming Project 1 4 . 1 2 . 8 : . Repeat Programming Project 1 4 . 1 2 . 7 , but use a

Programming Project 14.12.8: .
Repeat Programming Project 14.12.7, but use a queue instead.
here is the work that is done but not sure where to change queue
#include
#include
#include
using namespace std;
const int MAX_ROWS =10;
const int MAX_COLS =10;
struct Pair
{
int row;
int col;
Pair(int r =0, int c =0){
row = r;
col = c;
}
};
class Stack
{
private:
Pair data[MAX_ROWS*MAX_COLS];
int top;
public:
Stack()
{
top =0;
}
void push(Pair p){
data[top]= p;
top++;
}
bool empty(){
return top ==0;
}
Pair pop(){
return data[--top];
}
};
bool isColored(int pixels[][MAX_COLS], Pair p){
if(pixels[p.row][p.col]!=0)
return true;
else
return false;
}
bool isValid(Pair p){
if(p.row >=0 && p.row < MAX_ROWS && p.col >=0 && p.col < MAX_COLS)
return true;
else
return false;
}
int main(){
Stack s;
int pixels[MAX_ROWS][MAX_COLS]={0};
int row, col, count =1;
Pair p;
cout << "Enter starting row (0-9): ";
cin >> row;
cout << "Enter starting column (0-9): ";
cin >> col;
s.push(Pair(row, col));
while(!s.empty()){
p = s.pop();
if(!isColored(pixels, p)){
pixels[p.row][p.col]= count;
count++;
}
Pair north(p.row-1, p.col);
Pair east(p.row, p.col +1);
Pair south(p.row+1, p.col);
Pair west(p.row, p.col-1);
if(isValid(north) && !isColored(pixels, north))
s.push(north);
if(isValid(east) && !isColored(pixels, east))
s.push(east);
if(isValid(south) && !isColored(pixels, south))
s.push(south);
if(isValid(west) && !isColored(pixels, west))
s.push(west);
}
//display the pixel numbering
for(int i =0; i < MAX_ROWS; i++){
for(int j =0; j < MAX_COLS; j++){
cout << setw(5)<< pixels[i][j];
}
cout << endl;
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!