This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<math.h>
#include<assert.h>
using namespace std;
const double PI = (double) atan(1.0) * 4;
const int64_t INF = (int64_t) 1e18 + 777;
const int64_t mINF = (int64_t) 2e9 + 777;
const int64_t offset = (int64_t) 1e6 + 777;
const int64_t MOD = 1e9 + 7;
const int nbit = 11;
const int ndig = 10;
const int nchar = 26;
const int p1 = 31;
const int p2 = 53;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};
// 0 right // 1 down // 2 left // 3 up
int modadd(int t1, int t2)
{
int res = t1 + t2;
if(res >= MOD) res -= MOD;
return res;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return (res % MOD);
}
struct Cell
{
int r; int c;
Cell(int r_, int c_) : r(r_), c(c_) {}
bool isWithinGrid(int nr, int nc)
{
return 0 <= r && r < nr &&
0 <= c && c < nc;
}
};
struct Node
{
int w;
int x; int y;
Node(int w_, int x_, int y_) : w(w_), x(x_), y(y_) {}
};
struct Solution
{
int n; int m; int k;
Solution() {}
void solve()
{
cin >> n >> m >> k;
k--;
Cell s(-1, -1);
Cell e(-1, -1);
cin >> s.r >> s.c >> e.r >> e.c;
s.r--; s.c--; e.r--; e.c--;
vector<string> g(n);
for(int i = 0; i < n; i++)
{
cin >> g[i];
}
vector<vector<Node> > dis(n, vector<Node>(m, Node(mINF, 0, 0)));
vector<Cell> black;
vector<Cell> white;
white.emplace_back(s);
dis[s.r][s.c] = Node(0, 0, 0);
while((int) white.size() != 0)
{
for(int i = 0; i < (int) white.size(); i++)
{
Cell u = white[i];
int w = dis[u.r][u.c].w;
int x = dis[u.r][u.c].x;
int y = dis[u.r][u.c].y;
if(x == 0) continue;
Cell v = Cell(u.r - 1, u.c);
if(v.isWithinGrid(n, m) && dis[v.r][v.c].w > w)
{
dis[v.r][v.c] = Node(w, x - 1, y);
white.emplace_back(v);
}
v = Cell(u.r + 1, u.c);
if(v.isWithinGrid(n, m) && dis[v.r][v.c].w > w)
{
dis[v.r][v.c] = Node(w, x - 1, y);
white.emplace_back(v);
}
}
for(int i = 0; i < (int) white.size(); i++)
{
Cell u = white[i];
int w = dis[u.r][u.c].w;
int x = dis[u.r][u.c].x;
int y = dis[u.r][u.c].y;
if(y == 0) continue;
Cell v = Cell(u.r, u.c - 1);
if(v.isWithinGrid(n, m) && dis[v.r][v.c].w > w)
{
dis[v.r][v.c] = Node(w, x, y - 1);
white.emplace_back(v);
}
v = Cell(u.r, u.c + 1);
if(v.isWithinGrid(n, m) && dis[v.r][v.c].w > w)
{
dis[v.r][v.c] = Node(w, x, y - 1);
white.emplace_back(v);
}
}
for(int i = 0; i < (int) white.size(); i++)
{
Cell u = white[i];
int w = dis[u.r][u.c].w;
for(int j = 0; j < D; j++)
{
Cell v = Cell(u.r + dr[j], u.c + dc[j]);
if(!v.isWithinGrid(n, m)) continue;
if(g[v.r][v.c] == '.' && dis[v.r][v.c].w > w)
{
white.emplace_back(v);
dis[v.r][v.c] = Node(w, 0, 0);
}
if(g[v.r][v.c] == '#' && dis[v.r][v.c].w > w + 1)
{
black.emplace_back(v);
dis[v.r][v.c] = Node(w + 1, k, k);
}
}
}
swap(black, white);
black.clear();
}
cout << dis[e.r][e.c].w << "\n";
}
int modsub(int t1, int t2)
{
int res = t1 - t2;
if(res < 0) res += MOD;
return res;
}
bool BIT(int& mask, int& j)
{
return (mask & MASK(j));
}
int64_t Abs(int64_t t1)
{
if(t1 < 0) return -t1;
return t1;
}
int MASK(int j)
{
return (1 << j);
}
};
void __startup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__startup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
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... |