I was solving a Codeforces problem and encountered a runtime error in my code for a specific test case.
A quick summary of the problem statement is as follows:
- You're given a 2D array of dimensions n x m (n, m <= 40,000).
- k distinct cells are given to be "waste cells".
- Someone starts at the top-left corner, then moves across each row, cyclically planting Carrots, Kiwis, then Grapes in every non-waste cell.
- You're given t queries to find what, if anything, is planted in the cell at index [i][j]. Obviously, 0 <= i < n, 0 <= j < m.
Here is my attempt at solving this problem:
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
ll n, m, k, t;
cin >> n >> m >> k >> t;
ll farm[n][m];
//All cells are initialized to -2 initially.
for(ll i = 0; i < n; i++)
{
for(ll j = 0; j < m; j++)
{
farm[i][j] = -2;
}
}
// Mark all waste cells with -1
for(ll i = 0;i < k; i++)
{
ll r, c;
cin >> r >> c;
farm[r-1][c-1] = -1;
}
// Label all the cells with either a 0, 1, or 2, corresponding to
// Carrots, Kiwis, and Grapes respectively.
ll cc = 0;
for(ll i = 0; i < n; i++)
{
for(ll j = 0; j < m; j++)
{
if(farm[i][j] != -1)
{
farm[i][j] = (cc % 3);
cc++;
}
}
}
// Process all T Queries
for(ll p = 0; p < t; p++)
{
ll r, c;
cin >> r >> c;
r--; c--;
if(farm[r][c] == -1)
cout << "Waste\n";
if(farm[r][c] == 0)
cout << "Carrots\n";
if(farm[r][c] == 1)
cout << "Kiwis\n";
if(farm[r][c] == 2)
cout << "Grapes\n";
}
return 0;
}
Here is the test-case that's causing a runtime error:
39898 39898 3 1
4567 8901
12345 23456
24680 35679
29292 12121
Can you tell me why the code isn't passing the required test-case? Any alternative solution will also be appreciated.