//Template credits:
//Benq, Errichto and me
#ifndef OLYMPIAD_IN_INFORMATICS
#define OLYMPIAD_IN_INFORMATICS
#include <bits/stdc++.h>
using namespace std;
#define i32 signed
#define int long long
using str = string; //yay python!
#define X first
#define Y second
using pii = pair<int,int>;
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define f0r(i,a,b) for(auto i = (a); i != (b); i++)
#define r0f(i,a,b) for(auto i = (a); i >= (b); i--)
#define each(x,y) for(auto x : y)
#define sim template<class c
#define dor > dbg& operator<<
#define ris return *this
#define eni(x) sim> typename enable_if<sizeof dud<c>(0) x 1, dbg&>::type operator<<(c i)
sim> struct rge { c b,e; };
sim> rge<c> range(c i, c j) { return rge<c>{i,j}; }
sim> auto dud(c* x) -> decltype(cout << *x, 0);
sim> char dud(...);
template<class T, size_t R, size_t C>
struct matrik {
T (&arr)[R][C];
int r1, r2, c1, c2;
//pythonic: dim is either {r2, c2} or {r1, r2, c1, c2}
matrik(T (&a)[R][C], initializer_list<int> dims)
: arr(a), r1(0), r2(R), c1(0), c2(C)
{
auto it = dims.begin();
if (dims.size() == 2) {
r2 = *it++;
c2 = *it;
}
else if (dims.size() == 4) {
r1 = *it++;
r2 = *it++;
c1 = *it++;
c2 = *it;
}
}
};
sim>
struct mash {
vt<vt<c>> arr;
char whatevs;
mash(vt<vt<c>> what) {
arr = what;
}
};
sim, class b> ostream& operator<<(ostream &out, pair<b,c> d) {
out << "(" << d.X << ", " << d.Y << ")";
return out;
}
sim> ostream& operator<<(ostream &out, rge<c> d) {
out << "{";
f0r(it, d.b, d.e) out << ", " + 2*(it==d.b) << *it;
out << "}";
return out;
}
struct dbg {
//behaviour: it won't print spaces after debugging
~dbg() { cerr << '\n'; }
eni(!=) { // for non-iterables
cerr << boolalpha << i; ris;
}
eni(==) { // for iterables
ris << range(all(i));
}
// pair printer
sim, class b dor(pair<b,c> d) {
ris << "(" << d.X << ", " << d.Y << ")";
}
// range printer (should work for like most c++ stuffs)
sim dor(rge<c> d) {
*this << "{";
f0r(it, d.b, d.e) *this << ", " + 2*(it==d.b) << *it;
ris << "}";
}
sim, size_t R, size_t C dor(const matrik<c,R,C>& m) {
int rows = m.r2 - m.r1;
int cols = m.c2 - m.c1;
//ts only works for immediate printables...rip
vt<int> w(cols, 0);
for (int j = 0; j < cols; ++j)
for (int i = 0; i < rows; ++i) {
ostringstream buf;
buf << m.arr[m.r1 + i][m.c1 + j];
w[j] = max(w[j], sz(buf.str()));
}
*this << "[";
for (int i = 0; i < rows; ++i) {
if (i) cerr << ",\n ";
cerr << "[";
for (int j = 0; j < cols; ++j) {
if (j) cerr << ", ";
cerr << setw(w[j]) // right-justify to column width
<< m.arr[m.r1 + i][m.c1 + j];
}
cerr << "]";
}
ris << "]";
}
sim dor(const mash<c> &a) {
int n = sz(a.arr);
int m = 0;
f0r(i,0,n) m = max(m,sz(a.arr[i]));
vt<int> w(m);
f0r(i,0,n) {
f0r(j,0,sz(a.arr[i])) {
ostringstream buf;
buf << a.arr[i][j];
w[j] = max(w[j], sz(buf.str()));
}
}
cerr << "[";
f0r(i,0,n) {
if (i) cerr << ",\n ";
cerr << "[";
f0r(j,0,sz(a.arr[i])) {
if (j) cerr << ", ";
cerr << setw(w[j]) << a.arr[i][j];
}
cerr << "]";
}
ris << "]";
}
//3 tuple example
sim, class d, class e dor(const tuple<c,d,e>& t) {
ris << "(" << get<0>(t) << ", "
<< get<1>(t) << ", " << get<2>(t) << ")";
}
};
#define imie(r...) " [" << #r << ": " << (r) << "] "
sim, size_t depth>
struct _nvt {
using type = vt<typename _nvt<c,depth-1>::type>;
};
sim>
struct _nvt<c,1> {
using type = vt<c>;
};
sim, size_t depth>
using nvt = typename _nvt<c,depth>::type;
mt19937_64 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
mt19937 rng32((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
int rand(int a, int b) {
return a + rng() % (b-a+1);
}
//do not forget to invert priority queue comparisons
//never forget the cf "the way home" incident
template<class T> using pqm = priority_queue<T, vector<T>, greater<T>>;
constexpr int pct(int x) { return __builtin_popcountll((unsigned long long)x); }
//this is more mathematically cool than you think
//it should work for o(1) range max queries
//p2(bits(x)) returns the highest power of 2 <= x
constexpr int bits(int x) { return x == 0 ? 0 : 63 - __builtin_clzll((unsigned long long)x); }
constexpr int p2(int x) { return (int)1 << x; }
constexpr int msk2(int x) { return p2(x) - 1; }
inline long long cdiv(long long a, long long b) { return a / b + ((a ^ b) > 0 && a % b); }
inline long long fdiv(long long a, long long b) { return a / b - ((a ^ b) < 0 && a % b); }
//olympiad (ts is so tuff bro)
template<class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<class T, class U> T fstTrue(T lo, T hi, U f) { ++hi; assert(lo <= hi); while(lo < hi) { T mid = lo + (hi - lo) / 2; f(mid) ? hi = mid : lo = mid + 1; } return lo; }
template<class T, class U> T lstTrue(T lo, T hi, U f) { --lo; assert(lo <= hi); while(lo < hi) { T mid = lo + (hi - lo + 1) / 2; f(mid) ? lo = mid : hi = mid - 1; } return lo; }
template<class T> void remDup(vector<T> &v) { sort(all(v)); v.erase(unique(all(v)), end(v)); }
//never forget the aiio 2013 RJ incident
template<class T, class U> void safeErase(T &t, const U &u) { auto it = t.find(u); assert(it != end(t)); t.erase(it); }
const auto beg_time = chrono::high_resolution_clock::now();
double time_elapsed() { return chrono::duration<double>(chrono::high_resolution_clock::now() - beg_time).count(); }
int d4i[4] = {0,0,1,-1};
int d4j[4] = {1,-1,0,0};
using vi = vt<int>;
using vvi = vt<vt<int>>;
using vvvi = vt<vvi>;
using vvvvi = vt<vvvi>;
using vvvvvi = vt<vvvvi>;
#define dfw(nx,ny,x,y) for(i32 __g = 0, nx=x+d4i[__g],ny=y+d4j[__g]; __g < 4; ++__g, (__g < 4) ? (nx=x+d4i[__g], ny=y+d4j[__g]) :-1)
#endif
i32 main() {
cin.tie(0)->sync_with_stdio(0);
int r,c,n,sr,sc,gr,gc;
cin>>r>>c>>n>>sr>>sc>>gr>>gc;
sr--,sc--,gr--,gc--;
vvi mat(r, vi(c,1));
f0r(i,0,r) f0r(j,0,c) {
char x;cin>>x;
if(x=='.') {
mat[i][j] = 0;
}
}
deque<pair<int,pii>> pq;
nvt<int,2> mnd (r, vt<int>(c,1e9));
pq.push_front({0,{sr,sc}});
mnd[sr][sc] = 0;
while(sz(pq)) {
auto fr = pq.front();
auto [x,y] = fr.Y;
pq.pop_front();
mnd[x][y] = fr.X;
dfw(nx,ny,x,y) {
if(nx>=0 and nx<r and ny >=0 and ny<c) {
if(mnd[nx][ny] == 1e9) {
if(!mat[nx][ny]) {
mnd[nx][ny] = mnd[x][y];
pq.push_front({mnd[x][y],{nx,ny}});
} else {
pq.push_back({mnd[x][y]+1,{nx,ny}});
}
}
}
}
}
cout << mnd[gr][gc] << '\n';
}
# | 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... |