Knowee
Questions
Features
Study Tools

#include<bits/stdc++.h> using namespace std; #define ll long long int #define f(n,m) for(int i=n; i<m; ++i) #define fj(n,m) for(int j=n; j<m; ++j) #define sp " " #define rf(n,m) for(int i=n-1; i>=m; --i) #define all(v) v.begin(),v.end() #define srt(nums) sort(nums.begin(), nums.end()); #define mx_e(nums) *max_element(nums.begin(), nums.end()) #define mn_e(nums) *min_element(nums.begin(), nums.end()) ll mod = (ll)1e9+7; vector<bool> sieve(ll n) { vector<bool> isPrime(n + 1, true); isPrime[0] = isPrime[1] = false; for (ll i = 2; i * i <= n; ++i) { if (isPrime[i]) { for (ll j = i * i; j <= n; j += i) { isPrime[j] = false; } } } return isPrime; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // int help(int i, int j, vector<vector<int>>&grid){ // if(i<0 or j<0 or i) // } void solve(){ int n, m; cin >> n >> m; vector<string> arr(n); for (int i = 0; i < n; ++i) { cin >> arr[i]; } vector<vector<int>> dist(n, vector<int>(m, INT_MAX)); queue<pair<int, int>> q; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (arr[i][j] == '1') { dist[i][j] = 0; q.push({i, j}); } } } while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); int offset[] = {0, -1, 0 ,1, 0}; for (int i = 0; i < 4; ++i) { int nx = x + offset[i]; int ny = y + offset[i+1]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && dist[nx][ny] == INT_MAX) { dist[nx][ny] = dist[x][y] + 1; q.push({nx, ny}); } } } int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { ans = max(ans, dist[i][j]); } } cout << ans << endl; } int main(){ int t; cin >> t; while(t--){ solve(); } return 0; } check this code and correct it

Question

#include<bits/stdc++.h> using namespace std; #define ll long long int #define f(n,m) for(int i=n; i<m; ++i) #define fj(n,m) for(int j=n; j<m; ++j) #define sp " " #define rf(n,m) for(int i=n-1; i>=m; --i) #define all(v) v.begin(),v.end() #define srt(nums) sort(nums.begin(), nums.end()); #define mx_e(nums) *max_element(nums.begin(), nums.end()) #define mn_e(nums) *min_element(nums.begin(), nums.end()) ll mod = (ll)1e9+7; vector<bool> sieve(ll n) { vector<bool> isPrime(n + 1, true); isPrime[0] = isPrime[1] = false; for (ll i = 2; i * i <= n; ++i) { if (isPrime[i]) { for (ll j = i * i; j <= n; j += i) { isPrime[j] = false; } } } return isPrime; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // int help(int i, int j, vector<vector<int>>&grid){ // if(i<0 or j<0 or i) // }

void solve(){

    int n, m;
    cin &gt;&gt; n &gt;&gt; m;
    vector&lt;string&gt; arr(n);
    for (int i = 0; i &lt; n; ++i) {
        cin &gt;&gt; arr[i];
    }
    vector&lt;vector&lt;int&gt;&gt; dist(n, vector&lt;int&gt;(m, INT_MAX));
    queue&lt;pair&lt;int, int&gt;&gt; q;
    for (int i = 0; i &lt; n; ++i) {
        for (int j = 0; j &lt; m; ++j) {
            if (arr[i][j] == '1') {
                dist[i][j] = 0;
                q.push({i, j});
            }
        }
    }
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();

        int offset[] = {0, -1, 0 ,1, 0};
        for (int i = 0; i &lt; 4; ++i) {
            int nx = x + offset[i];
            int ny = y + offset[i+1];
            if (nx &gt;= 0 &amp;&amp; nx &lt; n &amp;&amp; ny &gt;= 0 &amp;&amp; ny &lt; m &amp;&amp; dist[nx][ny] == INT_MAX) {
                dist[nx][ny] = dist[x][y] + 1;
                q.push({nx, ny});
            }
        }
    }
    int ans = 0;
    for (int i = 0; i &lt; n; ++i) {
        for (int j = 0; j &lt; m; ++j) {
            ans = max(ans, dist[i][j]);
        }
    }
    cout &lt;&lt; ans &lt;&lt; endl;

}

int main(){ int t; cin >> t;

while(t--){
 solve();

} return 0;

} check this code and correct it

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The code you've written is in C++. It seems like you're trying to solve a problem related to graphs or matrices. The code is mostly correct, but there are a few things you might want to consider:

  1. The #include<bits/stdc++.h> is a GCC specific library which includes most of the standard libraries. It's not a good practice to use this in production code as it increases compilation time and is not portable. It's better to include only the libraries that you need.

  2. The #define directives you've used are not recommended in C++. It's better to use const or constexpr for constants and functions for code that is repeated.

  3. The sieve function is not used anywhere in the code. If you don't need it, you should remove it.

  4. The help function is commented out. If you don't need it, you should remove it.

This problem has been solved

Similar Questions

0/0

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.