I'm trying to solve the first problem "Wiggle Walk" of Google KickStart Round C 2019. You can find more information here: https://codingcompetitions.withgoogle.com/kickstart/round/0000000000050ff2/0000000000150aac I've tried some test input and everything seems to work fine. However when I submit my attempt I get a Runtime Error. Any idea of why is this happening?
#include <iostream>
using namespace std;
int main() {
int T, N, R, C, Sr, Sc;
string str;
cin >> T;
for (int i = 1; i <= T; i++) {
cin >> N >> R >> C >> Sr >> Sc;
cin >> str;
Sr -= 1;
Sc -= 1;
int a[R][C];
for (int j = 1; j <= R; j++) {
for (int k = 1; k <= C; k++) {
a[j-1][k-1] = 0;
}
}
a[Sr][Sc] = 1;
for (int j = 1; j <= N; j++) {
if (str.at(j-1) == 'E') {
while (a[Sr][Sc+1] == 1) {
Sc += 1;
}
Sc += 1;
a[Sr][Sc] = 1;
}
else if (str.at(j-1) == 'W') {
while (a[Sr][Sc-1] == 1) {
Sc -= 1;
}
Sc -= 1;
a[Sr][Sc] = 1;
}
else if (str.at(j-1) == 'S') {
while (a[Sr+1][Sc] == 1) {
Sr += 1;
}
Sr += 1;
a[Sr][Sc] = 1;
}
else if (str.at(j-1) == 'N') {
while (a[Sr-1][Sc] == 1) {
Sr -= 1;
}
Sr -= 1;
a[Sr][Sc] = 1;
}
}
cout << "Case #" << i << ": " << Sr+1 << " " << Sc+1;
if (i != T) {
cout << "" << endl;
}
}
return 0;
}
For those interested, I was able to solve both test sets with the following code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int T, N, R, C, SR, SC;
string str;
cin >> T;
for (int i = 1; i <= T; i++) {
cin >> N >> R >> C >> SR >> SC;
cin >> str;
SR -= 1;
SC -= 1;
vector<vector<bool> > a(R, vector<bool>(C));
a[SR][SC] = 1;
for (int j = 1; j <= N; j++) {
if (str.at(j-1) == 'E') {
while (a[SR][SC+1] == 1) {
SC += 1;
}
SC += 1;
a[SR][SC] = 1;
}
else if (str.at(j-1) == 'W') {
while (a[SR][SC-1] == 1) {
SC -= 1;
}
SC -= 1;
a[SR][SC] = 1;
}
else if (str.at(j-1) == 'S') {
while (a[SR+1][SC] == 1) {
SR += 1;
}
SR += 1;
a[SR][SC] = 1;
}
else if (str.at(j-1) == 'N') {
while (a[SR-1][SC] == 1) {
SR -= 1;
}
SR -= 1;
a[SR][SC] = 1;
}
}
cout << "Case #" << i << ": " << SR+1 << " " << SC+1 << endl;
}
return 0;
}