#include <bits/stdc++.h>
#include "rainbow.h"
using namespace std;
int xmin, xmax, ymin, ymax;
vector<vector<int>> S1, S2, S3, S4;
void init(int R, int C, int sr, int sc, int M, string S){
assert(R <= 1000 && C <= 1000);
sr--;
sc--;
vector<int> x(M + 1), y(M + 1);
x[0] = sr;
y[0] = sc;
for (int i = 0; i < M; i++){
x[i + 1] = x[i];
y[i + 1] = y[i];
if (S[i] == 'N'){
x[i + 1]--;
}
if (S[i] == 'S'){
x[i + 1]++;
}
if (S[i] == 'E'){
y[i + 1]++;
}
if (S[i] == 'W'){
y[i + 1]--;
}
}
xmin = sr;
xmax = sr;
ymin = sc;
ymax = sc;
for (int i = 1; i <= M; i++){
xmin = min(xmin, x[i]);
xmax = max(xmax, x[i]);
ymin = min(ymin, y[i]);
ymax = max(ymax, y[i]);
}
vector<vector<bool>> river(R, vector<bool>(C, false));
for (int i = 0; i <= M; i++){
river[x[i]][y[i]] = true;
}
S1 = vector<vector<int>>(R + 1, vector<int>(C + 1, 0));
for (int i = 0; i < R; i++){
for (int j = 0; j < C; j++){
S1[i + 1][j + 1] = S1[i + 1][j] + S1[i][j + 1] - S1[i][j];
if (!river[i][j]){
S1[i + 1][j + 1]++;
}
}
}
S2 = vector<vector<int>>(R, vector<int>(C + 1, 0));
for (int i = 0; i < R - 1; i++){
for (int j = 0; j < C; j++){
S2[i + 1][j + 1] = S2[i + 1][j] + S2[i][j + 1] - S2[i][j];
if (!river[i][j] && !river[i + 1][j]){
S2[i + 1][j + 1]++;
}
}
}
S3 = vector<vector<int>>(R + 1, vector<int>(C, 0));
for (int i = 0; i < R; i++){
for (int j = 0; j < C - 1; j++){
S3[i + 1][j + 1] = S3[i + 1][j] + S3[i][j + 1] - S3[i][j];
if (!river[i][j] && !river[i][j + 1]){
S3[i + 1][j + 1]++;
}
}
}
S4 = vector<vector<int>>(R, vector<int>(C, 0));
for (int i = 0; i < R - 1; i++){
for (int j = 0; j < C - 1; j++){
S4[i + 1][j + 1] = S4[i + 1][j] + S4[i][j + 1] - S4[i][j];
if (!river[i][j] && !river[i][j + 1] && !river[i + 1][j] && !river[i + 1][j + 1]){
S4[i + 1][j + 1]++;
}
}
}
}
int colours(int ar, int ac, int br, int bc){
ar--;
ac--;
int ans = 0;
ans += S1[br][bc] - S1[ar][bc] - S1[br][ac] + S1[ar][ac];
ans -= S2[br - 1][bc] - S2[ar][bc] - S2[br - 1][ac] + S2[ar][ac];
ans -= S3[br][bc - 1] - S3[ar][bc - 1] - S3[br][ac] + S3[ar][ac];
ans += S4[br - 1][bc - 1] - S4[ar][bc - 1] - S4[br - 1][ac] + S4[ar][ac];
if (ar < xmin && xmax < br && ac < ymin && ymax < bc){
ans++;
}
return ans;
}
int main(){
init(6, 4, 3, 3, 9, "NWESSWEWS");
cout << colours(2, 3, 2, 3) << endl;
cout << colours(3, 2, 4, 4) << endl;
cout << colours(5, 3, 6, 4) << endl;
cout << colours(1, 2, 5, 3) << endl;
cout << colours(1, 1, 6, 4) << endl;
}
Compilation message
rainbow.cpp: In function 'int main()':
rainbow.cpp:94:23: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
94 | init(6, 4, 3, 3, 9, "NWESSWEWS");
| ^~~~~~~~~~~
/usr/bin/ld: /tmp/cckskSxZ.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccTBaqfZ.o:rainbow.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccTBaqfZ.o: in function `main':
rainbow.cpp:(.text.startup+0x2a): undefined reference to `init(int, int, int, int, int, char*)'
/usr/bin/ld: /tmp/cckskSxZ.o: in function `main':
grader.cpp:(.text.startup+0xed): undefined reference to `init(int, int, int, int, int, char*)'
/usr/bin/ld: grader.cpp:(.text.startup+0x167): undefined reference to `colour(int, int, int, int)'
collect2: error: ld returned 1 exit status