Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- int main()
- {
- int n, m;
- cin >> n >> m;
- int r, k;
- cin >> r >> k;
- r--;
- k--;
- queue<int> q;
- q.push(r);
- q.push(k);
- q.push(0);
- bool visited[n][m];
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- visited[i][j] = false;
- }
- }
- visited[r][k] = true;
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, -1, 1};
- int mat[n][m];
- int maksimalna_brojka = 0;
- while(!q.empty()) {
- int ci = q.front();
- q.pop();
- int cj = q.front();
- q.pop();
- int brojka = q.front();
- q.pop();
- mat[ci][cj] = brojka;
- if(brojka > maksimalna_brojka) {
- maksimalna_brojka = brojka;
- }
- for(int i = 0; i < 4; i++) {
- int ti = ci + di[i];
- int tj = cj + dj[i];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj]) {
- visited[ti][tj] = true;
- q.push(ti);
- q.push(tj);
- q.push(brojka + 1);
- }
- }
- }
- int rez =0 ;
- for(int i =0 ; i < n; i++) {
- for(int j= 0; j < m; j++) {
- if(mat[i][j] == maksimalna_brojka) {
- rez++;
- }
- }
- }
- cout << maksimalna_brojka << endl;
- cout << rez << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement