#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
//typedef __int128 ull;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<pi, pi> piii;
ll const INF = 1e13;
ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0, 0, 1, -1};
int main()
{
ll n, r, c, sx, sy, gx, gy;
cin >> r >> c >> n;
cin >> sx >> sy >> gx >> gy;
sx--; sy--; gx--; gy--;
vector<vector<char> > mapp(r + 5, vector<char> (c + 5));
for (ll i=0; i<r; ++i)for (ll j=0; j<c; ++j) cin >> mapp[i][j];
vector<vector<ll> > dist(r + 5, vector<ll> (c + 5, INF));
vector<vector<pi> > dist2(r + 5, vector<pi> (c + 5, mp(INF, INF)));
vector<vector<bool> > vis(r + 5, vector<bool> (c + 5, 0));
deque<pi> dq;
dist[sx][sy] = 0;
dq.pb(mp(sx, sy));
ll now = 0;
while (dq.size())
{
//show(now);
//show('0');
// bfs - 0
vector<pi> cur;
while (dq.size())
{
ll x = dq.front().f,y = dq.front().s; dq.pop_front();
//show2(x, y);
cur.pb(mp(x, y));
dist[x][y] = now;
vis[x][y] = 1;
for (ll i=0; i<4; ++i)
{
ll newx = x + dx[i], newy = y + dy[i];
if (newx < 0 || newx >= r || newy < 0 || newy >= c || mapp[newx][newy] == '#') continue;
//show2(newx, newy);
if (dist[newx][newy] > dist[x][y])
{
dist[newx][newy] = dist[x][y];
dq.pb(mp(newx, newy));
}
}
}
deque<pi> newdq;
for (pi u: cur)
{
ll x = u.f, y = u.s;
for (ll i=0; i<4; ++i)
{
ll newx = x + dx[i], newy = y + dy[i];
if (newx < 0 || newx >= r || newy < 0 || newy >= c || vis[newx][newy]) continue;
vis[newx][newy] = 1;
dist2[newx][newy] = mp(1, 1);
newdq.pb(mp(newx, newy));
}
}
// bfs - 1
//show('1');
while (newdq.size())
{
//show(1);
ll x = newdq.front().f , y = newdq.front().s; newdq.pop_front();
//show2(x, y);
dq.pb(mp(x, y));
for (ll i=0; i<4; ++i)
{
ll newx = x + dx[i], newy = y + dy[i];
if (newx < 0 || newx >= r || newy < 0 || newy >= c || vis[newx][newy]) continue;
pi newdist = mp(dist2[x][y].f + abs(dx[i]), dist2[x][y].s + abs(dy[i]));
if (newdist.f > n || newdist.s > n) continue;
if (newdist < dist2[newx][newy])
{
dist2[newx][newy] = newdist;
vis[newx][newy] = 1;
newdq.pb(mp(newx, newy));
}
}
}
now++;
}
cout << dist[gx][gy] << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |