#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 >> 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
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:
-
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. -
The
#definedirectives you've used are not recommended in C++. It's better to useconstorconstexprfor constants and functions for code that is repeated. -
The
sievefunction is not used anywhere in the code. If you don't need it, you should remove it. -
The
helpfunction is commented out. If you don't need it, you should remove it.
Similar Questions
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.