forked from Ansi007/Programming-Fundamentals-Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionWhiteOut.cpp
More file actions
106 lines (80 loc) · 2.29 KB
/
Copy pathRecursionWhiteOut.cpp
File metadata and controls
106 lines (80 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "iostream"
#include "fstream"
using namespace std;
void eraseObjectIfOne(int **ar, int r, int c, int i, int j, char prev_index_symbol);
void setAr(int **ar, int i, int j){
ar[i][j] = 0;
}
//complete the implementation of eraseObject function
void eraseObject (int** ar, int r, int c, int i, int j)
{
if (ar[i][j] != 1)
cout << "Not a index containing black colour" << endl;
eraseObjectIfOne(ar, r, c, i, j, 'n');
}
void eraseObjectIfOne(int **ar, int r, int c, int i, int j, char prev_index_symbol) {
// Recursive cases
setAr(ar, i , j);
// Check for the right side of the given index
if (j + 1 < c && ar[i][j + 1] == 1 && prev_index_symbol != 'l')
eraseObjectIfOne(ar, r, c, i, j + 1, 'r');
// Check for the left side of the given index
if (j - 1 > 0 && ar[i][j - 1] == 1 && prev_index_symbol != 'r')
eraseObjectIfOne(ar, r, c, i, j - 1, 'l');
// Check for the above of the given index
if (i - 1 >= 0 && ar[i - 1][j] == 1 && prev_index_symbol != 'b')
eraseObjectIfOne(ar, r, c, i - 1, j, 'a');
// Check for the below of the given index
if (i + 1 < r && ar[i + 1][j] == 1 && prev_index_symbol != 'a')
eraseObjectIfOne(ar, r, c, i + 1, j, 'b');
// If there are no zeros in the neighbouring areas, then
return;
}
int main(void)
{
//opening file
ifstream inf("input.txt");
if(!inf)
{
cout << "Failed to open a file" << endl;
exit(0);
}
int ROWS, COLS, p_x, p_y;
inf >> ROWS >> COLS; //rading size of image from file
inf >> p_x >> p_y; //reading coordinates of the pixel from file
//array of pointers
int **ar = new int*[ROWS];
//each location has COLS size int array
for(int i = 0; i < ROWS; i++)
{
ar[i] = new int[COLS];
}
//reading image data from file into ar
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
{
inf >> ar[i][j];
}
}
//closing file
inf.close();
//making call to eraseObject function
eraseObject (ar, ROWS, COLS, p_x, p_y);
//displaying image data after processing
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
{
cout << ar[i][j] << " ";
}
cout << endl;
}
//freeing all the allocated memory
for(int i = 0; i < ROWS; i++)
{
delete[] ar[i];
}
delete[] ar;
return 0;
}