Language: C++

#include<bits/stdc++.h>
using namespace std;
int N, M, K, x0, x1, y0, y1;
vector<set<int>> rows, cols;
vector<set<int>> lotusR, lotusC;

pair<int,int> getRange(set<int> &st, int y, int Bound) {
        set<int>::iterator sec = st.upper_bound(y);
        int lo = y - K - 1, hi = min(Bound, y + K + 1);
        if (sec != st.end()) {
            hi = (*(sec)) ;
        }
        if (sec != st.begin()) {
            --sec;
            lo = (*(sec));
        }
        return {lo, hi};
}


int bfs(vector<set<int>> &rows, vector<set<int>> &cols, vector<set<int>> &lotusR, vector<set<int>> &lotusC) {
    vector<vector<int>> D(N, vector<int>(M, INT_MAX));
    queue<pair<int, int>> Q;
    Q.push({x0, y0});
    D[x0][y0] = 0;
    rows[x0].erase(y0);
    cols[y0].erase(x0);
    while (!Q.empty()) {
        auto firstEle = Q.front();
        Q.pop();
        vector<int> toBeRem;
        auto row = getRange(lotusR[firstEle.first], firstEle.second, M);
        for (auto it = rows[firstEle.first].upper_bound(row.first);
                        it != rows[firstEle.first].end() && (*it) < row.second;
                        ++it) {
            D[firstEle.first][(*it)] = D[firstEle.first][firstEle.second] + 1;
            Q.push({firstEle.first, (*it)});
            toBeRem.push_back(*it);
        }
        for (int ele: toBeRem) {
            rows[firstEle.first].erase(ele);
            cols[ele].erase(firstEle.first);
        }
        toBeRem.clear();

        auto col = getRange(lotusC[firstEle.second], firstEle.first, N);
        for (auto it = cols[firstEle.second].upper_bound(col.first);
                        it != cols[firstEle.second].end() && (*it) < col.second;
                        ++it) {

            D[(*it)][firstEle.second] = D[firstEle.first][firstEle.second] + 1;
            Q.push({(*it), firstEle.second});
            toBeRem.push_back(*it);
        }
        for (int ele: toBeRem) {
            rows[ele].erase(firstEle.second);
            cols[firstEle.second].erase(ele);
        }
    }
    return D[x1][y1] == INT_MAX ? -1 : D[x1][y1];
}

int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    cin >> N >> M >> K >> x0 >> y0 >> x1 >> y1;
    --x0;--y0;--x1;--y1;
    rows.resize(N);
    cols.resize(M);
    lotusR.resize(N);
    lotusC.resize(M);
    for (int i = 0; i < N; ++i) {
        string A;
        cin >> A;
        for (int j = 0; j < M; ++j) {
            if (A[j] == '.') {
                rows[i].insert(j);
                cols[j].insert(i);
            } else {
                lotusR[i].insert(j);
                lotusC[j].insert(i);
            }
        }
    }
    if (lotusR[x0].find(y0) != lotusR[x0].end() || lotusR[x1].find(y1) != lotusR[x1].end()) {
        cout << -1 << endl;
        return 0;
    }
    
    cout << bfs(rows, cols, lotusR, lotusC);

    return 0;
}


Compiler output:
a.cc:3:22: error: 'int y0' redeclared as different kind of entity
    3 | int N, M, K, x0, x1, y0, y1;
      |                      ^~
In file included from /usr/include/features.h:523,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:683,
                 from /usr/include/c++/14/cassert:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:33,
                 from a.cc:1:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:256:1: note: previous declaration 'double y0(double)'
  256 | __MATHCALL (y0,, (_Mdouble_));
      | ^~~~~~~~~~
a.cc:3:26: error: 'int y1' redeclared as different kind of entity
    3 | int N, M, K, x0, x1, y0, y1;
      |                          ^~
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:257:1: note: previous declaration 'double y1(double)'
  257 | __MATHCALL (y1,, (_Mdouble_));
      | ^~~~~~~~~~
a.cc: In function 'int bfs(std::vector<std::set<int> >&, std::vector<std::set<int> >&, std::vector<std::set<int> >&, std::vector<std::set<int> >&)':
a.cc:24:11: error: no matching function for call to 'std::queue<std::pair<int, int> >::push(<brace-enclosed initializer list>)'
   24 |     Q.push({x0, y0});
      |     ~~~~~~^~~~~~~~~~
In file included from /usr/include/c++/14/queue:66,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:157:
/usr/include/c++/14/bits/stl_queue.h:283:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = std::pair<int, int>; _Sequence = std::deque<std::pair<int, int>, std::allocator<std::pair<int, int> > >; value_type = std::pair<int, int>]'
  283 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:283:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::queue<std::pair<int, int> >::value_type&' {aka 'const std::pair<int, int>&'}
  283 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:288:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(value_type&&) [with _Tp = std::pair<int, int>; _Sequence = std::deque<std::pair<int, int>, std::allocator<std::pair<int, int> > >; value_type = std::pair<int, int>]'
  288 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:288:25: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::queue<std::pair<int, int> >::value_type&&' {aka 'std::pair<int, int>&&'}
  288 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~
a.cc:25:11: error: invalid conversion from 'double (*)(double) noexcept' to 'std::vector<int>::size_type' {aka 'long unsigned int'} [-fpermissive]
   25 |     D[x0][y0] = 0;
      |           ^~
      |           |
      |           double (*)(double) noexcept
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53:
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   initializing argument 1 of 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = int; _Alloc = std::allocator<int>; reference = int&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
a.cc:26:20: error: invalid conversion from 'double (*)(double) noexcept' to 'std::set<int>::key_type' {aka 'int'} [-fpermissive]
   26 |     rows[x0].erase(y0);
      |                    ^~
      |                    |
      |                    double (*)(double) noexcept
In file included from /usr/include/c++/14/set:63,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:158:
/usr/include/c++/14/bits/stl_set.h:686:29: note:   initializing argument 1 of 'std::set<_Key, _Compare, _Alloc>::size_type std::set<_Key, _Compare, _Alloc>::erase(const key_type&) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; size_type = long unsigned int; key_type = int]'
  686 |       erase(const key_type& __x)
      |             ~~~~~~~~~~~~~~~~^~~
a.cc:27:10: error: invalid conversion from 'double (*)(double) noexcept' to 'std::vector<std::set<int> >::size_type' {aka 'long unsigned int'} [-fpermissive]
   27 |     cols[y0].erase(x0);
      |          ^~
      |          |
      |          double (*)(double) noexcept
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   initializing argument 1 of 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = std::set<int>; _Alloc = std::allocator<std::set<int> >; reference = std::set<int>&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
a.cc:60:18: error: invalid conversion from 'double (*)(double) noexcept' to 'std::vector<int>::size_type' {aka 'long unsigned int'} [-fpermissive]
   60 |     return D[x1][y1] == INT_MAX ? -1 : D[x1][y1];
      |                  ^~
      |                  |
      |                  double (*)(double) noexcept
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   initializing argument 1 of 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = int; _Alloc = std::allocator<int>; reference = int&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
a.cc:60:46: error: invalid conversion from 'double (*)(double) noexcept' to 'std::vector<int>::size_type' {aka 'long unsigned int'} [-fpermissive]
   60 |     return D[x1][y1] == INT_MAX ? -1 : D[x1][y1];
      |                                              ^~
      |                                              |
      |                                              double (*)(double) noexcept
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   initializing argument 1 of 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = int; _Alloc = std::allocator<int>; reference = int&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
a.cc: In function 'int main()':
a.cc:65:30: error: no match for 'operator>>' (operand types are 'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} and 'double(double) noexcept')
   65 |     cin >> N >> M >> K >> x0 >> y0 >> x1 >> y1;
      |     ~~~~~~~~~~~~~~~~~~~~~~~~ ^~ ~~
      |                        |        |
      |                        |        double(double) noexcept
      |                        std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}
In file included from /usr/include/c++/14/sstream:40,
                 from /usr/include/c++/14/complex:45,
                 from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127:
/usr/include/c++/14/istream:170:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  170 |       operator>>(bool& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:170:7: note:   conversion of argument 1 would be ill-formed:
a.cc:65:33: error: cannot bind non-const lvalue reference of type 'bool&' to a value of type 'double (*)(double) noexcept'
   65 |     cin >> N >> M >> K >> x0 >> y0 >> x1 >> y1;
      |                                 ^~
/usr/include/c++/14/istream:174:7: note: candidate: 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits<char>]' (near match)
  174 |       operator>>(short& __n);
      |       ^~~~~~~~
/usr/include/c++/14/istream:174:7: note:   conversion of argument 1 would be ill-formed:
a.cc:65:33: error: invalid conversion from 'double (*)(double) noexcept' to 'short int' [-fpermissive]
   65 |     cin >> N >> M >> K >> x0 >> y0 >> x1 >> y1;
      |                                 ^~
      |                                 |
      |                                 double (*)(double) noexcept
a.cc:65:33: error: cannot bind rvalue '(short int)y0' to 'short int&'
/usr/include/c++/14/istream:177:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; __istream_type = std::basic_istream<char>]' (near match)
  177 |       operator>>(unsigned short& __n)
      |       ^~~~~~~~
/usr/include/c++/14/istream:177:7: note:   conversion of argument 1 would be ill-formed:
a.cc:65:33: error: invalid conversion from 'double (*)(double) noexcept' to 'short unsigned int' [-fpermissive]
   65 |     cin >> N >> M >> K >> x0 >> y0 >> x1 >> y1;
      |                                 ^~
      |                                 |
      |                                 double (*)(double) noexcept
a.cc:65:33: error: cannot bind rvalue '(short unsigned int)y0' to 'short unsigned int&'
/usr/include/c++/14/istream:181:7: note: candidate: 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char; _Traits = std::char_traits<char>]' (near match)
  181 |       operator>>(int& __n);
      |       ^~~~~~~~
/usr/include/c++/14/istream:181:7: note:   conversion of argument 1 would be ill-formed:
a.cc:65:33: error: invalid conversion from 'double (*)(double) noexcept' to 'int' [-fpermissive]
   65 |     cin >> N >> M >> K >> x0 >> y0 >> x1 >> y1;
      |                                 ^~
      |                                 |
      |                                 double (*)(double) noexcept
a.cc:65:33: error: cannot bind rvalue '(int)y0' to 'int&'
/usr/include/c++/14/istream:184:7: note: candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::opera