-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathads.cpp
More file actions
159 lines (137 loc) · 4.84 KB
/
ads.cpp
File metadata and controls
159 lines (137 loc) · 4.84 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <bits/stdc++.h>
using namespace std;
// Check if this image should be removed i.e. if there is an ad character in it
bool remove(char **grid, int sy, int sx, int ey, int ex)
{
// For every cell in this image
for(int i = sy+1; i < ey; i++)
{
for(int j = sx+1; j < ex; j++)
{
char c = grid[i][j]; // Get the character in the cell
// If the character is an ad character
if(!(
('A' <= c && c <= 'Z') || // Uppercase
('a' <= c && c <= 'z') || // Lowercase
('0' <= c && c <= '9') || // Number
c == '?' || c == '!' || c == ',' || c == '.' || c == ' ' || c == '+'// Other
))
{
// Return true as this image should be removed
return true;
}
// If this cell is the top left corner of another image
if(grid[i][j+1] == '+' && grid[i+1][j] == '+' && grid[i][j] == '+')
{
// Store the x and y coordinates of the top left corner
int y = i;
int x = j;
int dy = y+1; // y coordinate of the bottom right corner
// While the edge continues downwards
while(dy < ey && grid[dy][x] == '+')
{
dy++; // Increment the y coordinate
}
dy--; // Go back to the last coordinate that is a +
int dx = x+1; // x coordinate of the bottom right corner
// While the edge continues to the right
while(dx < ex && grid[dy][dx] == '+')
{
dx++; // Increment the x coordinate
}
dx--; // Go back to the last coordinate that is a +
// If this image should be removed
if(remove(grid, y, x, dy, dx))
{
// Loop over every cell in this area
for(int a = y; a <= dy; a++)
{
for(int b = x; b <= dx; b++)
{
// Set it to whitespace
grid[a][b] = ' ';
}
}
}
}
}
}
// Return false as no ad characters were found, so this image shouldn't be
// removed
return false;
}
int main()
{
// Get the height and width of the grid
int h, w;
cin >> h >> w;
cin.ignore(256, '\n');
// Get the grid
char **grid = new char*[h];
for(int i = 0; i < h; i++)
{
grid[i] = new char[w];
string s;
getline(cin, s);
for(int j = 0; j < w; j++)
{
grid[i][j] = s[j];
}
}
// For each cell in the grid
for(int i = 0; i < h; i++)
{
for(int j = 0; j < w; j++)
{
// If the cell isn't at the bottom or right edge
if((i + 1) < h && (j + 1) < w)
{
// If the cell is the top left corner of an image
if(grid[i][j+1] == '+' && grid[i+1][j] == '+' && grid[i][j] == '+')
{
// Store the x and y coordinates of the top left corner
int y = i;
int x = j;
int dy = y+1; // y coordinate of the bottom right corner
// While the edge continues downwards
while(dy < h && grid[dy][x] == '+')
{
dy++; // Increment the y coordinate
}
dy--; // Go back to the last coordinate that is a +
int dx = x+1; // x coordinate of the bottom right corner
// While the edge continues to the right
while(dx < w && grid[dy][dx] == '+')
{
dx++; // Increment the x coordinate
}
dx--; // Go back to the last coordinate that is a +
// If this image should be removed
if(remove(grid, y, x, dy, dx))
{
// Loop over every cell in this area
for(int a = y; a <= dy; a++)
{
for(int b = x; b <= dx; b++)
{
// Set it to whitespace
grid[a][b] = ' ';
}
}
}
}
}
}
}
// For every cell in the grid
for(int i = 0; i < h; i++)
{
for(int j = 0; j < w; j++)
{
// Print the cell
cout << grid[i][j];
}
cout << endl;
}
return 0;
}