#include <bits/stdc++.h>
#include "rainbow.h"
using namespace std;
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
vector<vector<int>> land;
void init(int R, int C, int sr, int sc, int M, char *S){
sr--;
sc--;
land = vector<vector<int>>(R, vector<int>(C, 1));
int x = sr, y = sc;
land[x][y] = 0;
for (int i = 0; i < M; i++){
if (S[i] == 'N'){
x--;
}
if (S[i] == 'S'){
x++;
}
if (S[i] == 'E'){
y++;
}
if (S[i] == 'W'){
y--;
}
land[x][y] = 0;
}
}
int colour(int ar, int ac, int br, int bc){
ar--;
ac--;
int R = land.size();
int C = land[0].size();
vector<vector<bool>> used(R, vector<bool>(C, false));
int ans = 0;
for (int i = ar; i < br; i++){
for (int j = ac; j < bc; j++){
if (land[i][j] == 1 && !used[i][j]){
ans++;
used[i][j] = true;
queue<pair<int, int>> Q;
Q.push(make_pair(i, j));
while (!Q.empty()){
int x = Q.front().first;
int y = Q.front().second;
Q.pop();
for (int k = 0; k < 4; k++){
int x2 = x + dx[k];
int y2 = y + dy[k];
if (ar <= x2 && x2 < br && ac <= y2 && y2 < bc){
if (land[x2][y2] == 1 && !used[x2][y2]){
used[x2][y2] = true;
Q.push(make_pair(x2, y2));
}
}
}
}
}
}
}
return ans;
}
int main(){
init(6, 4, 3, 3, 9, "NWESSWEWS");
cout << colour(2, 3, 2, 3) << endl;
cout << colour(3, 2, 4, 4) << endl;
cout << colour(5, 3, 6, 4) << endl;
cout << colour(1, 2, 5, 3) << endl;
}
Compilation message
rainbow.cpp: In function 'int main()':
rainbow.cpp:64:23: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
64 | init(6, 4, 3, 3, 9, "NWESSWEWS");
| ^~~~~~~~~~~
/usr/bin/ld: /tmp/ccgZiZNe.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccFrneFe.o:rainbow.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status