Google Kickstart Round A , 3rd problem 'Rabbit House' , HELP NEEDED

Revision en1, by Satyam1104, 2021-03-23 13:08:19

getting wrong answer

problem

[my code]

include <bits/stdc++.h>

include <stdio.h>

define ll long long int

define rep(i, a, n) for (ll i = a; i < n; i++)

define rev(i, a, n) for (ll i = a; i >= n; i--)

define sz size()

define ff first

define ss second

define vec vector

define vecpair vector<pair<ll, ll>>

define pb push_back

define po pop_back

define mk make_pair

define all(a) a.begin(), a.end()

define fill(a, b) memset(a, b, sizeof(a))

define mod 1000000007

define endl '\n'

const int N = 5e5 + 6; using namespace std; void solve() { ll n, m; cin >> n >> m; ll g[n][m]; ll x, y, mx = 0; queue<pair<ll, ll>>q; ll vis[n][m]; rep(i, 0, n) { rep(j, 0, m) { cin >> g[i][j]; vis[i][j] = 0; mx = max(mx, g[i][j]); } } rep(i, 0, n) { rep(j, 0, m) { if (g[i][j] == mx) { vis[i][j] = 1; q.push({i, j});} } }

ll xx[4] = { -1, 0, 1, 0};
ll yy[4] = {0, 1, 0, -1};

ll ans = 0;
while (!q.empty()) {
    x = q.front().ff;
    y = q.front().ss;
    q.pop();
    rep(i, 0, 4) {
       ll X = x + xx[i];
       ll Y = y + yy[i];
       if (X < 0 or Y <0 or Y > m - 1 or X > n - 1) continue;

       if (!vis[X][Y]) {
         if (g[X][Y] == g[x][y]) continue;
         else {
          if ((g[x][y] - g[X][Y]) == 1) {
              q.push({X, Y});
              vis[X][Y] = 1;
          }
          else if ((g[x][y] - g[X][Y]) > 1) {
              vis[X][Y] = 1;
              ans += (g[x][y] - g[X][Y]) - 1;
              g[X][Y] = g[x][y] - 1;
              q.push({X, Y});
          }
         }
       }
    }
}
cout << ans << endl;

}

int main() {

ifndef ONLINE_JUDGE

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);

endif

ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
rep(i, 1, t + 1)
{
    cout << "Case #" << i << ": ";
    solve();
}
return 0;

}

Tags #googlekickstart, #help

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English Satyam1104 2021-03-23 15:27:05 2 update: added the code part in block
en3 English Satyam1104 2021-03-23 14:54:56 18
en2 English Satyam1104 2021-03-23 13:09:45 448
en1 English Satyam1104 2021-03-23 13:08:19 1997 Initial revision (published)