problem_id stringlengths 6 6 | language stringclasses 2 values | original_status stringclasses 3 values | original_src stringlengths 19 243k | changed_src stringlengths 19 243k | change stringclasses 3 values | i1 int64 0 8.44k | i2 int64 0 8.44k | j1 int64 0 8.44k | j2 int64 0 8.44k | error stringclasses 270 values | stderr stringlengths 0 226k |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define rev2(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define yn \
{ puts("Yes"); } \
else { \
puts("No"); \
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pll = pair<long long, long long>;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
long long TEN(int i) { return !i ? 1 : TEN(i - 1) * 10LL; }
template <class T, class U> bool chmin(T &a, const U &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T, class U> bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} iosetup;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
int H, W, Ch, Cw, Dh, Dw;
V<string> S(1005);
const int INF = TEN(9);
#define int long long
signed main() {
cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
rep(i, H) cin >> S[i];
Ch--, Cw--, Dw--, Dh--;
VV<int> val(1005, V<int>(1005, INF));
val[Ch][Cw] = 0;
deque<P> Q;
VV<bool> visited(1005, V<bool>(1005, false));
Q.emplace_front(-1, -1);
Q.emplace_front(Ch, Cw);
int d = 0;
while (Q.size() > 1) {
auto [y, x] = Q.front();
Q.pop_front();
if (y == -1) {
d++;
Q.emplace_back(-1, -1);
tie(y, x) = Q.front();
} else {
visited[y][x] = true;
}
rep2(i, -2, 3) rep2(j, -2, 3) {
int ny = y + i;
int nx = x + j;
if ((!i && !j) || ny < 0 || ny >= H || nx < 0 || nx >= W ||
S[ny][nx] == '#' || visited[ny][nx]) {
continue;
}
if (abs(i) + abs(j) <= 1) {
chmin(val[ny][nx], d);
Q.emplace_front(ny, nx);
} else {
chmin(val[ny][nx], d + 1);
Q.emplace_back(ny, nx);
}
}
}
if (val[Dh][Dw] == INF)
cout << -1 << endl;
else
cout << val[Dh][Dw] << endl;
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define rev2(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define yn \
{ puts("Yes"); } \
else { \
puts("No"); \
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pll = pair<long long, long long>;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
long long TEN(int i) { return !i ? 1 : TEN(i - 1) * 10LL; }
template <class T, class U> bool chmin(T &a, const U &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T, class U> bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} iosetup;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
int H, W, Ch, Cw, Dh, Dw;
V<string> S(1005);
const int INF = TEN(9);
#define int long long
signed main() {
cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
rep(i, H) cin >> S[i];
Ch--, Cw--, Dw--, Dh--;
VV<int> val(1005, V<int>(1005, INF));
val[Ch][Cw] = 0;
deque<P> Q;
VV<bool> visited(1005, V<bool>(1005, false));
Q.emplace_front(-1, -1);
Q.emplace_front(Ch, Cw);
int d = 0;
while (Q.size() > 1) {
auto [y, x] = Q.front();
Q.pop_front();
if (y == -1) {
d++;
Q.emplace_back(-1, -1);
tie(y, x) = Q.front();
Q.pop_front();
} else {
visited[y][x] = true;
}
rep2(i, -2, 3) rep2(j, -2, 3) {
int ny = y + i;
int nx = x + j;
if ((!i && !j) || ny < 0 || ny >= H || nx < 0 || nx >= W ||
S[ny][nx] == '#' || visited[ny][nx]) {
continue;
}
if (abs(i) + abs(j) <= 1) {
chmin(val[ny][nx], d);
Q.emplace_front(ny, nx);
} else {
chmin(val[ny][nx], d + 1);
Q.emplace_back(ny, nx);
}
}
}
if (val[Dh][Dw] == INF)
cout << -1 << endl;
else
cout << val[Dh][Dw] << endl;
return 0;
}
| insert | 77 | 77 | 77 | 78 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// DEBUG BEGIN
#ifdef LOCAL
template <typename L, typename R>
ostream &operator<<(ostream &out, const pair<L, R> &p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template <typename Tuple, size_t N> struct TuplePrinter {
static ostream &print(ostream &out, const Tuple &t) {
return TuplePrinter<Tuple, N - 1>::print(out, t) << ", " << get<N - 1>(t);
}
};
template <typename Tuple> struct TuplePrinter<Tuple, 1> {
static ostream &print(ostream &out, const Tuple &t) {
return out << get<0>(t);
}
};
template <typename... Args>
ostream &print_tuple(ostream &out, const tuple<Args...> &t) {
return TuplePrinter<decltype(t), sizeof...(Args)>::print(out << "(", t)
<< ")";
}
template <typename... Args>
ostream &operator<<(ostream &out, const tuple<Args...> &t) {
return print_tuple(out, t);
}
template <typename T>
ostream &operator<<(enable_if_t<!is_same<T, string>::value, ostream> &out,
const T &arr) {
out << "{";
for (auto &x : arr)
out << x << ", ";
return out << (arr.size() ? "\b\b" : "") << "}";
}
template <size_t S> ostream &operator<<(ostream &out, const bitset<S> &b) {
for (int i = 0; i < S; ++i)
out << b[i];
return out;
}
void debug_out() { cerr << "\b\b " << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << H << ", ", debug_out(T...);
}
void debug2_out() { cerr << "-----DEBUG END-----\n"; }
template <typename Head, typename... Tail> void debug2_out(Head H, Tail... T) {
cerr << "\n";
for (auto x : H)
cerr << x << "\n";
debug2_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#define debug2(...) \
cerr << "----DEBUG BEGIN----\n[" << #__VA_ARGS__ << "]:", \
debug2_out(__VA_ARGS__)
#else
#define debug(...) 42
#define debug2(...) 42
#endif
// DEBUG END
int main() {
cin.tie(0)->sync_with_stdio(0);
int h, w, ch, cw, dh, dw;
cin >> h >> w >> ch >> cw >> dh >> dw, --ch, --cw, --dh, --dw;
vector<string> a(h);
for (auto &s : a) {
cin >> s;
}
static const int inf = numeric_limits<int>::max();
vector<vector<int>> dist(h, vector<int>(w, inf));
dist[ch][cw] = 0;
deque<array<int, 3>> q{{{ch, cw, 0}}};
static const vector<pair<int, int>> dr{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
auto inrange = [&](int x, int y) {
return 0 <= x && x < h && 0 <= y && y < w;
};
while (!q.empty()) {
auto [x, y, d] = q.front();
q.pop_front();
debug(x, y, d);
if (d == dist[x][y]) {
for (auto [dx, dy] : dr) {
int nx = x + dx, ny = y + dy;
if (inrange(nx, ny) && a[nx][ny] == '.' && dist[nx][ny] > d) {
q.push_front({nx, ny, dist[nx][ny] = d});
}
}
for (auto dx = -2; dx <= 2; ++dx) {
for (auto dy = -2; dy <= 2; ++dy) {
int nx = x + dx, ny = y + dy;
if (inrange(nx, ny) && a[nx][ny] == '.' && dist[nx][ny] > d + 1) {
q.push_front({nx, ny, dist[nx][ny] = d + 1});
}
}
}
}
}
debug(dist);
dist[dh][dw] < inf ? cout << dist[dh][dw] << "\n" : cout << "-1\n";
return 0;
}
/*
*/
////////////////////////////////////////////////////////////////////////////////////////
// //
// Coded by Aeren //
// //
//////////////////////////////////////////////////////////////////////////////////////// | #include <bits/stdc++.h>
using namespace std;
// DEBUG BEGIN
#ifdef LOCAL
template <typename L, typename R>
ostream &operator<<(ostream &out, const pair<L, R> &p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template <typename Tuple, size_t N> struct TuplePrinter {
static ostream &print(ostream &out, const Tuple &t) {
return TuplePrinter<Tuple, N - 1>::print(out, t) << ", " << get<N - 1>(t);
}
};
template <typename Tuple> struct TuplePrinter<Tuple, 1> {
static ostream &print(ostream &out, const Tuple &t) {
return out << get<0>(t);
}
};
template <typename... Args>
ostream &print_tuple(ostream &out, const tuple<Args...> &t) {
return TuplePrinter<decltype(t), sizeof...(Args)>::print(out << "(", t)
<< ")";
}
template <typename... Args>
ostream &operator<<(ostream &out, const tuple<Args...> &t) {
return print_tuple(out, t);
}
template <typename T>
ostream &operator<<(enable_if_t<!is_same<T, string>::value, ostream> &out,
const T &arr) {
out << "{";
for (auto &x : arr)
out << x << ", ";
return out << (arr.size() ? "\b\b" : "") << "}";
}
template <size_t S> ostream &operator<<(ostream &out, const bitset<S> &b) {
for (int i = 0; i < S; ++i)
out << b[i];
return out;
}
void debug_out() { cerr << "\b\b " << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << H << ", ", debug_out(T...);
}
void debug2_out() { cerr << "-----DEBUG END-----\n"; }
template <typename Head, typename... Tail> void debug2_out(Head H, Tail... T) {
cerr << "\n";
for (auto x : H)
cerr << x << "\n";
debug2_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#define debug2(...) \
cerr << "----DEBUG BEGIN----\n[" << #__VA_ARGS__ << "]:", \
debug2_out(__VA_ARGS__)
#else
#define debug(...) 42
#define debug2(...) 42
#endif
// DEBUG END
int main() {
cin.tie(0)->sync_with_stdio(0);
int h, w, ch, cw, dh, dw;
cin >> h >> w >> ch >> cw >> dh >> dw, --ch, --cw, --dh, --dw;
vector<string> a(h);
for (auto &s : a) {
cin >> s;
}
static const int inf = numeric_limits<int>::max();
vector<vector<int>> dist(h, vector<int>(w, inf));
dist[ch][cw] = 0;
deque<array<int, 3>> q{{{ch, cw, 0}}};
static const vector<pair<int, int>> dr{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
auto inrange = [&](int x, int y) {
return 0 <= x && x < h && 0 <= y && y < w;
};
while (!q.empty()) {
auto [x, y, d] = q.front();
q.pop_front();
debug(x, y, d);
if (d == dist[x][y]) {
for (auto [dx, dy] : dr) {
int nx = x + dx, ny = y + dy;
if (inrange(nx, ny) && a[nx][ny] == '.' && dist[nx][ny] > d) {
q.push_front({nx, ny, dist[nx][ny] = d});
}
}
for (auto dx = -2; dx <= 2; ++dx) {
for (auto dy = -2; dy <= 2; ++dy) {
int nx = x + dx, ny = y + dy;
if (inrange(nx, ny) && a[nx][ny] == '.' && dist[nx][ny] > d + 1) {
q.push_back({nx, ny, dist[nx][ny] = d + 1});
}
}
}
}
}
debug(dist);
dist[dh][dw] < inf ? cout << dist[dh][dw] << "\n" : cout << "-1\n";
return 0;
}
/*
*/
////////////////////////////////////////////////////////////////////////////////////////
// //
// Coded by Aeren //
// //
////////////////////////////////////////////////////////////////////////////////////////
| replace | 93 | 94 | 93 | 94 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define int ll
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define ALL(s) (s).begin(), (s).end()
#define ALLn(s, n) s, s + n
#define F first
#define S second
#define pb push_back
#define tc(t) \
int t; \
cin >> t; \
while (t--)
#define _ \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define D1(x) \
{ cerr << " [" << #x << ": " << x << "]\n"; }
#define D2(x) \
{ \
cerr << " [" << #x << ": "; \
for (auto it : x) \
cerr << it << " "; \
cerr << "]\n"; \
}
const ll MOD = 1e9 + 7;
const ll MAXN = 1e6 + 7;
const ll INF = LONG_LONG_MAX;
int n, m, a, b, c, d;
char g[1007][1007];
int vis[1007][1007];
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
int dm[5] = {-2, -1, 0, 1, 2};
bool check(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && g[x][y] == '.';
}
void solve() {
cin >> n >> m >> a >> b >> c >> d;
a--;
b--;
c--;
d--;
F0R(i, n) F0R(j, m) cin >> g[i][j], vis[i][j] = 0;
deque<pair<pii, int>> q;
q.push_front({{a, b}, 0});
while (!q.empty()) {
pair<pii, int> p = q.front();
q.pop_front();
int row = p.F.F;
int col = p.F.S;
int dist = p.S;
if (vis[row][col])
continue;
vis[row][col] = 1;
if (row == c && col == d) {
cout << dist;
return;
}
F0R(i, 4) {
int curr = row + dr[i];
int curc = col + dc[i];
if (check(curr, curc) && !vis[curr][curc]) {
q.push_front({{curr, curc}, dist});
// vis[curr][curc] = 1;
}
}
FOR(i, -2, 3) {
FOR(j, -2, 3) {
int curr = row + i;
int curc = col + j;
if (check(curr, curc) && !vis[curr][curc]) {
q.push_back({{curr, curc}, dist + 1});
// vis[curr][curc] = 1;
}
}
}
}
F0R(i, n) {
F0R(j, m) cerr << vis[i][j] << ' ';
cerr << '\n';
}
cout << -1;
}
int32_t main() {
_
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
// tc(t)
solve();
cerr << "[ Time : " << (float)clock() / CLOCKS_PER_SEC << " secs ]" << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define int ll
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define ALL(s) (s).begin(), (s).end()
#define ALLn(s, n) s, s + n
#define F first
#define S second
#define pb push_back
#define tc(t) \
int t; \
cin >> t; \
while (t--)
#define _ \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define D1(x) \
{ cerr << " [" << #x << ": " << x << "]\n"; }
#define D2(x) \
{ \
cerr << " [" << #x << ": "; \
for (auto it : x) \
cerr << it << " "; \
cerr << "]\n"; \
}
const ll MOD = 1e9 + 7;
const ll MAXN = 1e6 + 7;
const ll INF = LONG_LONG_MAX;
int n, m, a, b, c, d;
char g[1007][1007];
int vis[1007][1007];
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
int dm[5] = {-2, -1, 0, 1, 2};
bool check(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && g[x][y] == '.';
}
void solve() {
cin >> n >> m >> a >> b >> c >> d;
a--;
b--;
c--;
d--;
F0R(i, n) F0R(j, m) cin >> g[i][j], vis[i][j] = 0;
deque<pair<pii, int>> q;
q.push_front({{a, b}, 0});
while (!q.empty()) {
pair<pii, int> p = q.front();
q.pop_front();
int row = p.F.F;
int col = p.F.S;
int dist = p.S;
if (vis[row][col])
continue;
vis[row][col] = 1;
if (row == c && col == d) {
cout << dist;
return;
}
F0R(i, 4) {
int curr = row + dr[i];
int curc = col + dc[i];
if (check(curr, curc) && !vis[curr][curc]) {
q.push_front({{curr, curc}, dist});
// vis[curr][curc] = 1;
}
}
FOR(i, -2, 3) {
FOR(j, -2, 3) {
int curr = row + i;
int curc = col + j;
if (check(curr, curc) && !vis[curr][curc]) {
q.push_back({{curr, curc}, dist + 1});
// vis[curr][curc] = 1;
}
}
}
}
// F0R(i, n) {F0R(j, m) cerr << vis[i][j] << ' '; cerr << '\n';}
cout << -1;
}
int32_t main() {
_
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
// tc(t)
solve();
cerr << "[ Time : " << (float)clock() / CLOCKS_PER_SEC << " secs ]" << endl;
}
| replace | 107 | 111 | 107 | 108 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define el '\n'
#define pb push_back
#define sp ' '
#define all(x) (x).begin(), (x).end()
#define mset(x, y) memset(x, y, sizeof(x))
#define ff first
#define ss second
// clang-format off
bool chmin(int& a, int b){ return b < a ? a = b, true : false; }
bool chmax(int& a, int b){ return b > a ? a = b, true : false; }
bool chmin(long long& a, long long b){ return b < a ? a = b, true : false; }
bool chmax(long long& a, long long b){ return b > a ? a = b, true : false; }
#ifndef ONLINE_JUDGE
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < (int)s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)), __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += el; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += el; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += el; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl
#else
#define dbg(...) ;
#define dbgm(...) ;
#endif
// clang-format on
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, pii> ppi;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
const int inf = 1e09 + 5e3;
const ll linf = 2e18 + 5e3;
const int mod = 1e9 + 7;
const int mxn = 1e6 + 30;
int h, w, ch, cw, dh, dw;
vvi maz;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
bool check(int a, int b) {
return a >= 0 && a < h && b >= 0 && b < w && maz[a][b] == 0;
}
ll bfs(pii s, pii e) {
set<vll> q; // val, x,y,
vvll used(1e3 + 5, vll(1e3 + 5, linf));
q.insert({0, s.ff, s.ss});
used[s.ff][s.ss] = 0;
while (!q.empty()) {
auto v = *q.begin();
q.erase(q.begin());
// dbg(v);
if (v[1] == e.ff && v[2] == e.ss)
return v[0];
if (used[v[1]][v[2]] < v[0])
continue;
for (int z = 0; z < 4; z++) {
int l = dx[z];
int k = dy[z];
int nx = v[1] + k;
int ny = v[2] + l;
if (!check(nx, ny) || used[nx][ny] <= v[0])
continue;
used[nx][ny] = v[0];
q.insert({v[0], nx, ny});
}
for (int k : {-2, -1, 0, 1, 2}) {
for (int l : {-2, -1, 0, 1, 2})
if ((l != 0 || k != 0)) {
int nx = v[1] + k;
int ny = v[2] + l;
if (!check(nx, ny) || used[nx][ny] <= v[0] + 1)
continue;
used[nx][ny] = v[0] + 1;
q.insert({v[0] + 1, nx, ny});
}
}
}
return -1;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> h >> w >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
maz.assign(1e3 + 3, vi(1e3 + 3));
string stemp;
for (int i = 0; i < h; i++) {
cin >> stemp;
assert(stemp.size() == h);
for (int j = 0; j < h; j++) {
maz[i][j] = stemp[j] == '#';
}
}
// dbg(maz);
cout << bfs({ch, cw}, {dh, dw});
}
| #include <bits/stdc++.h>
using namespace std;
#define el '\n'
#define pb push_back
#define sp ' '
#define all(x) (x).begin(), (x).end()
#define mset(x, y) memset(x, y, sizeof(x))
#define ff first
#define ss second
// clang-format off
bool chmin(int& a, int b){ return b < a ? a = b, true : false; }
bool chmax(int& a, int b){ return b > a ? a = b, true : false; }
bool chmin(long long& a, long long b){ return b < a ? a = b, true : false; }
bool chmax(long long& a, long long b){ return b > a ? a = b, true : false; }
#ifndef ONLINE_JUDGE
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < (int)s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)), __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += el; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += el; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += el; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl
#else
#define dbg(...) ;
#define dbgm(...) ;
#endif
// clang-format on
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, pii> ppi;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
const int inf = 1e09 + 5e3;
const ll linf = 2e18 + 5e3;
const int mod = 1e9 + 7;
const int mxn = 1e6 + 30;
int h, w, ch, cw, dh, dw;
vvi maz;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
bool check(int a, int b) {
return a >= 0 && a < h && b >= 0 && b < w && maz[a][b] == 0;
}
ll bfs(pii s, pii e) {
set<vll> q; // val, x,y,
vvll used(1e3 + 5, vll(1e3 + 5, linf));
q.insert({0, s.ff, s.ss});
used[s.ff][s.ss] = 0;
while (!q.empty()) {
auto v = *q.begin();
q.erase(q.begin());
// dbg(v);
if (v[1] == e.ff && v[2] == e.ss)
return v[0];
if (used[v[1]][v[2]] < v[0])
continue;
for (int z = 0; z < 4; z++) {
int l = dx[z];
int k = dy[z];
int nx = v[1] + k;
int ny = v[2] + l;
if (!check(nx, ny) || used[nx][ny] <= v[0])
continue;
used[nx][ny] = v[0];
q.insert({v[0], nx, ny});
}
for (int k : {-2, -1, 0, 1, 2}) {
for (int l : {-2, -1, 0, 1, 2})
if ((l != 0 || k != 0)) {
int nx = v[1] + k;
int ny = v[2] + l;
if (!check(nx, ny) || used[nx][ny] <= v[0] + 1)
continue;
used[nx][ny] = v[0] + 1;
q.insert({v[0] + 1, nx, ny});
}
}
}
return -1;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> h >> w >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
maz.assign(1e3 + 3, vi(1e3 + 3));
string stemp;
for (int i = 0; i < h; i++) {
cin >> stemp;
assert(stemp.size() == w);
for (int j = 0; j < w; j++) {
maz[i][j] = stemp[j] == '#';
}
}
// dbg(maz);
cout << bfs({ch, cw}, {dh, dw});
}
| replace | 106 | 108 | 106 | 108 | 0 | |
p02579 | Python | Time Limit Exceeded | from collections import deque
def main():
def f(hi, wi):
return hi * w + wi
h, w = map(int, input().split())
sh, sw = map(int, input().split())
gh, gw = map(int, input().split())
s = [input() for _ in range(h)]
INF = 10**9
sh -= 1
sw -= 1
gh -= 1
gw -= 1
dq = deque([f(sh, sw)])
dist = [[INF] * w for _ in range(h)]
dist[sh][sw] = 0
while dq:
i = dq.popleft()
hi, wi = divmod(i, w)
for dh in range(-2, 3):
for dw in range(-2, 3):
if dh == 0 and dw == 0:
continue
nh = hi + dh
nw = wi + dw
if 0 <= nh <= h - 1 and 0 <= nw <= w - 1 and s[nh][nw] == ".":
if abs(dh) + abs(dw) == 1:
if dist[hi][wi] < dist[nh][nw]:
dist[nh][nw] = dist[hi][wi]
dq.appendleft(f(nh, nw))
else:
if dist[hi][wi] + 1 < dist[nh][nw]:
dist[nh][nw] = dist[hi][wi] + 1
dq.append(f(nh, nw))
ans = dist[gh][gw]
if ans == INF:
ans = -1
print(ans)
if __name__ == "__main__":
main()
| from collections import deque
def main():
def f(hi, wi):
return hi * w + wi
h, w = map(int, input().split())
sh, sw = map(int, input().split())
gh, gw = map(int, input().split())
s = [input() for _ in range(h)]
INF = 10**9
sh -= 1
sw -= 1
gh -= 1
gw -= 1
dq = deque([f(sh, sw)])
dist = [[INF] * w for _ in range(h)]
dist[sh][sw] = 0
while dq:
i = dq.popleft()
hi, wi = divmod(i, w)
for dh in range(-2, 3):
for dw in range(-2, 3):
nh = hi + dh
nw = wi + dw
if 0 <= nh <= h - 1 and 0 <= nw <= w - 1 and s[nh][nw] == ".":
if abs(dh) + abs(dw) == 1:
if dist[hi][wi] < dist[nh][nw]:
dist[nh][nw] = dist[hi][wi]
dq.appendleft(f(nh, nw))
else:
if dist[hi][wi] + 1 < dist[nh][nw]:
dist[nh][nw] = dist[hi][wi] + 1
dq.append(f(nh, nw))
ans = dist[gh][gw]
if ans == INF:
ans = -1
print(ans)
if __name__ == "__main__":
main()
| delete | 29 | 31 | 29 | 29 | TLE | |
p02579 | C++ | Time Limit Exceeded | // Ace
#include <bits/stdc++.h>
using namespace std;
using in = long long;
using str = string;
using pi = pair<in, in>;
using vi = vector<in>;
#define F first
#define S second
#define pb push_back
#define rep(i, k, n) for (in i = k; i < n; ++i)
#define repr(i, k, n) for (in i = k; i >= n; --i)
#define sz(x) (in) x.size()
#define all(x) begin(x), end(x)
const int MOD = 1e9 + 7;
const in N = 1e3 + 1;
in n, w;
in mx, my;
in dx, dy;
str g[N];
in vis[N][N];
in dist[N][N];
map<pi, in> dir;
in valid(in x, in y) { return (x >= 0 && x < n && y >= 0 && y < w); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(vis, 0, sizeof(vis));
rep(i, 0, N) rep(j, 0, N) dist[i][j] = INT_MAX;
rep(i, 0, 5) {
rep(j, 0, 5) {
in dx = abs(i - 2);
in dy = abs(j - 2);
if (dx + dy <= 1) {
if (dx + dy != 0)
dir[{i - 2, j - 2}] = 0;
} else {
dir[{i - 2, j - 2}] = 1;
}
}
}
cin >> n >> w;
cin >> mx >> my;
cin >> dx >> dy;
rep(i, 0, n) cin >> g[i];
priority_queue<pair<in, pi>, vector<pair<in, pi>>, greater<pair<in, pi>>> q;
q.push({0, {mx - 1, my - 1}});
dist[mx - 1][my - 1] = 0;
while (!q.empty()) {
pair<in, pi> node = q.top();
q.pop();
in d = dist[node.S.F][node.S.S];
vis[node.S.F][node.S.S] = 1;
if (node.S.F == dx - 1 && node.S.S == dy - 1) {
cout << d;
return 0;
}
for (auto i : dir) {
in xn = node.S.F + i.F.F;
in yn = node.S.S + i.F.S;
if (valid(xn, yn) && vis[xn][yn] == 0 && g[xn][yn] == '.') {
dist[xn][yn] = min(dist[xn][yn], d + i.S);
q.push({d + i.S, {xn, yn}});
}
}
}
cout << -1;
return 0;
} | // Ace
#include <bits/stdc++.h>
using namespace std;
using in = long long;
using str = string;
using pi = pair<in, in>;
using vi = vector<in>;
#define F first
#define S second
#define pb push_back
#define rep(i, k, n) for (in i = k; i < n; ++i)
#define repr(i, k, n) for (in i = k; i >= n; --i)
#define sz(x) (in) x.size()
#define all(x) begin(x), end(x)
const int MOD = 1e9 + 7;
const in N = 1e3 + 1;
in n, w;
in mx, my;
in dx, dy;
str g[N];
in vis[N][N];
in dist[N][N];
map<pi, in> dir;
in valid(in x, in y) { return (x >= 0 && x < n && y >= 0 && y < w); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(vis, 0, sizeof(vis));
rep(i, 0, N) rep(j, 0, N) dist[i][j] = INT_MAX;
rep(i, 0, 5) {
rep(j, 0, 5) {
in dx = abs(i - 2);
in dy = abs(j - 2);
if (dx + dy <= 1) {
if (dx + dy != 0)
dir[{i - 2, j - 2}] = 0;
} else {
dir[{i - 2, j - 2}] = 1;
}
}
}
cin >> n >> w;
cin >> mx >> my;
cin >> dx >> dy;
rep(i, 0, n) cin >> g[i];
priority_queue<pair<in, pi>, vector<pair<in, pi>>, greater<pair<in, pi>>> q;
q.push({0, {mx - 1, my - 1}});
dist[mx - 1][my - 1] = 0;
while (!q.empty()) {
pair<in, pi> node = q.top();
q.pop();
in d = dist[node.S.F][node.S.S];
vis[node.S.F][node.S.S] = 1;
if (node.S.F == dx - 1 && node.S.S == dy - 1) {
cout << d;
return 0;
}
for (auto i : dir) {
in xn = node.S.F + i.F.F;
in yn = node.S.S + i.F.S;
if (valid(xn, yn) && vis[xn][yn] == 0 && g[xn][yn] == '.') {
if (dist[xn][yn] > d + i.S) {
dist[xn][yn] = d + i.S;
q.push({dist[xn][yn], {xn, yn}});
}
}
}
}
cout << -1;
return 0;
} | replace | 71 | 73 | 71 | 75 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <chrono>
// #pragma GCC optimize("Ofast")
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll N, M, H, W, Q, K, A, B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL << 58);
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
int main() {
cin >> H >> W;
vec s(2), t(2);
cin >> s[0] >> s[1] >> t[0] >> t[1];
rep(i, 2) {
--s[i];
--t[i];
}
vector<string> G(H);
rep(i, H) cin >> G[i];
mat dist(H, vec(W, INF));
dist[s[0]][s[1]] = 0;
deque<P> que;
que.emplace_back(0, s[0] * W + s[1]);
const vector<int> dh = {-1, 0, 1, 0}, dw = {0, -1, 0, 1};
while (!que.empty()) {
P p = que.front();
que.pop_front();
int h = p.se / W, w = p.se % W;
// if(dist[h][w] < p.fi) continue;
rep(i, 4) {
int nh = h + dh[i], nw = w + dw[i];
if (nh >= 0 && nh < H && nw >= 0 && nw < W && G[nh][nw] != '#') {
if (chmin(dist[nh][nw], p.fi)) {
que.emplace_front(p.fi, nh * W + nw);
}
}
}
for (int i = -2; i <= 2; ++i) {
for (int j = -2; j <= 2; ++j) {
int nh = h + i, nw = w + j;
if (nh >= 0 && nh < H && nw >= 0 && nw < W && G[nh][nw] != '#') {
if (chmin(dist[nh][nw], p.fi + 1)) {
que.emplace_front(p.fi + 1, nh * W + nw);
}
}
}
}
}
cout << (dist[t[0]][t[1]] == INF ? -1 : dist[t[0]][t[1]]) << endl;
}
| #include <bits/stdc++.h>
#include <chrono>
// #pragma GCC optimize("Ofast")
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll N, M, H, W, Q, K, A, B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL << 58);
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
int main() {
cin >> H >> W;
vec s(2), t(2);
cin >> s[0] >> s[1] >> t[0] >> t[1];
rep(i, 2) {
--s[i];
--t[i];
}
vector<string> G(H);
rep(i, H) cin >> G[i];
mat dist(H, vec(W, INF));
dist[s[0]][s[1]] = 0;
deque<P> que;
que.emplace_back(0, s[0] * W + s[1]);
const vector<int> dh = {-1, 0, 1, 0}, dw = {0, -1, 0, 1};
while (!que.empty()) {
P p = que.front();
que.pop_front();
int h = p.se / W, w = p.se % W;
// if(dist[h][w] < p.fi) continue;
rep(i, 4) {
int nh = h + dh[i], nw = w + dw[i];
if (nh >= 0 && nh < H && nw >= 0 && nw < W && G[nh][nw] != '#') {
if (chmin(dist[nh][nw], p.fi)) {
que.emplace_front(p.fi, nh * W + nw);
}
}
}
for (int i = -2; i <= 2; ++i) {
for (int j = -2; j <= 2; ++j) {
int nh = h + i, nw = w + j;
if (nh >= 0 && nh < H && nw >= 0 && nw < W && G[nh][nw] != '#') {
if (chmin(dist[nh][nw], p.fi + 1)) {
que.emplace_back(p.fi + 1, nh * W + nw);
}
}
}
}
}
cout << (dist[t[0]][t[1]] == INF ? -1 : dist[t[0]][t[1]]) << endl;
}
| replace | 68 | 69 | 68 | 69 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define MOD 1000000007
using namespace std;
using ll = long long;
int mp[1005][1005] = {};
int main() {
int h, w;
cin >> h >> w;
int sx, sy, gx, gy;
cin >> sy >> sx >> gy >> gx;
rep(i, 1002) rep(j, 1002) {
if (i % (h + 1) == 0 || j % (w + 1) == 0)
mp[i][j] = -1;
else
mp[i][j] = 1e9;
}
rep(i, h) {
string s;
cin >> s;
rep(j, w) if (s[j] != '.') mp[i + 1][j + 1] = -1;
}
deque<pair<int, pair<int, int>>> q;
mp[sy][sx] = 0;
q.emplace_back(0, make_pair(sy, sx));
while (q.size()) {
deque<pair<int, pair<int, int>>> cp;
swap(q, cp);
while (cp.size()) {
int cost = cp.front().first;
int y = cp.front().second.first;
int x = cp.front().second.second;
cp.pop_front();
if (mp[y][x] < cost)
continue;
for (int i = y - 2; i <= y + 2; i++) {
for (int j = x - 2; j <= x + 2; j++) {
if (i < 1 || h < i || j < 1 || w < j)
continue;
if (i == y && j == x)
continue;
if (abs(i - y) + abs(j - x) == 1 && mp[i][j] > cost) {
mp[i][j] = cost;
q.emplace_front(cost, make_pair(i, j));
} else if (mp[i][j] > cost + 1) {
mp[i][j] = cost + 1;
q.emplace_back(cost + 1, make_pair(i, j));
}
}
}
}
}
if (mp[gy][gx] == 1e9)
mp[gy][gx] = -1;
cout << mp[gy][gx] << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define MOD 1000000007
using namespace std;
using ll = long long;
int mp[1005][1005] = {};
int main() {
int h, w;
cin >> h >> w;
int sx, sy, gx, gy;
cin >> sy >> sx >> gy >> gx;
rep(i, 1002) rep(j, 1002) {
if (i % (h + 1) == 0 || j % (w + 1) == 0)
mp[i][j] = -1;
else
mp[i][j] = 1e9;
}
rep(i, h) {
string s;
cin >> s;
rep(j, w) if (s[j] != '.') mp[i + 1][j + 1] = -1;
}
deque<pair<int, pair<int, int>>> q;
mp[sy][sx] = 0;
q.emplace_back(0, make_pair(sy, sx));
while (q.size()) {
int cost = q.front().first;
int y = q.front().second.first;
int x = q.front().second.second;
q.pop_front();
if (mp[y][x] < cost)
continue;
for (int i = y - 2; i <= y + 2; i++) {
for (int j = x - 2; j <= x + 2; j++) {
if (i < 1 || h < i || j < 1 || w < j)
continue;
if (i == y && j == x)
continue;
if (abs(i - y) + abs(j - x) == 1 && mp[i][j] > cost) {
mp[i][j] = cost;
q.emplace_front(cost, make_pair(i, j));
} else if (mp[i][j] > cost + 1) {
mp[i][j] = cost + 1;
q.emplace_back(cost + 1, make_pair(i, j));
}
}
}
}
if (mp[gy][gx] == 1e9)
mp[gy][gx] = -1;
cout << mp[gy][gx] << endl;
return 0;
}
| replace | 29 | 51 | 29 | 47 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
int main() {
int h, w;
cin >> h >> w;
int si, sj;
cin >> si >> sj;
int ti, tj;
cin >> ti >> tj;
--si;
--sj;
--ti;
--tj;
vector<string> s(h);
rep(i, h) cin >> s[i];
const int INF = 1e9;
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<P> q;
dist[si][sj] = 0;
q.emplace_back(si, sj);
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
int d = dist[i][j];
q.pop_front();
rep(v, 4) {
int ni = i + di[v], nj = j + dj[v];
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d)
continue;
dist[ni][nj] = d;
q.emplace_front(ni, nj);
}
for (int ei = -2; ei <= 2; ei++) {
for (int ej = -2; ej <= 2; ej++) {
int ni = i + ei, nj = j + ej;
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d + 1)
continue;
dist[ni][nj] = d + 1;
q.emplace_front(ni, nj);
}
}
}
int ans = dist[ti][tj];
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
int main() {
int h, w;
cin >> h >> w;
int si, sj;
cin >> si >> sj;
int ti, tj;
cin >> ti >> tj;
--si;
--sj;
--ti;
--tj;
vector<string> s(h);
rep(i, h) cin >> s[i];
const int INF = 1e9;
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<P> q;
dist[si][sj] = 0;
q.emplace_back(si, sj);
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
int d = dist[i][j];
q.pop_front();
rep(v, 4) {
int ni = i + di[v], nj = j + dj[v];
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d)
continue;
dist[ni][nj] = d;
q.emplace_front(ni, nj);
}
for (int ei = -2; ei <= 2; ei++) {
for (int ej = -2; ej <= 2; ej++) {
int ni = i + ei, nj = j + ej;
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d + 1)
continue;
dist[ni][nj] = d + 1;
q.emplace_back(ni, nj);
}
}
}
int ans = dist[ti][tj];
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
} | replace | 53 | 54 | 53 | 54 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
#define rrep(i, n, m) for (int i = (n)-1; i >= (m); i--)
#define pvec(vec) \
{ \
for (auto v : vec) \
cout << v << ' '; \
cout << endl; \
}
#define pivec(vec) \
{ \
rep(i, 0, vec.size()) cout << i << ':' << vec[i] << ' '; \
cout << endl; \
}
using namespace std;
using ll = long long;
int H, W;
struct UnionFind {
int n;
vector<int> parent;
vector<int> rank;
UnionFind(int _n) : n(_n) {
parent.assign(n, -1);
rank.assign(n, 1);
}
int find(int i) {
if (parent[i] == -1)
return i;
else
return parent[i] = find(parent[i]);
}
void unite(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return;
if (rank[i] == rank[j]) {
parent[j] = i;
++rank[i];
} else {
if (rank[i] < rank[j])
swap(i, j);
parent[j] = i;
}
}
bool is_same(int i, int j) { return find(i) == find(j); }
};
void connect(int th, int tw, int sh, int sw, UnionFind &uf,
vector<string> grid) {
if (sh < 0 or sh >= H)
return;
if (sw < 0 or sw >= W)
return;
if (grid[th][tw] == '#')
return;
if (grid[sh][sw] == '#')
return;
uf.unite(W * th + tw, W * sh + sw);
}
int main() {
cin >> H >> W;
int Ch, Cw, Dh, Dw;
cin >> Ch >> Cw;
cin >> Dh >> Dw;
--Ch, --Cw;
--Dh, --Dw;
vector<string> grid(H);
rep(i, 0, H) cin >> grid[i];
UnionFind uf(H * W);
rep(h, 0, H) rep(w, 0, W) {
connect(h, w, h - 1, w, uf, grid);
connect(h, w, h, w - 1, uf, grid);
connect(h, w, h + 1, w, uf, grid);
connect(h, w, h, w + 1, uf, grid);
}
// make graph
vector<set<int>> edge(H * W, set<int>());
rep(h, 0, H) rep(w, 0, W) {
if (grid[h][w] == '#')
continue;
int s = uf.find(h * W + w);
// printf("h: %d, w: %d, s: %d\n", h, w, s);
rep(dh, -2, 3) rep(dw, -2, 3) {
int th = h + dh;
int tw = w + dw;
if (th < 0 or th >= H)
continue;
if (tw < 0 or tw >= W)
continue;
if (grid[th][tw] == '#')
continue;
int t = uf.find(th * W + tw);
edge[s].insert(t);
edge[t].insert(s);
}
}
// dfs
int from = uf.find(Ch * W + Cw);
int to = uf.find(Dh * W + Dw);
queue<pair<int, int>> que;
que.emplace(from, 0);
set<int> visited;
while (que.size() > 0) {
int now, depth;
tie(now, depth) = que.front();
que.pop();
if (visited.count(now) > 0)
continue;
visited.insert(now);
if (now == to) {
cout << depth << endl;
return 0;
}
for (auto nxt : edge[now])
que.emplace(nxt, depth + 1);
}
cout << -1 << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
#define rrep(i, n, m) for (int i = (n)-1; i >= (m); i--)
#define pvec(vec) \
{ \
for (auto v : vec) \
cout << v << ' '; \
cout << endl; \
}
#define pivec(vec) \
{ \
rep(i, 0, vec.size()) cout << i << ':' << vec[i] << ' '; \
cout << endl; \
}
using namespace std;
using ll = long long;
int H, W;
struct UnionFind {
int n;
vector<int> parent;
vector<int> rank;
UnionFind(int _n) : n(_n) {
parent.assign(n, -1);
rank.assign(n, 1);
}
int find(int i) {
if (parent[i] == -1)
return i;
else
return parent[i] = find(parent[i]);
}
void unite(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return;
if (rank[i] == rank[j]) {
parent[j] = i;
++rank[i];
} else {
if (rank[i] < rank[j])
swap(i, j);
parent[j] = i;
}
}
bool is_same(int i, int j) { return find(i) == find(j); }
};
void connect(int th, int tw, int sh, int sw, UnionFind &uf,
const vector<string> &grid) {
if (sh < 0 or sh >= H)
return;
if (sw < 0 or sw >= W)
return;
if (grid[th][tw] == '#')
return;
if (grid[sh][sw] == '#')
return;
uf.unite(W * th + tw, W * sh + sw);
}
int main() {
cin >> H >> W;
int Ch, Cw, Dh, Dw;
cin >> Ch >> Cw;
cin >> Dh >> Dw;
--Ch, --Cw;
--Dh, --Dw;
vector<string> grid(H);
rep(i, 0, H) cin >> grid[i];
UnionFind uf(H * W);
rep(h, 0, H) rep(w, 0, W) {
connect(h, w, h - 1, w, uf, grid);
connect(h, w, h, w - 1, uf, grid);
connect(h, w, h + 1, w, uf, grid);
connect(h, w, h, w + 1, uf, grid);
}
// make graph
vector<set<int>> edge(H * W, set<int>());
rep(h, 0, H) rep(w, 0, W) {
if (grid[h][w] == '#')
continue;
int s = uf.find(h * W + w);
// printf("h: %d, w: %d, s: %d\n", h, w, s);
rep(dh, -2, 3) rep(dw, -2, 3) {
int th = h + dh;
int tw = w + dw;
if (th < 0 or th >= H)
continue;
if (tw < 0 or tw >= W)
continue;
if (grid[th][tw] == '#')
continue;
int t = uf.find(th * W + tw);
edge[s].insert(t);
edge[t].insert(s);
}
}
// dfs
int from = uf.find(Ch * W + Cw);
int to = uf.find(Dh * W + Dw);
queue<pair<int, int>> que;
que.emplace(from, 0);
set<int> visited;
while (que.size() > 0) {
int now, depth;
tie(now, depth) = que.front();
que.pop();
if (visited.count(now) > 0)
continue;
visited.insert(now);
if (now == to) {
cout << depth << endl;
return 0;
}
for (auto nxt : edge[now])
que.emplace(nxt, depth + 1);
}
cout << -1 << endl;
return 0;
}
| replace | 57 | 58 | 57 | 58 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"
void myadd(vector<vector<int>> &vec, queue<pair<int, int>> &q, int x, int y) {
if (vec[x][y] != -2) {
if (vec[x][y] < vec[x + 1][y] || vec[x + 1][y] == -1) {
vec[x + 1][y] = vec[x][y];
q.push(make_pair(x + 1, y));
}
if (vec[x][y] < vec[x - 1][y] || vec[x - 1][y] == -1) {
vec[x - 1][y] = vec[x][y];
q.push(make_pair(x - 1, y));
}
if (vec[x][y] < vec[x][y + 1] || vec[x][y + 1] == -1) {
vec[x][y + 1] = vec[x][y];
q.push(make_pair(x, y + 1));
}
if (vec[x][y] < vec[x][y - 1] || vec[x][y - 1] == -1) {
vec[x][y - 1] = vec[x][y];
q.push(make_pair(x, y - 1));
}
for (int i = -2; i < 3; ++i) {
for (int j = -2; j < 3; ++j) {
if (vec[x][y] < vec[x + i][y + j] || vec[x + i][y + j] == -1) {
vec[x + i][y + j] = vec[x][y] + 1;
q.push(make_pair(x + i, y + j));
}
}
}
}
}
int main() {
int H, W;
int Ch, Cw;
int Dh, Dw;
cin >> H >> W;
cin >> Ch >> Cw;
cin >> Dh >> Dw;
Ch += 3;
Cw += 3;
Dh += 3;
Dw += 3;
int offset = 4;
vector<vector<int>> vec(H + 10, vector<int>(W + 10, -2));
map<char, int> mp;
mp['.'] = -1;
mp['#'] = -2;
for (int i = offset; i < H + offset; ++i) {
for (int j = offset; j < W + offset; ++j) {
char c;
cin >> c;
vec[i][j] = mp[c];
}
}
queue<pair<int, int>> que;
vec[Ch][Cw] = 0;
myadd(vec, que, Ch, Cw);
while (que.empty() != 1) {
auto a = que.front();
que.pop();
myadd(vec, que, a.first, a.second);
}
/*
for (int i = offset; i < H + offset; ++i) {
for (int j = offset; j < W + offset; ++j) {
cout << vec[i][j] << "\t";
}
cout << endl;
}
*/
cout << vec[Dh][Dw] << endl;
;
return 0;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"
void myadd(vector<vector<int>> &vec, queue<pair<int, int>> &q, int x, int y) {
if (vec[x][y] != -2) {
if (vec[x][y] < vec[x + 1][y] || vec[x + 1][y] == -1) {
vec[x + 1][y] = vec[x][y];
q.push(make_pair(x + 1, y));
}
if (vec[x][y] < vec[x - 1][y] || vec[x - 1][y] == -1) {
vec[x - 1][y] = vec[x][y];
q.push(make_pair(x - 1, y));
}
if (vec[x][y] < vec[x][y + 1] || vec[x][y + 1] == -1) {
vec[x][y + 1] = vec[x][y];
q.push(make_pair(x, y + 1));
}
if (vec[x][y] < vec[x][y - 1] || vec[x][y - 1] == -1) {
vec[x][y - 1] = vec[x][y];
q.push(make_pair(x, y - 1));
}
for (int i = -2; i < 3; ++i) {
for (int j = -2; j < 3; ++j) {
if (vec[x][y] + 1 < vec[x + i][y + j] || vec[x + i][y + j] == -1) {
vec[x + i][y + j] = vec[x][y] + 1;
q.push(make_pair(x + i, y + j));
}
}
}
}
}
int main() {
int H, W;
int Ch, Cw;
int Dh, Dw;
cin >> H >> W;
cin >> Ch >> Cw;
cin >> Dh >> Dw;
Ch += 3;
Cw += 3;
Dh += 3;
Dw += 3;
int offset = 4;
vector<vector<int>> vec(H + 10, vector<int>(W + 10, -2));
map<char, int> mp;
mp['.'] = -1;
mp['#'] = -2;
for (int i = offset; i < H + offset; ++i) {
for (int j = offset; j < W + offset; ++j) {
char c;
cin >> c;
vec[i][j] = mp[c];
}
}
queue<pair<int, int>> que;
vec[Ch][Cw] = 0;
myadd(vec, que, Ch, Cw);
while (que.empty() != 1) {
auto a = que.front();
que.pop();
myadd(vec, que, a.first, a.second);
}
/*
for (int i = offset; i < H + offset; ++i) {
for (int j = offset; j < W + offset; ++j) {
cout << vec[i][j] << "\t";
}
cout << endl;
}
*/
cout << vec[Dh][Dw] << endl;
;
return 0;
} | replace | 40 | 41 | 40 | 41 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define me0(a) memset(a, 0, sizeof(a))
#define me1(a) memset(a, -1, sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("C:\\acm\\Cproj\\in.txt", "r", stdin);
#define pr freopen("C:\\acm\\Cproj\\out.txt", "w", stdout)
#define pr2 freopen("C:\\acm\\Cproj\\std.txt", "w", stdout)
#define pii pair<int, int>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
template <class T> void read(T &val) {
T x = 0;
T bz = 1;
char c;
for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
;
if (c == '-') {
bz = -1;
c = getchar();
}
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - 48;
val = x * bz;
}
const int maxn = 1e6 + 11;
const int mod = 1e9 + 7;
int n, m, x, y, t;
char g[1100][1100];
int to(int x, int y) { return (x - 1) * m + y; }
bool check(int x, int y) {
if (x < 1 || x > n || y < 1 || y > m || g[x][y] == '#')
return false;
return true;
}
bool vis[maxn];
LL dis[maxn];
int tot, head[maxn];
struct Edge {
int to, nxt, w;
} e[maxn << 1];
void add(int u, int v, int w) {
// cout<<"add "<<u<<" "<<v<<" "<<w<<endl;
e[++tot] = {v, head[u], w};
head[u] = tot;
}
struct HeapNode {
LL dis, u;
bool operator<(const HeapNode b) const { return dis > b.dis; }
};
void dijkstra(int s) {
me0(vis);
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
priority_queue<HeapNode> q;
q.push({0, s});
while (!q.empty()) {
HeapNode tp = q.top();
q.pop();
int u = tp.u;
if (vis[u])
continue;
vis[u] = 1;
for (int i = head[u]; i; i = e[i].nxt) {
int to = e[i].to;
if (dis[to] > dis[u] + e[i].w) {
dis[to] = dis[u] + e[i].w;
q.push({dis[to], to});
}
}
}
}
int main() {
// op;
read(n);
read(m);
int cx, cy, dx, dy;
read(cx);
read(cy);
read(dx);
read(dy);
fo(i, 1, n) scanf("%s", g[i] + 1);
fo(i, 1, n) {
fo(j, 1, m) {
int nx = i + 1, ny = j;
if (check(nx, ny))
add(to(i, j), to(nx, ny), 0);
nx = i - 1, ny = j;
if (check(nx, ny))
add(to(i, j), to(nx, ny), 0);
nx = i, ny = j + 1;
if (check(nx, ny))
add(to(i, j), to(nx, ny), 0);
nx = i, ny = j - 1;
if (check(nx, ny))
add(to(i, j), to(nx, ny), 0);
fo(a, -2, 2) {
fo(b, -2, 2) {
if (a == 0 && b == 0)
continue;
nx = i + a, ny = j + b;
if (check(nx, ny))
add(to(i, j), to(nx, ny), 1);
}
}
}
}
dijkstra(to(cx, cy));
if (dis[to(dx, dy)] >= INF)
puts("-1");
else
printf("%d\n", dis[to(dx, dy)]);
return 0;
} | #include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define me0(a) memset(a, 0, sizeof(a))
#define me1(a) memset(a, -1, sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("C:\\acm\\Cproj\\in.txt", "r", stdin);
#define pr freopen("C:\\acm\\Cproj\\out.txt", "w", stdout)
#define pr2 freopen("C:\\acm\\Cproj\\std.txt", "w", stdout)
#define pii pair<int, int>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
template <class T> void read(T &val) {
T x = 0;
T bz = 1;
char c;
for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
;
if (c == '-') {
bz = -1;
c = getchar();
}
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - 48;
val = x * bz;
}
const int maxn = 1e6 + 11;
const int mod = 1e9 + 7;
int n, m, x, y, t;
char g[1100][1100];
int to(int x, int y) { return (x - 1) * m + y; }
bool check(int x, int y) {
if (x < 1 || x > n || y < 1 || y > m || g[x][y] == '#')
return false;
return true;
}
bool vis[maxn];
LL dis[maxn];
int tot, head[maxn];
struct Edge {
int to, nxt, w;
} e[maxn * 100];
void add(int u, int v, int w) {
// cout<<"add "<<u<<" "<<v<<" "<<w<<endl;
e[++tot] = {v, head[u], w};
head[u] = tot;
}
struct HeapNode {
LL dis, u;
bool operator<(const HeapNode b) const { return dis > b.dis; }
};
void dijkstra(int s) {
me0(vis);
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
priority_queue<HeapNode> q;
q.push({0, s});
while (!q.empty()) {
HeapNode tp = q.top();
q.pop();
int u = tp.u;
if (vis[u])
continue;
vis[u] = 1;
for (int i = head[u]; i; i = e[i].nxt) {
int to = e[i].to;
if (dis[to] > dis[u] + e[i].w) {
dis[to] = dis[u] + e[i].w;
q.push({dis[to], to});
}
}
}
}
int main() {
// op;
read(n);
read(m);
int cx, cy, dx, dy;
read(cx);
read(cy);
read(dx);
read(dy);
fo(i, 1, n) scanf("%s", g[i] + 1);
fo(i, 1, n) {
fo(j, 1, m) {
int nx = i + 1, ny = j;
if (check(nx, ny))
add(to(i, j), to(nx, ny), 0);
nx = i - 1, ny = j;
if (check(nx, ny))
add(to(i, j), to(nx, ny), 0);
nx = i, ny = j + 1;
if (check(nx, ny))
add(to(i, j), to(nx, ny), 0);
nx = i, ny = j - 1;
if (check(nx, ny))
add(to(i, j), to(nx, ny), 0);
fo(a, -2, 2) {
fo(b, -2, 2) {
if (a == 0 && b == 0)
continue;
nx = i + a, ny = j + b;
if (check(nx, ny))
add(to(i, j), to(nx, ny), 1);
}
}
}
}
dijkstra(to(cx, cy));
if (dis[to(dx, dy)] >= INF)
puts("-1");
else
printf("%d\n", dis[to(dx, dy)]);
return 0;
} | replace | 43 | 44 | 43 | 44 | 0 | |
p02579 | C++ | Time Limit Exceeded | //
// main.cpp
// ervb
//
// Created by Kanak Gautam on 21/04/20.
// Copyright © 2020 Kanak Gautam. All rights reserved.
//
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define pb push_back
#define mk make_pair
#define endl "\n"
#define mod 1000000007
using namespace std;
typedef long long int lli;
typedef long double ld;
priority_queue<lli, vector<lli>, greater<lli>> ti;
vector<lli> p[200005], h, b(200005, 0), f(200005, 0);
lli vis[200005];
map<pair<lli, lli>, lli> mp;
set<pair<lli, lli>> s;
set<lli> st;
map<lli, lli> np, v;
queue<lli> qy;
lli gcd(lli a, lli b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
lli bpow(lli a, lli b) {
lli res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res % mod;
}
lli fact(lli i) {
f[0] = 1;
for (lli k = 1; k <= i; k++) {
(f[k] = f[k - 1] * k) %= mod;
}
return f[i];
}
lli isprime(lli n) {
if (n == 1)
return 0;
for (lli i = 2; i <= sqrt(n); i++)
if (n % i == 0)
return 0;
return 1;
}
bool cmp(lli i, lli j) { return p[i].size() < p[j].size(); }
void sieve() {
for (lli i = 2; i <= 5000000; i++) {
if (b[i] == 0) {
for (lli j = 2; i * j <= 5000000; j++) {
b[i * j] = 1;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
lli h, w;
cin >> h >> w;
lli cx, cy, dx, dy;
cin >> cx >> cy >> dx >> dy;
lli a1[] = {0, 0, -1, 1};
lli a2[] = {1, -1, 0, 0};
char s[h + 1][w + 1];
map<pair<lli, lli>, lli> mp;
queue<pair<lli, lli>> q1, q2;
vector<vector<lli>> dp(h + 1, vector<lli>(w + 1, 1e8));
dp[cx][cy] = 0;
for (lli i = 1; i <= h; i++) {
for (lli j = 1; j <= w; j++) {
cin >> s[i][j];
}
}
q1.push(make_pair(cx, cy));
while (!q1.empty()) {
lli x = q1.front().first;
lli y = q1.front().second;
q1.pop();
if (mp[{x, y}] == 0) {
// cout<<x<<" "<<y<<endl;
q2.push(make_pair(x, y));
while (!q2.empty()) {
x = q2.front().first;
y = q2.front().second;
// cout<<x<<" "<<y<<endl;
mp[{x, y}] = 1;
lli x1, y1;
for (lli i = 0; i < 4; i++) {
x1 = x + a1[i];
y1 = y + a2[i];
if (x1 >= 1 && x1 <= h && y1 >= 1 && y1 <= w && s[x1][y1] == '.' &&
!mp[{x1, y1}]) {
// cout<<1<<" "<<x1<<" "<<y1<<endl;
dp[x1][y1] = min(dp[x][y], dp[x1][y1]);
q2.push(make_pair(x1, y1));
}
}
q2.pop();
for (lli i = max(x - 2, 1ll); i <= min(h, x + 2); i++) {
for (lli j = max(y - 2, 1ll); j <= min(w, y + 2); j++) {
if (s[i][j] == '.' && !mp[{i, j}]) {
// cout<<0<<" "<<i<<" "<<j<<endl;
dp[i][j] = min(dp[x][y] + 1, dp[i][j]);
q1.push(make_pair(i, j));
}
}
}
}
}
}
/* for(lli i=1;i<=h;i++)
{
for(lli j=1;j<=w;j++)
{
cout<<dp[i][j]<<" ";
}
cout<<endl;
}*/
if (dp[dx][dy] == 1e8)
dp[dx][dy] = -1;
cout << dp[dx][dy] << endl;
}
| //
// main.cpp
// ervb
//
// Created by Kanak Gautam on 21/04/20.
// Copyright © 2020 Kanak Gautam. All rights reserved.
//
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define pb push_back
#define mk make_pair
#define endl "\n"
#define mod 1000000007
using namespace std;
typedef long long int lli;
typedef long double ld;
priority_queue<lli, vector<lli>, greater<lli>> ti;
vector<lli> p[200005], h, b(200005, 0), f(200005, 0);
lli vis[200005];
map<pair<lli, lli>, lli> mp;
set<pair<lli, lli>> s;
set<lli> st;
map<lli, lli> np, v;
queue<lli> qy;
lli gcd(lli a, lli b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
lli bpow(lli a, lli b) {
lli res = 1;
while (b > 0) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res % mod;
}
lli fact(lli i) {
f[0] = 1;
for (lli k = 1; k <= i; k++) {
(f[k] = f[k - 1] * k) %= mod;
}
return f[i];
}
lli isprime(lli n) {
if (n == 1)
return 0;
for (lli i = 2; i <= sqrt(n); i++)
if (n % i == 0)
return 0;
return 1;
}
bool cmp(lli i, lli j) { return p[i].size() < p[j].size(); }
void sieve() {
for (lli i = 2; i <= 5000000; i++) {
if (b[i] == 0) {
for (lli j = 2; i * j <= 5000000; j++) {
b[i * j] = 1;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
lli h, w;
cin >> h >> w;
lli cx, cy, dx, dy;
cin >> cx >> cy >> dx >> dy;
lli a1[] = {0, 0, -1, 1};
lli a2[] = {1, -1, 0, 0};
char s[h + 1][w + 1];
map<pair<lli, lli>, lli> mp;
queue<pair<lli, lli>> q1, q2;
vector<vector<lli>> dp(h + 1, vector<lli>(w + 1, 1e8));
dp[cx][cy] = 0;
for (lli i = 1; i <= h; i++) {
for (lli j = 1; j <= w; j++) {
cin >> s[i][j];
}
}
q1.push(make_pair(cx, cy));
while (!q1.empty()) {
lli x = q1.front().first;
lli y = q1.front().second;
q1.pop();
if (mp[{x, y}] == 0) {
// cout<<x<<" "<<y<<endl;
q2.push(make_pair(x, y));
while (!q2.empty()) {
x = q2.front().first;
y = q2.front().second;
// cout<<x<<" "<<y<<endl;
mp[{x, y}] = 1;
lli x1, y1;
for (lli i = 0; i < 4; i++) {
x1 = x + a1[i];
y1 = y + a2[i];
if (x1 >= 1 && x1 <= h && y1 >= 1 && y1 <= w && s[x1][y1] == '.' &&
!mp[{x1, y1}]) {
// cout<<1<<" "<<x1<<" "<<y1<<endl;
dp[x1][y1] = min(dp[x][y], dp[x1][y1]);
q2.push(make_pair(x1, y1));
mp[{x1, y1}] = 1;
}
}
q2.pop();
for (lli i = max(x - 2, 1ll); i <= min(h, x + 2); i++) {
for (lli j = max(y - 2, 1ll); j <= min(w, y + 2); j++) {
if (s[i][j] == '.' && !mp[{i, j}]) {
// cout<<0<<" "<<i<<" "<<j<<endl;
dp[i][j] = min(dp[x][y] + 1, dp[i][j]);
q1.push(make_pair(i, j));
}
}
}
}
}
}
/* for(lli i=1;i<=h;i++)
{
for(lli j=1;j<=w;j++)
{
cout<<dp[i][j]<<" ";
}
cout<<endl;
}*/
if (dp[dx][dy] == 1e8)
dp[dx][dy] = -1;
cout << dp[dx][dy] << endl;
}
| insert | 122 | 122 | 122 | 123 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
random_device rnd;
mt19937 mt(rnd());
using ll = long long;
using lld = long double;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;
const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1000000007;
int h, w;
vector<int> dijkstra(map<int, vector<pair<int, int>>> &g, int start) {
ll n = h * w;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
pque;
pque.push({0, start});
vector<int> ret(n + 2, IINF);
while (!pque.empty()) {
auto cost = pque.top().first;
auto cur = pque.top().second;
pque.pop();
if (ret[cur] <= cost)
continue;
ret[cur] = cost;
for (auto &&x : g[cur]) {
int next = x.first;
ll d = x.second;
if (cost + d < ret[next])
pque.push({cost + d, next});
}
}
return ret;
}
bool is_valid(int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; }
int main() {
cin >> h >> w;
int sy, sx, gy, gx;
cin >> sy >> sx >> gy >> gx;
sy--;
sx--;
gy--;
gx--;
vector<string> g(h);
rep(i, h) { cin >> g[i]; }
map<int, vector<pair<int, int>>> graph;
rep(y, h) {
rep(x, w) {
int dx[5] = {-2, -1, 0, 1, 2};
int dy[5] = {-2, -1, 0, 1, 2};
rep(i, 5) rep(j, 5) {
if (dy[i] == 0 && dx[j] == 0)
continue;
int ny = y + dy[i];
int nx = x + dx[j];
if (is_valid(ny, nx) && g[ny][nx] == '.') {
if ((dy[i] == 1 && dx[j] == 0) || (dy[i] == -1 && dx[j] == 0) ||
(dy[i] == 0 && dx[j] == 1) || (dy[i] == 0 && dx[j] == -1)) {
graph[w * y + x].push_back({w * ny + nx, 0});
} else {
graph[w * y + x].push_back({w * ny + nx, 1});
}
}
}
}
}
auto cost = dijkstra(graph, w * sy + sx);
if (cost[w * gy + gx] == IINF) {
cout << -1 << endl;
} else {
cout << cost[w * gy + gx] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
random_device rnd;
mt19937 mt(rnd());
using ll = long long;
using lld = long double;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;
const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1000000007;
int h, w;
vector<int> dijkstra(map<int, vector<pair<int, int>>> &g, int start) {
ll n = h * w;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
pque;
pque.push({0, start});
vector<int> ret(n + 2, IINF);
while (!pque.empty()) {
auto cost = pque.top().first;
auto cur = pque.top().second;
pque.pop();
if (ret[cur] <= cost)
continue;
ret[cur] = cost;
for (auto &&x : g[cur]) {
int next = x.first;
ll d = x.second;
if (cost + d < ret[next])
pque.push({cost + d, next});
}
}
return ret;
}
bool is_valid(int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; }
int main() {
cin >> h >> w;
int sy, sx, gy, gx;
cin >> sy >> sx >> gy >> gx;
sy--;
sx--;
gy--;
gx--;
vector<string> g(h);
rep(i, h) { cin >> g[i]; }
map<int, vector<pair<int, int>>> graph;
rep(y, h) {
rep(x, w) {
int dx[5] = {-2, -1, 0, 1, 2};
int dy[5] = {-2, -1, 0, 1, 2};
rep(i, 5) rep(j, 5) {
if (dy[i] == 0 && dx[j] == 0)
continue;
int ny = y + dy[i];
int nx = x + dx[j];
if (is_valid(ny, nx) && g[ny][nx] == '.') {
if ((dy[i] == 1 && dx[j] == 0) || (dy[i] == -1 && dx[j] == 0) ||
(dy[i] == 0 && dx[j] == 1) || (dy[i] == 0 && dx[j] == -1)) {
graph[w * y + x].push_back({w * ny + nx, 0});
} else {
graph[w * y + x].push_back({w * ny + nx, 1});
}
}
}
}
}
ll n = h * w;
int start = (w * sy) + sx;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
pque;
pque.push({0, start});
vector<int> ret(n + 2, IINF);
while (!pque.empty()) {
auto cost = pque.top().first;
auto cur = pque.top().second;
pque.pop();
if (ret[cur] <= cost)
continue;
ret[cur] = cost;
for (auto &&x : graph[cur]) {
int next = x.first;
ll d = x.second;
if (cost + d < ret[next])
pque.push({cost + d, next});
}
}
auto cost = ret;
if (cost[w * gy + gx] == IINF) {
cout << -1 << endl;
} else {
cout << cost[w * gy + gx] << endl;
}
return 0;
}
| replace | 82 | 83 | 82 | 106 | TLE | |
p02579 | Python | Runtime Error | from collections import deque
def solve(H, W, Ch, Cw, Dh, Dw, maze):
# 後の条件分岐を簡略化するためワープしても迷路外に出ないように壁で囲む
walled_maze = ["##{}##".format(row) for row in maze]
walled_maze.insert(0, "##{}##".format("#" * W))
walled_maze.insert(0, "##{}##".format("#" * W))
walled_maze.append("##{}##".format("#" * W))
walled_maze.append("##{}##".format("#" * W))
INF = 10**12
path = [[INF] * (H + 4) for _ in range(W + 4)]
walk = [(0, 1), (0, -1), (-1, 0), (1, 0)]
warp = [
(i, j)
for i in range(-2, 3)
for j in range(-2, 3)
if (i, j) not in [(0, 0)] + walk
]
yet = deque()
yet.append((Ch + 2, Cw + 2, 0)) # 囲った壁の分だけプラス
path[Ch + 2][Cw + 2] = 0
done = deque()
while yet:
y, x, s = yet.popleft()
done.append((y, x, s))
for dy, dx in walk:
ny = y + dy
nx = x + dx
if walled_maze[ny][nx] == "." and path[ny][nx] > s:
path[ny][nx] = s
yet.append((ny, nx, s))
if len(yet) == 0:
while done:
y, x, s = done.popleft()
for dy, dx in warp:
ny = y + dy
nx = x + dx
if walled_maze[ny][nx] == "." and path[ny][nx] > s + 1:
path[ny][nx] = s + 1
yet.append((ny, nx, s + 1))
ans = path[Dh + 2][Dw + 2] if path[Dh + 2][Dw + 2] < INF else -1
print(ans)
if __name__ == "__main__":
H, W = map(int, input().split())
Ch, Cw = map(lambda x: int(x) - 1, input().split())
Dh, Dw = map(lambda x: int(x) - 1, input().split())
maze = [input() for _ in range(H)]
solve(H, W, Ch, Cw, Dh, Dw, maze)
| from collections import deque
def solve(H, W, Ch, Cw, Dh, Dw, maze):
# 後の条件分岐を簡略化するためワープしても迷路外に出ないように壁で囲む
walled_maze = ["##{}##".format(row) for row in maze]
walled_maze.insert(0, "##{}##".format("#" * W))
walled_maze.insert(0, "##{}##".format("#" * W))
walled_maze.append("##{}##".format("#" * W))
walled_maze.append("##{}##".format("#" * W))
INF = 10**12
path = [[INF] * (W + 4) for _ in range(H + 4)]
walk = [(0, 1), (0, -1), (-1, 0), (1, 0)]
warp = [
(i, j)
for i in range(-2, 3)
for j in range(-2, 3)
if (i, j) not in [(0, 0)] + walk
]
yet = deque()
yet.append((Ch + 2, Cw + 2, 0)) # 囲った壁の分だけプラス
path[Ch + 2][Cw + 2] = 0
done = deque()
while yet:
y, x, s = yet.popleft()
done.append((y, x, s))
for dy, dx in walk:
ny = y + dy
nx = x + dx
if walled_maze[ny][nx] == "." and path[ny][nx] > s:
path[ny][nx] = s
yet.append((ny, nx, s))
if len(yet) == 0:
while done:
y, x, s = done.popleft()
for dy, dx in warp:
ny = y + dy
nx = x + dx
if walled_maze[ny][nx] == "." and path[ny][nx] > s + 1:
path[ny][nx] = s + 1
yet.append((ny, nx, s + 1))
ans = path[Dh + 2][Dw + 2] if path[Dh + 2][Dw + 2] < INF else -1
print(ans)
if __name__ == "__main__":
H, W = map(int, input().split())
Ch, Cw = map(lambda x: int(x) - 1, input().split())
Dh, Dw = map(lambda x: int(x) - 1, input().split())
maze = [input() for _ in range(H)]
solve(H, W, Ch, Cw, Dh, Dw, maze)
| replace | 12 | 13 | 12 | 13 | 0 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define matrix vector<vector<ll>>
#define zero(n, m) matrix(n, vector<ll>(m, 0))
#define one(n, m) matrix(n, vector<ll>(m, 1))
#define pii pair<ll, pair<ll, ll>>
const int N = 1e3 + 5;
ll n, m, stX, stY, enX, enY, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0},
cost[N][N];
char a[N][N];
bool isValid(int i, int j) {
if (i < 1 || i > n || j < 1 || j > m || a[i][j] == '#')
return 0;
return 1;
}
ll dijs() {
priority_queue<pii, vector<pii>, greater<pii>> q;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++)
cost[i][j] = 1e18;
}
cost[stX][stY] = 0;
q.push({0, {stX, stY}});
while (q.size()) {
ll i = q.top().s.f, j = q.top().s.s, magic = q.top().f;
q.pop();
if (cost[i][j] < magic)
continue;
for (int k = 0; k < 4; k++) {
int ii = i + dx[k], jj = j + dy[k];
if (isValid(ii, jj) && cost[ii][jj] > magic) {
cost[ii][jj] = magic;
q.push({cost[ii][jj], {ii, jj}});
}
}
int st = i - 2, stj = j - 2, en = i + 2, enj = j + 2;
for (int k = st; k <= en; k++) {
for (int kk = stj; kk <= enj; kk++) {
if (isValid(k, kk) && cost[k][kk] > magic + 1) {
cost[k][kk] = magic + 1;
q.push({cost[k][kk], {k, kk}});
}
}
}
}
if (cost[enX][enY] == 1e18)
cout << "-1\n";
else
cout << cost[enX][enY] << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
cin >> stX >> stY >> enX >> enY;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
for (int j = 1; j <= m; j++)
a[i][j] = s[j - 1];
}
dijs();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define matrix vector<vector<ll>>
#define zero(n, m) matrix(n, vector<ll>(m, 0))
#define one(n, m) matrix(n, vector<ll>(m, 1))
#define pii pair<ll, pair<ll, ll>>
const int N = 1e3 + 5;
ll n, m, stX, stY, enX, enY, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0},
cost[N][N];
char a[N][N];
bool isValid(int i, int j) {
if (i < 1 || i > n || j < 1 || j > m || a[i][j] == '#')
return 0;
return 1;
}
ll dijs() {
priority_queue<pii, vector<pii>, greater<pii>> q;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++)
cost[i][j] = 1e18;
}
cost[stX][stY] = 0;
q.push({0, {stX, stY}});
while (q.size()) {
ll i = q.top().s.f, j = q.top().s.s, magic = q.top().f;
q.pop();
if (cost[i][j] < magic)
continue;
for (int k = 0; k < 4; k++) {
int ii = i + dx[k], jj = j + dy[k];
if (isValid(ii, jj) && cost[ii][jj] > magic) {
cost[ii][jj] = magic;
q.push({cost[ii][jj], {ii, jj}});
}
}
int st = i - 2, stj = j - 2, en = i + 2, enj = j + 2;
for (int k = st; k <= en; k++) {
for (int kk = stj; kk <= enj; kk++) {
if (isValid(k, kk) && cost[k][kk] > magic + 1) {
cost[k][kk] = magic + 1;
q.push({cost[k][kk], {k, kk}});
}
}
}
}
if (cost[enX][enY] == 1e18)
cout << "-1\n";
else
cout << cost[enX][enY] << "\n";
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
cin >> stX >> stY >> enX >> enY;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
for (int j = 1; j <= m; j++)
a[i][j] = s[j - 1];
}
dijs();
return 0;
}
| insert | 60 | 60 | 60 | 61 | 0 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FIN \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef set<ii> sii;
typedef set<int> si;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef queue<ii> qii;
const int INF = 1000000010;
const ll MOD = 1000000007;
class UnionFind {
private:
vi p, rank;
public:
UnionFind(int N) {
rank.assign(N, 0);
p.assign(N, 0);
for (int i = 0; i < N; i++)
p[i] = i;
}
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (!isSameSet(i, j)) {
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]) {
p[y] = x;
} else {
p[x] = y;
if (rank[x] == rank[y])
rank[y]++;
}
}
}
};
int main() {
FIN
int n,
m, i, j, a, b, x, y, p, q, c = 0;
cin >> n >> m;
cin >> a >> b;
cin >> x >> y;
vs tb(n);
vvi pr(n, vi(m, -1));
UnionFind UF(n * m);
for (i = 0; i < n; i++) {
cin >> tb[i];
for (j = 0; j < m; j++) {
if (tb[i][j] == '.') {
if (i > 0 and tb[i - 1][j] == '.')
UF.unionSet(i * m + j, (i - 1) * m + j);
if (j > 0 and tb[i][j - 1] == '.')
UF.unionSet(i * m + j, i * m + (j - 1));
}
}
}
sii ed;
vvi gf(n * m);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (tb[i][j] == '.')
pr[i][j] = UF.findSet(i * m + j);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (pr[i][j] != -1) {
for (p = min(n - 1, i + 2); p >= max(0, i - 2); p--) {
for (q = min(m - 1, j + 2); q >= max(0, j - 2); q--) {
if (pr[p][q] != -1 and pr[i][j] != pr[p][q]) {
auto it =
ed.find(ii(min(pr[i][j], pr[p][q]), max(pr[i][j], pr[p][q])));
if (it == ed.end()) {
gf[pr[i][j]].push_back(pr[p][q]);
gf[pr[p][q]].push_back(pr[i][j]);
}
}
}
}
}
}
}
a--, b--, x--, y--;
if (pr[a][b] != pr[x][y]) {
qii tr;
tr.push(ii(pr[a][b], 0));
vi vis(n);
vis[pr[a][b]] = 1;
while (!tr.empty()) {
ii e = tr.front();
tr.pop();
for (i = 0; i < (int)gf[e.first].size(); i++) {
if (vis[gf[e.first][i]] == 0) {
if (gf[e.first][i] == pr[x][y]) {
cout << e.second + 1 << "\n";
return 0;
}
vis[gf[e.first][i]] = 1;
tr.push(ii(gf[e.first][i], e.second + 1));
}
}
}
c = -1;
}
cout << c << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define FIN \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef set<ii> sii;
typedef set<int> si;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef queue<ii> qii;
const int INF = 1000000010;
const ll MOD = 1000000007;
class UnionFind {
private:
vi p, rank;
public:
UnionFind(int N) {
rank.assign(N, 0);
p.assign(N, 0);
for (int i = 0; i < N; i++)
p[i] = i;
}
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (!isSameSet(i, j)) {
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]) {
p[y] = x;
} else {
p[x] = y;
if (rank[x] == rank[y])
rank[y]++;
}
}
}
};
int main() {
FIN
int n,
m, i, j, a, b, x, y, p, q, c = 0;
cin >> n >> m;
cin >> a >> b;
cin >> x >> y;
vs tb(n);
vvi pr(n, vi(m, -1));
UnionFind UF(n * m);
for (i = 0; i < n; i++) {
cin >> tb[i];
for (j = 0; j < m; j++) {
if (tb[i][j] == '.') {
if (i > 0 and tb[i - 1][j] == '.')
UF.unionSet(i * m + j, (i - 1) * m + j);
if (j > 0 and tb[i][j - 1] == '.')
UF.unionSet(i * m + j, i * m + (j - 1));
}
}
}
sii ed;
vvi gf(n * m);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (tb[i][j] == '.')
pr[i][j] = UF.findSet(i * m + j);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (pr[i][j] != -1) {
for (p = min(n - 1, i + 2); p >= max(0, i - 2); p--) {
for (q = min(m - 1, j + 2); q >= max(0, j - 2); q--) {
if (pr[p][q] != -1 and pr[i][j] != pr[p][q]) {
auto it =
ed.find(ii(min(pr[i][j], pr[p][q]), max(pr[i][j], pr[p][q])));
if (it == ed.end()) {
gf[pr[i][j]].push_back(pr[p][q]);
gf[pr[p][q]].push_back(pr[i][j]);
}
}
}
}
}
}
}
a--, b--, x--, y--;
if (pr[a][b] != pr[x][y]) {
qii tr;
tr.push(ii(pr[a][b], 0));
vi vis(n * m);
vis[pr[a][b]] = 1;
while (!tr.empty()) {
ii e = tr.front();
tr.pop();
for (i = 0; i < (int)gf[e.first].size(); i++) {
if (vis[gf[e.first][i]] == 0) {
if (gf[e.first][i] == pr[x][y]) {
cout << e.second + 1 << "\n";
return 0;
}
vis[gf[e.first][i]] = 1;
tr.push(ii(gf[e.first][i], e.second + 1));
}
}
}
c = -1;
}
cout << c << "\n";
return 0;
}
| replace | 111 | 112 | 111 | 112 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using usize = std::size_t;
using namespace std;
#define rep(i, max) for (i32 i = 0; i < (i32)max; ++i)
#define loop while (true)
#define allOf(collection) collection.begin(), collection.end()
unique_ptr<string> readLine() {
unique_ptr<string> line(new string());
getline(cin, *line);
return line;
}
template <typename T1, typename T2> pair<T1, T2> readPair() {
T1 res1;
cin >> res1;
T2 res2;
cin >> res2;
readLine();
return make_pair(res1, res2);
}
template <typename T> unique_ptr<vector<T>> readVector(usize num) {
unique_ptr<vector<T>> list(new vector<T>(num));
rep(i, num) cin >> (*list)[i];
readLine();
return list;
}
void readValues() { readLine(); }
template <typename H, typename... T> void readValues(H &head, T &&...tails) {
H a;
cin >> a;
head = a;
readValues(forward<T>(tails)...);
}
void writeLine() { cout << endl; }
template <typename T> void write(const T &arg) { cout << arg; }
template <typename T> void writeLine(const vector<T> &arg) {
if (arg.empty()) {
writeLine();
return;
}
write(arg[0]);
for (int i = 1; i < arg.size(); ++i) {
write(" ");
write(arg[i]);
}
writeLine();
}
template <typename T> void writeLine(const T &arg) { cout << arg << endl; }
void recursiveCombination(vector<i32> &indexes, i32 s, i32 rest,
function<void(vector<i32> &)> f) {
if (rest == 0) {
f(indexes);
} else {
if (s < 0)
return;
recursiveCombination(indexes, s - 1, rest, f);
indexes[rest - 1] = s;
recursiveCombination(indexes, s - 1, rest - 1, f);
}
}
void foreachWithCombination(i32 n, i32 k, function<void(vector<i32> &)> f) {
auto indexes = vector<i32>(k);
recursiveCombination(indexes, n - 1, k, f);
}
void foreachWithPermutaton(i32 n, function<void(vector<i32> &)> f) {
auto indexes = vector<i32>();
indexes.reserve(n);
rep(i, (usize)n) indexes.push_back(i);
do
f(indexes);
while (next_permutation(allOf(indexes)));
}
void foreachWithPermutaton(i32 n, i32 k, function<void(vector<i32> &)> f) {
foreachWithCombination(n, k, [=](vector<i32> &combIndexes) {
foreachWithPermutaton(k, [=](vector<i32> &permIndexes) {
auto indexes = vector<i32>();
indexes.reserve(k);
rep(i, (usize)k) { indexes.push_back(combIndexes[permIndexes[i]]); }
f(indexes);
});
});
}
void program();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
program();
}
struct Info {
i32 h, w;
Info(i32 h_, i32 w_) {
h = h_;
w = w_;
}
};
void program() {
i32 h, w, cH, cW, dH, dW;
readValues(h, w, cH, cW, dH, dW);
auto s = vector<string>();
rep(i, h) { s.push_back(*readLine()); }
auto que = queue<Info>();
s[cH - 1][cW - 1] = 'd';
que.emplace(cH - 1, cW - 1);
auto list = vector<set<Info>>();
int warpCount = 0;
bool goaled = false;
auto newPoints = vector<Info>();
newPoints.emplace_back(cH - 1, cW - 1);
loop {
auto alSet = vector<Info>();
for (const auto &item : newPoints) {
alSet.push_back(item);
}
while (!que.empty()) {
auto info = que.front();
que.pop();
if (info.h == dH - 1 && info.w == dW - 1) {
goaled = true;
goto exit5;
}
if (info.h + 1 < h && s[info.h + 1][info.w] == '.') {
s[info.h + 1][info.w] = 'd';
alSet.emplace_back(info.h + 1, info.w);
que.emplace(info.h + 1, info.w);
}
if (0 <= info.h - 1 && s[info.h - 1][info.w] == '.') {
s[info.h - 1][info.w] = 'd';
alSet.emplace_back(info.h - 1, info.w);
que.emplace(info.h - 1, info.w);
}
if (info.w + 1 < w && s[info.h][info.w + 1] == '.') {
s[info.h][info.w + 1] = 'd';
alSet.emplace_back(info.h, info.w + 1);
que.emplace(info.h, info.w + 1);
}
if (0 <= info.w - 1 && s[info.h][info.w - 1] == '.') {
s[info.h][info.w - 1] = 'd';
alSet.emplace_back(info.h, info.w - 1);
que.emplace(info.h, info.w - 1);
}
}
newPoints.clear();
for (const auto &item : alSet) {
for (i32 i = max(0, item.h - 2); i <= min(h - 1, item.h + 2); ++i) {
for (i32 j = max(0, item.w - 2); j <= min(w - 1, item.w + 2); ++j) {
if (s[i][j] == '.') {
s[i][j] = 'd';
que.emplace(i, j);
newPoints.emplace_back(i, j);
}
}
}
}
++warpCount;
}
exit5:
writeLine(goaled ? warpCount : -1);
} | #include <bits/stdc++.h>
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using usize = std::size_t;
using namespace std;
#define rep(i, max) for (i32 i = 0; i < (i32)max; ++i)
#define loop while (true)
#define allOf(collection) collection.begin(), collection.end()
unique_ptr<string> readLine() {
unique_ptr<string> line(new string());
getline(cin, *line);
return line;
}
template <typename T1, typename T2> pair<T1, T2> readPair() {
T1 res1;
cin >> res1;
T2 res2;
cin >> res2;
readLine();
return make_pair(res1, res2);
}
template <typename T> unique_ptr<vector<T>> readVector(usize num) {
unique_ptr<vector<T>> list(new vector<T>(num));
rep(i, num) cin >> (*list)[i];
readLine();
return list;
}
void readValues() { readLine(); }
template <typename H, typename... T> void readValues(H &head, T &&...tails) {
H a;
cin >> a;
head = a;
readValues(forward<T>(tails)...);
}
void writeLine() { cout << endl; }
template <typename T> void write(const T &arg) { cout << arg; }
template <typename T> void writeLine(const vector<T> &arg) {
if (arg.empty()) {
writeLine();
return;
}
write(arg[0]);
for (int i = 1; i < arg.size(); ++i) {
write(" ");
write(arg[i]);
}
writeLine();
}
template <typename T> void writeLine(const T &arg) { cout << arg << endl; }
void recursiveCombination(vector<i32> &indexes, i32 s, i32 rest,
function<void(vector<i32> &)> f) {
if (rest == 0) {
f(indexes);
} else {
if (s < 0)
return;
recursiveCombination(indexes, s - 1, rest, f);
indexes[rest - 1] = s;
recursiveCombination(indexes, s - 1, rest - 1, f);
}
}
void foreachWithCombination(i32 n, i32 k, function<void(vector<i32> &)> f) {
auto indexes = vector<i32>(k);
recursiveCombination(indexes, n - 1, k, f);
}
void foreachWithPermutaton(i32 n, function<void(vector<i32> &)> f) {
auto indexes = vector<i32>();
indexes.reserve(n);
rep(i, (usize)n) indexes.push_back(i);
do
f(indexes);
while (next_permutation(allOf(indexes)));
}
void foreachWithPermutaton(i32 n, i32 k, function<void(vector<i32> &)> f) {
foreachWithCombination(n, k, [=](vector<i32> &combIndexes) {
foreachWithPermutaton(k, [=](vector<i32> &permIndexes) {
auto indexes = vector<i32>();
indexes.reserve(k);
rep(i, (usize)k) { indexes.push_back(combIndexes[permIndexes[i]]); }
f(indexes);
});
});
}
void program();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
program();
}
struct Info {
i32 h, w;
Info(i32 h_, i32 w_) {
h = h_;
w = w_;
}
};
void program() {
i32 h, w, cH, cW, dH, dW;
readValues(h, w, cH, cW, dH, dW);
auto s = vector<string>();
rep(i, h) { s.push_back(*readLine()); }
auto que = queue<Info>();
s[cH - 1][cW - 1] = 'd';
que.emplace(cH - 1, cW - 1);
auto list = vector<set<Info>>();
int warpCount = 0;
bool goaled = false;
auto newPoints = vector<Info>();
newPoints.emplace_back(cH - 1, cW - 1);
loop {
auto alSet = vector<Info>();
for (const auto &item : newPoints) {
alSet.push_back(item);
}
while (!que.empty()) {
auto info = que.front();
que.pop();
if (info.h == dH - 1 && info.w == dW - 1) {
goaled = true;
goto exit5;
}
if (info.h + 1 < h && s[info.h + 1][info.w] == '.') {
s[info.h + 1][info.w] = 'd';
alSet.emplace_back(info.h + 1, info.w);
que.emplace(info.h + 1, info.w);
}
if (0 <= info.h - 1 && s[info.h - 1][info.w] == '.') {
s[info.h - 1][info.w] = 'd';
alSet.emplace_back(info.h - 1, info.w);
que.emplace(info.h - 1, info.w);
}
if (info.w + 1 < w && s[info.h][info.w + 1] == '.') {
s[info.h][info.w + 1] = 'd';
alSet.emplace_back(info.h, info.w + 1);
que.emplace(info.h, info.w + 1);
}
if (0 <= info.w - 1 && s[info.h][info.w - 1] == '.') {
s[info.h][info.w - 1] = 'd';
alSet.emplace_back(info.h, info.w - 1);
que.emplace(info.h, info.w - 1);
}
}
newPoints.clear();
for (const auto &item : alSet) {
for (i32 i = max(0, item.h - 2); i <= min(h - 1, item.h + 2); ++i) {
for (i32 j = max(0, item.w - 2); j <= min(w - 1, item.w + 2); ++j) {
if (s[i][j] == '.') {
s[i][j] = 'd';
que.emplace(i, j);
newPoints.emplace_back(i, j);
}
}
}
}
if (newPoints.size() == 0)
goto exit5;
++warpCount;
}
exit5:
writeLine(goaled ? warpCount : -1);
} | insert | 164 | 164 | 164 | 166 | TLE | |
p02579 | C++ | Runtime Error |
// Problem : D - Wizard in Maze
// Contest : AtCoder - AtCoder Beginner Contest 176
// URL : https://atcoder.jp/contests/abc176/tasks/abc176_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define RI(i, n) FOR(i, 1, (n))
#define REP(i, n) FOR(i, 0, (n)-1)
#define mini(a, b) a = min(a, b)
#define maxi(a, b) a = max(a, b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int)w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 1e3 + 5;
const ll MOD = 1e9 + 7;
int n, m, x_s, y_s, x2, y2;
int blocked[maxN][maxN];
int color[maxN][maxN];
bool used[maxN][maxN];
int dist[maxN * maxN];
set<int> graph[maxN];
vector<pii> neigh = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
void BFS_cell(int i, int j, int col) {
queue<pii> Q;
Q.push({i, j});
while (!Q.empty()) {
pii u = Q.front();
Q.pop();
// if (used[u.st][u.nd]) continue;
// cout << u.st << " " << u.nd << endl;
used[u.st][u.nd] = true;
color[u.st][u.nd] = col;
for (auto nn : neigh) {
int xx = u.st + nn.st;
int yy = u.nd + nn.nd;
// cout << "neigh " << xx << " " << yy << endl;
if (xx < 0 || xx >= n || yy < 0 || yy >= m)
continue;
if (!used[xx][yy] && !blocked[xx][yy]) {
used[xx][yy] = true;
Q.push({xx, yy});
}
}
}
// cout << endl;
}
// sprawdz MODULO!
int main() {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
cin >> x_s >> y_s;
cin >> x2 >> y2;
REP(i, n) {
string s;
cin >> s;
REP(j, m) {
if (s[j] == '#')
blocked[i][j] = true;
else
blocked[i][j] = false;
}
}
int col = 1;
REP(i, n) {
REP(j, m) {
if (!used[i][j] && !blocked[i][j]) {
BFS_cell(i, j, col++);
}
}
}
REP(i, n) {
REP(j, m) {
if (color[i][j] == 0)
continue;
FOR(k, -2, 2) {
FOR(l, -2, 2) {
int xx = i + k;
int yy = j + l;
if (xx < 0 || xx >= n || yy < 0 || yy >= m)
continue;
if (color[xx][yy] != color[i][j] && color[xx][yy] != 0) {
graph[color[i][j]].insert(color[xx][yy]);
graph[color[xx][yy]].insert(color[i][j]);
}
}
}
}
}
REP(i, 1e6 + 1) { dist[i] = -1; }
int start = color[x_s - 1][y_s - 1];
dist[start] = 0;
queue<int> Q;
Q.push(start);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
// cout << u << endl;
if (u == color[x2 - 1][y2 - 1])
break;
for (auto x : graph[u]) {
// cout << "edge " << x << endl;
if (dist[x] == -1) {
dist[x] = dist[u] + 1;
Q.push(x);
}
}
}
cout << dist[color[x2 - 1][y2 - 1]];
return 0;
}
|
// Problem : D - Wizard in Maze
// Contest : AtCoder - AtCoder Beginner Contest 176
// URL : https://atcoder.jp/contests/abc176/tasks/abc176_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define RI(i, n) FOR(i, 1, (n))
#define REP(i, n) FOR(i, 0, (n)-1)
#define mini(a, b) a = min(a, b)
#define maxi(a, b) a = max(a, b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int)w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 1e3 + 5;
const ll MOD = 1e9 + 7;
int n, m, x_s, y_s, x2, y2;
int blocked[maxN][maxN];
int color[maxN][maxN];
bool used[maxN][maxN];
int dist[maxN * maxN];
set<int> graph[maxN * maxN];
vector<pii> neigh = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
void BFS_cell(int i, int j, int col) {
queue<pii> Q;
Q.push({i, j});
while (!Q.empty()) {
pii u = Q.front();
Q.pop();
// if (used[u.st][u.nd]) continue;
// cout << u.st << " " << u.nd << endl;
used[u.st][u.nd] = true;
color[u.st][u.nd] = col;
for (auto nn : neigh) {
int xx = u.st + nn.st;
int yy = u.nd + nn.nd;
// cout << "neigh " << xx << " " << yy << endl;
if (xx < 0 || xx >= n || yy < 0 || yy >= m)
continue;
if (!used[xx][yy] && !blocked[xx][yy]) {
used[xx][yy] = true;
Q.push({xx, yy});
}
}
}
// cout << endl;
}
// sprawdz MODULO!
int main() {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
cin >> x_s >> y_s;
cin >> x2 >> y2;
REP(i, n) {
string s;
cin >> s;
REP(j, m) {
if (s[j] == '#')
blocked[i][j] = true;
else
blocked[i][j] = false;
}
}
int col = 1;
REP(i, n) {
REP(j, m) {
if (!used[i][j] && !blocked[i][j]) {
BFS_cell(i, j, col++);
}
}
}
REP(i, n) {
REP(j, m) {
if (color[i][j] == 0)
continue;
FOR(k, -2, 2) {
FOR(l, -2, 2) {
int xx = i + k;
int yy = j + l;
if (xx < 0 || xx >= n || yy < 0 || yy >= m)
continue;
if (color[xx][yy] != color[i][j] && color[xx][yy] != 0) {
graph[color[i][j]].insert(color[xx][yy]);
graph[color[xx][yy]].insert(color[i][j]);
}
}
}
}
}
REP(i, 1e6 + 1) { dist[i] = -1; }
int start = color[x_s - 1][y_s - 1];
dist[start] = 0;
queue<int> Q;
Q.push(start);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
// cout << u << endl;
if (u == color[x2 - 1][y2 - 1])
break;
for (auto x : graph[u]) {
// cout << "edge " << x << endl;
if (dist[x] == -1) {
dist[x] = dist[u] + 1;
Q.push(x);
}
}
}
cout << dist[color[x2 - 1][y2 - 1]];
return 0;
}
| replace | 44 | 45 | 44 | 45 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 9999999999;
ll dp[1001][1001];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, 1, -1, 0};
int main() {
int h, w;
cin >> h >> w;
int sh, sw, gh, gw;
cin >> sh >> sw >> gh >> gw;
sh--;
sw--;
gh--;
gw--;
vector<string> G(h);
for (int i = 0; i < h; i++)
cin >> G[i];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
dp[i][j] = INF;
}
}
dp[sh][sw] = 0;
priority_queue<PP> q;
q.push(PP(0, P(sh, sw)));
while (!q.empty()) {
PP pp = q.top();
q.pop();
int cost = pp.first;
int i = pp.second.first;
int j = pp.second.second;
if (cost > dp[i][j])
continue;
for (int d = 0; d < 4; d++) {
int ni = i + dy[d];
int nj = j + dx[d];
if (ni >= 0 && ni < h && nj >= 0 && nj < w && G[ni][nj] == '.' &&
dp[ni][nj] > dp[i][j]) {
q.push(PP(cost, P(ni, nj)));
dp[ni][nj] = dp[i][j];
}
}
for (int y = -2; y < 3; y++) {
for (int x = -2; x < 3; x++) {
int ni = i + y;
int nj = j + x;
if (ni >= 0 && ni < h && nj >= 0 && nj < w && G[ni][nj] == '.' &&
dp[ni][nj] > dp[i][j] + 1) {
q.push(PP(cost + 1, P(ni, nj)));
dp[ni][nj] = dp[i][j] + 1;
}
}
}
}
if (dp[gh][gw] == INF)
dp[gh][gw] = -1;
cout << dp[gh][gw] << endl;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 9999999999;
ll dp[1001][1001];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, 1, -1, 0};
int main() {
int h, w;
cin >> h >> w;
int sh, sw, gh, gw;
cin >> sh >> sw >> gh >> gw;
sh--;
sw--;
gh--;
gw--;
vector<string> G(h);
for (int i = 0; i < h; i++)
cin >> G[i];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
dp[i][j] = INF;
}
}
dp[sh][sw] = 0;
priority_queue<PP, vector<PP>, greater<PP>> q;
q.push(PP(0, P(sh, sw)));
while (!q.empty()) {
PP pp = q.top();
q.pop();
int cost = pp.first;
int i = pp.second.first;
int j = pp.second.second;
if (cost > dp[i][j])
continue;
for (int d = 0; d < 4; d++) {
int ni = i + dy[d];
int nj = j + dx[d];
if (ni >= 0 && ni < h && nj >= 0 && nj < w && G[ni][nj] == '.' &&
dp[ni][nj] > dp[i][j]) {
q.push(PP(cost, P(ni, nj)));
dp[ni][nj] = dp[i][j];
}
}
for (int y = -2; y < 3; y++) {
for (int x = -2; x < 3; x++) {
int ni = i + y;
int nj = j + x;
if (ni >= 0 && ni < h && nj >= 0 && nj < w && G[ni][nj] == '.' &&
dp[ni][nj] > dp[i][j] + 1) {
q.push(PP(cost + 1, P(ni, nj)));
dp[ni][nj] = dp[i][j] + 1;
}
}
}
}
if (dp[gh][gw] == INF)
dp[gh][gw] = -1;
cout << dp[gh][gw] << endl;
} | replace | 54 | 55 | 54 | 55 | TLE | |
p02579 | C++ | Time Limit Exceeded | /* Aditya0412 */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define ve vector
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define fi first
#define all(a) (a).begin(), (a).end()
#define si(x) (int)((x).size())
#define debug(x) \
cout << #x << " is " << (x) << endl; \
cout.flush();
#define se second
#define endl '\n'
#define f(i, a, b) for (int i = a; i < b; i++)
#define mem(a, x) memset(a, x, sizeof(a))
// === Debug macro starts here ===
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) \
{ \
++recur_depth; \
auto x_ = x; \
--recur_depth; \
cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":" \
<< __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl; \
}
#else
#define dbg(x)
#endif
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream &>::type
operator<<(Ostream &os, const Cont &v) {
os << "[";
for (auto &x : v) {
os << x << ", ";
}
return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream &operator<<(Ostream &os, const pair<Ts...> &p) {
return os << "{" << p.first << ", " << p.second << "}";
}
// === Debug macro ends here ===
// const ll mod=998244353;
const ll mod = 1000000000 + 7;
const ll N = 1000000 + 6;
#define M_PI 3.14159265358979323846
void inp(ll *arr, ll n) {
for (int i = 0; i < n; i++)
cin >> arr[i];
}
void inp_mat(ll *arr, ll n, ll m) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
ll x;
cin >> x;
*((arr + i * m) + j) = x;
}
}
// syntax for inp_mat inp_mat((ll *)arr,n,m);
int x2[] = {+1, -1, +0, +0};
int y2[] = {+0, +0, +1, -1};
char arr[1005][1005];
int r, c;
int sr, sc, er, ec;
ll ff(int i, int j) { return (i - 1) * c + j; }
int par[N];
int sz[N];
int root(int x) {
while (par[x] != x) {
par[x] = par[par[x]];
x = par[x];
}
return x;
}
void join(int x, int y) {
int xx = root(x), yy = root(y);
if (xx != yy) {
if (sz[xx] > sz[yy])
swap(xx, yy);
par[xx] = yy;
sz[yy] += sz[xx];
}
}
vector<int> graph[N];
bool vis[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> r >> c;
mem(vis, false);
cin >> sr >> sc >> er >> ec;
int snode = ff(sr, sc);
int enode = ff(er, ec);
for (int i = 1; i <= r; i++)
for (int j = 1; j <= c; j++) {
cin >> arr[i][j];
if (arr[i][j] == '.') {
par[ff(i, j)] = ff(i, j);
sz[ff(i, j)] = 1;
}
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
for (int k = 0; k < 4; k++) {
int ii = i + x2[k], jj = j + y2[k];
if (ii >= 1 and ii <= r and jj >= 1 and jj <= c) {
if (arr[ii][jj] == '.' and arr[i][j] == '.') {
join(ff(ii, jj), ff(i, j));
// join(ff(i,j),ff(ii,jj));
}
}
}
}
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
if (arr[i][j] == '.') {
par[ff(i, j)] = root(ff(i, j));
// dbg(i);dbg(j);dbg(par[ff(i,j)]);
}
}
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
for (int ii = i - 2; ii <= i + 2; ii++) {
for (int jj = j - 2; jj <= j + 2; jj++) {
if (ii >= 1 and ii <= r and jj >= 1 and jj <= c) {
if (arr[ii][jj] == '.' and arr[i][j] == '.') {
if (par[ff(ii, jj)] != par[ff(i, j)]) {
graph[par[ff(ii, jj)]].pb(par[ff(i, j)]);
graph[par[ff(i, j)]].pb(par[ff(ii, jj)]);
}
}
}
}
}
}
}
snode = par[snode];
enode = par[enode];
if (snode == enode) {
cout << 0;
return 0;
}
// dbg(snode);dbg(enode);
queue<pair<int, int>> q;
q.push({snode, 0});
vis[snode] = true;
while (!q.empty()) {
int node = q.front().fi;
// dbg(node);
int v = q.front().se;
if (node == enode) {
cout << v;
return 0;
}
q.pop();
for (auto child : graph[node]) {
if (!vis[child]) {
q.push({child, v + 1});
}
}
}
cout << "-1";
} | /* Aditya0412 */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define ve vector
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define fi first
#define all(a) (a).begin(), (a).end()
#define si(x) (int)((x).size())
#define debug(x) \
cout << #x << " is " << (x) << endl; \
cout.flush();
#define se second
#define endl '\n'
#define f(i, a, b) for (int i = a; i < b; i++)
#define mem(a, x) memset(a, x, sizeof(a))
// === Debug macro starts here ===
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) \
{ \
++recur_depth; \
auto x_ = x; \
--recur_depth; \
cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":" \
<< __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl; \
}
#else
#define dbg(x)
#endif
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream &>::type
operator<<(Ostream &os, const Cont &v) {
os << "[";
for (auto &x : v) {
os << x << ", ";
}
return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream &operator<<(Ostream &os, const pair<Ts...> &p) {
return os << "{" << p.first << ", " << p.second << "}";
}
// === Debug macro ends here ===
// const ll mod=998244353;
const ll mod = 1000000000 + 7;
const ll N = 1000000 + 6;
#define M_PI 3.14159265358979323846
void inp(ll *arr, ll n) {
for (int i = 0; i < n; i++)
cin >> arr[i];
}
void inp_mat(ll *arr, ll n, ll m) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
ll x;
cin >> x;
*((arr + i * m) + j) = x;
}
}
// syntax for inp_mat inp_mat((ll *)arr,n,m);
int x2[] = {+1, -1, +0, +0};
int y2[] = {+0, +0, +1, -1};
char arr[1005][1005];
int r, c;
int sr, sc, er, ec;
ll ff(int i, int j) { return (i - 1) * c + j; }
int par[N];
int sz[N];
int root(int x) {
while (par[x] != x) {
par[x] = par[par[x]];
x = par[x];
}
return x;
}
void join(int x, int y) {
int xx = root(x), yy = root(y);
if (xx != yy) {
if (sz[xx] > sz[yy])
swap(xx, yy);
par[xx] = yy;
sz[yy] += sz[xx];
}
}
vector<int> graph[N];
bool vis[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> r >> c;
mem(vis, false);
cin >> sr >> sc >> er >> ec;
int snode = ff(sr, sc);
int enode = ff(er, ec);
for (int i = 1; i <= r; i++)
for (int j = 1; j <= c; j++) {
cin >> arr[i][j];
if (arr[i][j] == '.') {
par[ff(i, j)] = ff(i, j);
sz[ff(i, j)] = 1;
}
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
for (int k = 0; k < 4; k++) {
int ii = i + x2[k], jj = j + y2[k];
if (ii >= 1 and ii <= r and jj >= 1 and jj <= c) {
if (arr[ii][jj] == '.' and arr[i][j] == '.') {
join(ff(ii, jj), ff(i, j));
// join(ff(i,j),ff(ii,jj));
}
}
}
}
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
if (arr[i][j] == '.') {
par[ff(i, j)] = root(ff(i, j));
// dbg(i);dbg(j);dbg(par[ff(i,j)]);
}
}
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
for (int ii = i - 2; ii <= i + 2; ii++) {
for (int jj = j - 2; jj <= j + 2; jj++) {
if (ii >= 1 and ii <= r and jj >= 1 and jj <= c) {
if (arr[ii][jj] == '.' and arr[i][j] == '.') {
if (par[ff(ii, jj)] != par[ff(i, j)]) {
graph[par[ff(ii, jj)]].pb(par[ff(i, j)]);
graph[par[ff(i, j)]].pb(par[ff(ii, jj)]);
}
}
}
}
}
}
}
snode = par[snode];
enode = par[enode];
if (snode == enode) {
cout << 0;
return 0;
}
// dbg(snode);dbg(enode);
queue<pair<int, int>> q;
q.push({snode, 0});
vis[snode] = true;
while (!q.empty()) {
int node = q.front().fi;
// dbg(node);
int v = q.front().se;
if (node == enode) {
cout << v;
return 0;
}
q.pop();
for (auto child : graph[node]) {
if (!vis[child]) {
q.push({child, v + 1});
vis[child] = true;
}
}
}
cout << "-1";
} | insert | 176 | 176 | 176 | 177 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
#define ALL(a) a.begin(), a.end()
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<int, int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int visited[1000005];
vector<int> to[1000005];
vector<int> connect[1000005];
int color[1000005];
int parent[1005][1005];
int dist[1000005];
int w, h;
void dfs1(int u) {
color[u] = 1;
for (int v : connect[u]) {
if (color[v] == 0) {
parent[v / w][v % w] = parent[u / w][u % w];
dfs1(v);
}
}
color[u] = 2;
return;
}
/*
int dfs(int now,int gl,int cnt){
visited[now]=1;
for(int v:to[now]){
if(v==gl){
return cnt+1;
}
if(visited[v]==0){
return dfs(v,gl,cnt+1);
}
}
return -1;
}
*/
int main() {
cin >> h >> w;
int ch, cw;
cin >> ch >> cw;
ch--;
cw--;
int dh, dw;
cin >> dh >> dw;
dh--;
dw--;
string s[h];
rep(i, h) { cin >> s[i]; }
rep(i, 1000001) { dist[i] = INF; }
rep(i, h) {
rep(j, w) {
if (s[i][j] == '.') {
parent[i][j] = i * h + j;
} else {
parent[i][j] = INF;
}
}
}
rep(i, h) {
rep(j, w) {
if (s[i][j] == '.') {
if (i > 0 && s[i - 1][j] == '.') {
connect[parent[i][j]].push_back(parent[i - 1][j]);
connect[parent[i - 1][j]].push_back(parent[i][j]);
}
if (j > 0 && s[i][j - 1] == '.') {
connect[parent[i][j]].push_back(parent[i][j - 1]);
connect[parent[i][j - 1]].push_back(parent[i][j]);
}
if (i < h - 1 && s[i + 1][j] == '.') {
connect[parent[i][j]].push_back(parent[i + 1][j]);
connect[parent[i + 1][j]].push_back(parent[i][j]);
}
if (j < w - 1 && s[i][j + 1] == '.') {
connect[parent[i][j]].push_back(parent[i][j + 1]);
connect[parent[i][j + 1]].push_back(parent[i][j]);
}
}
}
}
rep(i, h) {
rep(j, w) { dfs1(i * w + j); }
}
/*
cerr<<endl;
rep(i,h){
rep(j,w){
dfs1(i*w+j);
cerr<<parent[i][j]<<' ';
}
cerr<<endl;
}
*/
rep(i, h) {
rep(j, w) {
for (int l = -2; l <= 2; l++) {
for (int r = -2; r <= 2; r++) {
if (i + l >= 0 && i + l <= h - 1 && j + r >= 0 && j + r <= w - 1) {
if (s[i + l][j + r] == '.' && s[i][j] == '.' &&
parent[i + l][j + r] != parent[i][j]) {
to[parent[i + l][j + r]].push_back(parent[i][j]);
to[parent[i][j]].push_back(parent[i + l][j + r]);
}
}
}
}
}
}
int st = parent[ch][cw];
int gl = parent[dh][dw];
// cerr<<st<<' '<<gl<<endl;
if (st == gl) {
cout << 0 << endl;
return 0;
}
dist[st] = 0;
qu.push(st);
while (!qu.empty()) {
int u = qu.front();
qu.pop();
for (int v : to[u]) {
if (dist[v] > dist[u] + 1) {
dist[v] = dist[u] + 1;
qu.push(v);
}
}
}
int ans = dist[gl];
if (dist[gl] == INF) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
#define ALL(a) a.begin(), a.end()
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<int, int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int visited[1000005];
vector<int> to[1000005];
vector<int> connect[1000005];
int color[1000005];
int parent[1005][1005];
int dist[1000005];
int w, h;
void dfs1(int u) {
color[u] = 1;
for (int v : connect[u]) {
if (color[v] == 0) {
parent[v / w][v % w] = parent[u / w][u % w];
dfs1(v);
}
}
color[u] = 2;
return;
}
/*
int dfs(int now,int gl,int cnt){
visited[now]=1;
for(int v:to[now]){
if(v==gl){
return cnt+1;
}
if(visited[v]==0){
return dfs(v,gl,cnt+1);
}
}
return -1;
}
*/
int main() {
cin >> h >> w;
int ch, cw;
cin >> ch >> cw;
ch--;
cw--;
int dh, dw;
cin >> dh >> dw;
dh--;
dw--;
string s[h];
rep(i, h) { cin >> s[i]; }
rep(i, 1000001) { dist[i] = INF; }
rep(i, h) {
rep(j, w) {
if (s[i][j] == '.') {
parent[i][j] = i * w + j;
} else {
parent[i][j] = INF;
}
}
}
rep(i, h) {
rep(j, w) {
if (s[i][j] == '.') {
if (i > 0 && s[i - 1][j] == '.') {
connect[parent[i][j]].push_back(parent[i - 1][j]);
connect[parent[i - 1][j]].push_back(parent[i][j]);
}
if (j > 0 && s[i][j - 1] == '.') {
connect[parent[i][j]].push_back(parent[i][j - 1]);
connect[parent[i][j - 1]].push_back(parent[i][j]);
}
if (i < h - 1 && s[i + 1][j] == '.') {
connect[parent[i][j]].push_back(parent[i + 1][j]);
connect[parent[i + 1][j]].push_back(parent[i][j]);
}
if (j < w - 1 && s[i][j + 1] == '.') {
connect[parent[i][j]].push_back(parent[i][j + 1]);
connect[parent[i][j + 1]].push_back(parent[i][j]);
}
}
}
}
rep(i, h) {
rep(j, w) { dfs1(i * w + j); }
}
/*
cerr<<endl;
rep(i,h){
rep(j,w){
dfs1(i*w+j);
cerr<<parent[i][j]<<' ';
}
cerr<<endl;
}
*/
rep(i, h) {
rep(j, w) {
for (int l = -2; l <= 2; l++) {
for (int r = -2; r <= 2; r++) {
if (i + l >= 0 && i + l <= h - 1 && j + r >= 0 && j + r <= w - 1) {
if (s[i + l][j + r] == '.' && s[i][j] == '.' &&
parent[i + l][j + r] != parent[i][j]) {
to[parent[i + l][j + r]].push_back(parent[i][j]);
to[parent[i][j]].push_back(parent[i + l][j + r]);
}
}
}
}
}
}
int st = parent[ch][cw];
int gl = parent[dh][dw];
// cerr<<st<<' '<<gl<<endl;
if (st == gl) {
cout << 0 << endl;
return 0;
}
dist[st] = 0;
qu.push(st);
while (!qu.empty()) {
int u = qu.front();
qu.pop();
for (int v : to[u]) {
if (dist[v] > dist[u] + 1) {
dist[v] = dist[u] + 1;
qu.push(v);
}
}
}
int ans = dist[gl];
if (dist[gl] == INF) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
return 0;
} | replace | 82 | 83 | 82 | 83 | 0 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;
const ll MAX = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
ll H, W, h1, w1, h2, w2, ans = -1, tmp = 0;
string S;
ll dy[] = {1, 0, -1, 0};
ll dx[] = {0, 1, 0, -1};
vector<ll> visited;
vector<vector<ll>> color, G;
vector<vector<char>> A;
ll dfs(ll h, ll w, ll id) {
for (int i = 0; i < 4; i++) {
int ny = h + dy[i], nx = w + dx[i];
if (!(0 <= ny && ny < H && 0 <= nx && nx < W))
continue;
if (A[ny][nx] == '.' && color[ny][nx] == -1) {
color[ny][nx] = id;
dfs(ny, nx, id);
}
}
}
void bfs(ll bd1, ll bd2) {
queue<pair<ll, ll>> Q;
Q.push(make_pair(bd1, 0));
while (!Q.empty()) {
pair<ll, ll> p = Q.front();
Q.pop();
if (visited[p.first] != -1)
continue;
visited[p.first] = p.second;
if (p.first == bd2) {
ans = p.second;
break;
}
for (int i = 0; i < G[p.first].size(); i++) {
if (visited[G[p.first][i]] == -1)
Q.push(make_pair(G[p.first][i], p.second + 1));
}
}
}
signed main() {
cin >> H >> W >> h1 >> w1 >> h2 >> w2;
h1--;
w1--;
h2--;
w2--;
A.resize(H);
color.resize(H);
for (int i = 0; i < H; i++) {
color[i].resize(W, -1);
for (int j = 0; j < W; j++) {
char a;
cin >> a;
A[i].push_back(a);
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (A[i][j] == '.' && color[i][j] == -1) {
color[i][j] = tmp;
dfs(i, j, tmp);
tmp++;
}
}
}
G.resize(tmp);
visited.resize(tmp, -1);
// cout << "OK" << endl;
/*
for(int i=0;i<H;i++) {
for(int j=0;j<W;j++) {
cout << color[i][j] << " ";
}
cout << endl;
}
*/
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
for (int l = i - 2; l < i + 3; l++) {
if (l >= 0 && l < H) {
for (int k = j - 2; k < j + 3; k++) {
if (k >= 0 && k < W) {
if (color[l][k] != -1 && color[i][j] != -1 &&
color[l][k] != color[i][j]) {
G[color[i][j]].push_back(color[l][k]);
G[color[l][k]].push_back(color[i][j]);
}
}
}
}
}
}
}
// cout << "OK" << endl;
if (color[h1][w1] != -1 && color[h2][w2] != -1)
bfs(color[h1][w1], color[h2][w2]);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;
const ll MAX = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
ll H, W, h1, w1, h2, w2, ans = -1, tmp = 0;
string S;
ll dy[] = {1, 0, -1, 0};
ll dx[] = {0, 1, 0, -1};
vector<ll> visited;
vector<vector<ll>> color, G;
vector<vector<char>> A;
void dfs(ll h, ll w, ll id) {
for (int i = 0; i < 4; i++) {
int ny = h + dy[i], nx = w + dx[i];
if (!(0 <= ny && ny < H && 0 <= nx && nx < W))
continue;
if (A[ny][nx] == '.' && color[ny][nx] == -1) {
color[ny][nx] = id;
dfs(ny, nx, id);
}
}
}
void bfs(ll bd1, ll bd2) {
queue<pair<ll, ll>> Q;
Q.push(make_pair(bd1, 0));
while (!Q.empty()) {
pair<ll, ll> p = Q.front();
Q.pop();
if (visited[p.first] != -1)
continue;
visited[p.first] = p.second;
if (p.first == bd2) {
ans = p.second;
break;
}
for (int i = 0; i < G[p.first].size(); i++) {
if (visited[G[p.first][i]] == -1)
Q.push(make_pair(G[p.first][i], p.second + 1));
}
}
}
signed main() {
cin >> H >> W >> h1 >> w1 >> h2 >> w2;
h1--;
w1--;
h2--;
w2--;
A.resize(H);
color.resize(H);
for (int i = 0; i < H; i++) {
color[i].resize(W, -1);
for (int j = 0; j < W; j++) {
char a;
cin >> a;
A[i].push_back(a);
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (A[i][j] == '.' && color[i][j] == -1) {
color[i][j] = tmp;
dfs(i, j, tmp);
tmp++;
}
}
}
G.resize(tmp);
visited.resize(tmp, -1);
// cout << "OK" << endl;
/*
for(int i=0;i<H;i++) {
for(int j=0;j<W;j++) {
cout << color[i][j] << " ";
}
cout << endl;
}
*/
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
for (int l = i - 2; l < i + 3; l++) {
if (l >= 0 && l < H) {
for (int k = j - 2; k < j + 3; k++) {
if (k >= 0 && k < W) {
if (color[l][k] != -1 && color[i][j] != -1 &&
color[l][k] != color[i][j]) {
G[color[i][j]].push_back(color[l][k]);
G[color[l][k]].push_back(color[i][j]);
}
}
}
}
}
}
}
// cout << "OK" << endl;
if (color[h1][w1] != -1 && color[h2][w2] != -1)
bfs(color[h1][w1], color[h2][w2]);
cout << ans << endl;
return 0;
} | replace | 18 | 19 | 18 | 19 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\n"
using namespace std;
typedef long long ll;
const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;
int main() {
int h, w;
cin >> h >> w;
int ch, cw, dh, dw;
cin >> ch >> cw;
cin >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
vector<string> s(h);
rep(i, h) { cin >> s[i]; }
deque<pair<int, pair<int, int>>> deq;
vector<vector<int>> dist(h, vector<int>(w, h * w));
deq.push_front(make_pair(0, make_pair(ch, cw)));
while (!deq.empty()) {
int tmpd = deq.front().first;
int tmpy = deq.front().second.first;
int tmpx = deq.front().second.second;
deq.pop_front();
if (tmpd < dist[tmpy][tmpx]) {
cerr << tmpy << " " << tmpx << " " << tmpd << endl;
dist[tmpy][tmpx] = tmpd;
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int nexty = tmpy + i;
int nextx = tmpx + j;
if (0 <= nexty && nexty < h && 0 <= nextx && nextx < w &&
s[nexty][nextx] == '.') {
if (abs(i) + abs(j) == 0) {
} else if (abs(i) + abs(j) == 1) {
deq.push_front(make_pair(tmpd, make_pair(nexty, nextx)));
} else {
deq.push_back(make_pair(tmpd + 1, make_pair(nexty, nextx)));
}
}
}
}
}
}
cout << (dist[dh][dw] == h * w ? -1 : dist[dh][dw]) << endl;
} | #include <bits/stdc++.h>
#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\n"
using namespace std;
typedef long long ll;
const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;
int main() {
int h, w;
cin >> h >> w;
int ch, cw, dh, dw;
cin >> ch >> cw;
cin >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
vector<string> s(h);
rep(i, h) { cin >> s[i]; }
deque<pair<int, pair<int, int>>> deq;
vector<vector<int>> dist(h, vector<int>(w, h * w));
deq.push_front(make_pair(0, make_pair(ch, cw)));
while (!deq.empty()) {
int tmpd = deq.front().first;
int tmpy = deq.front().second.first;
int tmpx = deq.front().second.second;
deq.pop_front();
if (tmpd < dist[tmpy][tmpx]) {
dist[tmpy][tmpx] = tmpd;
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int nexty = tmpy + i;
int nextx = tmpx + j;
if (0 <= nexty && nexty < h && 0 <= nextx && nextx < w &&
s[nexty][nextx] == '.') {
if (abs(i) + abs(j) == 0) {
} else if (abs(i) + abs(j) == 1) {
deq.push_front(make_pair(tmpd, make_pair(nexty, nextx)));
} else {
deq.push_back(make_pair(tmpd + 1, make_pair(nexty, nextx)));
}
}
}
}
}
}
cout << (dist[dh][dw] == h * w ? -1 : dist[dh][dw]) << endl;
} | delete | 44 | 45 | 44 | 44 | TLE | |
p02579 | C++ | Time Limit Exceeded | /* Aa^~ kokoro ga pyonpyon suru n jaa^~
// ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```
```?Wfpppbpbbpbbpbbbkbkk
// ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` `` ` ` ` ` ` ` ` ` `
dppbbkkkkkkkkkkqkqkk
// HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` ``
````(Wpbkkkkkkkkqkkkkkqk
// mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` ``
```(yppbkkkkkkkkkkkkkkk
// ggmgmmqqqH$ ``````````....````` ` ````````.`````` ``
``````.yfpppbbbbkkkkqqqqqH
// gmmmmmqqqkW<```` `````...````` .,.` ````....````` ```````
(Wbqqmgmmgmggggggggg
// qmmmqqqqkkWk.``````````````````` ;:<``
`````.`````````````-_<-?WHHqmmmmmmgmmgggg
// @@@@@@@gggHH6- ``````````````` `` _ `` ````````````````
._~~_.`-?Wkqmmmmmmmggg@g
// @@@@g@gggHY~.-<_- ``````````````````````````````````
._~~(<-``.`.(WHqqqmmggggmmm
// @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.``
OHHWUWHmqHWXW
// gggggmqK1.``..~.. _<<+-(____.. ```````` ..__~~_((<<!.`.``` .``.``
j0C1XUHmHIdW
// ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..```
jOuWHHqHIdH
// gmmqkW!`(_ J>` `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__``` zXWHg@HkXH
// gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. ` _ ?&._.I`_``````
.XyVfppppW
// HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd}
.`..````jqg@@@@@@
// XHWZ{..<Jk~!.`..
(<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH
// bkWt~.-jCz(_..`.(+<.(;<
._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH
// WUUO..(f.(c...__+z<-(+~`
_-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk
//
kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW
//
@@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX
// @@mR.(j:..~.._<z!`.(>~```
~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW
//
@gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH
//
mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@
//
qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g
// HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~``
(jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg
// 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ````
d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq
// X0XWHKXXw$<(z<.( ``
WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W
//
XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy
// kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<``
(v!`._..(??_.(1._.._=dUOOzzzwX
// @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.``
:.~+=?~.(;_(...jdQQQQQkkk
//
H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@
//
@@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g
//
@H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm
//
@@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y
//
@@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_
//
MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz
//
HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk
//
HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH
//
rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy
// HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%``
(Ctwwtz_~.<(Wg@H
// NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ `
_``-zwrt1~~_<(MNM
// MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>`
~````.OXtt>~._<?MM
*/
#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
return is >> p.FF >> p.SS;
}
template <class T> istream &operator>>(istream &is, vector<T> &xs) {
for (auto &x : xs)
is >> x;
return is;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
return os << p.FF << " " << p.SS;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) {
for (unsigned int i = 0; i < xs.size(); ++i)
os << (i ? " " : "") << xs[i];
return os;
}
template <class T> void maxi(T &x, T y) {
if (x < y)
x = y;
}
template <class T> void mini(T &x, T y) {
if (x > y)
x = y;
}
void debug(istringstream &) {}
template <char sep = ',', class Head, class... Tail>
void debug(istringstream &iss, Head &&head, Tail &&...tail) {
string name;
getline(iss, name, ',');
cout << sep << name << "=" << head;
debug(iss, forward<Tail>(tail)...);
}
#ifdef PYONPOI
#define DEBUG(...) \
do { \
istringstream ss(#__VA_ARGS__); \
debug<' '>(ss, __VA_ARGS__); \
cout << endl; \
} while (0)
#else
#define DEBUG
#endif
void init_io(int argc, char *argv[]) {
cin.tie(0);
ios_base::sync_with_stdio(false);
#ifdef PYONPOI
if (argc > 1) {
ifstream *ifs = new ifstream();
ifs->open(argv[1]);
cin.rdbuf(ifs->rdbuf());
}
#endif
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int main(int argc, char *argv[]) {
init_io(argc, argv);
int H, W;
cin >> H >> W;
PII S, T;
cin >> S >> T;
S.FF--;
S.SS--;
T.FF--;
T.SS--;
VVI obs(H, VI(W));
REP(y, H) REP(x, W) {
char c;
cin >> c;
obs[y][x] = (c == '#');
}
const int INF = 1e9;
VVI dist(H, VI(W, INF));
deque<PII> dq;
dq.push_back(S);
dist[S.FF][S.SS] = 0;
auto isin = [&](PII p) {
return 0 <= p.FF && p.FF < H && 0 <= p.SS && p.SS < W;
};
while (!dq.empty()) {
auto p = dq.front();
dq.pop_front();
int cur = dist[p.FF][p.SS];
REP(d, 4) {
PII np = p;
np.FF += dy[d];
np.SS += dx[d];
if (!isin(np) || obs[np.FF][np.SS] || dist[np.FF][np.SS] <= cur)
continue;
dist[np.FF][np.SS] = cur;
dq.push_front(np);
}
FOR(ty, -2, 2 + 1) FOR(tx, -2, 2 + 1) {
PII np = p;
np.FF += ty;
np.SS += tx;
if (!isin(np) || obs[np.FF][np.SS] || dist[np.FF][np.SS] <= cur + 1)
continue;
dist[np.FF][np.SS] = cur + 1;
dq.push_front(np);
}
}
int ans = dist[T.FF][T.SS];
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| /* Aa^~ kokoro ga pyonpyon suru n jaa^~
// ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```
```?Wfpppbpbbpbbpbbbkbkk
// ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` `` ` ` ` ` ` ` ` ` `
dppbbkkkkkkkkkkqkqkk
// HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` ``
````(Wpbkkkkkkkkqkkkkkqk
// mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` ``
```(yppbkkkkkkkkkkkkkkk
// ggmgmmqqqH$ ``````````....````` ` ````````.`````` ``
``````.yfpppbbbbkkkkqqqqqH
// gmmmmmqqqkW<```` `````...````` .,.` ````....````` ```````
(Wbqqmgmmgmggggggggg
// qmmmqqqqkkWk.``````````````````` ;:<``
`````.`````````````-_<-?WHHqmmmmmmgmmgggg
// @@@@@@@gggHH6- ``````````````` `` _ `` ````````````````
._~~_.`-?Wkqmmmmmmmggg@g
// @@@@g@gggHY~.-<_- ``````````````````````````````````
._~~(<-``.`.(WHqqqmmggggmmm
// @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.``
OHHWUWHmqHWXW
// gggggmqK1.``..~.. _<<+-(____.. ```````` ..__~~_((<<!.`.``` .``.``
j0C1XUHmHIdW
// ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..```
jOuWHHqHIdH
// gmmqkW!`(_ J>` `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__``` zXWHg@HkXH
// gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. ` _ ?&._.I`_``````
.XyVfppppW
// HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd}
.`..````jqg@@@@@@
// XHWZ{..<Jk~!.`..
(<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH
// bkWt~.-jCz(_..`.(+<.(;<
._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH
// WUUO..(f.(c...__+z<-(+~`
_-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk
//
kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW
//
@@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX
// @@mR.(j:..~.._<z!`.(>~```
~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW
//
@gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH
//
mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@
//
qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g
// HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~``
(jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg
// 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ````
d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq
// X0XWHKXXw$<(z<.( ``
WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W
//
XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy
// kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<``
(v!`._..(??_.(1._.._=dUOOzzzwX
// @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.``
:.~+=?~.(;_(...jdQQQQQkkk
//
H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@
//
@@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g
//
@H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm
//
@@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y
//
@@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_
//
MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz
//
HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk
//
HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH
//
rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy
// HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%``
(Ctwwtz_~.<(Wg@H
// NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ `
_``-zwrt1~~_<(MNM
// MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>`
~````.OXtt>~._<?MM
*/
#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
return is >> p.FF >> p.SS;
}
template <class T> istream &operator>>(istream &is, vector<T> &xs) {
for (auto &x : xs)
is >> x;
return is;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
return os << p.FF << " " << p.SS;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) {
for (unsigned int i = 0; i < xs.size(); ++i)
os << (i ? " " : "") << xs[i];
return os;
}
template <class T> void maxi(T &x, T y) {
if (x < y)
x = y;
}
template <class T> void mini(T &x, T y) {
if (x > y)
x = y;
}
void debug(istringstream &) {}
template <char sep = ',', class Head, class... Tail>
void debug(istringstream &iss, Head &&head, Tail &&...tail) {
string name;
getline(iss, name, ',');
cout << sep << name << "=" << head;
debug(iss, forward<Tail>(tail)...);
}
#ifdef PYONPOI
#define DEBUG(...) \
do { \
istringstream ss(#__VA_ARGS__); \
debug<' '>(ss, __VA_ARGS__); \
cout << endl; \
} while (0)
#else
#define DEBUG
#endif
void init_io(int argc, char *argv[]) {
cin.tie(0);
ios_base::sync_with_stdio(false);
#ifdef PYONPOI
if (argc > 1) {
ifstream *ifs = new ifstream();
ifs->open(argv[1]);
cin.rdbuf(ifs->rdbuf());
}
#endif
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int main(int argc, char *argv[]) {
init_io(argc, argv);
int H, W;
cin >> H >> W;
PII S, T;
cin >> S >> T;
S.FF--;
S.SS--;
T.FF--;
T.SS--;
VVI obs(H, VI(W));
REP(y, H) REP(x, W) {
char c;
cin >> c;
obs[y][x] = (c == '#');
}
const int INF = 1e9;
VVI dist(H, VI(W, INF));
deque<PII> dq;
dq.push_back(S);
dist[S.FF][S.SS] = 0;
auto isin = [&](PII p) {
return 0 <= p.FF && p.FF < H && 0 <= p.SS && p.SS < W;
};
while (!dq.empty()) {
auto p = dq.front();
dq.pop_front();
int cur = dist[p.FF][p.SS];
REP(d, 4) {
PII np = p;
np.FF += dy[d];
np.SS += dx[d];
if (!isin(np) || obs[np.FF][np.SS] || dist[np.FF][np.SS] <= cur)
continue;
dist[np.FF][np.SS] = cur;
dq.push_front(np);
}
FOR(ty, -2, 2 + 1) FOR(tx, -2, 2 + 1) {
PII np = p;
np.FF += ty;
np.SS += tx;
if (!isin(np) || obs[np.FF][np.SS] || dist[np.FF][np.SS] <= cur + 1)
continue;
dist[np.FF][np.SS] = cur + 1;
dq.push_back(np);
}
}
int ans = dist[T.FF][T.SS];
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| replace | 233 | 234 | 233 | 234 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define PI 3.14159265359
typedef long long ll;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
using namespace std;
ll llmin(ll x, ll y) {
if (x < y)
return x;
return y;
}
ll llmax(ll x, ll y) {
if (x > y)
return x;
return y;
}
char grid[1001][1001];
bool visited[1001][1001];
queue<pair<int, int>> que;
queue<pair<int, int>> que2;
int h, w, ans;
int ch, cw, gh, gw;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
bool is_range(int nowx, int nowy) {
if (nowx <= h && nowx >= 0 && nowy <= w && nowy >= 0)
return true;
return false;
}
int main() {
cin >> h >> w;
cin >> ch >> cw >> gh >> gw;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> grid[i][j];
}
}
que.push(make_pair(ch, cw));
while (1) {
if (que.empty() && que2.empty()) {
cout << -1 << endl;
return 0;
}
while (!que.empty()) {
pair<int, int> p = que.front();
que.pop();
int nx = p.first, ny = p.second;
visited[nx][ny] = true;
que2.push(make_pair(nx, ny));
if (nx == gh && ny == gw) {
cout << ans << endl;
return 0;
}
for (int i = 0; i < 4; i++) {
if (is_range(nx + dx[i], ny + dy[i])) {
if (!visited[nx + dx[i]][ny + dy[i]] &&
grid[nx + dx[i]][ny + dy[i]] == '.') {
visited[nx + dx[i]][ny + dy[i]] = true;
que.push(make_pair(nx + dx[i], ny + dy[i]));
que2.push(make_pair(nx + dx[i], ny + dy[i]));
}
}
}
}
while (!que2.empty()) {
pair<int, int> p = que2.front();
que2.pop();
int nx = p.first, ny = p.second;
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
if (is_range(nx + i, ny + j)) {
if (!visited[nx + i][ny + j] && grid[nx + i][ny + j] == '.') {
que.push(make_pair(nx + i, ny + j));
}
}
}
}
}
ans++;
for (int i = 0; i <= 1000; i++) {
for (int j = 0; j <= 1000; j++) {
visited[i][j] = false;
}
}
}
return 0;
}
| #include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define PI 3.14159265359
typedef long long ll;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
using namespace std;
ll llmin(ll x, ll y) {
if (x < y)
return x;
return y;
}
ll llmax(ll x, ll y) {
if (x > y)
return x;
return y;
}
char grid[1001][1001];
bool visited[1001][1001];
queue<pair<int, int>> que;
queue<pair<int, int>> que2;
int h, w, ans;
int ch, cw, gh, gw;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
bool is_range(int nowx, int nowy) {
if (nowx <= h && nowx >= 0 && nowy <= w && nowy >= 0)
return true;
return false;
}
int main() {
cin >> h >> w;
cin >> ch >> cw >> gh >> gw;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> grid[i][j];
}
}
que.push(make_pair(ch, cw));
while (1) {
if (que.empty() && que2.empty()) {
cout << -1 << endl;
return 0;
}
while (!que.empty()) {
pair<int, int> p = que.front();
que.pop();
int nx = p.first, ny = p.second;
visited[nx][ny] = true;
que2.push(make_pair(nx, ny));
if (nx == gh && ny == gw) {
cout << ans << endl;
return 0;
}
for (int i = 0; i < 4; i++) {
if (is_range(nx + dx[i], ny + dy[i])) {
if (!visited[nx + dx[i]][ny + dy[i]] &&
grid[nx + dx[i]][ny + dy[i]] == '.') {
visited[nx + dx[i]][ny + dy[i]] = true;
que.push(make_pair(nx + dx[i], ny + dy[i]));
que2.push(make_pair(nx + dx[i], ny + dy[i]));
}
}
}
}
while (!que2.empty()) {
pair<int, int> p = que2.front();
que2.pop();
int nx = p.first, ny = p.second;
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
if (is_range(nx + i, ny + j)) {
if (!visited[nx + i][ny + j] && grid[nx + i][ny + j] == '.') {
que.push(make_pair(nx + i, ny + j));
}
}
}
}
}
ans++;
}
return 0;
}
| delete | 95 | 100 | 95 | 95 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
#define each(i, mp) for (auto &i : mp)
const int MAX_W = 500;
const int MAX_H = 500;
const int INF = 1001001;
int h, w, sy, sx, gy, gx;
char maze[MAX_W][MAX_H];
bool reached[MAX_W][MAX_H];
bool isGool = false;
int d[MAX_W][MAX_H];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int bfs() {
deque<P> que;
rep(i, w + 1) rep(j, h + 1) d[i][j] = INF;
que.push_back(P(sx, sy));
d[sx][sy] = 0;
while (!que.empty()) {
P p = que.front();
int now_x = p.first, now_y = p.second;
int now_cost = d[now_x][now_y];
que.pop_front();
// cout << "gx= " << gx << "gy= " << gy << '\n';
// cout << "now_x" << now_x << "now_y" << now_y << "now_cost" << now_cost
// << '\n';
if (now_x == gx && now_y == gy) {
break;
}
rep(i, 4) {
int nx = now_x + dx[i], ny = now_y + dy[i];
// cout << "nx=" << nx << "ny=" << ny << '\n';
if (0 < nx && nx <= w && 0 < ny && ny <= h && maze[nx][ny] != '#' &&
d[nx][ny] > now_cost) {
// 普通の移動
que.push_front(P(nx, ny));
d[nx][ny] = now_cost;
}
}
repr(i, -2, 3) {
repr(j, -2, 3) {
int nx = now_x + i, ny = now_y + j;
if (0 < nx && nx <= w && 0 < ny && ny <= h && maze[nx][ny] != '#' &&
d[nx][ny] > now_cost + 1) {
// ワープ
que.push_back(P(nx, ny));
d[nx][ny] = now_cost + 1;
}
}
}
}
return d[gx][gy];
}
int main() {
cin >> h >> w;
cin >> sy >> sx;
cin >> gy >> gx;
repr(i, 1, h + 1) repr(j, 1, w + 1) cin >> maze[j][i];
// cout << "d[gx][gy]= " << d[gx][gy] << '\n';
ll ans = bfs();
if (ans == INF) {
ans = -1;
}
cout << ans << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
#define each(i, mp) for (auto &i : mp)
const int MAX_W = 5000;
const int MAX_H = 5000;
const int INF = 1001001;
int h, w, sy, sx, gy, gx;
char maze[MAX_W][MAX_H];
bool reached[MAX_W][MAX_H];
bool isGool = false;
int d[MAX_W][MAX_H];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int bfs() {
deque<P> que;
rep(i, w + 1) rep(j, h + 1) d[i][j] = INF;
que.push_back(P(sx, sy));
d[sx][sy] = 0;
while (!que.empty()) {
P p = que.front();
int now_x = p.first, now_y = p.second;
int now_cost = d[now_x][now_y];
que.pop_front();
// cout << "gx= " << gx << "gy= " << gy << '\n';
// cout << "now_x" << now_x << "now_y" << now_y << "now_cost" << now_cost
// << '\n';
if (now_x == gx && now_y == gy) {
break;
}
rep(i, 4) {
int nx = now_x + dx[i], ny = now_y + dy[i];
// cout << "nx=" << nx << "ny=" << ny << '\n';
if (0 < nx && nx <= w && 0 < ny && ny <= h && maze[nx][ny] != '#' &&
d[nx][ny] > now_cost) {
// 普通の移動
que.push_front(P(nx, ny));
d[nx][ny] = now_cost;
}
}
repr(i, -2, 3) {
repr(j, -2, 3) {
int nx = now_x + i, ny = now_y + j;
if (0 < nx && nx <= w && 0 < ny && ny <= h && maze[nx][ny] != '#' &&
d[nx][ny] > now_cost + 1) {
// ワープ
que.push_back(P(nx, ny));
d[nx][ny] = now_cost + 1;
}
}
}
}
return d[gx][gy];
}
int main() {
cin >> h >> w;
cin >> sy >> sx;
cin >> gy >> gx;
repr(i, 1, h + 1) repr(j, 1, w + 1) cin >> maze[j][i];
// cout << "d[gx][gy]= " << d[gx][gy] << '\n';
ll ans = bfs();
if (ans == INF) {
ans = -1;
}
cout << ans << '\n';
return 0;
}
| replace | 8 | 10 | 8 | 10 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);
using tp = tuple<int, int, int>;
int main() {
int H, W, ch, cw, dh, dw;
cin >> H >> W >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
vector<vector<char>> g(H, vector<char>(W));
rep(i, H) rep(j, W) cin >> g[i][j];
vector<vector<int>> seen(H, vector<int>(W, -1));
priority_queue<tp, vector<tp>, greater<tp>> q;
q.push(make_tuple(0, ch, cw));
while (q.size()) {
auto now = q.top();
q.pop();
int nowc = get<0>(now), nowh = get<1>(now), noww = get<2>(now);
if (seen[nowh][noww] == -1)
seen[nowh][noww] = nowc;
Rep(i, -2, 3) {
Rep(j, -2, 3) {
int nc = nowc;
int nh = nowh + i;
int nw = noww + j;
if (nh < 0 || H <= nh || nw < 0 || W <= nw)
continue;
if (seen[nh][nw] != -1)
continue;
if (g[nh][nw] == '#')
continue;
if (abs(i) + abs(j) > 1)
nc++;
q.push(make_tuple(nc, nh, nw));
}
}
}
// rep(i, H) {rep(j, W) cout << seen[i][j]; cout << endl;}
cout << seen[dh][dw] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);
using tp = tuple<int, int, int>;
int main() {
int H, W, ch, cw, dh, dw;
cin >> H >> W >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
vector<vector<char>> g(H, vector<char>(W));
rep(i, H) rep(j, W) cin >> g[i][j];
vector<vector<int>> seen(H, vector<int>(W, -1));
priority_queue<tp, vector<tp>, greater<tp>> q;
q.push(make_tuple(0, ch, cw));
while (q.size()) {
auto now = q.top();
q.pop();
int nowc = get<0>(now), nowh = get<1>(now), noww = get<2>(now);
if (seen[nowh][noww] != -1)
continue;
seen[nowh][noww] = nowc;
Rep(i, -2, 3) {
Rep(j, -2, 3) {
int nc = nowc;
int nh = nowh + i;
int nw = noww + j;
if (nh < 0 || H <= nh || nw < 0 || W <= nw)
continue;
if (seen[nh][nw] != -1)
continue;
if (g[nh][nw] == '#')
continue;
if (abs(i) + abs(j) > 1)
nc++;
q.push(make_tuple(nc, nh, nw));
}
}
}
// rep(i, H) {rep(j, W) cout << seen[i][j]; cout << endl;}
cout << seen[dh][dw] << endl;
}
| replace | 41 | 43 | 41 | 44 | TLE | |
p02579 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
int main() {
int h, w;
cin >> h >> w;
int si, sj;
cin >> si >> sj;
int ti, tj;
cin >> ti >> tj;
si--;
sj--;
ti--;
tj--;
vector<string> s(h);
rep(i, h) { cin >> s[i]; }
const int INF = 1e9;
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<P> q;
dist[si][sj] = 0;
q.emplace_back(si, sj);
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
int d = dist[i][j];
q.pop_front();
if (dist[i][j] != d) {
continue;
}
rep(v, 4) {
int ni = i + di[v];
int nj = j + dj[v];
if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
continue;
}
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d) {
continue;
}
dist[nj][nj] = d;
q.emplace_front(ni, nj);
}
for (int ei = -2; ei <= 2; ei++) {
for (int ej = -2; ej <= 2; ej++) {
int ni = i + ei;
int nj = j + ej;
if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
continue;
}
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d + 1)
continue;
dist[ni][nj] = d + 1;
q.emplace_back(ni, nj);
}
}
}
int ans = dist[ti][tj];
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| #include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
int main() {
int h, w;
cin >> h >> w;
int si, sj;
cin >> si >> sj;
int ti, tj;
cin >> ti >> tj;
si--;
sj--;
ti--;
tj--;
vector<string> s(h);
rep(i, h) { cin >> s[i]; }
const int INF = 1e9;
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<P> q;
dist[si][sj] = 0;
q.emplace_back(si, sj);
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
int d = dist[i][j];
q.pop_front();
if (dist[i][j] != d) {
continue;
}
rep(v, 4) {
int ni = i + di[v];
int nj = j + dj[v];
if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
continue;
}
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d) {
continue;
}
dist[ni][nj] = d;
q.emplace_front(ni, nj);
}
for (int ei = -2; ei <= 2; ei++) {
for (int ej = -2; ej <= 2; ej++) {
int ni = i + ei;
int nj = j + ej;
if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
continue;
}
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d + 1)
continue;
dist[ni][nj] = d + 1;
q.emplace_back(ni, nj);
}
}
}
int ans = dist[ti][tj];
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| replace | 54 | 55 | 54 | 55 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
int main() {
int H, W, Ch, Cw, Dh, Dw;
std::cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
std::vector<std::string> S(H);
for (int i = 0; i < H; i++) {
std::cin >> S[i];
}
std::vector<std::vector<int>> warp_min(H, std::vector<int>(W, 1e9));
std::deque<int> H_pos;
std::deque<int> W_pos;
std::deque<int> warp_cnt;
H_pos.push_back(Ch - 1);
W_pos.push_back(Cw - 1);
warp_cnt.push_back(0);
while (!H_pos.empty()) {
int cur_H = H_pos[0];
int cur_W = W_pos[0];
int cur_warp = warp_cnt[0];
H_pos.pop_front();
W_pos.pop_front();
warp_cnt.pop_front();
if (cur_H < 0 || cur_H >= H) {
continue;
}
if (cur_W < 0 || cur_W >= W) {
continue;
}
if (warp_min[cur_H][cur_W] <= cur_warp) {
continue;
}
if (S[cur_H][cur_W] == '#') {
continue;
}
warp_min[cur_H][cur_W] = cur_warp;
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int dist = abs(i) + abs(j);
if (dist == 0) {
continue;
} else if (dist == 1) {
H_pos.push_back(cur_H + i);
W_pos.push_back(cur_W + j);
warp_cnt.push_back(cur_warp);
} else {
H_pos.push_back(cur_H + i);
W_pos.push_back(cur_W + j);
warp_cnt.push_back(cur_warp + 1);
}
}
}
}
// for(int i=0; i<H; i++){
// for(int j=0; j<W; j++){
// std::cout << warp_min[i][j] << " ";
// }
// std::cout << std::endl;
// }
if (warp_min[Dh - 1][Dw - 1] == 1e9) {
std::cout << -1 << std::endl;
} else {
std::cout << warp_min[Dh - 1][Dw - 1] << std::endl;
}
return 0;
}
| #include <bits/stdc++.h>
int main() {
int H, W, Ch, Cw, Dh, Dw;
std::cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
std::vector<std::string> S(H);
for (int i = 0; i < H; i++) {
std::cin >> S[i];
}
std::vector<std::vector<int>> warp_min(H, std::vector<int>(W, 1e9));
std::deque<int> H_pos;
std::deque<int> W_pos;
std::deque<int> warp_cnt;
H_pos.push_back(Ch - 1);
W_pos.push_back(Cw - 1);
warp_cnt.push_back(0);
while (!H_pos.empty()) {
int cur_H = H_pos[0];
int cur_W = W_pos[0];
int cur_warp = warp_cnt[0];
H_pos.pop_front();
W_pos.pop_front();
warp_cnt.pop_front();
if (cur_H < 0 || cur_H >= H) {
continue;
}
if (cur_W < 0 || cur_W >= W) {
continue;
}
if (warp_min[cur_H][cur_W] <= cur_warp) {
continue;
}
if (S[cur_H][cur_W] == '#') {
continue;
}
warp_min[cur_H][cur_W] = cur_warp;
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int dist = abs(i) + abs(j);
if (dist == 0) {
continue;
} else if (dist == 1) {
H_pos.push_front(cur_H + i);
W_pos.push_front(cur_W + j);
warp_cnt.push_front(cur_warp);
} else {
H_pos.push_back(cur_H + i);
W_pos.push_back(cur_W + j);
warp_cnt.push_back(cur_warp + 1);
}
}
}
}
// for(int i=0; i<H; i++){
// for(int j=0; j<W; j++){
// std::cout << warp_min[i][j] << " ";
// }
// std::cout << std::endl;
// }
if (warp_min[Dh - 1][Dw - 1] == 1e9) {
std::cout << -1 << std::endl;
} else {
std::cout << warp_min[Dh - 1][Dw - 1] << std::endl;
}
return 0;
}
| replace | 49 | 52 | 49 | 52 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const int MOD = 1000000007;
const double PI = acos(-1);
int H, W, Ch, Cw, Dh, Dw;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
bool movable_a(vector<vector<char>> &S, int x, int y) {
if (x < 0 || x >= W || y < 0 || y >= H) {
return false;
} else if (S[y][x] == '.') {
return true;
} else {
return false;
}
}
void bfs(vector<vector<char>> &S, vector<vector<int>> &seen) {
queue<P> que;
set<P> warp;
que.push(make_pair(Ch, Cw));
int cnt = 0;
while (1) {
P now = que.front();
int now_x, now_y;
now_x = now.second;
now_y = now.first;
que.pop();
rep(i, 4) {
int x, y;
x = now_x + dx[i];
y = now_y + dy[i];
if (movable_a(S, x, y)) {
if (seen[y][x] == -1) {
que.push(make_pair(y, x));
seen[y][x] = cnt;
}
}
}
rep(h, 5) {
rep(w, 5) {
int xx, yy;
xx = now_x + w - 2;
yy = now_y + h - 2;
if (movable_a(S, xx, yy)) {
if (seen[yy][xx] == -1) {
warp.insert(make_pair(yy, xx));
}
}
}
}
if (que.size() == 0) {
cnt++;
for (auto p : warp) {
int xx, yy;
xx = p.second;
yy = p.first;
if (seen[yy][xx] == -1) {
que.push(make_pair(yy, xx));
seen[yy][xx] = cnt;
}
}
}
// cout << que.size() << endl;
if (que.size() == 0) {
break;
}
}
}
int main() {
cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
Ch--, Cw--, Dh--, Dw--;
vector<vector<char>> S(H, vector<char>(W));
vector<vector<int>> seen(H, vector<int>(W, -1));
rep(i, H) {
string tmp;
cin >> tmp;
rep(j, W) { S[i][j] = tmp[j]; }
}
bfs(S, seen);
cout << seen[Dh][Dw] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const int MOD = 1000000007;
const double PI = acos(-1);
int H, W, Ch, Cw, Dh, Dw;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
bool movable_a(vector<vector<char>> &S, int x, int y) {
if (x < 0 || x >= W || y < 0 || y >= H) {
return false;
} else if (S[y][x] == '.') {
return true;
} else {
return false;
}
}
void bfs(vector<vector<char>> &S, vector<vector<int>> &seen) {
queue<P> que;
set<P> warp;
que.push(make_pair(Ch, Cw));
int cnt = 0;
while (1) {
P now = que.front();
int now_x, now_y;
now_x = now.second;
now_y = now.first;
que.pop();
rep(i, 4) {
int x, y;
x = now_x + dx[i];
y = now_y + dy[i];
if (movable_a(S, x, y)) {
if (seen[y][x] == -1) {
que.push(make_pair(y, x));
seen[y][x] = cnt;
}
}
}
rep(h, 5) {
rep(w, 5) {
int xx, yy;
xx = now_x + w - 2;
yy = now_y + h - 2;
if (movable_a(S, xx, yy)) {
if (seen[yy][xx] == -1) {
warp.insert(make_pair(yy, xx));
}
}
}
}
if (que.size() == 0) {
cnt++;
for (auto p : warp) {
int xx, yy;
xx = p.second;
yy = p.first;
if (seen[yy][xx] == -1) {
que.push(make_pair(yy, xx));
seen[yy][xx] = cnt;
}
}
warp.clear();
}
// cout << que.size() << endl;
if (que.size() == 0) {
break;
}
}
}
int main() {
cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
Ch--, Cw--, Dh--, Dw--;
vector<vector<char>> S(H, vector<char>(W));
vector<vector<int>> seen(H, vector<int>(W, -1));
rep(i, H) {
string tmp;
cin >> tmp;
rep(j, W) { S[i][j] = tmp[j]; }
}
bfs(S, seen);
cout << seen[Dh][Dw] << endl;
}
| insert | 73 | 73 | 73 | 74 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18 + 1;
const ll DIV = 1000000007;
// #define TEST
bool inside(int i, int j, long H, long W, vector<vector<char>> mat) {
return (i >= 0 && i < H && j >= 0 && j < W && mat[i][j] == '.');
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
#ifdef TEST
chrono::system_clock::time_point start, end;
start = chrono::system_clock::now();
#endif
long H, W;
cin >> H >> W;
long Cx, Cy;
cin >> Cy >> Cx;
Cy--;
Cx--;
long Dx, Dy;
cin >> Dy >> Dx;
Dy--;
Dx--;
std::vector<std::vector<char>> mat(H, vector<char>(W));
string S;
for (size_t i = 0; i < H; i++) {
cin >> S;
for (size_t j = 0; j < S.size(); j++) {
mat[i][j] = S[j];
}
}
vector<vector<ll>> dist(H, vector<ll>(W, INF));
dist[Cy][Cx] = 0;
deque<pair<int, int>> que;
que.push_front(pair<int, int>(Cy, Cx));
while (que.size()) {
pair<int, int> p = que.front();
que.pop_front();
int y = p.first, x = p.second;
for (int i = y - 2; i <= y + 2; i++) {
for (int j = x - 2; j <= x + 2; j++) {
if (!inside(i, j, H, W, mat))
continue;
int adj = (abs(y - i) + abs(x - j) != 1);
int d = dist[y][x] + adj;
if (d < dist[i][j]) {
dist[i][j] = d;
if (adj)
que.push_back(pair<long, long>(i, j));
else
que.push_front(pair<long, long>(i, j));
}
}
}
}
ll ans = dist[Dy][Dx];
if (ans == INF)
ans = -1;
cout << ans << endl;
#ifdef TEST
end = chrono::system_clock::now();
cerr << static_cast<double>(
chrono::duration_cast<chrono::microseconds>(end - start).count() /
1000.0)
<< "[ms]" << endl;
#endif
return 0;
} | #include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18 + 1;
const ll DIV = 1000000007;
// #define TEST
bool inside(const int &i, const int &j, const long &H, const long &W,
const vector<vector<char>> &mat) {
return (i >= 0 && i < H && j >= 0 && j < W && mat[i][j] == '.');
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
#ifdef TEST
chrono::system_clock::time_point start, end;
start = chrono::system_clock::now();
#endif
long H, W;
cin >> H >> W;
long Cx, Cy;
cin >> Cy >> Cx;
Cy--;
Cx--;
long Dx, Dy;
cin >> Dy >> Dx;
Dy--;
Dx--;
std::vector<std::vector<char>> mat(H, vector<char>(W));
string S;
for (size_t i = 0; i < H; i++) {
cin >> S;
for (size_t j = 0; j < S.size(); j++) {
mat[i][j] = S[j];
}
}
vector<vector<ll>> dist(H, vector<ll>(W, INF));
dist[Cy][Cx] = 0;
deque<pair<int, int>> que;
que.push_front(pair<int, int>(Cy, Cx));
while (que.size()) {
pair<int, int> p = que.front();
que.pop_front();
int y = p.first, x = p.second;
for (int i = y - 2; i <= y + 2; i++) {
for (int j = x - 2; j <= x + 2; j++) {
if (!inside(i, j, H, W, mat))
continue;
int adj = (abs(y - i) + abs(x - j) != 1);
int d = dist[y][x] + adj;
if (d < dist[i][j]) {
dist[i][j] = d;
if (adj)
que.push_back(pair<long, long>(i, j));
else
que.push_front(pair<long, long>(i, j));
}
}
}
}
ll ans = dist[Dy][Dx];
if (ans == INF)
ans = -1;
cout << ans << endl;
#ifdef TEST
end = chrono::system_clock::now();
cerr << static_cast<double>(
chrono::duration_cast<chrono::microseconds>(end - start).count() /
1000.0)
<< "[ms]" << endl;
#endif
return 0;
} | replace | 19 | 20 | 19 | 21 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> T div_floor(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a > 0 ? (a - 1) / b + 1 : a / b;
}
constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;
struct state {
int x, y, d;
state(int x, int y, int d) : x(x), y(y), d(d) {}
};
int h, w;
int sx, sy, gx, gy;
char c[1010][1010];
int dist[1010][1010];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
scanf("%d%d", &h, &w);
scanf("%d%d%d%d", &sx, &sy, &gx, &gy);
--sx;
--sy;
--gx;
--gy;
rep(i, h) rep(j, w) scanf(" %c", &c[i][j]);
rep(i, h) rep(j, w) dist[i][j] = mod;
deque<state> dq;
dist[sx][sy] = 0;
dq.emplace_back(sx, sy, 0);
while (!dq.empty()) {
auto s = dq.front();
dq.pop_front();
if (s.d > dist[s.x][s.y])
continue;
rep(i, 4) {
int nx = s.x + dx[i], ny = s.y + dy[i];
if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == '.') {
if (chmin(dist[nx][ny], s.d)) {
dq.emplace_front(nx, ny, dist[nx][ny]);
}
}
}
for (int nx = s.x - 2; nx <= s.x + 2; ++nx) {
for (int ny = s.y - 2; ny <= s.y + 2; ++ny) {
if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == '.') {
if (chmin(dist[nx][ny], s.d + 1)) {
dq.emplace_front(nx, ny, dist[nx][ny]);
}
}
}
}
}
printf("%d\n", dist[gx][gy] == mod ? -1 : dist[gx][gy]);
} | #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> T div_floor(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
if (b < 0)
a *= -1, b *= -1;
return a > 0 ? (a - 1) / b + 1 : a / b;
}
constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;
struct state {
int x, y, d;
state(int x, int y, int d) : x(x), y(y), d(d) {}
};
int h, w;
int sx, sy, gx, gy;
char c[1010][1010];
int dist[1010][1010];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
scanf("%d%d", &h, &w);
scanf("%d%d%d%d", &sx, &sy, &gx, &gy);
--sx;
--sy;
--gx;
--gy;
rep(i, h) rep(j, w) scanf(" %c", &c[i][j]);
rep(i, h) rep(j, w) dist[i][j] = mod;
deque<state> dq;
dist[sx][sy] = 0;
dq.emplace_back(sx, sy, 0);
while (!dq.empty()) {
auto s = dq.front();
dq.pop_front();
if (s.d > dist[s.x][s.y])
continue;
rep(i, 4) {
int nx = s.x + dx[i], ny = s.y + dy[i];
if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == '.') {
if (chmin(dist[nx][ny], s.d)) {
dq.emplace_front(nx, ny, dist[nx][ny]);
}
}
}
for (int nx = s.x - 2; nx <= s.x + 2; ++nx) {
for (int ny = s.y - 2; ny <= s.y + 2; ++ny) {
if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == '.') {
if (chmin(dist[nx][ny], s.d + 1)) {
dq.emplace_back(nx, ny, dist[nx][ny]);
}
}
}
}
}
printf("%d\n", dist[gx][gy] == mod ? -1 : dist[gx][gy]);
} | replace | 83 | 84 | 83 | 84 | TLE | |
p02579 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// MACROS
#define _crt_secure_no_warnings
#define FREEELO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define mp make_pair
#define INF 0x3F3F3F3F
#define MAX 1001
#define MOD 1000000007
#define ALPHABET 128
#define loop(i, n) for (int i = 1; i <= (n); i++)
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> llll;
typedef pair<int, int> ii;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;
typedef unordered_set<int> usi;
typedef unordered_multiset<int> usmi;
int n, m, cnt = 0;
int sx, sy, fx, fy;
string g[MAX];
bool used[MAX][MAX];
int comp[MAX][MAX];
int dist[MAX];
vector<vector<int>> v;
int check(int i, int j) {
if (i <= 0 || i > n || j <= 0 || j > m)
return 0;
return 1;
}
void dfs(int i, int j) {
if (check(i, j) == 0)
return;
comp[i][j] = cnt;
used[i][j] = 1;
if (!used[i + 1][j] && g[i + 1][j] != '#')
dfs(i + 1, j);
if (!used[i - 1][j] && g[i - 1][j] != '#')
dfs(i - 1, j);
if (!used[i][j + 1] && g[i][j + 1] != '#')
dfs(i, j + 1);
if (!used[i][j - 1] && g[i][j - 1] != '#')
dfs(i, j - 1);
}
int bfs(int source, int final) {
dist[source] = 0;
queue<int> q;
q.push(source);
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int &to : v[cur]) {
if (dist[to] == -1) {
dist[to] = dist[cur] + 1;
q.push(to);
}
}
}
return dist[final];
}
int main() {
FREEELO
cin >> n >> m;
cin >> sx >> sy >> fx >> fy;
for (int i = 1; i <= n; i++) {
cin >> g[i];
g[i] = " " + g[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (comp[i][j] == 0 && g[i][j] != '#' && used[i][j] == 0) {
cnt++;
dfs(i, j);
}
}
}
map<ii, int> edge;
v.resize(cnt + 2);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (g[i][j] == '#')
continue;
for (int k = i - 2; k <= i + 2; k++) {
for (int l = j - 2; l <= j + 2; l++) {
if (check(k, l) && g[k][l] == '.') {
if (edge[{comp[k][l], comp[i][j]}] == 0 &&
comp[k][l] != comp[i][j]) {
edge[{comp[k][l], comp[i][j]}] = 1;
edge[{comp[i][j], comp[k][l]}] = 1;
v[comp[i][j]].pb(comp[k][l]);
v[comp[k][l]].pb(comp[i][j]);
}
}
}
}
}
}
int source = comp[sx][sy];
int final = comp[fx][fy];
memset(dist, -1, sizeof(dist));
cout << bfs(source, final) << endl;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// MACROS
#define _crt_secure_no_warnings
#define FREEELO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define mp make_pair
#define INF 0x3F3F3F3F
#define MAX 1001
#define MOD 1000000007
#define ALPHABET 128
#define loop(i, n) for (int i = 1; i <= (n); i++)
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> llll;
typedef pair<int, int> ii;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;
typedef unordered_set<int> usi;
typedef unordered_multiset<int> usmi;
int n, m, cnt = 0;
int sx, sy, fx, fy;
string g[MAX];
bool used[MAX][MAX];
int comp[MAX][MAX];
int dist[MAX * MAX];
vector<vector<int>> v;
int check(int i, int j) {
if (i <= 0 || i > n || j <= 0 || j > m)
return 0;
return 1;
}
void dfs(int i, int j) {
if (check(i, j) == 0)
return;
comp[i][j] = cnt;
used[i][j] = 1;
if (!used[i + 1][j] && g[i + 1][j] != '#')
dfs(i + 1, j);
if (!used[i - 1][j] && g[i - 1][j] != '#')
dfs(i - 1, j);
if (!used[i][j + 1] && g[i][j + 1] != '#')
dfs(i, j + 1);
if (!used[i][j - 1] && g[i][j - 1] != '#')
dfs(i, j - 1);
}
int bfs(int source, int final) {
dist[source] = 0;
queue<int> q;
q.push(source);
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int &to : v[cur]) {
if (dist[to] == -1) {
dist[to] = dist[cur] + 1;
q.push(to);
}
}
}
return dist[final];
}
int main() {
FREEELO
cin >> n >> m;
cin >> sx >> sy >> fx >> fy;
for (int i = 1; i <= n; i++) {
cin >> g[i];
g[i] = " " + g[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (comp[i][j] == 0 && g[i][j] != '#' && used[i][j] == 0) {
cnt++;
dfs(i, j);
}
}
}
map<ii, int> edge;
v.resize(cnt + 2);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (g[i][j] == '#')
continue;
for (int k = i - 2; k <= i + 2; k++) {
for (int l = j - 2; l <= j + 2; l++) {
if (check(k, l) && g[k][l] == '.') {
if (edge[{comp[k][l], comp[i][j]}] == 0 &&
comp[k][l] != comp[i][j]) {
edge[{comp[k][l], comp[i][j]}] = 1;
edge[{comp[i][j], comp[k][l]}] = 1;
v[comp[i][j]].pb(comp[k][l]);
v[comp[k][l]].pb(comp[i][j]);
}
}
}
}
}
}
int source = comp[sx][sy];
int final = comp[fx][fy];
memset(dist, -1, sizeof(dist));
cout << bfs(source, final) << endl;
}
| replace | 48 | 49 | 48 | 49 | 0 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int H, W, C_h, C_w, D_h, D_w;
cin >> H >> W >> C_h >> C_w >> D_h >> D_w;
C_h--;
C_w--;
D_h--;
D_w--;
char S[H][W];
for (int i = 0; i < H; i++) {
string s;
cin >> s;
for (int j = 0; j < W; j++) {
S[i][j] = s[j];
}
}
int MAX = 100000000;
int G[H][W];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
G[i][j] = -1;
}
}
G[H][W] = 0;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
deque<pair<int, int>> q;
int idx = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (G[i][j] == -1 && S[i][j] == '.') {
G[i][j] = idx;
vector<pair<int, int>> v;
q.push_back(make_pair(i, j));
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop_front();
int x = p.second;
int y = p.first;
for (int k = 0; k < 4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (S[ny][nx] == '.' && G[ny][nx] == -1) {
G[ny][nx] = idx;
q.push_back(make_pair(ny, nx));
}
}
}
idx++;
}
}
}
int ex[] = {1, 1, -1, -1, 2, 2, 2, 2, 2, 1,
1, 0, 0, -1, -1, -2, -2, -2, -2, -2};
int ey[] = {1, -1, 1, -1, -2, -1, 0, 1, 2, 2,
-2, 2, -2, 2, -2, 2, 1, 0, 1, 2};
set<int> adj[idx];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (G[i][j] == -1)
continue;
for (int k = 0; k < 20; k++) {
int nx = j + ex[k];
int ny = i + ey[k];
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (S[ny][nx] == '.' && G[i][j] != G[ny][nx]) {
adj[G[i][j]].insert(G[ny][nx]);
adj[G[ny][nx]].insert(G[i][j]);
}
}
}
}
int L[idx];
for (int i = 0; i < idx; i++) {
L[i] = MAX;
}
L[G[C_h][C_w]] = 0;
deque<int> dq;
dq.push_back(G[C_h][C_w]);
while (!dq.empty()) {
int p = dq.front();
dq.pop_front();
for (int i : adj[p]) {
if (L[p] + 1 < L[i]) {
L[i] = L[p] + 1;
dq.push_back(i);
}
}
}
if (L[G[D_h][D_w]] != MAX) {
cout << L[G[D_h][D_w]] << "\n";
} else {
cout << "-1\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int H, W, C_h, C_w, D_h, D_w;
cin >> H >> W >> C_h >> C_w >> D_h >> D_w;
C_h--;
C_w--;
D_h--;
D_w--;
char S[H][W];
for (int i = 0; i < H; i++) {
string s;
cin >> s;
for (int j = 0; j < W; j++) {
S[i][j] = s[j];
}
}
int MAX = 100000000;
int G[H][W];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
G[i][j] = -1;
}
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
deque<pair<int, int>> q;
int idx = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (G[i][j] == -1 && S[i][j] == '.') {
G[i][j] = idx;
vector<pair<int, int>> v;
q.push_back(make_pair(i, j));
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop_front();
int x = p.second;
int y = p.first;
for (int k = 0; k < 4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (S[ny][nx] == '.' && G[ny][nx] == -1) {
G[ny][nx] = idx;
q.push_back(make_pair(ny, nx));
}
}
}
idx++;
}
}
}
int ex[] = {1, 1, -1, -1, 2, 2, 2, 2, 2, 1,
1, 0, 0, -1, -1, -2, -2, -2, -2, -2};
int ey[] = {1, -1, 1, -1, -2, -1, 0, 1, 2, 2,
-2, 2, -2, 2, -2, 2, 1, 0, 1, 2};
set<int> adj[idx];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (G[i][j] == -1)
continue;
for (int k = 0; k < 20; k++) {
int nx = j + ex[k];
int ny = i + ey[k];
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (S[ny][nx] == '.' && G[i][j] != G[ny][nx]) {
adj[G[i][j]].insert(G[ny][nx]);
adj[G[ny][nx]].insert(G[i][j]);
}
}
}
}
int L[idx];
for (int i = 0; i < idx; i++) {
L[i] = MAX;
}
L[G[C_h][C_w]] = 0;
deque<int> dq;
dq.push_back(G[C_h][C_w]);
while (!dq.empty()) {
int p = dq.front();
dq.pop_front();
for (int i : adj[p]) {
if (L[p] + 1 < L[i]) {
L[i] = L[p] + 1;
dq.push_back(i);
}
}
}
if (L[G[D_h][D_w]] != MAX) {
cout << L[G[D_h][D_w]] << "\n";
} else {
cout << "-1\n";
}
return 0;
} | replace | 26 | 27 | 26 | 27 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
typedef long long ll;
const int INF = 1e9;
typedef pair<int, pair<int, int>> P;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
int H, W, CH, CW, DH, DW;
cin >> H >> W >> CH >> CW >> DH >> DW;
vector<string> maze(H);
CH--;
CW--;
DH--;
DW--;
rep(i, H) cin >> maze[i];
vector<vector<int>> res(H, vector<int>(W, INF));
priority_queue<P> que;
res[CH][CW] = 0;
que.push({0, {CH, CW}});
while (!que.empty()) {
int d = que.top().first;
int y = que.top().second.first, x = que.top().second.second;
que.pop();
if (res[y][x] < d)
continue;
for (int j = -2; j <= 2; j++) {
for (int k = -2; k <= 2; k++) {
if (j == 0 && k == 0)
continue;
int cx = 1;
if (abs(j) + abs(k) == 1)
cx = 0;
int tx = x + j, ty = y + k;
if (tx < 0 || tx >= W || ty < 0 || ty >= H || maze[ty][tx] == '#')
continue;
if (res[ty][tx] > d + cx) {
res[ty][tx] = d + cx;
que.push({res[ty][tx], {ty, tx}});
}
}
}
}
int ans = res[DH][DW];
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
typedef long long ll;
const int INF = 1e9;
typedef pair<int, pair<int, int>> P;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
int H, W, CH, CW, DH, DW;
cin >> H >> W >> CH >> CW >> DH >> DW;
vector<string> maze(H);
CH--;
CW--;
DH--;
DW--;
rep(i, H) cin >> maze[i];
vector<vector<int>> res(H, vector<int>(W, INF));
priority_queue<P, vector<P>, greater<P>> que;
res[CH][CW] = 0;
que.push({0, {CH, CW}});
while (!que.empty()) {
int d = que.top().first;
int y = que.top().second.first, x = que.top().second.second;
que.pop();
if (res[y][x] < d)
continue;
for (int j = -2; j <= 2; j++) {
for (int k = -2; k <= 2; k++) {
if (j == 0 && k == 0)
continue;
int cx = 1;
if (abs(j) + abs(k) == 1)
cx = 0;
int tx = x + j, ty = y + k;
if (tx < 0 || tx >= W || ty < 0 || ty >= H || maze[ty][tx] == '#')
continue;
if (res[ty][tx] > d + cx) {
res[ty][tx] = d + cx;
que.push({res[ty][tx], {ty, tx}});
}
}
}
}
int ans = res[DH][DW];
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| replace | 37 | 38 | 37 | 38 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MAX = 100;
const int INF = 1e9;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int ei[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
const int ej[] = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0,
1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2};
#define PI 3.14159265358979323846264338327950L
#define WHITE 0
#define GRAY 1
#define BLACK 2
int d[MAX][MAX];
void dijkstra(vector<string> s, int h, int w, int x, int y) {
priority_queue<tuple<int, int, int>> PQ;
int color[MAX][MAX];
rep(i, MAX) {
rep(j, MAX) {
d[i][j] = INF;
color[i][j] = WHITE;
}
}
d[y][x] = 0;
PQ.push(make_tuple(0, x, y));
color[y][x] = GRAY;
while (!PQ.empty()) {
tuple<int, int, int> f = PQ.top();
PQ.pop();
int c = get<0>(f);
int xx = get<1>(f);
int yy = get<2>(f);
color[yy][xx] = BLACK;
if (d[yy][xx] < c * (-1))
continue;
rep(v, 4) {
int nx = xx + dj[v];
int ny = yy + di[v];
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (s[ny][nx] == '#')
continue;
if (color[ny][nx] == BLACK)
continue;
if (d[ny][nx] > d[yy][xx]) {
d[ny][nx] = d[yy][xx];
PQ.push(make_tuple(d[ny][nx] * (-1), nx, ny));
color[ny][nx] = GRAY;
}
}
rep(v, 25) {
int nx = xx + ej[v];
int ny = yy + ei[v];
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (s[ny][nx] == '#')
continue;
if (color[ny][nx] == BLACK)
continue;
if (d[ny][nx] > d[yy][xx] + 1) {
d[ny][nx] = d[yy][xx] + 1;
PQ.push(make_tuple(d[ny][nx] * (-1), nx, ny));
color[ny][nx] = GRAY;
}
}
}
}
int main() {
int h, w;
cin >> h >> w;
int sx, sy, tx, ty;
cin >> sy >> sx >> ty >> tx;
sx--;
sy--;
tx--;
ty--;
vector<string> s(h);
rep(i, h) cin >> s[i];
dijkstra(s, h, w, sx, sy);
if (d[ty][tx] == INF) {
cout << -1 << endl;
} else {
cout << d[ty][tx] << endl;
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MAX = 1000;
const int INF = 1e9;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int ei[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
const int ej[] = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0,
1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2};
#define PI 3.14159265358979323846264338327950L
#define WHITE 0
#define GRAY 1
#define BLACK 2
int d[MAX][MAX];
void dijkstra(vector<string> s, int h, int w, int x, int y) {
priority_queue<tuple<int, int, int>> PQ;
int color[MAX][MAX];
rep(i, MAX) {
rep(j, MAX) {
d[i][j] = INF;
color[i][j] = WHITE;
}
}
d[y][x] = 0;
PQ.push(make_tuple(0, x, y));
color[y][x] = GRAY;
while (!PQ.empty()) {
tuple<int, int, int> f = PQ.top();
PQ.pop();
int c = get<0>(f);
int xx = get<1>(f);
int yy = get<2>(f);
color[yy][xx] = BLACK;
if (d[yy][xx] < c * (-1))
continue;
rep(v, 4) {
int nx = xx + dj[v];
int ny = yy + di[v];
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (s[ny][nx] == '#')
continue;
if (color[ny][nx] == BLACK)
continue;
if (d[ny][nx] > d[yy][xx]) {
d[ny][nx] = d[yy][xx];
PQ.push(make_tuple(d[ny][nx] * (-1), nx, ny));
color[ny][nx] = GRAY;
}
}
rep(v, 25) {
int nx = xx + ej[v];
int ny = yy + ei[v];
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (s[ny][nx] == '#')
continue;
if (color[ny][nx] == BLACK)
continue;
if (d[ny][nx] > d[yy][xx] + 1) {
d[ny][nx] = d[yy][xx] + 1;
PQ.push(make_tuple(d[ny][nx] * (-1), nx, ny));
color[ny][nx] = GRAY;
}
}
}
}
int main() {
int h, w;
cin >> h >> w;
int sx, sy, tx, ty;
cin >> sy >> sx >> ty >> tx;
sx--;
sy--;
tx--;
ty--;
vector<string> s(h);
rep(i, h) cin >> s[i];
dijkstra(s, h, w, sx, sy);
if (d[ty][tx] == INF) {
cout << -1 << endl;
} else {
cout << d[ty][tx] << endl;
}
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\n"
i64 dh[4] = {-1, 1, 0, 0}, dw[4] = {0, 0, -1, 1};
int main() {
i64 H, W, Ch, Cw, Dh, Dw;
cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
Ch--;
Cw--;
Dh--;
Dw--;
vector<string> S(H);
for (i64 i = 0; i < H; i++)
cin >> S[i];
vector<vector<i64>> dis(H, vector<i64>(W, 1e9));
deque<pair<i64, i64>> que;
dis[Ch][Cw] = 0;
que.push_back({Ch, Cw});
while (que.size()) {
pair<i64, i64> p = que.front();
que.pop_front();
for (i64 i = 0; i < 4; i++) {
i64 ddh = p.first + dh[i], ddw = p.second + dw[i];
if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
dis[ddh][ddw] <= dis[p.first][p.second])
continue;
dis[ddh][ddh] = dis[p.first][p.second];
que.push_front({ddh, ddw});
}
for (i64 i = -2; i < 3; i++)
for (i64 j = -2; j < 3; j++) {
i64 ddh = p.first + i, ddw = p.second + j;
if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
dis[ddh][ddw] <= dis[p.first][p.second] + 1)
continue;
dis[ddh][ddw] = dis[p.first][p.second] + 1;
que.push_back({ddh, ddw});
}
}
cout << (dis[Dh][Dw] == 1e9 ? -1 : dis[Dh][Dw]) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\n"
i64 dh[4] = {-1, 1, 0, 0}, dw[4] = {0, 0, -1, 1};
int main() {
i64 H, W, Ch, Cw, Dh, Dw;
cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
Ch--;
Cw--;
Dh--;
Dw--;
vector<string> S(H);
for (i64 i = 0; i < H; i++)
cin >> S[i];
vector<vector<i64>> dis(H, vector<i64>(W, 1e9));
deque<pair<i64, i64>> que;
dis[Ch][Cw] = 0;
que.push_back({Ch, Cw});
while (que.size()) {
pair<i64, i64> p = que.front();
que.pop_front();
for (i64 i = 0; i < 4; i++) {
i64 ddh = p.first + dh[i], ddw = p.second + dw[i];
if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
dis[ddh][ddw] <= dis[p.first][p.second])
continue;
dis[ddh][ddw] = dis[p.first][p.second];
que.push_front({ddh, ddw});
}
for (i64 i = -2; i < 3; i++)
for (i64 j = -2; j < 3; j++) {
i64 ddh = p.first + i, ddw = p.second + j;
if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
dis[ddh][ddw] <= dis[p.first][p.second] + 1)
continue;
dis[ddh][ddw] = dis[p.first][p.second] + 1;
que.push_back({ddh, ddw});
}
}
cout << (dis[Dh][Dw] == 1e9 ? -1 : dis[Dh][Dw]) << endl;
return 0;
}
| replace | 29 | 30 | 29 | 30 | 0 | |
p02579 | C++ | Time Limit Exceeded | #line 1 "/workspaces/compro/lib/template.hpp"
#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>
#ifndef IO_VECTOR
#define IO_VECTOR
template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
int size = v.size();
for (int i = 0; i < size; i++) {
std::cout << v[i];
if (i != size - 1)
std::cout << " ";
}
return out;
}
template <class T>
std::istream &operator>>(std::istream &in, std::vector<T> &v) {
for (auto &el : v) {
std::cin >> el;
}
return in;
}
#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<
using namespace std;
template <class T> void say(bool val, T yes, T no) {
cout << (val ? yes : no) << "\n";
}
void say(bool val, string yes = "Yes", string no = "No") {
say<string>(val, yes, no);
}
template <class T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
// C++ 17に完全移行したら消す
// 最大公約数を求める
template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; }
// 最小公倍数を求める
template <class T> T lcm(T n, T m) {
int g = gcd(n, m);
return n * m / g;
}
// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());
}
#line 2 "main.cpp"
// generated by online-judge-template-generator v4.4.0
// (https://github.com/kmyk/online-judge-template-generator)
int main() {
int h, w;
cin >> h >> w;
int ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
vector<string> s(h);
REP(i, h) { cin >> s[i]; }
ch--;
cw--;
dh--;
dw--;
vector<vector<int>> dp(h, vi(w, 50000000));
priority_queue<pair<int, pair<int, int>>> q;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
q.push({0, {ch, cw}});
auto isIn = [&](int i, int j) -> bool {
return 0 <= i && i < h && 0 <= j && j < w;
};
while (!q.empty()) {
auto [score, pos] = q.top();
auto [i, j] = pos;
q.pop();
if (dp[i][j] <= score)
continue;
dp[i][j] = score;
REP(k, 4) {
int ni = i + dx[k], nj = j + dy[k];
if (isIn(ni, nj) && s[ni][nj] == '.' && dp[ni][nj] > dp[i][j]) {
q.push({score, {ni, nj}});
}
}
for (int di = -2; di <= 2; di++) {
for (int dj = -2; dj <= 2; dj++) {
int ni = i + di, nj = j + dj;
if (isIn(ni, nj) && s[ni][nj] == '.' && dp[ni][nj] > dp[i][j] + 1) {
q.push({score + 1, {ni, nj}});
}
}
}
}
if (dp[dh][dw] == 50000000) {
cout << -1 << endl;
} else {
cout << dp[dh][dw] << endl;
}
return 0;
}
| #line 1 "/workspaces/compro/lib/template.hpp"
#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>
#ifndef IO_VECTOR
#define IO_VECTOR
template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
int size = v.size();
for (int i = 0; i < size; i++) {
std::cout << v[i];
if (i != size - 1)
std::cout << " ";
}
return out;
}
template <class T>
std::istream &operator>>(std::istream &in, std::vector<T> &v) {
for (auto &el : v) {
std::cin >> el;
}
return in;
}
#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<
using namespace std;
template <class T> void say(bool val, T yes, T no) {
cout << (val ? yes : no) << "\n";
}
void say(bool val, string yes = "Yes", string no = "No") {
say<string>(val, yes, no);
}
template <class T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
// C++ 17に完全移行したら消す
// 最大公約数を求める
template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; }
// 最小公倍数を求める
template <class T> T lcm(T n, T m) {
int g = gcd(n, m);
return n * m / g;
}
// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());
}
#line 2 "main.cpp"
// generated by online-judge-template-generator v4.4.0
// (https://github.com/kmyk/online-judge-template-generator)
int main() {
int h, w;
cin >> h >> w;
int ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
vector<string> s(h);
REP(i, h) { cin >> s[i]; }
ch--;
cw--;
dh--;
dw--;
vector<vector<int>> dp(h, vi(w, 50000000));
using pii = pair<int, pair<int, int>>;
priority_queue<pii, vector<pii>, greater<pii>> q;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
q.push({0, {ch, cw}});
auto isIn = [&](int i, int j) -> bool {
return 0 <= i && i < h && 0 <= j && j < w;
};
while (!q.empty()) {
auto [score, pos] = q.top();
auto [i, j] = pos;
q.pop();
if (dp[i][j] <= score)
continue;
dp[i][j] = score;
REP(k, 4) {
int ni = i + dx[k], nj = j + dy[k];
if (isIn(ni, nj) && s[ni][nj] == '.' && dp[ni][nj] > dp[i][j]) {
q.push({score, {ni, nj}});
}
}
for (int di = -2; di <= 2; di++) {
for (int dj = -2; dj <= 2; dj++) {
int ni = i + di, nj = j + dj;
if (isIn(ni, nj) && s[ni][nj] == '.' && dp[ni][nj] > dp[i][j] + 1) {
q.push({score + 1, {ni, nj}});
}
}
}
}
if (dp[dh][dw] == 50000000) {
cout << -1 << endl;
} else {
cout << dp[dh][dw] << endl;
}
return 0;
}
| replace | 93 | 94 | 93 | 95 | TLE | |
p02579 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
using LL = long long;
bool is_visited(vector<vector<int>> &num, vector<vector<int>> &S, int ig,
int jg, int is, int js, int H, int W) {
if (ig <= 0 || ig > H || jg <= 0 || jg > W) {
return false;
}
if (num[ig][jg] == -1 && S[ig][jg] == 1) {
return true;
} else if ((num[ig][jg] >= 0 && num[ig][jg] > num[is][js]) &&
S[ig][jg] == 1) {
return true;
}
return false;
}
bool is_visited2(vector<vector<int>> &num, vector<vector<int>> &S, int ig,
int jg, int is, int js, int H, int W) {
if (ig <= 0 || ig > H || jg <= 0 || jg > W) {
return false;
}
if (num[ig][jg] == -1 && S[ig][jg] == 1) {
return true;
} else if ((num[ig][jg] >= 0 && num[ig][jg] > num[is][js] + 1) &&
S[ig][jg] == 1) {
return true;
}
return false;
}
int main() {
int H, W;
cin >> H >> W;
int Ch, Dh, Cw, Dw;
cin >> Ch >> Cw >> Dh >> Dw;
vector<vector<int>> num(H + 2, vector<int>(W + 2, -1));
vector<vector<int>> S(H + 2, vector<int>(W + 2, -1));
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
char a;
cin >> a;
if (a == '.') {
S[i][j] = 1;
}
}
}
vector<queue<pair<int, int>>> v_que(H * W);
queue<pair<int, int>> que;
que.push(make_pair(Ch, Cw));
v_que[0].push(make_pair(Ch, Cw));
int qi = 0;
num[Ch][Cw] = 0;
while (qi < H * W) {
pair<int, int> p = v_que[qi].front();
v_que[qi].pop();
int i = p.first;
int j = p.second;
if (is_visited(num, S, i + 1, j, i, j, H, W)) {
num[i + 1][j] = num[i][j];
v_que[qi].push(make_pair(i + 1, j));
}
if (is_visited(num, S, i, j + 1, i, j, H, W)) {
num[i][j + 1] = num[i][j];
v_que[qi].push(make_pair(i, j + 1));
}
if (is_visited(num, S, i - 1, j, i, j, H, W)) {
num[i - 1][j] = num[i][j];
v_que[qi].push(make_pair(i - 1, j));
}
if (is_visited(num, S, i, j - 1, i, j, H, W)) {
num[i][j - 1] = num[i][j];
v_que[qi].push(make_pair(i, j - 1));
}
if (is_visited2(num, S, i + 1, j + 1, i, j, H, W)) {
num[i + 1][j + 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 1, j + 1));
}
if (is_visited2(num, S, i + 1, j - 1, i, j, H, W)) {
num[i + 1][j - 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 1, j - 1));
}
if (is_visited2(num, S, i - 1, j - 1, i, j, H, W)) {
num[i - 1][j - 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 1, j - 1));
}
if (is_visited2(num, S, i + 1, j - 1, i, j, H, W)) {
num[i + 1][j - 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 1, j - 1));
}
if (is_visited2(num, S, i - 2, j - 2, i, j, H, W)) {
num[i - 2][j - 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 2, j - 2));
}
if (is_visited2(num, S, i - 2, j - 1, i, j, H, W)) {
num[i - 2][j - 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 2, j - 1));
}
if (is_visited2(num, S, i - 2, j, i, j, H, W)) {
num[i - 2][j] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 2, j));
}
if (is_visited2(num, S, i - 2, j + 1, i, j, H, W)) {
num[i - 2][j + 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 2, j + 1));
}
if (is_visited2(num, S, i - 2, j + 2, i, j, H, W)) {
num[i - 2][j + 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 2, j + 2));
}
if (is_visited2(num, S, i - 1, j + 2, i, j, H, W)) {
num[i - 1][j + 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 1, j + 2));
}
if (is_visited2(num, S, i, j + 2, i, j, H, W)) {
num[i][j + 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i, j + 2));
}
if (is_visited2(num, S, i + 1, j + 2, i, j, H, W)) {
num[i + 1][j + 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 1, j + 2));
}
if (is_visited2(num, S, i + 2, j + 2, i, j, H, W)) {
num[i + 2][j + 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 2, j + 2));
}
if (is_visited2(num, S, i + 2, j + 1, i, j, H, W)) {
num[i + 2][j + 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 2, j + 1));
}
if (is_visited2(num, S, i + 2, j, i, j, H, W)) {
num[i + 2][j] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 2, j));
}
if (is_visited2(num, S, i + 2, j - 1, i, j, H, W)) {
num[i + 2][j - 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 2, j - 1));
}
if (is_visited2(num, S, i + 2, j - 2, i, j, H, W)) {
num[i + 2][j - 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 2, j - 2));
}
if (is_visited2(num, S, i + 1, j - 2, i, j, H, W)) {
num[i + 1][j - 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 1, j - 2));
}
if (is_visited2(num, S, i, j - 2, i, j, H, W)) {
num[i][j - 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i, j - 2));
}
if (is_visited2(num, S, i - 1, j - 2, i, j, H, W)) {
num[i - 1][j - 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 1, j - 2));
}
if (v_que[qi].empty()) {
qi++;
}
}
cout << num[Dh][Dw] << endl;
return 0;
} | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
using LL = long long;
bool is_visited(vector<vector<int>> &num, vector<vector<int>> &S, int ig,
int jg, int is, int js, int H, int W) {
if (ig <= 0 || ig > H || jg <= 0 || jg > W) {
return false;
}
if (num[ig][jg] == -1 && S[ig][jg] == 1) {
return true;
} else if ((num[ig][jg] >= 0 && num[ig][jg] > num[is][js]) &&
S[ig][jg] == 1) {
return true;
}
return false;
}
bool is_visited2(vector<vector<int>> &num, vector<vector<int>> &S, int ig,
int jg, int is, int js, int H, int W) {
if (ig <= 0 || ig > H || jg <= 0 || jg > W) {
return false;
}
if (num[ig][jg] == -1 && S[ig][jg] == 1) {
return true;
} else if ((num[ig][jg] >= 0 && num[ig][jg] > num[is][js] + 1) &&
S[ig][jg] == 1) {
return true;
}
return false;
}
int main() {
int H, W;
cin >> H >> W;
int Ch, Dh, Cw, Dw;
cin >> Ch >> Cw >> Dh >> Dw;
vector<vector<int>> num(H + 2, vector<int>(W + 2, -1));
vector<vector<int>> S(H + 2, vector<int>(W + 2, -1));
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
char a;
cin >> a;
if (a == '.') {
S[i][j] = 1;
}
}
}
vector<queue<pair<int, int>>> v_que(H * W);
queue<pair<int, int>> que;
que.push(make_pair(Ch, Cw));
v_que[0].push(make_pair(Ch, Cw));
int qi = 0;
num[Ch][Cw] = 0;
while (qi < H * W) {
pair<int, int> p = v_que[qi].front();
v_que[qi].pop();
int i = p.first;
int j = p.second;
if (is_visited(num, S, i + 1, j, i, j, H, W)) {
num[i + 1][j] = num[i][j];
v_que[qi].push(make_pair(i + 1, j));
}
if (is_visited(num, S, i, j + 1, i, j, H, W)) {
num[i][j + 1] = num[i][j];
v_que[qi].push(make_pair(i, j + 1));
}
if (is_visited(num, S, i - 1, j, i, j, H, W)) {
num[i - 1][j] = num[i][j];
v_que[qi].push(make_pair(i - 1, j));
}
if (is_visited(num, S, i, j - 1, i, j, H, W)) {
num[i][j - 1] = num[i][j];
v_que[qi].push(make_pair(i, j - 1));
}
if (is_visited2(num, S, i + 1, j + 1, i, j, H, W)) {
num[i + 1][j + 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 1, j + 1));
}
if (is_visited2(num, S, i + 1, j - 1, i, j, H, W)) {
num[i + 1][j - 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 1, j - 1));
}
if (is_visited2(num, S, i - 1, j - 1, i, j, H, W)) {
num[i - 1][j - 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 1, j - 1));
}
if (is_visited2(num, S, i + 1, j - 1, i, j, H, W)) {
num[i + 1][j - 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 1, j - 1));
}
if (is_visited2(num, S, i - 2, j - 2, i, j, H, W)) {
num[i - 2][j - 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 2, j - 2));
}
if (is_visited2(num, S, i - 2, j - 1, i, j, H, W)) {
num[i - 2][j - 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 2, j - 1));
}
if (is_visited2(num, S, i - 2, j, i, j, H, W)) {
num[i - 2][j] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 2, j));
}
if (is_visited2(num, S, i - 2, j + 1, i, j, H, W)) {
num[i - 2][j + 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 2, j + 1));
}
if (is_visited2(num, S, i - 2, j + 2, i, j, H, W)) {
num[i - 2][j + 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 2, j + 2));
}
if (is_visited2(num, S, i - 1, j + 2, i, j, H, W)) {
num[i - 1][j + 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 1, j + 2));
}
if (is_visited2(num, S, i, j + 2, i, j, H, W)) {
num[i][j + 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i, j + 2));
}
if (is_visited2(num, S, i + 1, j + 2, i, j, H, W)) {
num[i + 1][j + 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 1, j + 2));
}
if (is_visited2(num, S, i + 2, j + 2, i, j, H, W)) {
num[i + 2][j + 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 2, j + 2));
}
if (is_visited2(num, S, i + 2, j + 1, i, j, H, W)) {
num[i + 2][j + 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 2, j + 1));
}
if (is_visited2(num, S, i + 2, j, i, j, H, W)) {
num[i + 2][j] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 2, j));
}
if (is_visited2(num, S, i + 2, j - 1, i, j, H, W)) {
num[i + 2][j - 1] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 2, j - 1));
}
if (is_visited2(num, S, i + 2, j - 2, i, j, H, W)) {
num[i + 2][j - 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 2, j - 2));
}
if (is_visited2(num, S, i + 1, j - 2, i, j, H, W)) {
num[i + 1][j - 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i + 1, j - 2));
}
if (is_visited2(num, S, i, j - 2, i, j, H, W)) {
num[i][j - 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i, j - 2));
}
if (is_visited2(num, S, i - 1, j - 2, i, j, H, W)) {
num[i - 1][j - 2] = num[i][j] + 1;
v_que[qi + 1].push(make_pair(i - 1, j - 2));
}
if (v_que[qi].empty()) {
qi++;
if (v_que[qi].empty()) {
break;
}
}
}
cout << num[Dh][Dw] << endl;
return 0;
} | insert | 162 | 162 | 162 | 165 | -11 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int nax = 1e3 + 3;
using pi = pair<int, int>;
#define x first
#define y second
pair<int, int> operator+(const pi &a, const pi &b) {
return make_pair(a.x + b.x, a.y + b.y);
}
// 0-1 BFS is Dijkstra.
bool bor[nax][nax];
int lvl[nax][nax];
int n, m;
bool safe(int i, int j) {
return !(i < 0 or j < 0 or i > n - 1 or j > m - 1 or !bor[i][j]);
}
vector<pair<int, int>> pts(int i, int j) {
vector<pi> ret;
for (int x = i - 2; x <= i + 2; x++) {
for (int y = j - 2; y <= j + 2; y++) {
if (!safe(x, y))
continue;
ret.emplace_back(x, y);
}
}
return ret;
}
void _read() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
// freopen("input.txt","r",stdin);
}
int main() {
_read();
cin >> n >> m;
pi src, des;
cin >> src.x >> src.y >> des.x >> des.y;
src.x--, src.y--, des.x--, des.y--;
for (int i = 0; i < n; i++) {
char c;
for (int j = 0; j < m; j++) {
cin >> c;
bor[i][j] = (c == '.');
lvl[i][j] = (int)1e9 + 7;
}
}
deque<pair<int, int>> q;
q.push_back(src);
lvl[src.x][src.y] = 0;
while (!q.empty()) {
pi u = q.front();
q.pop_front();
for (pi dir : (vector<pi>){{1, 0}, {0, 1}, {-1, 0}, {0, -1}}) {
pi v = u + dir;
if (!safe(v.x, v.y))
continue;
int x = v.x, y = v.y;
if (lvl[x][y] > lvl[u.x][u.y]) {
lvl[x][y] = lvl[u.x][u.y];
q.push_back(v);
}
}
vector<pi> r = pts(u.x, u.y);
for (pi v : r) {
if (lvl[u.x][u.y] + 1 < lvl[v.x][v.y]) {
lvl[v.x][v.y] = lvl[u.x][u.y] + 1;
q.push_back(v);
}
}
}
int ans = lvl[des.x][des.y];
cout << (ans == (int)1e9 + 7 ? -1 : ans) << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int nax = 1e3 + 3;
using pi = pair<int, int>;
#define x first
#define y second
pair<int, int> operator+(const pi &a, const pi &b) {
return make_pair(a.x + b.x, a.y + b.y);
}
// 0-1 BFS is Dijkstra.
bool bor[nax][nax];
int lvl[nax][nax];
int n, m;
bool safe(int i, int j) {
return !(i < 0 or j < 0 or i > n - 1 or j > m - 1 or !bor[i][j]);
}
vector<pair<int, int>> pts(int i, int j) {
vector<pi> ret;
for (int x = i - 2; x <= i + 2; x++) {
for (int y = j - 2; y <= j + 2; y++) {
if (!safe(x, y))
continue;
ret.emplace_back(x, y);
}
}
return ret;
}
void _read() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
// freopen("input.txt","r",stdin);
}
int main() {
_read();
cin >> n >> m;
pi src, des;
cin >> src.x >> src.y >> des.x >> des.y;
src.x--, src.y--, des.x--, des.y--;
for (int i = 0; i < n; i++) {
char c;
for (int j = 0; j < m; j++) {
cin >> c;
bor[i][j] = (c == '.');
lvl[i][j] = (int)1e9 + 7;
}
}
deque<pair<int, int>> q;
q.push_back(src);
lvl[src.x][src.y] = 0;
while (!q.empty()) {
pi u = q.front();
q.pop_front();
for (pi dir : (vector<pi>){{1, 0}, {0, 1}, {-1, 0}, {0, -1}}) {
pi v = u + dir;
if (!safe(v.x, v.y))
continue;
int x = v.x, y = v.y;
if (lvl[x][y] > lvl[u.x][u.y]) {
lvl[x][y] = lvl[u.x][u.y];
q.push_front(v);
}
}
vector<pi> r = pts(u.x, u.y);
for (pi v : r) {
if (lvl[u.x][u.y] + 1 < lvl[v.x][v.y]) {
lvl[v.x][v.y] = lvl[u.x][u.y] + 1;
q.push_back(v);
}
}
}
int ans = lvl[des.x][des.y];
cout << (ans == (int)1e9 + 7 ? -1 : ans) << '\n';
return 0;
}
| replace | 65 | 66 | 65 | 66 | TLE | |
p02579 | C++ | Time Limit Exceeded | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = int;
const ll MOD = 1000000007;
// const ll MOD=998244353;
const long long INF = 1 << 30;
const double pi = acos(-1.0);
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll H, W;
vector<string> grid;
bool movable(ll gx, ll gy) {
if (gx < 0 || gx >= W || gy < 0 || gy >= H)
return false;
if (grid[gy][gx] == '#')
return false;
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> H >> W;
ll sy, sx;
cin >> sy >> sx;
sx--;
sy--;
ll gy, gx;
cin >> gy >> gx;
gx--;
gy--;
grid = vector<string>(H);
rep(i, H) cin >> grid[i];
// bfs
priority_queue<tuple<ll, ll, ll>> que;
vector<vector<ll>> dp(H, vector<ll>(W, INF));
vector<vector<int>> checked(H, vector<int>(W, 0));
que.push({0, sx, sy});
dp[sy][sx] = 0;
vector<ll> dx = {-1, 0, 1, 0};
vector<ll> dy = {0, -1, 0, 1};
while (!que.empty()) {
auto v = que.top();
que.pop();
ll cost, vx, vy;
tie(cost, vx, vy) = v;
if (checked[vy][vx])
continue;
if (vx == gx && vy == gy)
break;
checked[vy][vx] = 1;
cost *= -1;
rep(i, 4) {
if (movable(vx + dx[i], vy + dy[i]) &&
chmin(dp[vy + dy[i]][vx + dx[i]], cost)) {
que.push({-cost, vx + dx[i], vy + dy[i]});
}
}
if (movable(vx + dx[0], vy + dy[0]) && movable(vx + dx[1], vy + dy[1]) &&
movable(vx + dx[2], vy + dy[2]) && movable(vx + dx[3], vy + dy[3]))
continue;
rep(i, 3) {
rep(j, 3) {
if (abs(i) + abs(j) <= 1)
continue;
if (movable(vx + i, vy + j) && chmin(dp[vy + j][vx + i], cost + 1))
que.push({-cost - 1, vx + i, vy + j});
if (movable(vx - i, vy + j) && chmin(dp[vy + j][vx - i], cost + 1))
que.push({-cost - 1, vx - i, vy + j});
if (movable(vx + i, vy - j) && chmin(dp[vy - j][vx + i], cost + 1))
que.push({-cost - 1, vx + i, vy - j});
if (movable(vx - i, vy - j) && chmin(dp[vy - j][vx - i], cost + 1))
que.push({-cost - 1, vx - i, vy - j});
}
}
}
if (dp[gy][gx] == INF)
cout << -1 << endl;
else
cout << dp[gy][gx] << endl;
return 0;
}
| // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = int;
const ll MOD = 1000000007;
// const ll MOD=998244353;
const long long INF = 1 << 30;
const double pi = acos(-1.0);
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll H, W;
vector<string> grid;
bool movable(ll gx, ll gy) {
if (gx < 0 || gx >= W || gy < 0 || gy >= H)
return false;
if (grid[gy][gx] == '#')
return false;
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> H >> W;
ll sy, sx;
cin >> sy >> sx;
sx--;
sy--;
ll gy, gx;
cin >> gy >> gx;
gx--;
gy--;
grid = vector<string>(H);
rep(i, H) cin >> grid[i];
// bfs
priority_queue<tuple<ll, ll, ll>> que;
vector<vector<ll>> dp(H, vector<ll>(W, INF));
vector<vector<int>> checked(H, vector<int>(W, 0));
que.push({0, sx, sy});
dp[sy][sx] = 0;
vector<ll> dx = {-1, 0, 1, 0};
vector<ll> dy = {0, -1, 0, 1};
while (!que.empty()) {
auto v = que.top();
que.pop();
ll cost, vx, vy;
tie(cost, vx, vy) = v;
if (checked[vy][vx])
continue;
if (vx == gx && vy == gy)
break;
checked[vy][vx] = 1;
cost *= -1;
rep(i, 4) {
if (movable(vx + dx[i], vy + dy[i]) &&
chmin(dp[vy + dy[i]][vx + dx[i]], cost)) {
que.push({-cost, vx + dx[i], vy + dy[i]});
}
}
if (movable(vx + dx[0], vy + dy[0]) && movable(vx + dx[1], vy + dy[1]) &&
movable(vx + dx[2], vy + dy[2]) && movable(vx + dx[3], vy + dy[3]))
continue;
rep(i, 3) {
rep(j, 3) {
if (abs(i) + abs(j) <= 1)
continue;
if (movable(vx + i, vy + j) && chmin(dp[vy + j][vx + i], cost + 1))
que.push({-cost - 1, vx + i, vy + j});
if (movable(vx - i, vy + j) && chmin(dp[vy + j][vx - i], cost + 1))
que.push({-cost - 1, vx - i, vy + j});
if (movable(vx + i, vy - j) && chmin(dp[vy - j][vx + i], cost + 1))
que.push({-cost - 1, vx + i, vy - j});
if (movable(vx - i, vy - j) && chmin(dp[vy - j][vx - i], cost + 1))
que.push({-cost - 1, vx - i, vy - j});
}
}
}
if (dp[gy][gx] == INF)
cout << -1 << endl;
else
cout << dp[gy][gx] << endl;
return 0;
}
| replace | 0 | 1 | 0 | 1 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
struct node {
int x, y;
} t;
char a[1005][1005];
int inq[1005][1005];
int n, m;
deque<node> q;
bool check(int x, int y, int w) {
return !(inq[x][y] <= w || x < 1 || x > n || y < 1 || y > m);
}
signed main() {
int i, j, sx, sy, ex, ey;
string s;
cin >> n >> m >> sx >> sy >> ex >> ey;
for (i = 1; i <= n; i++) {
cin >> s;
for (j = 1; j <= m; j++) {
a[i][j] = s[j - 1];
if (a[i][j] == '#')
inq[i][j] = -1;
else
inq[i][j] = 2147483647;
}
}
q.push_front((node){sx, sy});
inq[sx][sy] = 0;
while (q.size()) {
t = q.front();
q.pop_front();
if (t.x == ex && t.y == ey) {
cout << inq[ex][ey];
while (q.size())
q.pop_front();
return 0;
}
for (i = 0; i < 4; i++)
if (check(t.x + dx[i], t.y + dy[i], inq[t.x][t.y])) {
inq[t.x + dx[i]][t.y + dy[i]] = inq[t.x][t.y];
q.push_front((node){t.x + dx[i], t.y + dy[i]});
}
for (i = -2; i < 3; i++)
for (j = -2; j < 3; j++)
if (check(t.x + i, t.y + j, inq[t.x][t.y] + 1)) {
inq[t.x + i][t.y + j] = inq[t.x][t.y] + 1;
q.push_back((node){t.x + i, t.y + j});
}
}
cout << -1;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
struct node {
int x, y;
} t;
char a[1005][1005];
int inq[1005][1005];
int n, m;
deque<node> q;
bool check(int x, int y, int w) {
return !(x < 1 || x > n || y < 1 || y > m || inq[x][y] <= w);
}
signed main() {
int i, j, sx, sy, ex, ey;
string s;
cin >> n >> m >> sx >> sy >> ex >> ey;
for (i = 1; i <= n; i++) {
cin >> s;
for (j = 1; j <= m; j++) {
a[i][j] = s[j - 1];
if (a[i][j] == '#')
inq[i][j] = -1;
else
inq[i][j] = 2147483647;
}
}
q.push_front((node){sx, sy});
inq[sx][sy] = 0;
while (q.size()) {
t = q.front();
q.pop_front();
if (t.x == ex && t.y == ey) {
cout << inq[ex][ey];
while (q.size())
q.pop_front();
return 0;
}
for (i = 0; i < 4; i++)
if (check(t.x + dx[i], t.y + dy[i], inq[t.x][t.y])) {
inq[t.x + dx[i]][t.y + dy[i]] = inq[t.x][t.y];
q.push_front((node){t.x + dx[i], t.y + dy[i]});
}
for (i = -2; i < 3; i++)
for (j = -2; j < 3; j++)
if (check(t.x + i, t.y + j, inq[t.x][t.y] + 1)) {
inq[t.x + i][t.y + j] = inq[t.x][t.y] + 1;
q.push_back((node){t.x + i, t.y + j});
}
}
cout << -1;
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll h, w, ch, cw, dh, dw;
char tb[1005][1005];
ll mp[1005][1005];
vector<ll> v[100005];
map<pair<ll, ll>, ll> mp2;
map<ll, ll> mp3;
void dfs(ll rw, ll col, ll ind) {
if (rw <= 0 || rw > h || col <= 0 || col > w)
return;
if (mp[rw][col] != 0)
return;
if (tb[rw][col] == '#')
return;
mp[rw][col] = ind;
dfs(rw - 1, col, ind);
dfs(rw + 1, col, ind);
dfs(rw, col - 1, ind);
dfs(rw, col + 1, ind);
}
void func(ll start_row, ll start_col, ll end_row, ll end_col, ll rw, ll col) {
ll i, j;
for (i = start_row; i <= end_row; i++) {
for (j = start_col; j <= end_col; j++) {
if (((mp2.find({mp[i][j], mp[rw][col]}) == mp2.end()) &&
(mp[i][j] != mp[rw][col])) &&
(mp[i][j] != 0)) {
v[mp[rw][col]].push_back(mp[i][j]);
v[mp[i][j]].push_back(mp[rw][col]);
mp2[{mp[i][j], mp[rw][col]}]++;
mp2[{mp[rw][col], mp[i][j]}]++;
}
}
}
}
ll bfs(ll ind, ll dest) {
if (ind == dest)
return 0;
bool flag = false;
ll i, j, z = 1, ans = 0;
queue<ll> q;
q.push(ind);
mp3[ind]++;
while (!q.empty()) {
j = 0;
z = q.size();
while (j < z) {
ll x = q.front();
if (x == dest) {
flag = true;
break;
}
q.pop();
ll p = v[x].size();
for (i = 0; i < p; i++) {
if (mp3[v[x][i]] == 0) {
mp3[v[x][i]]++;
q.push(v[x][i]);
}
}
j++;
}
if (flag)
break;
ans++;
}
if (flag)
return ans;
return -1;
}
int main() {
ll i, j, ind = 1;
cin >> h >> w >> ch >> cw >> dh >> dw;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
cin >> tb[i][j];
mp[i][j] = 0;
}
}
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (mp[i][j] == 0 && (tb[i][j] == '.')) {
dfs(i, j, ind);
ind++;
}
}
}
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
ll sr = max(i - 2, (ll)1);
ll sc = max(j - 2, (ll)1);
ll er = min(i + 2, h);
ll ec = min(j + 2, w);
if (tb[i][j] != '#')
func(sr, sc, er, ec, i, j);
}
}
/*for(i=1;i<=h;i++)
{
for(j=1;j<=w;j++)
{
cout<<mp[i][j]<<" ";
}
cout<<"\n";
}
for(i=1;i<=ind;i++)
{
cout<<i<<": ";
for(j=0;j<v[i].size();j++)
{
cout<<v[i][j]<<" ";
}
cout<<"\n";
}*/
ll x = mp[ch][cw];
ll y = mp[dh][dw];
cout << bfs(x, y);
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll h, w, ch, cw, dh, dw;
char tb[1005][1005];
ll mp[1005][1005];
vector<ll> v[1000005];
map<pair<ll, ll>, ll> mp2;
map<ll, ll> mp3;
void dfs(ll rw, ll col, ll ind) {
if (rw <= 0 || rw > h || col <= 0 || col > w)
return;
if (mp[rw][col] != 0)
return;
if (tb[rw][col] == '#')
return;
mp[rw][col] = ind;
dfs(rw - 1, col, ind);
dfs(rw + 1, col, ind);
dfs(rw, col - 1, ind);
dfs(rw, col + 1, ind);
}
void func(ll start_row, ll start_col, ll end_row, ll end_col, ll rw, ll col) {
ll i, j;
for (i = start_row; i <= end_row; i++) {
for (j = start_col; j <= end_col; j++) {
if (((mp2.find({mp[i][j], mp[rw][col]}) == mp2.end()) &&
(mp[i][j] != mp[rw][col])) &&
(mp[i][j] != 0)) {
v[mp[rw][col]].push_back(mp[i][j]);
v[mp[i][j]].push_back(mp[rw][col]);
mp2[{mp[i][j], mp[rw][col]}]++;
mp2[{mp[rw][col], mp[i][j]}]++;
}
}
}
}
ll bfs(ll ind, ll dest) {
if (ind == dest)
return 0;
bool flag = false;
ll i, j, z = 1, ans = 0;
queue<ll> q;
q.push(ind);
mp3[ind]++;
while (!q.empty()) {
j = 0;
z = q.size();
while (j < z) {
ll x = q.front();
if (x == dest) {
flag = true;
break;
}
q.pop();
ll p = v[x].size();
for (i = 0; i < p; i++) {
if (mp3[v[x][i]] == 0) {
mp3[v[x][i]]++;
q.push(v[x][i]);
}
}
j++;
}
if (flag)
break;
ans++;
}
if (flag)
return ans;
return -1;
}
int main() {
ll i, j, ind = 1;
cin >> h >> w >> ch >> cw >> dh >> dw;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
cin >> tb[i][j];
mp[i][j] = 0;
}
}
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (mp[i][j] == 0 && (tb[i][j] == '.')) {
dfs(i, j, ind);
ind++;
}
}
}
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
ll sr = max(i - 2, (ll)1);
ll sc = max(j - 2, (ll)1);
ll er = min(i + 2, h);
ll ec = min(j + 2, w);
if (tb[i][j] != '#')
func(sr, sc, er, ec, i, j);
}
}
/*for(i=1;i<=h;i++)
{
for(j=1;j<=w;j++)
{
cout<<mp[i][j]<<" ";
}
cout<<"\n";
}
for(i=1;i<=ind;i++)
{
cout<<i<<": ";
for(j=0;j<v[i].size();j++)
{
cout<<v[i][j]<<" ";
}
cout<<"\n";
}*/
ll x = mp[ch][cw];
ll y = mp[dh][dw];
cout << bfs(x, y);
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
const int dy[4] = {1, 0, -1, 0};
const int dx[4] = {0, 1, 0, -1};
int main() {
int h, w, ch, cw, dh, dw;
cin >> h >> w >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
vector<string> s(h);
rep(i, h) cin >> s[i];
vector<vector<int>> dist(h, vector<int>(w, -1));
deque<pair<int, int>> que;
dist[ch][cw] = 0;
que.push_front(pair<int, int>(ch, cw));
while (!que.empty()) {
auto v = que.front();
que.pop_front();
int y = v.first, x = v.second;
cout << y << ", " << x << ": " << dist[x][y] << endl;
for (int dir = 0; dir < 4; dir++) {
int ny = y + dy[dir], nx = x + dx[dir];
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (s[ny][nx] == '#')
continue;
if (dist[ny][nx] == -1) {
dist[ny][nx] = dist[y][x];
que.push_front(pair<int, int>(ny, nx));
} else if (dist[ny][nx] > dist[y][x]) {
dist[ny][nx] = dist[y][x];
que.push_front(pair<int, int>(ny, nx));
}
}
for (int ny = y - 2; ny <= y + 2; ny++) {
for (int nx = x - 2; nx <= x + 2; nx++) {
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (s[ny][nx] == '#')
continue;
if (dist[ny][nx] == -1) {
dist[ny][nx] = dist[y][x] + 1;
que.push_back(pair<int, int>(ny, nx));
} else if (dist[ny][nx] > dist[y][x] + 1) {
dist[ny][nx] = dist[y][x] + 1;
que.push_front(pair<int, int>(ny, nx));
}
}
}
}
cout << dist[dh][dw] << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
const int dy[4] = {1, 0, -1, 0};
const int dx[4] = {0, 1, 0, -1};
int main() {
int h, w, ch, cw, dh, dw;
cin >> h >> w >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
vector<string> s(h);
rep(i, h) cin >> s[i];
vector<vector<int>> dist(h, vector<int>(w, -1));
deque<pair<int, int>> que;
dist[ch][cw] = 0;
que.push_front(pair<int, int>(ch, cw));
while (!que.empty()) {
auto v = que.front();
que.pop_front();
int y = v.first, x = v.second;
for (int dir = 0; dir < 4; dir++) {
int ny = y + dy[dir], nx = x + dx[dir];
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (s[ny][nx] == '#')
continue;
if (dist[ny][nx] == -1) {
dist[ny][nx] = dist[y][x];
que.push_front(pair<int, int>(ny, nx));
} else if (dist[ny][nx] > dist[y][x]) {
dist[ny][nx] = dist[y][x];
que.push_front(pair<int, int>(ny, nx));
}
}
for (int ny = y - 2; ny <= y + 2; ny++) {
for (int nx = x - 2; nx <= x + 2; nx++) {
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (s[ny][nx] == '#')
continue;
if (dist[ny][nx] == -1) {
dist[ny][nx] = dist[y][x] + 1;
que.push_back(pair<int, int>(ny, nx));
} else if (dist[ny][nx] > dist[y][x] + 1) {
dist[ny][nx] = dist[y][x] + 1;
que.push_front(pair<int, int>(ny, nx));
}
}
}
}
cout << dist[dh][dw] << endl;
return 0;
} | delete | 23 | 24 | 23 | 23 | 0 | |
p02579 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) (int)(v).size()
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<int, int>;
template <typename T> struct Edge {
int u, v;
T cost;
Edge(int u, int v, T c) : u(u), v(v), cost(c) {}
bool operator<(const Edge &e) const { return cost < e.cost; }
};
static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;
ll ans;
string S[1005];
int dw[] = {0, 0, -1, 1}, dh[] = {-1, 1, 0, 0};
int dp[1005][1005];
queue<P> Q;
int H, W;
void bfs(int h, int w) {
queue<P> q;
q.push(mp(h, w));
while (!q.empty()) {
P p = q.front();
q.pop();
int sh = p.first, sw = p.second;
rep(i, -2, 3) rep(j, -2, 3) {
if (dp[sh + i][sw + j] > dp[sh][sw] + 1 && S[sh + i][sw + j] != '#') {
Q.push(mp(sh + i, sw + j));
dp[sh + i][sw + j] = dp[sh][sw] + 1;
}
}
rep(r, 0, 4) {
int th = sh + dh[r], tw = sw + dw[r];
if (tw < 0 || th < 0 || tw >= W || th >= H || S[th][tw] == '#')
continue;
if (dp[th][tw] <= dp[sh][sw])
continue;
dp[th][tw] = dp[sh][sw];
q.push(mp(th, tw));
}
}
}
int main(void) {
int Ch, Cw, Dh, Dw;
cin >> H >> W;
cin >> Ch >> Cw;
Ch--;
Cw--;
cin >> Dh >> Dw;
Dh--;
Dw--;
rep(i, 0, H) cin >> S[i];
rep(i, 0, H) rep(j, 0, W) dp[i][j] = INF;
Q.push(mp(Ch, Cw));
dp[Ch][Cw] = 0;
while (!Q.empty()) {
P p = Q.front();
Q.pop();
bfs(p.first, p.second);
}
if (dp[Dh][Dw] != INF)
cout << dp[Dh][Dw] << endl;
else
cout << -1 << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) (int)(v).size()
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<int, int>;
template <typename T> struct Edge {
int u, v;
T cost;
Edge(int u, int v, T c) : u(u), v(v), cost(c) {}
bool operator<(const Edge &e) const { return cost < e.cost; }
};
static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;
ll ans;
string S[1005];
int dw[] = {0, 0, -1, 1}, dh[] = {-1, 1, 0, 0};
int dp[1005][1005];
queue<P> Q;
int H, W;
void bfs(int h, int w) {
queue<P> q;
q.push(mp(h, w));
while (!q.empty()) {
P p = q.front();
q.pop();
int sh = p.first, sw = p.second;
rep(i, -2, 3) rep(j, -2, 3) {
if (sh + i < 0 || sh + i >= H || sw + j < 0 || sw + j >= W)
continue;
if (dp[sh + i][sw + j] > dp[sh][sw] + 1 && S[sh + i][sw + j] != '#') {
Q.push(mp(sh + i, sw + j));
dp[sh + i][sw + j] = dp[sh][sw] + 1;
}
}
rep(r, 0, 4) {
int th = sh + dh[r], tw = sw + dw[r];
if (tw < 0 || th < 0 || tw >= W || th >= H || S[th][tw] == '#')
continue;
if (dp[th][tw] <= dp[sh][sw])
continue;
dp[th][tw] = dp[sh][sw];
q.push(mp(th, tw));
}
}
}
int main(void) {
int Ch, Cw, Dh, Dw;
cin >> H >> W;
cin >> Ch >> Cw;
Ch--;
Cw--;
cin >> Dh >> Dw;
Dh--;
Dw--;
rep(i, 0, H) cin >> S[i];
rep(i, 0, H) rep(j, 0, W) dp[i][j] = INF;
Q.push(mp(Ch, Cw));
dp[Ch][Cw] = 0;
while (!Q.empty()) {
P p = Q.front();
Q.pop();
bfs(p.first, p.second);
}
if (dp[Dh][Dw] != INF)
cout << dp[Dh][Dw] << endl;
else
cout << -1 << endl;
return 0;
}
| insert | 52 | 52 | 52 | 54 | -11 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template <class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define eFOR(i, a, n) for (int i = (a); i <= (n); ++i)
#define rFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define erFOR(i, a, n) for (int i = (n); i >= (a); --i)
#define each(i, a) for (auto &i : a)
#define SORT(a) sort(a.begin(), a.end())
#define rSORT(a) sort(a.rbegin(), a.rend())
#define fSORT(a, f) sort(a.begin(), a.end(), f)
#define all(a) a.begin(), a.end()
#define out(y, x) ((y) < 0 || h <= (y) || (x) < 0 || w <= (x))
#define tp(a, i) get<i>(a)
#define line cout << "-----------------------------\n"
#define ENDL(i, n) ((i) == (n)-1 ? "\n" : " ")
#define stop system("pause")
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template <class T> inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
inline void init() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
template <class T> inline istream &operator>>(istream &is, vector<T> &v) {
for (auto &a : v)
is >> a;
return is;
}
template <class T> inline istream &operator>>(istream &is, deque<T> &v) {
for (auto &a : v)
is >> a;
return is;
}
template <class T, class U>
inline istream &operator>>(istream &is, pair<T, U> &p) {
is >> p.first >> p.second;
return is;
}
template <class T> inline vector<T> vec(size_t a) { return vector<T>(a); }
template <class T> inline vector<T> defvec(T def, size_t a) {
return vector<T>(a, def);
}
template <class T, class... Ts> inline auto vec(size_t a, Ts... ts) {
return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}
template <class T, class... Ts> inline auto defvec(T def, size_t a, Ts... ts) {
return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...));
}
template <class T> inline void print(const T &a) { cout << a << "\n"; }
template <class T, class... Ts> inline void print(const T &a, const Ts &...ts) {
cout << a << " ";
print(ts...);
}
template <class T> inline void print(const vector<T> &v) {
for (int i = 0; i < v.size(); ++i)
cout << v[i] << (i == v.size() - 1 ? "\n" : " ");
}
template <class T> inline void print(const vector<vector<T>> &v) {
for (auto &a : v)
print(a);
}
inline string reversed(const string &s) {
string t = s;
reverse(all(t));
return t;
}
int main() {
init();
int h, w;
cin >> h >> w;
int sy, sx, gy, gx;
cin >> sy >> sx >> gy >> gx;
--sy, --sx, --gy, --gx;
VS s(h);
cin >> s;
auto dp = defvec<int>(INF, h, w);
dp[sy][sx] = 0;
deque<pair<int, int>> bfs;
bfs.emplace_back(sy, sx);
while (!bfs.empty()) {
auto [y, x] = bfs.front();
bfs.pop_front();
eFOR(ny, max(0, y - 2), min(h - 1, y + 2))
eFOR(nx, max(0, x - 2), min(w - 1, x + 2)) {
if (s[ny][nx] == '.' &&
chmin(dp[ny][nx],
dp[y][x] + (abs(y - ny) + abs(x - nx) == 1 ? 0 : 1))) {
bfs.emplace_back(ny, nx);
}
}
}
print(dp[gy][gx] == INF ? -1 : dp[gy][gx]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template <class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define eFOR(i, a, n) for (int i = (a); i <= (n); ++i)
#define rFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define erFOR(i, a, n) for (int i = (n); i >= (a); --i)
#define each(i, a) for (auto &i : a)
#define SORT(a) sort(a.begin(), a.end())
#define rSORT(a) sort(a.rbegin(), a.rend())
#define fSORT(a, f) sort(a.begin(), a.end(), f)
#define all(a) a.begin(), a.end()
#define out(y, x) ((y) < 0 || h <= (y) || (x) < 0 || w <= (x))
#define tp(a, i) get<i>(a)
#define line cout << "-----------------------------\n"
#define ENDL(i, n) ((i) == (n)-1 ? "\n" : " ")
#define stop system("pause")
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template <class T> inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
inline void init() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
template <class T> inline istream &operator>>(istream &is, vector<T> &v) {
for (auto &a : v)
is >> a;
return is;
}
template <class T> inline istream &operator>>(istream &is, deque<T> &v) {
for (auto &a : v)
is >> a;
return is;
}
template <class T, class U>
inline istream &operator>>(istream &is, pair<T, U> &p) {
is >> p.first >> p.second;
return is;
}
template <class T> inline vector<T> vec(size_t a) { return vector<T>(a); }
template <class T> inline vector<T> defvec(T def, size_t a) {
return vector<T>(a, def);
}
template <class T, class... Ts> inline auto vec(size_t a, Ts... ts) {
return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}
template <class T, class... Ts> inline auto defvec(T def, size_t a, Ts... ts) {
return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...));
}
template <class T> inline void print(const T &a) { cout << a << "\n"; }
template <class T, class... Ts> inline void print(const T &a, const Ts &...ts) {
cout << a << " ";
print(ts...);
}
template <class T> inline void print(const vector<T> &v) {
for (int i = 0; i < v.size(); ++i)
cout << v[i] << (i == v.size() - 1 ? "\n" : " ");
}
template <class T> inline void print(const vector<vector<T>> &v) {
for (auto &a : v)
print(a);
}
inline string reversed(const string &s) {
string t = s;
reverse(all(t));
return t;
}
int main() {
init();
int h, w;
cin >> h >> w;
int sy, sx, gy, gx;
cin >> sy >> sx >> gy >> gx;
--sy, --sx, --gy, --gx;
VS s(h);
cin >> s;
auto dp = defvec<int>(INF, h, w);
dp[sy][sx] = 0;
deque<pair<int, int>> bfs;
bfs.emplace_back(sy, sx);
while (!bfs.empty()) {
auto [y, x] = bfs.front();
bfs.pop_front();
eFOR(ny, max(0, y - 2), min(h - 1, y + 2))
eFOR(nx, max(0, x - 2), min(w - 1, x + 2)) {
if (s[ny][nx] == '#')
continue;
int cost = (abs(y - ny) + abs(x - nx) == 1 ? 0 : 1);
if (cost == 1) {
if (chmin(dp[ny][nx], dp[y][x] + 1)) {
bfs.emplace_back(ny, nx);
}
} else {
if (chmin(dp[ny][nx], dp[y][x])) {
bfs.emplace_front(ny, nx);
}
}
}
}
print(dp[gy][gx] == INF ? -1 : dp[gy][gx]);
return 0;
} | replace | 111 | 115 | 111 | 122 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
char s[1005][1005];
int cnt[1005][1005];
int INF = 1001001001;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int H, W;
vector<vector<P>> X(100000);
void umeru(int h, int w, int d) {
cnt[h][w] = d;
X[d].push_back(make_pair(h, w));
rep(i, 4) {
int hn = h + dy[i];
if (hn < 0 || hn >= H)
continue;
int wn = w + dx[i];
if (wn < 0 || wn >= W)
continue;
if (s[hn][wn] == '.' && cnt[hn][wn] == INF) {
umeru(hn, wn, d);
}
}
return;
}
void warp(int d) {
int ss = X[d].size();
rep(k, ss) {
int h = X[d][k].first;
int w = X[d][k].second;
for (int i = -2; i <= 2; i++) {
int hn = h + i;
if (hn < 0 || hn >= H)
continue;
for (int j = -2; j <= 2; j++) {
int wn = w + j;
if (wn < 0 || wn >= W)
continue;
if (s[hn][wn] == '.' && cnt[hn][wn] == INF) {
umeru(hn, wn, d + 1);
}
}
}
}
return;
}
int main() {
cin >> H >> W;
int ch, cw;
cin >> ch >> cw;
ch--;
cw--;
int dh, dw;
cin >> dh >> dw;
dh--;
dw--;
rep(i, H) {
rep(j, W) { cin >> s[i][j]; }
}
rep(i, H) {
rep(j, W) {
if (s[i][j] == '#') {
cnt[i][j] = -1;
} else {
cnt[i][j] = INF;
}
}
}
umeru(ch, cw, 0);
/*rep(i, H){
rep(j, W){
cout << i << " " << j << " " << cnt[i][j] << endl;
}
}*/
int cnt2 = 0;
while (1) {
warp(cnt2);
if (cnt[dh][dw] != INF) {
break;
}
if (X[cnt2 + 1].size() == 0) {
cout << -1 << endl;
return 0;
}
cnt2++;
}
/*rep(i, H){
rep(j, W){
cout << i << " " << j << " " << cnt[i][j] << endl;
}
}*/
cout << cnt[dh][dw] << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
char s[1005][1005];
int cnt[1005][1005];
int INF = 1001001001;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int H, W;
vector<vector<P>> X(1000000);
void umeru(int h, int w, int d) {
cnt[h][w] = d;
X[d].push_back(make_pair(h, w));
rep(i, 4) {
int hn = h + dy[i];
if (hn < 0 || hn >= H)
continue;
int wn = w + dx[i];
if (wn < 0 || wn >= W)
continue;
if (s[hn][wn] == '.' && cnt[hn][wn] == INF) {
umeru(hn, wn, d);
}
}
return;
}
void warp(int d) {
int ss = X[d].size();
rep(k, ss) {
int h = X[d][k].first;
int w = X[d][k].second;
for (int i = -2; i <= 2; i++) {
int hn = h + i;
if (hn < 0 || hn >= H)
continue;
for (int j = -2; j <= 2; j++) {
int wn = w + j;
if (wn < 0 || wn >= W)
continue;
if (s[hn][wn] == '.' && cnt[hn][wn] == INF) {
umeru(hn, wn, d + 1);
}
}
}
}
return;
}
int main() {
cin >> H >> W;
int ch, cw;
cin >> ch >> cw;
ch--;
cw--;
int dh, dw;
cin >> dh >> dw;
dh--;
dw--;
rep(i, H) {
rep(j, W) { cin >> s[i][j]; }
}
rep(i, H) {
rep(j, W) {
if (s[i][j] == '#') {
cnt[i][j] = -1;
} else {
cnt[i][j] = INF;
}
}
}
umeru(ch, cw, 0);
/*rep(i, H){
rep(j, W){
cout << i << " " << j << " " << cnt[i][j] << endl;
}
}*/
int cnt2 = 0;
while (1) {
warp(cnt2);
if (cnt[dh][dw] != INF) {
break;
}
if (X[cnt2 + 1].size() == 0) {
cout << -1 << endl;
return 0;
}
cnt2++;
}
/*rep(i, H){
rep(j, W){
cout << i << " " << j << " " << cnt[i][j] << endl;
}
}*/
cout << cnt[dh][dw] << endl;
return 0;
} | replace | 15 | 16 | 15 | 16 | 0 | |
p02579 | C++ | Time Limit Exceeded | //{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name) \
template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
for (auto &e : v) {
in >> e;
}
return in;
}
//}}}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int h, w;
cin >> h >> w;
int ch, cw;
cin >> ch >> cw;
ch--;
cw--;
int dh, dw;
cin >> dh >> dw;
dh--;
dw--;
vector<string> ss(h);
cin >> ss;
vector<pair<int, int>> stack;
vector<pair<int, int>> nexts;
stack.emplace_back(ch, cw);
ss[ch][cw] = 'o';
int ans = 0;
while (ss[dh][dw] == '.') {
auto add = [&](int y, int x) {
if (y < 0 || h <= y || x < 0 || w <= x) {
return;
}
if (ss[y][x] != '.') {
return;
}
ss[y][x] = 'o';
stack.emplace_back(y, x);
};
if (stack.empty()) {
ans++;
for (auto &p : nexts) {
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
add(p.first + i, p.second + j);
}
}
}
if (stack.empty()) {
break;
}
}
auto p = stack.back();
nexts.push_back(p);
stack.pop_back();
add(p.first + 1, p.second);
add(p.first, p.second + 1);
add(p.first - 1, p.second);
add(p.first, p.second - 1);
}
cout << (ss[dh][dw] == '.' ? -1 : ans) << '\n';
}
| //{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name) \
template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
for (auto &e : v) {
in >> e;
}
return in;
}
//}}}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int h, w;
cin >> h >> w;
int ch, cw;
cin >> ch >> cw;
ch--;
cw--;
int dh, dw;
cin >> dh >> dw;
dh--;
dw--;
vector<string> ss(h);
cin >> ss;
vector<pair<int, int>> stack;
vector<pair<int, int>> nexts;
stack.emplace_back(ch, cw);
ss[ch][cw] = 'o';
int ans = 0;
while (ss[dh][dw] == '.') {
auto add = [&](int y, int x) {
if (y < 0 || h <= y || x < 0 || w <= x) {
return;
}
if (ss[y][x] != '.') {
return;
}
ss[y][x] = 'o';
stack.emplace_back(y, x);
};
if (stack.empty()) {
ans++;
for (auto &p : nexts) {
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
add(p.first + i, p.second + j);
}
}
}
nexts.clear();
if (stack.empty()) {
break;
}
}
auto p = stack.back();
nexts.push_back(p);
stack.pop_back();
add(p.first + 1, p.second);
add(p.first, p.second + 1);
add(p.first - 1, p.second);
add(p.first, p.second - 1);
}
cout << (ss[dh][dw] == '.' ? -1 : ans) << '\n';
}
| insert | 78 | 78 | 78 | 79 | TLE | |
p02579 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define print(x) cout << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
const int MAX_V = 1000100 * 25;
int h, w;
const ll INF = 1000000007;
struct edge {
int to;
ll cost;
};
vector<edge> G[MAX_V];
vl dijkstra(int V, int s) {
priority_queue<P, vector<P>, greater<P>> que;
vl d(V, INF);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.se;
if (d[v] < p.first)
continue;
rep(i, sz(G[v])) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
return d;
}
int main() {
cin >> h >> w;
int ch, cw;
cin >> ch >> cw;
int dh, dw;
cin >> dh >> dw;
vector<string> a(h);
rep(i, h) cin >> a[i];
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int wx[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
0, 1, 1, 1, 1, 2, 2, 2, 2, 2};
const int wy[] = {-2, -1, 0, 1, 2, -2, -1, 1, 2, -2,
2, -2, -1, 1, 2, -2, -1, 0, 1, 2};
rep(i, h) {
rep(j, w) {
rep(k, 4) {
int ny = i + dy[k];
int nx = j + dx[k];
if (nx < 0 || nx >= w || ny < 0 || ny >= h)
continue;
int from = i * w + j;
int to = ny * w + nx;
edge e1, e2;
e1.to = to;
e2.to = from;
if (a[i][j] == '#')
continue;
if (a[ny][nx] == '#')
continue;
e1.cost = 0;
e2.cost = 0;
G[from].push_back(e1);
G[to].push_back(e2);
}
rep(k, 20) {
int ny = i + wy[k];
int nx = j + wx[k];
if (nx < 0 || nx >= w || ny < 0 || ny >= h)
continue;
if (a[i][j] == '#')
continue;
if (a[ny][nx] == '#')
continue;
int from = i * w + j;
int to = ny * w + nx;
edge e1, e2;
e1.to = to;
e2.to = from;
e1.cost = 1;
e2.cost = 1;
G[from].push_back(e1);
G[to].push_back(e2);
}
}
}
ll ans = 1LL << 60;
ch--;
dh--;
auto dist = dijkstra(MAX_V, ch * w + cw - 1);
int k = dh * w + dw - 1;
mins(ans, dist[k]);
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define print(x) cout << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
const int MAX_V = 1000100;
int h, w;
const ll INF = 1000000007;
struct edge {
int to;
ll cost;
};
vector<edge> G[MAX_V];
vl dijkstra(int V, int s) {
priority_queue<P, vector<P>, greater<P>> que;
vl d(V, INF);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.se;
if (d[v] < p.first)
continue;
rep(i, sz(G[v])) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
return d;
}
int main() {
cin >> h >> w;
int ch, cw;
cin >> ch >> cw;
int dh, dw;
cin >> dh >> dw;
vector<string> a(h);
rep(i, h) cin >> a[i];
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int wx[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
0, 1, 1, 1, 1, 2, 2, 2, 2, 2};
const int wy[] = {-2, -1, 0, 1, 2, -2, -1, 1, 2, -2,
2, -2, -1, 1, 2, -2, -1, 0, 1, 2};
rep(i, h) {
rep(j, w) {
rep(k, 4) {
int ny = i + dy[k];
int nx = j + dx[k];
if (nx < 0 || nx >= w || ny < 0 || ny >= h)
continue;
int from = i * w + j;
int to = ny * w + nx;
edge e1, e2;
e1.to = to;
e2.to = from;
if (a[i][j] == '#')
continue;
if (a[ny][nx] == '#')
continue;
e1.cost = 0;
e2.cost = 0;
G[from].push_back(e1);
G[to].push_back(e2);
}
rep(k, 20) {
int ny = i + wy[k];
int nx = j + wx[k];
if (nx < 0 || nx >= w || ny < 0 || ny >= h)
continue;
if (a[i][j] == '#')
continue;
if (a[ny][nx] == '#')
continue;
int from = i * w + j;
int to = ny * w + nx;
edge e1, e2;
e1.to = to;
e2.to = from;
e1.cost = 1;
e2.cost = 1;
G[from].push_back(e1);
G[to].push_back(e2);
}
}
}
ll ans = 1LL << 60;
ch--;
dh--;
auto dist = dijkstra(MAX_V, ch * w + cw - 1);
int k = dh * w + dw - 1;
mins(ans, dist[k]);
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| replace | 38 | 39 | 38 | 39 | MLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#define lint long long
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define N 103
using namespace std;
int mar[N][N];
char grid[N][N];
int id = 1;
int n, m;
void go(int i, int j, int x) {
if (i < 0 || i >= n)
return;
if (j < 0 || j >= m)
return;
if (grid[i][j] == '#')
return;
if (mar[i][j] != 0)
return;
mar[i][j] = x;
go(i + 1, j, x);
go(i - 1, j, x);
go(i, j + 1, x);
go(i, j - 1, x);
}
int bi, bj;
int ei, ej;
int dist[N][N];
bool vis[N][N];
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int inf = 5000000;
bool valid(int i, int j) {
if (i < 0 || i >= n)
return 0;
if (j < 0 || j >= m)
return 0;
if (grid[i][j] == '#')
return 0;
if (vis[i][j])
return 0;
return 1;
}
int dij() {
if (!valid(bi, bj))
return inf;
dist[bi][bj] = 0;
priority_queue<piii, vector<piii>, greater<piii>> q;
q.push({0, {bi, bj}});
while (!q.empty()) {
int x, y;
tie(x, y) = q.top().nd;
int d = q.top().st;
vis[x][y] = 1;
if (tie(x, y) == tie(ei, ej))
return dist[ei][ej];
q.pop();
for (int i = x - 2; i <= x + 2; i++) {
for (int j = y - 2; j <= y + 2; j++) {
if (!valid(i, j))
continue;
int d2 = d + (mar[x][y] != mar[i][j]);
if (d2 < dist[i][j]) {
dist[i][j] = d2;
q.push({d2, {i, j}});
}
}
}
}
return dist[ei][ej];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
cin >> bi >> bj >> ei >> ej;
bi--;
bj--;
ei--;
ej--;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++)
grid[i][j] = s[j];
}
int t = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
dist[i][j] = inf;
if (grid[i][j] == '.' && mar[i][j] == 0) {
go(i, j, t);
t++;
}
}
auto aux = dij();
if (aux == inf)
cout << "-1\n";
else
cout << aux << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define lint long long
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define N 1003
using namespace std;
int mar[N][N];
char grid[N][N];
int id = 1;
int n, m;
void go(int i, int j, int x) {
if (i < 0 || i >= n)
return;
if (j < 0 || j >= m)
return;
if (grid[i][j] == '#')
return;
if (mar[i][j] != 0)
return;
mar[i][j] = x;
go(i + 1, j, x);
go(i - 1, j, x);
go(i, j + 1, x);
go(i, j - 1, x);
}
int bi, bj;
int ei, ej;
int dist[N][N];
bool vis[N][N];
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int inf = 5000000;
bool valid(int i, int j) {
if (i < 0 || i >= n)
return 0;
if (j < 0 || j >= m)
return 0;
if (grid[i][j] == '#')
return 0;
if (vis[i][j])
return 0;
return 1;
}
int dij() {
if (!valid(bi, bj))
return inf;
dist[bi][bj] = 0;
priority_queue<piii, vector<piii>, greater<piii>> q;
q.push({0, {bi, bj}});
while (!q.empty()) {
int x, y;
tie(x, y) = q.top().nd;
int d = q.top().st;
vis[x][y] = 1;
if (tie(x, y) == tie(ei, ej))
return dist[ei][ej];
q.pop();
for (int i = x - 2; i <= x + 2; i++) {
for (int j = y - 2; j <= y + 2; j++) {
if (!valid(i, j))
continue;
int d2 = d + (mar[x][y] != mar[i][j]);
if (d2 < dist[i][j]) {
dist[i][j] = d2;
q.push({d2, {i, j}});
}
}
}
}
return dist[ei][ej];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
cin >> bi >> bj >> ei >> ej;
bi--;
bj--;
ei--;
ej--;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++)
grid[i][j] = s[j];
}
int t = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
dist[i][j] = inf;
if (grid[i][j] == '.' && mar[i][j] == 0) {
go(i, j, t);
t++;
}
}
auto aux = dij();
if (aux == inf)
cout << "-1\n";
else
cout << aux << "\n";
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
// using namespace atcoder;
int main() {
int h, w, sy, sx, gy, gx;
cin >> h >> w >> sy >> sx >> gy >> gx;
sy--, sx--, gy--, gx--;
string s[h];
for (int i = 0; i < h; i++)
cin >> s[i];
int dis[h][w];
rep(i, h) rep(j, w) dis[i][j] = 1e9;
dis[sy][sx] = 0;
deque<pair<int, int>> q;
q.push_back({sy, sx});
int dy[] = {0, 0, -1, 1}, dx[] = {-1, 1, 0, 0};
while (q.size()) {
auto a = q.back();
q.pop_back();
int y = a.first, x = a.second;
rep(i, 4) {
int ny = y + dy[i], nx = x + dx[i];
if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
dis[y][x] < dis[ny][nx]) {
dis[ny][nx] = dis[y][x];
q.push_front({ny, nx});
}
}
for (int i = -2; i <= 2; i++)
for (int j = -2; j <= 2; j++) {
int ny = y + i, nx = x + j;
if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
dis[y][x] + 1 < dis[ny][nx]) {
dis[ny][nx] = dis[y][x] + 1;
q.push_back({ny, nx});
}
}
}
if (dis[gy][gx] == 1e9)
cout << -1 << endl;
else
cout << dis[gy][gx] << endl;
return 0;
} | #include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
// using namespace atcoder;
int main() {
int h, w, sy, sx, gy, gx;
cin >> h >> w >> sy >> sx >> gy >> gx;
sy--, sx--, gy--, gx--;
string s[h];
for (int i = 0; i < h; i++)
cin >> s[i];
int dis[h][w];
rep(i, h) rep(j, w) dis[i][j] = 1e9;
dis[sy][sx] = 0;
deque<pair<int, int>> q;
q.push_back({sy, sx});
int dy[] = {0, 0, -1, 1}, dx[] = {-1, 1, 0, 0};
while (q.size()) {
auto a = q.front();
q.pop_front();
int y = a.first, x = a.second;
rep(i, 4) {
int ny = y + dy[i], nx = x + dx[i];
if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
dis[y][x] < dis[ny][nx]) {
dis[ny][nx] = dis[y][x];
q.push_front({ny, nx});
}
}
for (int i = -2; i <= 2; i++)
for (int j = -2; j <= 2; j++) {
int ny = y + i, nx = x + j;
if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
dis[y][x] + 1 < dis[ny][nx]) {
dis[ny][nx] = dis[y][x] + 1;
q.push_back({ny, nx});
}
}
}
if (dis[gy][gx] == 1e9)
cout << -1 << endl;
else
cout << dis[gy][gx] << endl;
return 0;
} | replace | 21 | 23 | 21 | 23 | TLE | |
p02579 | C++ | Runtime Error | /* In The Name of Anton*/
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, a, n) for (int i = a; i <= n; i++)
#define repb(i, a, n) for (int i = a; i >= n; i--)
#define trav(a, x) for (auto a = x.begin(); a != x.end(); a++)
#define all(x) x.begin(), x.end()
#define fst first
#define snd second
#define pb push_back
#define mp make_pair
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
const int mxN = 1e3 + 5;
int a[mxN][mxN];
string s[mxN];
int temp;
int n, m;
vi adj[1000005];
void pre() {}
void dfs(int i, int j) {
a[i][j] = temp;
if (i + 1 < n) {
if (a[i + 1][j] == 0 && s[i + 1][j] == '.') {
dfs(i + 1, j);
}
}
if (i - 1 >= 0) {
if (a[i - 1][j] == 0 && s[i - 1][j] == '.') {
dfs(i - 1, j);
}
}
if (j - 1 >= 0) {
if (a[i][j - 1] == 0 && s[i][j - 1] == '.') {
dfs(i, j - 1);
}
}
if (j + 1 < m) {
if (a[i][j + 1] == 0 && s[i][j + 1] == '.') {
dfs(i, j + 1);
}
}
}
void solve() {
temp = 0;
cin >> n >> m;
for (int i = 0; i < mxN; i++) {
for (int j = 0; j < mxN; j++) {
a[i][j] = 0;
}
}
int sx, sy, dx, dy;
cin >> sx >> sy >> dx >> dy;
sx--;
sy--;
dx--;
dy--;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 0 && s[i][j] == '.') {
temp++;
dfs(i, j);
}
}
}
map<pii, int> m1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int ctemp1 = i - 2, ctemp2 = i + 2;
int dtemp1 = j - 2, dtemp2 = j + 2;
for (int x = ctemp1; x <= ctemp2; x++) {
for (int y = dtemp1; y <= dtemp2; y++) {
if (x >= 0 && x < n && y >= 0 && y < m) {
if (a[i][j] != a[x][y] && a[i][j] != 0 && a[x][y] != 0) {
if (m1[{a[i][j], a[x][y]}] == 0) {
m1[{a[i][j], a[x][y]}] = 1;
adj[a[i][j]].pb(a[x][y]);
}
}
}
}
}
}
}
int s = a[sx][sy];
int d = a[dx][dy];
int dis[mxN];
bool used[mxN];
memset(dis, -1, sizeof(dis));
memset(used, false, sizeof(used));
dis[s] = 0;
used[s] = true;
queue<int> q;
q.push(s);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u : adj[v]) {
if (!used[u]) {
used[u] = true;
q.push(u);
dis[u] = dis[v] + 1;
}
}
}
cout << dis[d] << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
pre();
int t;
t = 1;
rep(i, t) solve();
return 0;
}
| /* In The Name of Anton*/
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, a, n) for (int i = a; i <= n; i++)
#define repb(i, a, n) for (int i = a; i >= n; i--)
#define trav(a, x) for (auto a = x.begin(); a != x.end(); a++)
#define all(x) x.begin(), x.end()
#define fst first
#define snd second
#define pb push_back
#define mp make_pair
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
const int mxN = 1e3 + 5;
int a[mxN][mxN];
string s[mxN];
int temp;
int n, m;
vi adj[1000005];
void pre() {}
void dfs(int i, int j) {
a[i][j] = temp;
if (i + 1 < n) {
if (a[i + 1][j] == 0 && s[i + 1][j] == '.') {
dfs(i + 1, j);
}
}
if (i - 1 >= 0) {
if (a[i - 1][j] == 0 && s[i - 1][j] == '.') {
dfs(i - 1, j);
}
}
if (j - 1 >= 0) {
if (a[i][j - 1] == 0 && s[i][j - 1] == '.') {
dfs(i, j - 1);
}
}
if (j + 1 < m) {
if (a[i][j + 1] == 0 && s[i][j + 1] == '.') {
dfs(i, j + 1);
}
}
}
void solve() {
temp = 0;
cin >> n >> m;
for (int i = 0; i < mxN; i++) {
for (int j = 0; j < mxN; j++) {
a[i][j] = 0;
}
}
int sx, sy, dx, dy;
cin >> sx >> sy >> dx >> dy;
sx--;
sy--;
dx--;
dy--;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 0 && s[i][j] == '.') {
temp++;
dfs(i, j);
}
}
}
map<pii, int> m1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int ctemp1 = i - 2, ctemp2 = i + 2;
int dtemp1 = j - 2, dtemp2 = j + 2;
for (int x = ctemp1; x <= ctemp2; x++) {
for (int y = dtemp1; y <= dtemp2; y++) {
if (x >= 0 && x < n && y >= 0 && y < m) {
if (a[i][j] != a[x][y] && a[i][j] != 0 && a[x][y] != 0) {
if (m1[{a[i][j], a[x][y]}] == 0) {
m1[{a[i][j], a[x][y]}] = 1;
adj[a[i][j]].pb(a[x][y]);
}
}
}
}
}
}
}
int s = a[sx][sy];
int d = a[dx][dy];
int dis[1000005];
bool used[1000005];
memset(dis, -1, sizeof(dis));
memset(used, false, sizeof(used));
dis[s] = 0;
used[s] = true;
queue<int> q;
q.push(s);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u : adj[v]) {
if (!used[u]) {
used[u] = true;
q.push(u);
dis[u] = dis[v] + 1;
}
}
}
cout << dis[d] << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
pre();
int t;
t = 1;
rep(i, t) solve();
return 0;
}
| replace | 112 | 114 | 112 | 114 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
int main() {
int h, w;
cin >> h >> w;
int si, sj;
cin >> si >> sj;
int ti, tj;
cin >> ti >> tj;
si--;
sj--;
ti--;
tj--;
vector<string> s(h);
rep(i, h) cin >> s[i];
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<P> q;
dist[si][sj] = 0;
q.emplace_back(si, sj);
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
int d = dist[i][j];
q.pop_front();
rep(v, 4) {
int ni = i + di[v], nj = j + dj[v];
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d)
continue;
dist[ni][nj] = d;
q.emplace_front(ni, nj);
}
for (int ei = -2; ei <= 2; ei++) {
for (int ej = -2; ej <= 2; ej++) {
int ni = i + ei, nj = j + ej;
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d + 1)
continue;
dist[ni][nj] = d + 1;
q.emplace_front(ni, nj);
}
}
}
int ans = dist[ti][tj];
if (ans == INF)
ans = -1;
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
int main() {
int h, w;
cin >> h >> w;
int si, sj;
cin >> si >> sj;
int ti, tj;
cin >> ti >> tj;
si--;
sj--;
ti--;
tj--;
vector<string> s(h);
rep(i, h) cin >> s[i];
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<P> q;
dist[si][sj] = 0;
q.emplace_back(si, sj);
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
int d = dist[i][j];
q.pop_front();
rep(v, 4) {
int ni = i + di[v], nj = j + dj[v];
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d)
continue;
dist[ni][nj] = d;
q.emplace_front(ni, nj);
}
for (int ei = -2; ei <= 2; ei++) {
for (int ej = -2; ej <= 2; ej++) {
int ni = i + ei, nj = j + ej;
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d + 1)
continue;
dist[ni][nj] = d + 1;
q.emplace_back(ni, nj);
}
}
}
int ans = dist[ti][tj];
if (ans == INF)
ans = -1;
cout << ans << endl;
} | replace | 54 | 55 | 54 | 55 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int dr[] = {1, 0, -1, 0}; // S,SE,E,NE,N,NW,W,SW
int dc[] = {0, 1, 0, -1}; // neighbors
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int h, w, ch, cw, dh, dw;
cin >> h >> w >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
int dist[h][w];
char s[h][w];
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
cin >> s[i][j];
dist[i][j] = 1e9;
}
}
int cost = 0;
dist[ch][cw] = 0;
deque<pair<int, int>> q;
q.push_back(pair<int, int>(ch, cw));
while (!q.empty()) {
pair<int, int> u = q.front();
q.pop_front(); // queue: layer by layer!
int x = u.first;
int y = u.second;
cost = dist[x][y];
// cout<<x<<" "<<y<<" "<<cost<<endl;
for (int i = 0; i < 4; ++i) {
int r = x + dr[i];
int c = y + dc[i];
if (r < 0 || r >= h || c < 0 || c >= w)
continue; // outside
if (s[r][c] == '#')
continue;
if (dist[r][c] <= cost)
continue;
dist[r][c] = cost;
// cout<<r<<" "<<c<<" "<<cost<<endl;
q.push_back(pair<int, int>(r, c));
}
cost++;
for (int i = -2; i <= 2; ++i) {
for (int j = -2; j <= 2; ++j) {
int r = x + i;
int c = y + j;
if (r < 0 || r >= h || c < 0 || c >= w)
continue; // outside
if (s[r][c] == '#')
continue;
if (dist[r][c] <= cost)
continue;
dist[r][c] = cost;
q.push_back(pair<int, int>(r, c));
}
}
}
dist[dh][dw] == 1e9 ? cout << "-1" : cout << dist[dh][dw];
} | #include <bits/stdc++.h>
using namespace std;
int dr[] = {1, 0, -1, 0}; // S,SE,E,NE,N,NW,W,SW
int dc[] = {0, 1, 0, -1}; // neighbors
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int h, w, ch, cw, dh, dw;
cin >> h >> w >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
int dist[h][w];
char s[h][w];
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
cin >> s[i][j];
dist[i][j] = 1e9;
}
}
int cost = 0;
dist[ch][cw] = 0;
deque<pair<int, int>> q;
q.push_back(pair<int, int>(ch, cw));
while (!q.empty()) {
pair<int, int> u = q.front();
q.pop_front(); // queue: layer by layer!
int x = u.first;
int y = u.second;
cost = dist[x][y];
// cout<<x<<" "<<y<<" "<<cost<<endl;
for (int i = 0; i < 4; ++i) {
int r = x + dr[i];
int c = y + dc[i];
if (r < 0 || r >= h || c < 0 || c >= w)
continue; // outside
if (s[r][c] == '#')
continue;
if (dist[r][c] <= cost)
continue;
dist[r][c] = cost;
// cout<<r<<" "<<c<<" "<<cost<<endl;
q.push_front(pair<int, int>(r, c));
}
cost++;
for (int i = -2; i <= 2; ++i) {
for (int j = -2; j <= 2; ++j) {
int r = x + i;
int c = y + j;
if (r < 0 || r >= h || c < 0 || c >= w)
continue; // outside
if (s[r][c] == '#')
continue;
if (dist[r][c] <= cost)
continue;
dist[r][c] = cost;
q.push_back(pair<int, int>(r, c));
}
}
}
dist[dh][dw] == 1e9 ? cout << "-1" : cout << dist[dh][dw];
} | replace | 46 | 47 | 46 | 47 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define bug cout << "ok" << endl
const ll INF = 1e9 + 5, N = 1e2 + 5, mod = 1e9 + 7, M = 1e6 + 5;
char a[N][N];
int main() {
speed;
int n, m;
cin >> n >> m;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int d[N][N];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
d[i][j] = INF;
}
}
set<pair<int, pair<int, int>>> q;
d[x1][y1] = 0;
q.insert({0, {x1, y1}});
while (q.size()) {
int v1 = q.begin()->second.first, v2 = q.begin()->second.second;
q.erase(q.begin());
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i != 0 && j != 0)
continue;
if (v1 + i < 1 || v1 + i > n || v2 + j < 1 || v2 + j > m)
continue;
if (a[v1 + i][v2 + j] == '#')
continue;
if (d[v1][v2] < d[v1 + i][v2 + j]) {
q.erase({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
d[v1 + i][v2 + j] = d[v1][v2];
q.insert({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
}
}
}
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
if (v1 + i < 1 || v1 + i > n || v2 + j < 1 || v2 + j > m)
continue;
if (a[v1 + i][v2 + j] == '#')
continue;
if (d[v1][v2] + 1 < d[v1 + i][v2 + j]) {
q.erase({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
d[v1 + i][v2 + j] = d[v1][v2] + 1;
q.insert({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
}
}
}
}
if (d[x2][y2] == INF)
d[x2][y2] = -1;
cout << d[x2][y2] << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define bug cout << "ok" << endl
const ll INF = 1e9 + 5, N = 1e3 + 5, mod = 1e9 + 7, M = 1e6 + 5;
char a[N][N];
int main() {
speed;
int n, m;
cin >> n >> m;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int d[N][N];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
d[i][j] = INF;
}
}
set<pair<int, pair<int, int>>> q;
d[x1][y1] = 0;
q.insert({0, {x1, y1}});
while (q.size()) {
int v1 = q.begin()->second.first, v2 = q.begin()->second.second;
q.erase(q.begin());
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i != 0 && j != 0)
continue;
if (v1 + i < 1 || v1 + i > n || v2 + j < 1 || v2 + j > m)
continue;
if (a[v1 + i][v2 + j] == '#')
continue;
if (d[v1][v2] < d[v1 + i][v2 + j]) {
q.erase({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
d[v1 + i][v2 + j] = d[v1][v2];
q.insert({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
}
}
}
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
if (v1 + i < 1 || v1 + i > n || v2 + j < 1 || v2 + j > m)
continue;
if (a[v1 + i][v2 + j] == '#')
continue;
if (d[v1][v2] + 1 < d[v1 + i][v2 + j]) {
q.erase({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
d[v1 + i][v2 + j] = d[v1][v2] + 1;
q.insert({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
}
}
}
}
if (d[x2][y2] == INF)
d[x2][y2] = -1;
cout << d[x2][y2] << endl;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02579 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define inf 0x7fffffff
#define minn(x, y) x = min(x, y)
#define maxx(x, y) x = max(x, y)
using namespace std;
string s[1010];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int vis[1010][1010], n, m;
int dfs(int x, int y, int j) {
vis[x][y] = j;
for (int i = 0; i < 4; i++) {
if (dx[i] + x >= 0 && dx[i] + x < n && dy[i] + y >= 0 && dy[i] + y < m &&
s[x + dx[i]][y + dy[i]] == '.' && !vis[x + dx[i]][y + dy[i]]) {
dfs(x + dx[i], y + dy[i], j);
}
}
}
map<pii, int> ma;
int main() {
int i, j, k, x, y, z, t, l, c1, c2, d1, d2;
scanf("%d%d", &n, &m);
scanf("%d%d%d%d", &c1, &c2, &d1, &d2);
c1--;
c2--;
d1--;
d2--;
for (i = 0; i < n; i++) {
cin >> s[i];
}
x = 1;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (s[i][j] == '.' && !vis[i][j]) {
dfs(i, j, x);
x++;
}
}
}
vector<vector<int>> ve(x);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (s[i][j] == '.') {
for (k = max(0, i - 2); k <= min(i + 2, n - 1); k++) {
for (l = max(0, j - 2); l <= min(j + 2, m - 1); l++) {
if (vis[k][l] != vis[i][j] && vis[k][l]) {
if (!ma.count(mp(vis[k][l], vis[i][j])) &&
!ma.count(mp(vis[i][j], vis[k][l]))) {
ve[vis[k][l]].push_back(vis[i][j]);
ve[vis[i][j]].push_back(vis[k][l]);
ma[mp(vis[i][j], vis[k][l])] = 1;
}
}
}
}
}
}
}
queue<int> q;
vector<int> vi(x);
vector<int> d(x, 1e8);
q.push(vis[c1][c2]);
vi[vis[c1][c2]] = 1;
d[vis[c1][c2]] = 0;
while (!q.empty()) {
x = q.front();
q.pop();
vi[x] = 0;
if (x == vis[d1][d2]) {
printf("%d", d[x]);
return 0;
}
for (i = 0; i < ve[x].size(); i++) {
if (d[x] + 1 < d[ve[x][i]]) {
d[ve[x][i]] = d[x] + 1;
if (!vi[ve[x][i]]) {
vi[ve[x][i]] = 1;
q.push(ve[x][i]);
}
}
}
}
printf("-1");
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define inf 0x7fffffff
#define minn(x, y) x = min(x, y)
#define maxx(x, y) x = max(x, y)
using namespace std;
string s[1010];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int vis[1010][1010], n, m;
int dfs(int x, int y, int j) {
vis[x][y] = j;
for (int i = 0; i < 4; i++) {
if (dx[i] + x >= 0 && dx[i] + x < n && dy[i] + y >= 0 && dy[i] + y < m &&
s[x + dx[i]][y + dy[i]] == '.' && !vis[x + dx[i]][y + dy[i]]) {
dfs(x + dx[i], y + dy[i], j);
}
}
return 0;
}
map<pii, int> ma;
int main() {
int i, j, k, x, y, z, t, l, c1, c2, d1, d2;
scanf("%d%d", &n, &m);
scanf("%d%d%d%d", &c1, &c2, &d1, &d2);
c1--;
c2--;
d1--;
d2--;
for (i = 0; i < n; i++) {
cin >> s[i];
}
x = 1;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (s[i][j] == '.' && !vis[i][j]) {
dfs(i, j, x);
x++;
}
}
}
vector<vector<int>> ve(x);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (s[i][j] == '.') {
for (k = max(0, i - 2); k <= min(i + 2, n - 1); k++) {
for (l = max(0, j - 2); l <= min(j + 2, m - 1); l++) {
if (vis[k][l] != vis[i][j] && vis[k][l]) {
if (!ma.count(mp(vis[k][l], vis[i][j])) &&
!ma.count(mp(vis[i][j], vis[k][l]))) {
ve[vis[k][l]].push_back(vis[i][j]);
ve[vis[i][j]].push_back(vis[k][l]);
ma[mp(vis[i][j], vis[k][l])] = 1;
}
}
}
}
}
}
}
queue<int> q;
vector<int> vi(x);
vector<int> d(x, 1e8);
q.push(vis[c1][c2]);
vi[vis[c1][c2]] = 1;
d[vis[c1][c2]] = 0;
while (!q.empty()) {
x = q.front();
q.pop();
vi[x] = 0;
if (x == vis[d1][d2]) {
printf("%d", d[x]);
return 0;
}
for (i = 0; i < ve[x].size(); i++) {
if (d[x] + 1 < d[ve[x][i]]) {
d[ve[x][i]] = d[x] + 1;
if (!vi[ve[x][i]]) {
vi[ve[x][i]] = 1;
q.push(ve[x][i]);
}
}
}
}
printf("-1");
}
| insert | 32 | 32 | 32 | 33 | 0 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// dijkstra
struct edge {
int to;
int cost;
};
using P = pair<int, int>;
const int MAX_N = 1e6;
const int INF = 1e9;
vector<edge> G[MAX_N];
vector<int> dist(MAX_N, INF);
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P>> PQ;
dist[s] = 0;
PQ.push(P(0, s));
while (PQ.size()) {
P p = PQ.top();
PQ.pop();
int v = p.second; // from
if (dist[v] < p.first) { // 現在わかっている距離が更新できない
continue;
}
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
PQ.push(P(dist[e.to], e.to));
}
}
}
}
int main() {
int H, W, sx, sy, gx, gy;
cin >> H >> W >> sx >> sy >> gx >> gy;
vector<vector<char>> maze(H, vector<char>(W));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> maze[i][j];
}
}
vector<int> dx = {0, 1, 2, -1, -2};
vector<int> dy = {0, 1, 2, -1, -2};
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (maze[i][j] == '.') {
for (int k = 0; k < 5; k++) {
for (int l = 0; l < 5; l++) {
if (i + k >= 0 && i + k < H && j + l >= 0 && j + l < W) {
if (abs(dx[k]) + abs(dy[l]) == 1) {
int t = (i + dx[k]) * H + (j + dy[l]);
edge add;
add.to = t;
add.cost = 0;
G[i * H + j].push_back(add);
}
if (abs(dx[k]) + abs(dy[l]) > 1) {
int t = (i + dx[k]) * H + (j + dy[l]);
edge add;
add.to = t;
add.cost = 1;
G[i * H + j].push_back(add);
}
}
}
}
}
}
}
sx--;
sy--;
gx--;
gy--;
dijkstra(sx * H + sy);
if (dist[gx * H + gy] == INF) {
cout << -1 << endl;
} else {
cout << dist[gx * H + gy] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
// dijkstra
struct edge {
int to;
int cost;
};
using P = pair<int, int>;
const int MAX_N = 1e6;
const int INF = 1e9;
vector<edge> G[MAX_N];
vector<int> dist(MAX_N, INF);
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P>> PQ;
dist[s] = 0;
PQ.push(P(0, s));
while (PQ.size()) {
P p = PQ.top();
PQ.pop();
int v = p.second; // from
if (dist[v] < p.first) { // 現在わかっている距離が更新できない
continue;
}
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
PQ.push(P(dist[e.to], e.to));
}
}
}
}
int main() {
int H, W, sx, sy, gx, gy;
cin >> H >> W >> sx >> sy >> gx >> gy;
vector<vector<char>> maze(H, vector<char>(W));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> maze[i][j];
}
}
vector<int> dx = {0, 1, 2, -1, -2};
vector<int> dy = {0, 1, 2, -1, -2};
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (maze[i][j] == '.') {
for (int k = 0; k < 5; k++) {
for (int l = 0; l < 5; l++) {
if (i + dx[k] >= 0 && i + dx[k] < H && j + dy[l] >= 0 &&
j + dy[l] < W) {
if (abs(dx[k]) + abs(dy[l]) == 1) {
int t = (i + dx[k]) * H + (j + dy[l]);
edge add;
add.to = t;
add.cost = 0;
G[i * H + j].push_back(add);
}
if (abs(dx[k]) + abs(dy[l]) > 1) {
int t = (i + dx[k]) * H + (j + dy[l]);
edge add;
add.to = t;
add.cost = 1;
G[i * H + j].push_back(add);
}
}
}
}
}
}
}
sx--;
sy--;
gx--;
gy--;
dijkstra(sx * H + sy);
if (dist[gx * H + gy] == INF) {
cout << -1 << endl;
} else {
cout << dist[gx * H + gy] << endl;
}
}
| replace | 56 | 57 | 56 | 58 | -11 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using lint = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N, M;
cin >> N >> M;
int sx, sy, ex, ey;
cin >> sx >> sy >> ex >> ey;
sx--, sy--, ex--, ey--;
vector<string> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
deque<pair<pair<int, int>, int>> dq;
dq.push_back({{sx, sy}, 0});
vector<vector<int>> dist(N, vector<int>(M, -1));
dist[sx][sy] = 0;
while (!dq.empty()) {
int x = dq.front().first.first;
int y = dq.front().first.second;
int d = dq.front().second;
dq.pop_front();
if (dist[x][y] != d) {
continue;
}
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int nx = x + i, ny = y + j;
if (abs(i) + abs(j) == 1 && 0 <= nx && nx < N && 0 <= ny && ny < M &&
A[nx][ny] != '#') {
if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y]) {
dist[nx][ny] = dist[x][y];
dq.push_front({{nx, ny}, dist[nx][ny]});
}
}
if (0 <= nx && nx < N && 0 <= ny && ny < M && A[nx][ny] != '#') {
if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y] + 1) {
dist[nx][ny] = dist[x][y] + 1;
dq.push_front({{nx, ny}, dist[nx][ny]});
}
}
}
}
}
cout << dist[ex][ey] << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using lint = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N, M;
cin >> N >> M;
int sx, sy, ex, ey;
cin >> sx >> sy >> ex >> ey;
sx--, sy--, ex--, ey--;
vector<string> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
deque<pair<pair<int, int>, int>> dq;
dq.push_back({{sx, sy}, 0});
vector<vector<int>> dist(N, vector<int>(M, -1));
dist[sx][sy] = 0;
while (!dq.empty()) {
int x = dq.front().first.first;
int y = dq.front().first.second;
int d = dq.front().second;
dq.pop_front();
if (dist[x][y] != d) {
continue;
}
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int nx = x + i, ny = y + j;
if (abs(i) + abs(j) == 1 && 0 <= nx && nx < N && 0 <= ny && ny < M &&
A[nx][ny] != '#') {
if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y]) {
dist[nx][ny] = dist[x][y];
dq.push_front({{nx, ny}, dist[nx][ny]});
}
}
if (0 <= nx && nx < N && 0 <= ny && ny < M && A[nx][ny] != '#') {
if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y] + 1) {
dist[nx][ny] = dist[x][y] + 1;
dq.push_back({{nx, ny}, dist[nx][ny]});
}
}
}
}
}
cout << dist[ex][ey] << "\n";
return 0;
}
| replace | 42 | 43 | 42 | 43 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define pp pair<int, int>
#define ppn pair<int, pp>
#define F first
#define S second
#define N 300005
#define mod 1000000007
#define pi 3.14159265358979323846
using namespace std;
int n, m, a, b, xxx, yyy, ans;
char s[1005][1005];
int dp[1005][1005];
int xx[] = {-1, 0, 0, 1}, yy[] = {0, -1, 1, 0};
priority_queue<ppn, vector<ppn>, greater<ppn>> q;
bool check() {
q.push({0, {a - 1, b - 1}});
while (!q.empty()) {
ppn t = q.top();
q.pop();
int u = t.S.F;
int v = t.S.S;
int c = t.F;
dp[u][v] = 1;
// cout<<t.F<<" "<<t.S.F<<" "<<t.S.S<<endl;
if (u == xxx - 1 && v == yyy - 1) {
ans = c;
return true;
}
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
// if(!i&&!j)continue;
int x = u + i;
int y = v + j;
if (x < 0 || x >= n || y < 0 || y >= m)
continue;
if (dp[x][y] || s[x][y] == '#')
continue;
q.push({t.F + 1, {x, y}});
}
}
for (int i = 0; i < 4; i++) {
int x = u + xx[i];
int y = v + yy[i];
if (x < 0 || x >= n || y < 0 || y >= m)
continue;
if (dp[x][y] || s[x][y] == '#')
continue;
q.push({t.F, {x, y}});
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// memset(dp,-1,sizeof dp);
cin >> n >> m;
cin >> a >> b >> xxx >> yyy;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> s[i][j];
if (check())
cout << ans << endl;
else
cout << -1 << endl;
}
| #include <bits/stdc++.h>
#define ll long long
#define pp pair<int, int>
#define ppn pair<int, pp>
#define F first
#define S second
#define N 300005
#define mod 1000000007
#define pi 3.14159265358979323846
using namespace std;
int n, m, a, b, xxx, yyy, ans;
char s[1005][1005];
int dp[1005][1005];
int xx[] = {-1, 0, 0, 1}, yy[] = {0, -1, 1, 0};
priority_queue<ppn, vector<ppn>, greater<ppn>> q;
bool check() {
q.push({0, {a - 1, b - 1}});
while (!q.empty()) {
ppn t = q.top();
q.pop();
int u = t.S.F;
int v = t.S.S;
int c = t.F;
if (dp[u][v])
continue;
dp[u][v] = 1;
// cout<<t.F<<" "<<t.S.F<<" "<<t.S.S<<endl;
if (u == xxx - 1 && v == yyy - 1) {
ans = c;
return true;
}
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
// if(!i&&!j)continue;
int x = u + i;
int y = v + j;
if (x < 0 || x >= n || y < 0 || y >= m)
continue;
if (dp[x][y] || s[x][y] == '#')
continue;
q.push({t.F + 1, {x, y}});
}
}
for (int i = 0; i < 4; i++) {
int x = u + xx[i];
int y = v + yy[i];
if (x < 0 || x >= n || y < 0 || y >= m)
continue;
if (dp[x][y] || s[x][y] == '#')
continue;
q.push({t.F, {x, y}});
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// memset(dp,-1,sizeof dp);
cin >> n >> m;
cin >> a >> b >> xxx >> yyy;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> s[i][j];
if (check())
cout << ans << endl;
else
cout << -1 << endl;
}
| insert | 29 | 29 | 29 | 31 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_HW = 1005;
char S[MAX_HW][MAX_HW];
int ans[MAX_HW][MAX_HW];
int H, W;
queue<P> Q[10005];
int dx[5] = {-2, -1, 0, 1, 2}, dy[5] = {-2, -1, 0, 1, 2};
int dx2[4] = {0, 1, 0, -1}, dy2[4] = {1, 0, -1, 0};
int now = 0;
void init() {
rep(i, MAX_HW) {
rep(j, MAX_HW) { ans[i][j] = 1e9; }
}
}
void dfs(int i, int j, int c) {
// cout << i << " " << j << endl;
ans[i][j] = min(ans[i][j], c);
rep(ii, 5) {
rep(jj, 5) {
int newi = i + dx[ii], newj = j + dy[jj];
if (newi >= 0 && newi < H && newj >= 0 && newj < W &&
S[newi][newj] == '.') {
if (ans[newi][newj] > c) {
ans[newi][newj] = c + 1;
Q[now + 1].push(P(newi, newj));
}
}
}
}
rep(ii, 4) {
int newi = i + dx2[ii], newj = j + dy2[ii];
// cout << newi << " " << newj << endl;
if (newi >= 0 && newi < H && newj >= 0 && newj < W &&
S[newi][newj] == '.') {
// cout << "b" << endl;
if (ans[newi][newj] > c) {
dfs(newi, newj, c);
}
}
}
return;
}
int main() {
init();
cin >> H >> W;
int Ch, Cw, Dh, Dw;
cin >> Ch >> Cw >> Dh >> Dw;
rep(i, H) {
rep(j, W) { cin >> S[i][j]; }
}
dfs(Ch - 1, Cw - 1, 0);
int cnt = 1;
while (true) {
now++;
while (!Q[now].empty()) {
P p = Q[now].front();
Q[now].pop();
// cout << "aa" << p.first << " " << p.second << " " <<
// ans[p.first][p.second] << " " << cnt << endl;
if (ans[p.first][p.second] == cnt) {
// cout << "b" << endl;
dfs(p.first, p.second, cnt);
}
}
if (Q[now + 1].empty())
break;
cnt++;
}
/*
rep(i,H)
{
rep(j,W)
{
cout << ans[i][j] << " ";
}
cout << endl;
}
*/
if (ans[Dh - 1][Dw - 1] != 1e9)
cout << ans[Dh - 1][Dw - 1] << endl;
else
cout << -1 << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_HW = 1005;
char S[MAX_HW][MAX_HW];
int ans[MAX_HW][MAX_HW];
int H, W;
queue<P> Q[500000];
int dx[5] = {-2, -1, 0, 1, 2}, dy[5] = {-2, -1, 0, 1, 2};
int dx2[4] = {0, 1, 0, -1}, dy2[4] = {1, 0, -1, 0};
int now = 0;
void init() {
rep(i, MAX_HW) {
rep(j, MAX_HW) { ans[i][j] = 1e9; }
}
}
void dfs(int i, int j, int c) {
// cout << i << " " << j << endl;
ans[i][j] = min(ans[i][j], c);
rep(ii, 5) {
rep(jj, 5) {
int newi = i + dx[ii], newj = j + dy[jj];
if (newi >= 0 && newi < H && newj >= 0 && newj < W &&
S[newi][newj] == '.') {
if (ans[newi][newj] > c) {
ans[newi][newj] = c + 1;
Q[now + 1].push(P(newi, newj));
}
}
}
}
rep(ii, 4) {
int newi = i + dx2[ii], newj = j + dy2[ii];
// cout << newi << " " << newj << endl;
if (newi >= 0 && newi < H && newj >= 0 && newj < W &&
S[newi][newj] == '.') {
// cout << "b" << endl;
if (ans[newi][newj] > c) {
dfs(newi, newj, c);
}
}
}
return;
}
int main() {
init();
cin >> H >> W;
int Ch, Cw, Dh, Dw;
cin >> Ch >> Cw >> Dh >> Dw;
rep(i, H) {
rep(j, W) { cin >> S[i][j]; }
}
dfs(Ch - 1, Cw - 1, 0);
int cnt = 1;
while (true) {
now++;
while (!Q[now].empty()) {
P p = Q[now].front();
Q[now].pop();
// cout << "aa" << p.first << " " << p.second << " " <<
// ans[p.first][p.second] << " " << cnt << endl;
if (ans[p.first][p.second] == cnt) {
// cout << "b" << endl;
dfs(p.first, p.second, cnt);
}
}
if (Q[now + 1].empty())
break;
cnt++;
}
/*
rep(i,H)
{
rep(j,W)
{
cout << ans[i][j] << " ";
}
cout << endl;
}
*/
if (ans[Dh - 1][Dw - 1] != 1e9)
cout << ans[Dh - 1][Dw - 1] << endl;
else
cout << -1 << endl;
return 0;
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02579 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long double LD;
typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#ifdef LOCAL
#define DEBUG(x) \
{ cerr << "# " << #x << ": " << x << endl; }
#else
#define DEBUG(x)
#endif
mt19937_64 mt(time(0));
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
const int MAXN = 1000 + 10;
string mat[MAXN];
int n, m;
int graph[MAXN][MAXN];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
vi edges[MAXN];
int dist[MAXN];
void dfs(int i, int j, int id) {
graph[i][j] = id;
for (int k = 0; k < 4; k++) {
int ii = i + dx[k];
int jj = j + dy[k];
if (ii < 0 || ii >= n || jj < 0 || jj >= m)
continue;
if (mat[ii][jj] == '#' || graph[ii][jj] != 0)
continue;
dfs(ii, jj, id);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(10);
cout << fixed;
#ifdef LOCAL
freopen("../test.txt", "r", stdin);
// freopen("../output.txt", "w", stdout);
#endif
cin >> n >> m;
int a, b, c, d;
cin >> a >> b >> c >> d;
a--;
b--;
c--;
d--;
for (int i = 0; i < n; i++) {
cin >> mat[i];
}
int id = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (graph[i][j] != 0)
continue;
if (mat[i][j] == '#')
graph[i][j] = -1;
else
dfs(i, j, id++);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (graph[i][j] <= 0)
continue;
for (int u = -2; u <= 2; u++) {
if (i + u < 0 || i + u >= n)
continue;
for (int v = -2; v <= 2; v++) {
if (j + v < 0 || j + v >= m)
continue;
if (graph[i + u][j + v] <= 0 || graph[i + u][j + v] == graph[i][j])
continue;
edges[graph[i][j]].push_back(graph[i + u][j + v]);
edges[graph[i + u][j + v]].push_back(graph[i][j]);
}
}
}
}
memset(dist, -1, sizeof(dist));
queue<int> q;
q.push(graph[a][b]);
dist[graph[a][b]] = 0;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int nxt : edges[cur]) {
if (dist[nxt] != -1)
continue;
dist[nxt] = dist[cur] + 1;
q.push(nxt);
}
}
cout << dist[graph[c][d]] << '\n';
/*---------------------------------------Coding Space
* End------------------------------------------*/
#ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
return 0;
}
/* author: txingml */
/* picture is from hamayanhamayan(CF handle) */
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long double LD;
typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#ifdef LOCAL
#define DEBUG(x) \
{ cerr << "# " << #x << ": " << x << endl; }
#else
#define DEBUG(x)
#endif
mt19937_64 mt(time(0));
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
const int MAXN = 1000 + 10;
string mat[MAXN];
int n, m;
int graph[MAXN][MAXN];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
vi edges[MAXN * MAXN];
int dist[MAXN * MAXN];
void dfs(int i, int j, int id) {
graph[i][j] = id;
for (int k = 0; k < 4; k++) {
int ii = i + dx[k];
int jj = j + dy[k];
if (ii < 0 || ii >= n || jj < 0 || jj >= m)
continue;
if (mat[ii][jj] == '#' || graph[ii][jj] != 0)
continue;
dfs(ii, jj, id);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(10);
cout << fixed;
#ifdef LOCAL
freopen("../test.txt", "r", stdin);
// freopen("../output.txt", "w", stdout);
#endif
cin >> n >> m;
int a, b, c, d;
cin >> a >> b >> c >> d;
a--;
b--;
c--;
d--;
for (int i = 0; i < n; i++) {
cin >> mat[i];
}
int id = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (graph[i][j] != 0)
continue;
if (mat[i][j] == '#')
graph[i][j] = -1;
else
dfs(i, j, id++);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (graph[i][j] <= 0)
continue;
for (int u = -2; u <= 2; u++) {
if (i + u < 0 || i + u >= n)
continue;
for (int v = -2; v <= 2; v++) {
if (j + v < 0 || j + v >= m)
continue;
if (graph[i + u][j + v] <= 0 || graph[i + u][j + v] == graph[i][j])
continue;
edges[graph[i][j]].push_back(graph[i + u][j + v]);
edges[graph[i + u][j + v]].push_back(graph[i][j]);
}
}
}
}
memset(dist, -1, sizeof(dist));
queue<int> q;
q.push(graph[a][b]);
dist[graph[a][b]] = 0;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int nxt : edges[cur]) {
if (dist[nxt] != -1)
continue;
dist[nxt] = dist[cur] + 1;
q.push(nxt);
}
}
cout << dist[graph[c][d]] << '\n';
/*---------------------------------------Coding Space
* End------------------------------------------*/
#ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
return 0;
}
/* author: txingml */
/* picture is from hamayanhamayan(CF handle) */
| replace | 47 | 49 | 47 | 49 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 7;
int h, w, ch, cw, dh, dw;
char grid[N][N];
int visited[N][N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> h >> w >> ch >> cw >> dh >> dw;
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++)
cin >> grid[i][j];
queue<tuple<int, int, int>> bfs_curr;
queue<tuple<int, int, int>> bfs_next;
bfs_curr.push({ch, cw, 0});
visited[ch][cw] = 1;
while (!bfs_curr.empty()) {
int x, y, dist;
tie(x, y, dist) = bfs_curr.front();
if (x == dh && y == dw)
return cout << dist << "\n", 0;
bfs_curr.pop();
if (x > 1 && grid[x - 1][y] == '.' && !visited[x - 1][y])
bfs_curr.push({x - 1, y, dist}), visited[x - 1][y] = 1;
if (x < h && grid[x + 1][y] == '.' && !visited[x + 1][y])
bfs_curr.push({x + 1, y, dist}), visited[x + 1][y] = 1;
if (y > 1 && grid[x][y - 1] == '.' && !visited[x][y - 1])
bfs_curr.push({x, y - 1, dist}), visited[x][y - 1] = 1;
if (y < w && grid[x][y + 1] == '.' && !visited[x][y + 1])
bfs_curr.push({x, y + 1, dist}), visited[x][y + 1] = 1;
for (int i = x - 2; i <= x + 2; i++)
for (int j = y - 2; j <= y + 2; j++)
if (i >= 1 && i <= h && j >= 1 && j <= w && !visited[i][j] &&
grid[i][j] == '.')
bfs_next.push({i, j, dist + 1});
if (bfs_curr.empty()) {
bfs_curr = bfs_next;
bfs_next = queue<tuple<int, int, int>>();
}
}
cout << -1;
return 0;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 7;
int h, w, ch, cw, dh, dw;
char grid[N][N];
int visited[N][N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> h >> w >> ch >> cw >> dh >> dw;
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++)
cin >> grid[i][j];
queue<tuple<int, int, int>> bfs_curr;
queue<tuple<int, int, int>> bfs_next;
bfs_curr.push({ch, cw, 0});
visited[ch][cw] = 1;
while (!bfs_curr.empty()) {
int x, y, dist;
tie(x, y, dist) = bfs_curr.front();
if (x == dh && y == dw)
return cout << dist << "\n", 0;
bfs_curr.pop();
if (x > 1 && grid[x - 1][y] == '.' && !visited[x - 1][y])
bfs_curr.push({x - 1, y, dist}), visited[x - 1][y] = 1;
if (x < h && grid[x + 1][y] == '.' && !visited[x + 1][y])
bfs_curr.push({x + 1, y, dist}), visited[x + 1][y] = 1;
if (y > 1 && grid[x][y - 1] == '.' && !visited[x][y - 1])
bfs_curr.push({x, y - 1, dist}), visited[x][y - 1] = 1;
if (y < w && grid[x][y + 1] == '.' && !visited[x][y + 1])
bfs_curr.push({x, y + 1, dist}), visited[x][y + 1] = 1;
for (int i = x - 2; i <= x + 2; i++)
for (int j = y - 2; j <= y + 2; j++)
if (i >= 1 && i <= h && j >= 1 && j <= w && !visited[i][j] &&
grid[i][j] == '.')
bfs_next.push({i, j, dist + 1});
if (bfs_curr.empty()) {
while (!bfs_next.empty()) {
tie(x, y, dist) = bfs_next.front();
visited[x][y] = 1;
bfs_curr.push({x, y, dist});
bfs_next.pop();
}
}
}
cout << -1;
return 0;
} | replace | 46 | 48 | 46 | 52 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[ ";
for (auto v : V)
os << v << " ";
return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 998244353;
const int maxn = 3005;
#define endl '\n'
#define ld long double
#define int ll
#define all(x) (x).begin(), (x).end()
typedef long double dbl;
#define pw(x) (1LL << (x))
#define forn(i, n) for (int i = 0; i < (n); i++)
string s[maxn];
int h, w;
int get(int i, int j) { return i * w + j; }
int isvalid(int i, int j) {
return (i >= 0 and j >= 0 and i < h and j < w and s[i][j] != '#');
}
vector<pair<int, int>> g[maxn];
vector<int> djikstra(ll src, ll n) {
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
pq;
vector<ll> dist(n + 1, 1e18), vis(n + 1, 0);
pq.push(make_pair(0, src));
dist[src] = 0;
while (!pq.empty()) {
ll u = pq.top().second;
pq.pop();
if (vis[u])
continue;
vis[u] = 1;
for (auto i : g[u]) {
ll v = i.first;
ll weight = i.second;
if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v], v));
}
}
}
return dist;
}
int32_t main() {
IOS
cin >>
h >> w;
int ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
for (int i = 0; i < h; i++) {
cin >> s[i];
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
for (int ii = max(0ll, i - 2); ii <= min(h - 1, i + 2); ii++) {
for (int jj = max(0ll, j - 2); jj <= min(w - 1, j + 2); jj++) {
if (isvalid(i, j) and isvalid(ii, jj))
g[get(i, j)].push_back({get(ii, jj), 1});
}
}
if (isvalid(i, j) and isvalid(i - 1, j))
g[get(i, j)].push_back({get(i - 1, j), 0});
if (isvalid(i, j) and isvalid(i, j - 1))
g[get(i, j)].push_back({get(i, j - 1), 0});
if (isvalid(i, j) and isvalid(i + 1, j))
g[get(i, j)].push_back({get(i + 1, j), 0});
if (isvalid(i, j) and isvalid(i, j + 1))
g[get(i, j)].push_back({get(i, j + 1), 0});
}
}
vector<int> dist = djikstra(get(ch, cw), h * w);
if (dist[get(dh, dw)] < 1e15) {
cout << dist[get(dh, dw)] << endl;
} else {
cout << -1 << endl;
}
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[ ";
for (auto v : V)
os << v << " ";
return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 998244353;
const int maxn = 3005;
#define endl '\n'
#define ld long double
#define int ll
#define all(x) (x).begin(), (x).end()
typedef long double dbl;
#define pw(x) (1LL << (x))
#define forn(i, n) for (int i = 0; i < (n); i++)
string s[maxn];
int h, w;
int get(int i, int j) { return i * w + j; }
int isvalid(int i, int j) {
return (i >= 0 and j >= 0 and i < h and j < w and s[i][j] != '#');
}
vector<pair<int, int>> g[maxn * maxn];
vector<int> djikstra(ll src, ll n) {
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
pq;
vector<ll> dist(n + 1, 1e18), vis(n + 1, 0);
pq.push(make_pair(0, src));
dist[src] = 0;
while (!pq.empty()) {
ll u = pq.top().second;
pq.pop();
if (vis[u])
continue;
vis[u] = 1;
for (auto i : g[u]) {
ll v = i.first;
ll weight = i.second;
if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v], v));
}
}
}
return dist;
}
int32_t main() {
IOS
cin >>
h >> w;
int ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
for (int i = 0; i < h; i++) {
cin >> s[i];
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
for (int ii = max(0ll, i - 2); ii <= min(h - 1, i + 2); ii++) {
for (int jj = max(0ll, j - 2); jj <= min(w - 1, j + 2); jj++) {
if (isvalid(i, j) and isvalid(ii, jj))
g[get(i, j)].push_back({get(ii, jj), 1});
}
}
if (isvalid(i, j) and isvalid(i - 1, j))
g[get(i, j)].push_back({get(i - 1, j), 0});
if (isvalid(i, j) and isvalid(i, j - 1))
g[get(i, j)].push_back({get(i, j - 1), 0});
if (isvalid(i, j) and isvalid(i + 1, j))
g[get(i, j)].push_back({get(i + 1, j), 0});
if (isvalid(i, j) and isvalid(i, j + 1))
g[get(i, j)].push_back({get(i, j + 1), 0});
}
}
vector<int> dist = djikstra(get(ch, cw), h * w);
if (dist[get(dh, dw)] < 1e15) {
cout << dist[get(dh, dw)] << endl;
} else {
cout << -1 << endl;
}
}
| replace | 55 | 56 | 55 | 56 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int ddx[] = {0, 1, 1, 0, 2, 1, -1, -2, 2, 2,
-2, -2, 1, -1, 1, -1, 2, 2, -2, -2};
const int ddy[] = {2, 1, -1, -2, 0, 1, 1, 0, 2, -2,
2, -2, 2, 2, -2, -2, 1, -1, 1, -1};
struct dat {
int d, r, c;
};
int main() {
int H, W;
cin >> H >> W;
int ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
vector<string> S(H);
for (int n = 0; n < H; ++n) {
cin >> S[n];
}
vector<vector<int>> D(H, vector<int>(W, 1e9)); // 回数
auto c = [](dat a, dat b) { return a.d > b.d; };
priority_queue<dat, vector<dat>, decltype(c)> q(c);
q.push({0, ch, cw});
D[ch][cw] = 0;
while (!q.empty()) {
int x = q.top().r;
int y = q.top().c;
int z = q.top().d;
q.pop();
// 範囲内かつ0を4セット
for (int l = 0; l < 4; ++l) {
int x1 = x + dx[l];
int y1 = y + dy[l];
if ((0 <= x1) && (H > x1) && (0 <= y1) && (W > y1) &&
('.' == S[x1][y1])) {
if (D[x1][y1] > D[x][y]) {
D[x1][y1] = D[x][y];
q.push({D[x1][y1], x1, y1});
}
}
}
// 範囲内かつ0を20セット
for (int l = 0; l < 20; ++l) {
int x1 = x + ddx[l];
int y1 = y + ddy[l];
if ((0 <= x1) && (H > x1) && (0 <= y1) && (W > y1) &&
('.' == S[x1][y1])) {
if (D[x1][y1] > D[x][y]) {
D[x1][y1] = D[x][y] + 1;
q.push({D[x1][y1], x1, y1});
}
}
}
}
if (1e9 == D[dh][dw]) {
cout << -1 << endl;
} else {
cout << D[dh][dw] << endl;
}
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int ddx[] = {0, 1, 1, 0, 2, 1, -1, -2, 2, 2,
-2, -2, 1, -1, 1, -1, 2, 2, -2, -2};
const int ddy[] = {2, 1, -1, -2, 0, 1, 1, 0, 2, -2,
2, -2, 2, 2, -2, -2, 1, -1, 1, -1};
struct dat {
int d, r, c;
};
int main() {
int H, W;
cin >> H >> W;
int ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
vector<string> S(H);
for (int n = 0; n < H; ++n) {
cin >> S[n];
}
vector<vector<int>> D(H, vector<int>(W, 1e9)); // 回数
auto c = [](dat a, dat b) { return a.d > b.d; };
priority_queue<dat, vector<dat>, decltype(c)> q(c);
q.push({0, ch, cw});
D[ch][cw] = 0;
while (!q.empty()) {
int x = q.top().r;
int y = q.top().c;
int z = q.top().d;
q.pop();
if (D[x][y] < z)
continue;
if (x == dh && y == dw)
continue;
// 範囲内かつ0を4セット
for (int l = 0; l < 4; ++l) {
int x1 = x + dx[l];
int y1 = y + dy[l];
if ((0 <= x1) && (H > x1) && (0 <= y1) && (W > y1) &&
('.' == S[x1][y1])) {
if (D[x1][y1] > D[x][y]) {
D[x1][y1] = D[x][y];
q.push({D[x1][y1], x1, y1});
}
}
}
// 範囲内かつ0を20セット
for (int l = 0; l < 20; ++l) {
int x1 = x + ddx[l];
int y1 = y + ddy[l];
if ((0 <= x1) && (H > x1) && (0 <= y1) && (W > y1) &&
('.' == S[x1][y1])) {
if (D[x1][y1] > D[x][y]) {
D[x1][y1] = D[x][y] + 1;
q.push({D[x1][y1], x1, y1});
}
}
}
}
if (1e9 == D[dh][dw]) {
cout << -1 << endl;
} else {
cout << D[dh][dw] << endl;
}
return 0;
}
| insert | 39 | 39 | 39 | 43 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;
int main() {
int h, w, ch, cw, dh, dw;
cin >> h >> w >> ch >> cw >> dh >> dw;
char s[h + 2][w + 2];
int mp[h + 2][w + 2];
int lf[h + 2][w + 2];
for (int i = 0; i < h + 2; i++) {
for (int j = 0; j < w + 2; j++) {
if (i == 0 || i == h + 1 || j == 0 || j == w + 1) {
s[i][j] = '#';
} else {
cin >> s[i][j];
}
mp[i][j] = 999999999;
lf[i][j] = 0;
}
}
queue<pair<int, int>> q_walk;
queue<pair<int, int>> q_walked;
q_walk.push(make_pair(ch, cw));
int dir_x[] = {1, 0, -1, 0};
int dir_y[] = {0, 1, 0, -1};
int ans = -1;
int wrp_num = 0;
do {
while (q_walk.size() != 0) {
int x, y;
auto pos = q_walk.front();
x = pos.first;
y = pos.second;
q_walk.pop();
// cout << "x:" << x << " y:" << y << " wrp:" << wrp_num << endl;
if (mp[x][y] < wrp_num)
continue;
q_walked.push(make_pair(x, y));
if (x == dh && y == dw) {
ans = wrp_num;
break;
}
mp[x][y] = wrp_num;
lf[x][y] = 1;
for (int i = 0; i < 4; i++) {
if (s[x + dir_x[i]][y + dir_y[i]] == '.' &&
lf[x + dir_x[i]][y + dir_y[i]] == 0) {
q_walk.push(make_pair(x + dir_x[i], y + dir_y[i]));
}
}
}
if (ans != -1)
break;
while (q_walked.size() > 0) {
auto pos = q_walked.front();
int x = pos.first;
int y = pos.second;
q_walked.pop();
for (int i = -2; i < 3; i++) {
for (int j = -2; j < 3; j++) {
if (x + i < 0 || x + i > h + 1 || y + j < 0 || y + j > w + 1)
continue;
if (s[x + i][y + j] == '.' && lf[x + i][y + j] == 0) {
// cout << "x_i:" << x+i << " y_j:" << y+j << endl;
q_walk.push(make_pair(x + i, y + j));
lf[x + i][y + j] = 1;
}
}
}
}
wrp_num++;
if (q_walk.size() == 0)
break;
} while (true);
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;
int main() {
int h, w, ch, cw, dh, dw;
cin >> h >> w >> ch >> cw >> dh >> dw;
char s[h + 2][w + 2];
int mp[h + 2][w + 2];
int lf[h + 2][w + 2];
for (int i = 0; i < h + 2; i++) {
for (int j = 0; j < w + 2; j++) {
if (i == 0 || i == h + 1 || j == 0 || j == w + 1) {
s[i][j] = '#';
} else {
cin >> s[i][j];
}
mp[i][j] = 999999999;
lf[i][j] = 0;
}
}
queue<pair<int, int>> q_walk;
queue<pair<int, int>> q_walked;
q_walk.push(make_pair(ch, cw));
int dir_x[] = {1, 0, -1, 0};
int dir_y[] = {0, 1, 0, -1};
int ans = -1;
int wrp_num = 0;
do {
while (q_walk.size() != 0) {
int x, y;
auto pos = q_walk.front();
x = pos.first;
y = pos.second;
q_walk.pop();
// cout << "x:" << x << " y:" << y << " wrp:" << wrp_num << endl;
if (mp[x][y] < wrp_num)
continue;
q_walked.push(make_pair(x, y));
if (x == dh && y == dw) {
ans = wrp_num;
break;
}
mp[x][y] = wrp_num;
lf[x][y] = 1;
for (int i = 0; i < 4; i++) {
if (s[x + dir_x[i]][y + dir_y[i]] == '.' &&
lf[x + dir_x[i]][y + dir_y[i]] == 0) {
q_walk.push(make_pair(x + dir_x[i], y + dir_y[i]));
lf[x + dir_x[i]][y + dir_y[i]] = 1;
}
}
}
if (ans != -1)
break;
while (q_walked.size() > 0) {
auto pos = q_walked.front();
int x = pos.first;
int y = pos.second;
q_walked.pop();
for (int i = -2; i < 3; i++) {
for (int j = -2; j < 3; j++) {
if (x + i < 0 || x + i > h + 1 || y + j < 0 || y + j > w + 1)
continue;
if (s[x + i][y + j] == '.' && lf[x + i][y + j] == 0) {
// cout << "x_i:" << x+i << " y_j:" << y+j << endl;
q_walk.push(make_pair(x + i, y + j));
lf[x + i][y + j] = 1;
}
}
}
}
wrp_num++;
if (q_walk.size() == 0)
break;
} while (true);
cout << ans << endl;
return 0;
} | insert | 67 | 67 | 67 | 68 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef long double db;
#define pb push_back
#define mp make_pair
#define ub(v, val) upper_bound(v.begin(), v.end(), val)
#define np(str) next_permutation(str.begin(), str.end())
#define lb(v, val) lower_bound(v.begin(), v.end(), val)
#define sortv(vec) sort(vec.begin(), vec.end())
#define rev(p) reverse(p.begin(), p.end());
#define v vector
#define PI 3.1415926535
#define len length()
#define repc(i, s, e) for (ll i = s; i < e; i++)
#define fi first
#define se second
#define mset(a, val) memset(a, val, sizeof(a));
#define mt make_tuple
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
const ll M = 1e9 + 7;
ll INF = 1e18;
ll n, m;
v<v<char>> s;
v<v<ll>> used;
ll cnt = 0;
void dfs(ll i, ll j) {
used[i][j] = cnt;
v<pair<ll, ll>> move = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
for (auto mv : move) {
ll x = i + mv.fi;
ll y = j + mv.se;
if (x >= 0 && x < n && y >= 0 && y < m) {
if (s[x][y] == '.' && used[x][y] == 0) {
dfs(x, y);
}
}
}
}
v<v<ll>> adj;
v<bool> vis;
int main() {
// your code goes here
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll i, j, t, k, x, y, z, N;
cin >> n >> m;
ll x1, x2, y1, y2;
cin >> x1 >> y1;
cin >> x2 >> y2;
s.resize(n, v<char>(m));
used.assign(n, v<ll>(m, 0));
rep(i, n) {
rep(j, m) { cin >> s[i][j]; }
}
rep(i, n) {
rep(j, m) {
if (used[i][j] == 0 && s[i][j] == '.') {
cnt++;
dfs(i, j);
}
}
}
adj.resize(cnt);
vis.assign(cnt, false);
map<pair<ll, ll>, ll> edge;
rep(i, n) {
rep(j, m) {
if (s[i][j] == '.') {
for (x = i - 2; x <= i + 2; x++) {
for (y = j - 2; y <= j + 2; y++) {
if (x >= 0 && x < n && y >= 0 && y < m) {
if (s[x][y] == '.') {
if (edge[{min(used[x][y], used[i][j]),
max(used[x][y], used[i][j])}] == 0 &&
used[x][y] != used[i][j]) {
edge[{min(used[x][y], used[i][j]),
max(used[x][y], used[i][j])}] = 1;
adj[used[x][y] - 1].pb(used[i][j] - 1);
adj[used[i][j] - 1].pb(used[x][y] - 1);
}
}
}
}
}
}
}
}
ll s = used[x1 - 1][y1 - 1] - 1;
queue<ll> q;
v<ll> d(n, INF);
d[s] = 0;
q.push(s);
vis[s] = true;
while (!q.empty()) {
ll v = q.front();
q.pop();
for (ll u : adj[v]) {
if (!vis[u]) {
vis[u] = true;
q.push(u);
d[u] = d[v] + 1;
}
}
}
y = used[x2 - 1][y2 - 1] - 1;
if (d[y] == INF)
cout << -1;
else
cout << d[y];
return 0;
} | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef long double db;
#define pb push_back
#define mp make_pair
#define ub(v, val) upper_bound(v.begin(), v.end(), val)
#define np(str) next_permutation(str.begin(), str.end())
#define lb(v, val) lower_bound(v.begin(), v.end(), val)
#define sortv(vec) sort(vec.begin(), vec.end())
#define rev(p) reverse(p.begin(), p.end());
#define v vector
#define PI 3.1415926535
#define len length()
#define repc(i, s, e) for (ll i = s; i < e; i++)
#define fi first
#define se second
#define mset(a, val) memset(a, val, sizeof(a));
#define mt make_tuple
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
const ll M = 1e9 + 7;
ll INF = 1e18;
ll n, m;
v<v<char>> s;
v<v<ll>> used;
ll cnt = 0;
void dfs(ll i, ll j) {
used[i][j] = cnt;
v<pair<ll, ll>> move = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
for (auto mv : move) {
ll x = i + mv.fi;
ll y = j + mv.se;
if (x >= 0 && x < n && y >= 0 && y < m) {
if (s[x][y] == '.' && used[x][y] == 0) {
dfs(x, y);
}
}
}
}
v<v<ll>> adj;
v<bool> vis;
int main() {
// your code goes here
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll i, j, t, k, x, y, z, N;
cin >> n >> m;
ll x1, x2, y1, y2;
cin >> x1 >> y1;
cin >> x2 >> y2;
s.resize(n, v<char>(m));
used.assign(n, v<ll>(m, 0));
rep(i, n) {
rep(j, m) { cin >> s[i][j]; }
}
rep(i, n) {
rep(j, m) {
if (used[i][j] == 0 && s[i][j] == '.') {
cnt++;
dfs(i, j);
}
}
}
adj.resize(cnt);
vis.assign(cnt, false);
map<pair<ll, ll>, ll> edge;
rep(i, n) {
rep(j, m) {
if (s[i][j] == '.') {
for (x = i - 2; x <= i + 2; x++) {
for (y = j - 2; y <= j + 2; y++) {
if (x >= 0 && x < n && y >= 0 && y < m) {
if (s[x][y] == '.') {
if (edge[{min(used[x][y], used[i][j]),
max(used[x][y], used[i][j])}] == 0 &&
used[x][y] != used[i][j]) {
edge[{min(used[x][y], used[i][j]),
max(used[x][y], used[i][j])}] = 1;
adj[used[x][y] - 1].pb(used[i][j] - 1);
adj[used[i][j] - 1].pb(used[x][y] - 1);
}
}
}
}
}
}
}
}
ll s = used[x1 - 1][y1 - 1] - 1;
queue<ll> q;
v<ll> d(cnt, INF);
d[s] = 0;
q.push(s);
vis[s] = true;
while (!q.empty()) {
ll v = q.front();
q.pop();
for (ll u : adj[v]) {
if (!vis[u]) {
vis[u] = true;
q.push(u);
d[u] = d[v] + 1;
}
}
}
y = used[x2 - 1][y2 - 1] - 1;
if (d[y] == INF)
cout << -1;
else
cout << d[y];
return 0;
} | replace | 101 | 102 | 101 | 102 | -11 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
int x, y;
cin >> x >> y;
int t_x, t_y;
cin >> t_x >> t_y;
x--, y--;
t_x--, t_y--;
vector<vector<char>> grid(n, vector<char>(m));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> grid[i][j];
vector<vector<long long>> sp(n, vector<long long>(m, -1));
priority_queue<pair<long long, pair<int, int>>> pq;
pq.push({0, {x, y}});
vector<int> X0 = {1, -1, 0, 0};
vector<int> Y0 = {0, 0, 1, -1};
vector<int> X1 = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
0, 1, 1, 1, 1, 2, 2, 2, 2, 2};
vector<int> Y1 = {-2, -1, 0, 1, 2, -2, -1, 1, 2, -2,
2, -2, -1, 1, 2, -2, -1, 0, 1, 2};
while (pq.empty() ^ 1) {
int cx = pq.top().second.first;
int cy = pq.top().second.second;
int cost = pq.top().first * -1;
pq.pop();
if (cx < 0 || cx >= n || cy < 0 || cy >= m || grid[cx][cy] == '#' ||
sp[cx][cy] != -1)
continue;
sp[cx][cy] = cost;
for (int i = 0; i < 4; ++i)
pq.push({-cost, {cx + X0[i], cy + Y0[i]}});
for (int i = 0; i < 20; ++i)
pq.push({-(cost + 1), {cx + X1[i], cy + Y1[i]}});
};
cout << sp[t_x][t_y];
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
int x, y;
cin >> x >> y;
int t_x, t_y;
cin >> t_x >> t_y;
x--, y--;
t_x--, t_y--;
vector<vector<char>> grid(n, vector<char>(m));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> grid[i][j];
vector<vector<long long>> sp(n, vector<long long>(m, -1));
priority_queue<pair<long long, pair<int, int>>> pq;
pq.push({0, {x, y}});
vector<int> X0 = {1, -1, 0, 0};
vector<int> Y0 = {0, 0, 1, -1};
vector<int> X1 = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
0, 1, 1, 1, 1, 2, 2, 2, 2, 2};
vector<int> Y1 = {-2, -1, 0, 1, 2, -2, -1, 1, 2, -2,
2, -2, -1, 1, 2, -2, -1, 0, 1, 2};
while (pq.empty() ^ 1) {
int cx = pq.top().second.first;
int cy = pq.top().second.second;
int cost = pq.top().first * -1;
pq.pop();
if (cx < 0 || cx >= n || cy < 0 || cy >= m || grid[cx][cy] == '#' ||
sp[cx][cy] != -1)
continue;
sp[cx][cy] = cost;
if (cx == t_x && cy == t_y) {
cout << cost;
return 0;
}
for (int i = 0; i < 4; ++i)
pq.push({-cost, {cx + X0[i], cy + Y0[i]}});
for (int i = 0; i < 20; ++i)
pq.push({-(cost + 1), {cx + X1[i], cy + Y1[i]}});
};
cout << sp[t_x][t_y];
}
| replace | 46 | 47 | 46 | 50 | TLE | |
p02579 | C++ | Runtime Error | /**
* author: Dooloper
* created: 16.09.2020 23:48:27
**/
#pragma region Macros
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repn(i, n) for (int i = 1; i <= (n); i++)
#define pb push_back
void debug_out() { cout << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
const double PI = acos(-1);
const int mod = 1000000007;
class mint {
long long x;
public:
mint(long long x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint &a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint &a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint &a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint &a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint &a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint &a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
mint operator/(const mint &a) const {
mint res(*this);
return res /= a;
}
friend ostream &operator<<(ostream &os, const mint &m) {
os << m.x;
return os;
}
};
#pragma endregion
int h, w;
char maze[105][105];
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> h >> w;
int sh, sw, gh, gw;
cin >> sh >> sw >> gh >> gw;
sh--;
sw--;
gh--;
gw--;
rep(i, h) rep(j, w) cin >> maze[i][j];
const int INF = 1e9;
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<P> q;
dist[sh][sw] = 0;
q.emplace_back(sh, sw);
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
int d = dist[i][j];
q.pop_front();
if (dist[i][j] != d)
continue;
rep(v, 4) {
int ni = i + di[v], nj = j + dj[v];
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (maze[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d)
continue;
dist[ni][nj] = d;
q.emplace_front(ni, nj);
}
for (int ei = -2; ei <= 2; ei++) {
for (int ej = -2; ej <= 2; ej++) {
int ni = i + ei, nj = j + ej;
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (maze[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d + 1)
continue;
dist[ni][nj] = d + 1;
q.emplace_back(ni, nj);
}
}
}
int ans = (dist[gh][gw] == INF) ? -1 : dist[gh][gw];
cout << ans << endl;
return 0;
} | /**
* author: Dooloper
* created: 16.09.2020 23:48:27
**/
#pragma region Macros
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repn(i, n) for (int i = 1; i <= (n); i++)
#define pb push_back
void debug_out() { cout << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
const double PI = acos(-1);
const int mod = 1000000007;
class mint {
long long x;
public:
mint(long long x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint &a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint &a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint &a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint &a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint &a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint &a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
mint operator/(const mint &a) const {
mint res(*this);
return res /= a;
}
friend ostream &operator<<(ostream &os, const mint &m) {
os << m.x;
return os;
}
};
#pragma endregion
int h, w;
char maze[1005][1005];
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> h >> w;
int sh, sw, gh, gw;
cin >> sh >> sw >> gh >> gw;
sh--;
sw--;
gh--;
gw--;
rep(i, h) rep(j, w) cin >> maze[i][j];
const int INF = 1e9;
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<P> q;
dist[sh][sw] = 0;
q.emplace_back(sh, sw);
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
int d = dist[i][j];
q.pop_front();
if (dist[i][j] != d)
continue;
rep(v, 4) {
int ni = i + di[v], nj = j + dj[v];
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (maze[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d)
continue;
dist[ni][nj] = d;
q.emplace_front(ni, nj);
}
for (int ei = -2; ei <= 2; ei++) {
for (int ej = -2; ej <= 2; ej++) {
int ni = i + ei, nj = j + ej;
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (maze[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d + 1)
continue;
dist[ni][nj] = d + 1;
q.emplace_back(ni, nj);
}
}
}
int ans = (dist[gh][gw] == INF) ? -1 : dist[gh][gw];
cout << ans << endl;
return 0;
} | replace | 83 | 84 | 83 | 84 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << endl
#define dump_4(x1, x2, x3, x4) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...) \
CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
using ll = long long;
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};
const ll LINF = 2e18;
const int INF = 1e9;
int H, W;
int Ch, Cw;
int Dh, Dw;
vector<vector<char>> S;
struct point {
int y, x;
bool operator==(const point &rhs) const { return (x == rhs.x && y == rhs.y); }
bool operator<(const point &rhs) const {
return (x < rhs.x || (x == rhs.x && y < rhs.y));
}
};
typedef struct point Point;
void solve() {
vector<vector<bool>> dist(H, vector<bool>(W, false));
priority_queue<pair<int, Point>, vector<pair<int, Point>>,
greater<pair<int, Point>>>
q;
q.push({0, {Ch, Cw}});
ll result = -1;
while (!q.empty()) {
auto v = q.top();
q.pop();
int w = v.first; // ワープカウント
Point p = v.second;
dist.at(p.y).at(p.x) = true;
if (p.y == Dh && p.x == Dw) {
result = w;
break;
}
// Move
rep(i, 4) {
int nx = p.x + dx[i];
int ny = p.y + dy[i];
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (S.at(ny).at(nx) == '#')
continue;
if (!dist.at(ny).at(nx)) {
q.push({w, {ny, nx}});
}
}
// Warp
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int nx = p.x + i;
int ny = p.y + j;
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (S.at(ny).at(nx) == '#')
continue;
if (!dist.at(ny).at(nx)) {
q.push({w + 1, {ny, nx}});
}
}
}
}
cout << result << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
cin >> H >> W;
cin >> Ch >> Cw;
cin >> Dh >> Dw;
// 0-indexed
Ch--;
Cw--;
Dh--;
Dw--;
S.resize(H, vector<char>(W));
rep(i, H) {
rep(j, W) {
char c;
cin >> c;
S.at(i).at(j) = c;
}
}
solve();
return 0;
}
| #include <bits/stdc++.h>
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << endl
#define dump_4(x1, x2, x3, x4) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5) \
cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
<< x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...) \
CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
using ll = long long;
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};
const ll LINF = 2e18;
const int INF = 1e9;
int H, W;
int Ch, Cw;
int Dh, Dw;
vector<vector<char>> S;
struct point {
int y, x;
bool operator==(const point &rhs) const { return (x == rhs.x && y == rhs.y); }
bool operator<(const point &rhs) const {
return (x < rhs.x || (x == rhs.x && y < rhs.y));
}
};
typedef struct point Point;
void solve() {
vector<vector<bool>> dist(H, vector<bool>(W, false));
priority_queue<pair<int, Point>, vector<pair<int, Point>>,
greater<pair<int, Point>>>
q;
q.push({0, {Ch, Cw}});
ll result = -1;
while (!q.empty()) {
auto v = q.top();
q.pop();
int w = v.first; // ワープカウント
Point p = v.second;
if (dist.at(p.y).at(p.x))
continue;
dist.at(p.y).at(p.x) = true;
if (p.y == Dh && p.x == Dw) {
result = w;
break;
}
// Move
rep(i, 4) {
int nx = p.x + dx[i];
int ny = p.y + dy[i];
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (S.at(ny).at(nx) == '#')
continue;
if (!dist.at(ny).at(nx)) {
q.push({w, {ny, nx}});
}
}
// Warp
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int nx = p.x + i;
int ny = p.y + j;
if (nx < 0 || ny < 0 || nx >= W || ny >= H)
continue;
if (S.at(ny).at(nx) == '#')
continue;
if (!dist.at(ny).at(nx)) {
q.push({w + 1, {ny, nx}});
}
}
}
}
cout << result << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
cin >> H >> W;
cin >> Ch >> Cw;
cin >> Dh >> Dw;
// 0-indexed
Ch--;
Cw--;
Dh--;
Dw--;
S.resize(H, vector<char>(W));
rep(i, H) {
rep(j, W) {
char c;
cin >> c;
S.at(i).at(j) = c;
}
}
solve();
return 0;
}
| insert | 69 | 69 | 69 | 71 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#define FOR(i, a, b) for (register int i = (a); i < (b); ++i)
#define ROF(i, a, b) for (register int i = (a); i >= (b); --i)
#define pi pair<int, int>
#define mk(a, b) make_pair(a, b)
#define mygc(c) (c) = getchar()
#define mypc(c) putchar(c)
#define fi first
#define se second
#define ls(x) t[x].lson
#define rs(x) t[x].rson
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1005;
const int maxm = 1000005;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a, ll b, ll c) {
ll ans = 1;
while (b) {
if (b & 1)
ans = ans * a % c;
a = a * a % c;
b >>= 1;
}
return ans;
}
inline void rd(int *x) {
int k, m = 0;
*x = 0;
for (;;) {
mygc(k);
if (k == '-') {
m = 1;
break;
}
if ('0' <= k && k <= '9') {
*x = k - '0';
break;
}
}
for (;;) {
mygc(k);
if (k < '0' || k > '9')
break;
*x = (*x) * 10 + k - '0';
}
if (m)
(*x) = -(*x);
}
inline void rd(ll *x) {
int k, m = 0;
*x = 0;
for (;;) {
mygc(k);
if (k == '-') {
m = 1;
break;
}
if ('0' <= k && k <= '9') {
*x = k - '0';
break;
}
}
for (;;) {
mygc(k);
if (k < '0' || k > '9')
break;
*x = (*x) * 10 + k - '0';
}
if (m)
(*x) = -(*x);
}
inline void rd(db *x) { scanf("%lf", x); }
inline int rd(char c[]) {
int i, s = 0;
for (;;) {
mygc(i);
if (i != ' ' && i != '\n' && i != '\r' && i != '\t' && i != EOF)
break;
}
c[s++] = i;
for (;;) {
mygc(i);
if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF)
break;
c[s++] = i;
}
c[s] = '\0';
return s;
}
inline void rd(int a[], int n) { FOR(i, 0, n) rd(&a[i]); }
inline void rd(ll a[], int n) { FOR(i, 0, n) rd(&a[i]); }
template <class T, class S> inline void rd(T *x, S *y) {
rd(x);
rd(y);
}
template <class T, class S, class U> inline void rd(T *x, S *y, U *z) {
rd(x);
rd(y);
rd(z);
}
template <class T, class S, class U, class V>
inline void rd(T *x, S *y, U *z, V *w) {
rd(x);
rd(y);
rd(z);
rd(w);
}
inline void wr(int x) {
if (x < 10)
putchar('0' + x);
else
wr(x / 10), wr(x % 10);
}
inline void wr(int x, char c) {
int s = 0, m = 0;
char f[10];
if (x < 0)
m = 1, x = -x;
while (x)
f[s++] = x % 10, x /= 10;
if (!s)
f[s++] = 0;
if (m)
mypc('-');
while (s--)
mypc(f[s] + '0');
mypc(c);
}
inline void wr(ll x, char c) {
int s = 0, m = 0;
char f[20];
if (x < 0)
m = 1, x = -x;
while (x)
f[s++] = x % 10, x /= 10;
if (!s)
f[s++] = 0;
if (m)
mypc('-');
while (s--)
mypc(f[s] + '0');
mypc(c);
}
inline void wr(db x, char c) {
printf("%.15f", x);
mypc(c);
}
inline void wr(const char c[]) {
int i;
for (i = 0; c[i] != '\0'; i++)
mypc(c[i]);
}
inline void wr(const char x[], char c) {
int i;
for (i = 0; x[i] != '\0'; i++)
mypc(x[i]);
mypc(c);
}
template <class T> inline void wrn(T x) { wr(x, '\n'); }
template <class T, class S> inline void wrn(T x, S y) {
wr(x, ' ');
wr(y, '\n');
}
template <class T, class S, class U> inline void wrn(T x, S y, U z) {
wr(x, ' ');
wr(y, ' ');
wr(z, '\n');
}
template <class T, class S, class U, class H>
inline void wrn(T x, S y, U z, H h) {
wr(x, ' ');
wr(y, ' ');
wr(z, ' ');
wr(h, '\n');
}
template <class T> inline void wra(T x[], int n) {
int i;
if (!n) {
mypc('\n');
return;
}
FOR(i, 0, n - 1) wr(x[i], ' ');
wr(x[n - 1], '\n');
}
int n, m, sr, sc, tr, tc, id[maxn][maxn], tot,
dr[4] = {-1, 1, 0, 0}, dc[4] = {0, 0, 1, -1}, vis[maxn], d[maxn], inq[maxn];
vector<pi> g[maxn];
char s[maxn][maxn];
void dfs(int r, int c, int col) {
id[r][c] = col;
g[col].push_back(mk(r, c));
FOR(i, 0, 4) {
int rr = r + dr[i], cc = c + dc[i];
if (!rr || !cc || rr > n || cc > m || id[rr][cc] || s[rr][cc] == '#')
continue;
dfs(rr, cc, col);
}
}
queue<int> q;
void fd(int u) {
FOR(i, 0, g[u].size()) {
int r = g[u][i].fi, c = g[u][i].se;
FOR(drr, -2, 3)
FOR(dcc, -2, 3) {
if (!drr && !dcc)
continue;
int rr = r + drr, cc = c + dcc;
if (rr <= 0 || cc <= 0 || rr > n || cc > m || vis[id[rr][cc]] ||
s[rr][cc] == '#' || d[id[rr][cc]] <= d[u] + 1)
continue;
d[id[rr][cc]] = min(d[id[rr][cc]], d[u] + 1);
if (!inq[id[rr][cc]])
q.push(id[rr][cc]);
inq[id[rr][cc]] = 1;
}
}
}
int main() {
rd(&n, &m);
rd(&sr, &sc, &tr, &tc);
FOR(i, 1, n + 1) rd(s[i] + 1);
FOR(i, 1, n + 1)
FOR(j, 1, m + 1) {
if (!id[i][j] && s[i][j] != '#')
dfs(i, j, ++tot);
}
FOR(i, 1, tot + 1) d[i] = inf / 2;
d[id[sr][sc]] = 0;
q.push(id[sr][sc]);
while (!q.empty()) {
vis[q.front()] = 1;
fd(q.front());
q.pop();
}
if (d[id[tr][tc]] == inf / 2)
wrn(-1);
else
wrn(d[id[tr][tc]]);
} | #include <bits/stdc++.h>
#define FOR(i, a, b) for (register int i = (a); i < (b); ++i)
#define ROF(i, a, b) for (register int i = (a); i >= (b); --i)
#define pi pair<int, int>
#define mk(a, b) make_pair(a, b)
#define mygc(c) (c) = getchar()
#define mypc(c) putchar(c)
#define fi first
#define se second
#define ls(x) t[x].lson
#define rs(x) t[x].rson
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1005;
const int maxm = 1000005;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a, ll b, ll c) {
ll ans = 1;
while (b) {
if (b & 1)
ans = ans * a % c;
a = a * a % c;
b >>= 1;
}
return ans;
}
inline void rd(int *x) {
int k, m = 0;
*x = 0;
for (;;) {
mygc(k);
if (k == '-') {
m = 1;
break;
}
if ('0' <= k && k <= '9') {
*x = k - '0';
break;
}
}
for (;;) {
mygc(k);
if (k < '0' || k > '9')
break;
*x = (*x) * 10 + k - '0';
}
if (m)
(*x) = -(*x);
}
inline void rd(ll *x) {
int k, m = 0;
*x = 0;
for (;;) {
mygc(k);
if (k == '-') {
m = 1;
break;
}
if ('0' <= k && k <= '9') {
*x = k - '0';
break;
}
}
for (;;) {
mygc(k);
if (k < '0' || k > '9')
break;
*x = (*x) * 10 + k - '0';
}
if (m)
(*x) = -(*x);
}
inline void rd(db *x) { scanf("%lf", x); }
inline int rd(char c[]) {
int i, s = 0;
for (;;) {
mygc(i);
if (i != ' ' && i != '\n' && i != '\r' && i != '\t' && i != EOF)
break;
}
c[s++] = i;
for (;;) {
mygc(i);
if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF)
break;
c[s++] = i;
}
c[s] = '\0';
return s;
}
inline void rd(int a[], int n) { FOR(i, 0, n) rd(&a[i]); }
inline void rd(ll a[], int n) { FOR(i, 0, n) rd(&a[i]); }
template <class T, class S> inline void rd(T *x, S *y) {
rd(x);
rd(y);
}
template <class T, class S, class U> inline void rd(T *x, S *y, U *z) {
rd(x);
rd(y);
rd(z);
}
template <class T, class S, class U, class V>
inline void rd(T *x, S *y, U *z, V *w) {
rd(x);
rd(y);
rd(z);
rd(w);
}
inline void wr(int x) {
if (x < 10)
putchar('0' + x);
else
wr(x / 10), wr(x % 10);
}
inline void wr(int x, char c) {
int s = 0, m = 0;
char f[10];
if (x < 0)
m = 1, x = -x;
while (x)
f[s++] = x % 10, x /= 10;
if (!s)
f[s++] = 0;
if (m)
mypc('-');
while (s--)
mypc(f[s] + '0');
mypc(c);
}
inline void wr(ll x, char c) {
int s = 0, m = 0;
char f[20];
if (x < 0)
m = 1, x = -x;
while (x)
f[s++] = x % 10, x /= 10;
if (!s)
f[s++] = 0;
if (m)
mypc('-');
while (s--)
mypc(f[s] + '0');
mypc(c);
}
inline void wr(db x, char c) {
printf("%.15f", x);
mypc(c);
}
inline void wr(const char c[]) {
int i;
for (i = 0; c[i] != '\0'; i++)
mypc(c[i]);
}
inline void wr(const char x[], char c) {
int i;
for (i = 0; x[i] != '\0'; i++)
mypc(x[i]);
mypc(c);
}
template <class T> inline void wrn(T x) { wr(x, '\n'); }
template <class T, class S> inline void wrn(T x, S y) {
wr(x, ' ');
wr(y, '\n');
}
template <class T, class S, class U> inline void wrn(T x, S y, U z) {
wr(x, ' ');
wr(y, ' ');
wr(z, '\n');
}
template <class T, class S, class U, class H>
inline void wrn(T x, S y, U z, H h) {
wr(x, ' ');
wr(y, ' ');
wr(z, ' ');
wr(h, '\n');
}
template <class T> inline void wra(T x[], int n) {
int i;
if (!n) {
mypc('\n');
return;
}
FOR(i, 0, n - 1) wr(x[i], ' ');
wr(x[n - 1], '\n');
}
int n, m, sr, sc, tr, tc, id[maxn][maxn], tot,
dr[4] = {-1, 1, 0, 0}, dc[4] = {0, 0, 1, -1}, vis[maxm], d[maxm], inq[maxm];
vector<pi> g[maxm];
char s[maxn][maxn];
void dfs(int r, int c, int col) {
id[r][c] = col;
g[col].push_back(mk(r, c));
FOR(i, 0, 4) {
int rr = r + dr[i], cc = c + dc[i];
if (!rr || !cc || rr > n || cc > m || id[rr][cc] || s[rr][cc] == '#')
continue;
dfs(rr, cc, col);
}
}
queue<int> q;
void fd(int u) {
FOR(i, 0, g[u].size()) {
int r = g[u][i].fi, c = g[u][i].se;
FOR(drr, -2, 3)
FOR(dcc, -2, 3) {
if (!drr && !dcc)
continue;
int rr = r + drr, cc = c + dcc;
if (rr <= 0 || cc <= 0 || rr > n || cc > m || vis[id[rr][cc]] ||
s[rr][cc] == '#' || d[id[rr][cc]] <= d[u] + 1)
continue;
d[id[rr][cc]] = min(d[id[rr][cc]], d[u] + 1);
if (!inq[id[rr][cc]])
q.push(id[rr][cc]);
inq[id[rr][cc]] = 1;
}
}
}
int main() {
rd(&n, &m);
rd(&sr, &sc, &tr, &tc);
FOR(i, 1, n + 1) rd(s[i] + 1);
FOR(i, 1, n + 1)
FOR(j, 1, m + 1) {
if (!id[i][j] && s[i][j] != '#')
dfs(i, j, ++tot);
}
FOR(i, 1, tot + 1) d[i] = inf / 2;
d[id[sr][sc]] = 0;
q.push(id[sr][sc]);
while (!q.empty()) {
vis[q.front()] = 1;
fd(q.front());
q.pop();
}
if (d[id[tr][tc]] == inf / 2)
wrn(-1);
else
wrn(d[id[tr][tc]]);
} | replace | 190 | 192 | 190 | 192 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
int main() {
int H, W;
cin >> H >> W;
int Ch, Cw, Dh, Dw;
cin >> Ch >> Cw >> Dh >> Dw;
Ch--;
Cw--;
Dh--;
Dw--;
auto S = make_v<char>(H, W);
auto dist = make_v<int>(H, W);
fill_v(dist, 1000000000);
dist[Ch][Cw] = 0;
for (size_t i = 0; i < H; i++) {
for (size_t j = 0; j < W; j++) {
cin >> S[i][j];
}
}
using P = pair<int, int>;
using T = pair<P, int>;
deque<T> deq;
deq.push_back({{Ch, Cw}, 0});
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
while (!deq.empty()) {
auto [cur, cost] = deq.front();
deq.pop_front();
auto [y, x] = cur;
if (dist[y][x] < cost)
continue;
// dist[y][x]=cost;
for (size_t i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ny < H and 0 <= ny and nx < W and 0 <= nx and S[ny][nx] != '#') {
if (dist[ny][nx] <= cost)
continue;
dist[ny][nx] = dist[y][x];
deq.push_front({{ny, nx}, dist[y][x]});
}
}
for (int i = -2; i < 3; i++) {
for (int j = -2; j < 3; j++) {
int ny = y + i;
int nx = x + j;
if (0 <= ny and ny < H and 0 <= nx and nx < W and S[ny][nx] != '#') {
if (dist[ny][nx] <= cost + 1)
continue;
dist[ny][nx] = dist[y][x] + 1;
deq.push_front({{ny, nx}, dist[y][x] + 1});
}
}
}
}
cout << ((dist[Dh][Dw] < 1000000000) ? dist[Dh][Dw] : -1) << endl;
/* for (size_t i = 0; i < H; i++)
{
for (size_t j = 0; j < W; j++)
{
cout<<dist[i][j]<<" ";
}
cout<<endl;
}*/
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
int main() {
int H, W;
cin >> H >> W;
int Ch, Cw, Dh, Dw;
cin >> Ch >> Cw >> Dh >> Dw;
Ch--;
Cw--;
Dh--;
Dw--;
auto S = make_v<char>(H, W);
auto dist = make_v<int>(H, W);
fill_v(dist, 1000000000);
dist[Ch][Cw] = 0;
for (size_t i = 0; i < H; i++) {
for (size_t j = 0; j < W; j++) {
cin >> S[i][j];
}
}
using P = pair<int, int>;
using T = pair<P, int>;
deque<T> deq;
deq.push_back({{Ch, Cw}, 0});
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
while (!deq.empty()) {
auto [cur, cost] = deq.front();
deq.pop_front();
auto [y, x] = cur;
if (dist[y][x] < cost)
continue;
// dist[y][x]=cost;
for (size_t i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ny < H and 0 <= ny and nx < W and 0 <= nx and S[ny][nx] != '#') {
if (dist[ny][nx] <= cost)
continue;
dist[ny][nx] = dist[y][x];
deq.push_front({{ny, nx}, dist[y][x]});
}
}
for (int i = -2; i < 3; i++) {
for (int j = -2; j < 3; j++) {
int ny = y + i;
int nx = x + j;
if (0 <= ny and ny < H and 0 <= nx and nx < W and S[ny][nx] != '#') {
if (dist[ny][nx] <= cost + 1)
continue;
dist[ny][nx] = dist[y][x] + 1;
deq.push_back({{ny, nx}, dist[y][x] + 1});
}
}
}
}
cout << ((dist[Dh][Dw] < 1000000000) ? dist[Dh][Dw] : -1) << endl;
/* for (size_t i = 0; i < H; i++)
{
for (size_t j = 0; j < W; j++)
{
cout<<dist[i][j]<<" ";
}
cout<<endl;
}*/
return 0;
} | replace | 89 | 90 | 89 | 90 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;
int h, w;
int sx, sy, ex, ey;
int d[N][N];
string s[N];
int check(int x, int y) {
if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.')
return 1;
return 0;
}
int bfs() {
deque<pair<int, int>> q;
q.push_back({sx, sy});
d[sx][sy] = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop_front();
for (int i = -2; i <= 2; i++)
for (int j = -2; j <= 2; j++) {
if (i == 0 && j == 0)
continue;
if (abs(i) + abs(j) == 1) {
int tx = x + i;
int ty = y + j;
if (check(tx, ty) && d[tx][ty] > d[x][y]) {
d[tx][ty] = d[x][y];
q.push_front({tx, ty});
}
} else {
int tx = x + i;
int ty = y + j;
if (check(tx, ty) && d[tx][ty] > d[x][y] + 1) {
d[tx][ty] = d[x][y] + 1;
q.push_back({tx, ty});
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> h >> w;
cin >> sx >> sy >> ex >> ey;
for (int i = 0; i < h; i++)
cin >> s[i];
sx--;
sy--;
ex--;
ey--;
memset(d, 0x3f, sizeof(d));
bfs();
if (d[ex][ey] == 0x3f3f3f3f)
cout << -1 << endl;
else
cout << d[ex][ey] << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;
int h, w;
int sx, sy, ex, ey;
int d[N][N];
string s[N];
int check(int x, int y) {
if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.')
return 1;
return 0;
}
void bfs() {
deque<pair<int, int>> q;
q.push_back({sx, sy});
d[sx][sy] = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop_front();
for (int i = -2; i <= 2; i++)
for (int j = -2; j <= 2; j++) {
if (i == 0 && j == 0)
continue;
if (abs(i) + abs(j) == 1) {
int tx = x + i;
int ty = y + j;
if (check(tx, ty) && d[tx][ty] > d[x][y]) {
d[tx][ty] = d[x][y];
q.push_front({tx, ty});
}
} else {
int tx = x + i;
int ty = y + j;
if (check(tx, ty) && d[tx][ty] > d[x][y] + 1) {
d[tx][ty] = d[x][y] + 1;
q.push_back({tx, ty});
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> h >> w;
cin >> sx >> sy >> ex >> ey;
for (int i = 0; i < h; i++)
cin >> s[i];
sx--;
sy--;
ex--;
ey--;
memset(d, 0x3f, sizeof(d));
bfs();
if (d[ex][ey] == 0x3f3f3f3f)
cout << -1 << endl;
else
cout << d[ex][ey] << endl;
} | replace | 15 | 16 | 15 | 16 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif
#define N 1024
char s[N][N] = {};
vpii v;
queue<tuple<int, int, int>> que;
int dy[4] = {0, 1, 0, -1};
int dx[4] = {1, 0, -1, 0};
void dfs(int y, int x) {
if (s[y][x] == '#')
return;
s[y][x] = '#';
v.EB(y, x);
REP(k, 4) dfs(y + dy[k], x + dx[k]);
}
int main() {
int h, w;
scanf("%d%d", &h, &w);
int sy, sx, ty, tx;
scanf("%d%d%d%d", &sy, &sx, &ty, &tx);
sy++;
sx++;
ty++;
tx++;
REP(i, h) scanf("%s", s[i + 2] + 2);
REP(i, N) {
REP(j, N) {
if (i < 2 || 2 + h <= i || j < 2 || 2 + w <= j) {
s[i][j] = '#';
}
}
}
que.emplace(0, sy, sx);
while (que.size()) {
auto [ans, y, x] = que.front();
que.pop();
dfs(y, x);
for (auto [i, j] : v) {
if (i == ty && j == tx) {
printf("%d\n", ans);
return 0;
}
FOR(k, i - 2, i + 3) {
FOR(l, j - 2, j + 3) {
if (s[k][l] == '.') {
que.emplace(ans + 1, k, l);
}
}
}
}
}
printf("-1\n");
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif
#define N 1024
char s[N][N] = {};
vpii v;
queue<tuple<int, int, int>> que;
int dy[4] = {0, 1, 0, -1};
int dx[4] = {1, 0, -1, 0};
void dfs(int y, int x) {
if (s[y][x] == '#')
return;
s[y][x] = '#';
v.EB(y, x);
REP(k, 4) dfs(y + dy[k], x + dx[k]);
}
int main() {
int h, w;
scanf("%d%d", &h, &w);
int sy, sx, ty, tx;
scanf("%d%d%d%d", &sy, &sx, &ty, &tx);
sy++;
sx++;
ty++;
tx++;
REP(i, h) scanf("%s", s[i + 2] + 2);
REP(i, N) {
REP(j, N) {
if (i < 2 || 2 + h <= i || j < 2 || 2 + w <= j) {
s[i][j] = '#';
}
}
}
que.emplace(0, sy, sx);
while (que.size()) {
auto [ans, y, x] = que.front();
que.pop();
v.clear();
dfs(y, x);
for (auto [i, j] : v) {
if (i == ty && j == tx) {
printf("%d\n", ans);
return 0;
}
FOR(k, i - 2, i + 3) {
FOR(l, j - 2, j + 3) {
if (s[k][l] == '.') {
que.emplace(ans + 1, k, l);
}
}
}
}
}
printf("-1\n");
}
| insert | 60 | 60 | 60 | 61 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll mod = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll gcd(ll A, ll B) {
if (B == 0)
return A;
return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }
template <typename T> T pow(T a, ll n, T e = 1) {
T ret = e;
while (n) {
if (n & 1)
ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
template <int mod> struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(long long x_) {
if ((x = x_ % mod + mod) >= mod)
x -= mod;
}
ModInt &operator+=(ModInt rhs) {
if ((x += rhs.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(ModInt rhs) {
if ((x -= rhs.x) < 0)
x += mod;
return *this;
}
ModInt &operator*=(ModInt rhs) {
x = (unsigned long long)x * rhs.x % mod;
return *this;
}
ModInt &operator/=(ModInt rhs) {
x = (unsigned long long)x * rhs.inv().x % mod;
return *this;
}
ModInt operator-() const { return -x < 0 ? mod - x : -x; }
ModInt operator+(ModInt rhs) const { return ModInt(*this) += rhs; }
ModInt operator-(ModInt rhs) const { return ModInt(*this) -= rhs; }
ModInt operator*(ModInt rhs) const { return ModInt(*this) *= rhs; }
ModInt operator/(ModInt rhs) const { return ModInt(*this) /= rhs; }
bool operator==(ModInt rhs) const { return x == rhs.x; }
bool operator!=(ModInt rhs) const { return x != rhs.x; }
ModInt inv() const { return pow(*this, mod - 2); }
friend ostream &operator<<(ostream &s, ModInt<mod> a) {
s << a.x;
return s;
}
friend istream &operator>>(istream &s, ModInt<mod> &a) {
s >> a.x;
return s;
}
};
using mint = ModInt<1000000007>;
using Graph = vector<vector<int>>;
Graph G;
/*------------------------------------------------------------------*/
int h, w;
char s[1010][1010];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w;
int ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
rep(i, h) rep(j, w) cin >> s[i][j];
int INF = 1e8;
deque<pair<int, int>> dq;
dq.push_back({ch, cw});
vector<vector<int>> dist(h, vector<int>(w, INF));
dist[ch][cw] = 0;
while (!dq.empty()) {
int x = dq.front().first;
int y = dq.front().second;
dq.pop_front();
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int nx = x + i;
int ny = y + j;
if (nx < 0 or nx >= h or ny < 0 or ny >= w)
continue;
if (s[nx][ny] == '#')
continue;
if (i == 0 and j == 0)
continue;
if (abs(i) + abs(j) <= 1 and dist[x][y] < dist[nx][ny]) {
dist[nx][ny] = dist[x][y];
dq.push_back({nx, ny});
}
if (dist[x][y] + 1 < dist[nx][ny]) {
dist[nx][ny] = dist[x][y] + 1;
dq.push_back({nx, ny});
}
}
}
}
if (dist[dh][dw] < INF)
cout << dist[dh][dw] << endl;
else
cout << -1 << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll mod = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll gcd(ll A, ll B) {
if (B == 0)
return A;
return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }
template <typename T> T pow(T a, ll n, T e = 1) {
T ret = e;
while (n) {
if (n & 1)
ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
template <int mod> struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(long long x_) {
if ((x = x_ % mod + mod) >= mod)
x -= mod;
}
ModInt &operator+=(ModInt rhs) {
if ((x += rhs.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(ModInt rhs) {
if ((x -= rhs.x) < 0)
x += mod;
return *this;
}
ModInt &operator*=(ModInt rhs) {
x = (unsigned long long)x * rhs.x % mod;
return *this;
}
ModInt &operator/=(ModInt rhs) {
x = (unsigned long long)x * rhs.inv().x % mod;
return *this;
}
ModInt operator-() const { return -x < 0 ? mod - x : -x; }
ModInt operator+(ModInt rhs) const { return ModInt(*this) += rhs; }
ModInt operator-(ModInt rhs) const { return ModInt(*this) -= rhs; }
ModInt operator*(ModInt rhs) const { return ModInt(*this) *= rhs; }
ModInt operator/(ModInt rhs) const { return ModInt(*this) /= rhs; }
bool operator==(ModInt rhs) const { return x == rhs.x; }
bool operator!=(ModInt rhs) const { return x != rhs.x; }
ModInt inv() const { return pow(*this, mod - 2); }
friend ostream &operator<<(ostream &s, ModInt<mod> a) {
s << a.x;
return s;
}
friend istream &operator>>(istream &s, ModInt<mod> &a) {
s >> a.x;
return s;
}
};
using mint = ModInt<1000000007>;
using Graph = vector<vector<int>>;
Graph G;
/*------------------------------------------------------------------*/
int h, w;
char s[1010][1010];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w;
int ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
rep(i, h) rep(j, w) cin >> s[i][j];
int INF = 1e8;
deque<pair<int, int>> dq;
dq.push_back({ch, cw});
vector<vector<int>> dist(h, vector<int>(w, INF));
dist[ch][cw] = 0;
while (!dq.empty()) {
int x = dq.front().first;
int y = dq.front().second;
dq.pop_front();
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int nx = x + i;
int ny = y + j;
if (nx < 0 or nx >= h or ny < 0 or ny >= w)
continue;
if (s[nx][ny] == '#')
continue;
if (i == 0 and j == 0)
continue;
if (abs(i) + abs(j) <= 1 and dist[x][y] < dist[nx][ny]) {
dist[nx][ny] = dist[x][y];
dq.push_front({nx, ny});
}
if (dist[x][y] + 1 < dist[nx][ny]) {
dist[nx][ny] = dist[x][y] + 1;
dq.push_back({nx, ny});
}
}
}
}
if (dist[dh][dw] < INF)
cout << dist[dh][dw] << endl;
else
cout << -1 << endl;
} | replace | 113 | 114 | 113 | 114 | TLE | |
p02579 | Python | Time Limit Exceeded | # 参考: White_Pie_46
# https://atcoder.jp/contests/abc176/submissions/16168825
from collections import deque
def main():
height, width = [int(x) for x in input().split()]
init_h, init_w = [int(x) - 1 for x in input().split()]
dest_h, dest_w = [int(x) - 1 for x in input().split()]
grid = [list(input()) for _ in range(height)]
adjacents = ((1, 0), (0, 1), (-1, 0), (0, -1))
def gen_warp_can_reach():
for h in range(-2, 3):
for w in range(-2, 3):
if h == 0 == w:
continue
if (h, w) in adjacents:
continue
yield (h, w)
warp_can_reach = tuple(gen_warp_can_reach())
qu = deque()
qu.append((init_h, init_w, 0)) # 座標とワープ回数を保持
while qu:
h, w, warps = qu.popleft()
if h == dest_h and w == dest_w:
return warps
grid[h][w] = "R" # reached
for h_delta, w_delta in adjacents:
new_h, new_w = h + h_delta, w + w_delta
if new_h < 0 or new_h >= height or new_w < 0 or new_w >= width:
continue
if grid[new_h][new_w] == ".":
qu.appendleft((new_h, new_w, warps))
for h_delta, w_delta in warp_can_reach:
new_h, new_w = h + h_delta, w + w_delta
if new_h < 0 or new_h >= height or new_w < 0 or new_w >= width:
continue
if grid[new_h][new_w] == ".":
qu.append((new_h, new_w, warps + 1))
return -1
if __name__ == "__main__":
print(main())
| # 参考: White_Pie_46
# https://atcoder.jp/contests/abc176/submissions/16168825
from collections import deque
def main():
height, width = [int(x) for x in input().split()]
init_h, init_w = [int(x) - 1 for x in input().split()]
dest_h, dest_w = [int(x) - 1 for x in input().split()]
grid = [list(input()) for _ in range(height)]
adjacents = ((1, 0), (0, 1), (-1, 0), (0, -1))
def gen_warp_can_reach():
for h in range(-2, 3):
for w in range(-2, 3):
if h == 0 == w:
continue
if (h, w) in adjacents:
continue
yield (h, w)
warp_can_reach = tuple(gen_warp_can_reach())
qu = deque()
qu.append((init_h, init_w, 0)) # 座標とワープ回数を保持
while qu:
h, w, warps = qu.popleft()
if h == dest_h and w == dest_w:
return warps
if grid[h][w] == "R":
# 同じマスを複数回キューに追加しているかもしれない。
# 短距離で調査済みなら再調査は不要。
continue
grid[h][w] = "R" # reached
for h_delta, w_delta in adjacents:
new_h, new_w = h + h_delta, w + w_delta
if new_h < 0 or new_h >= height or new_w < 0 or new_w >= width:
continue
if grid[new_h][new_w] == ".":
qu.appendleft((new_h, new_w, warps))
for h_delta, w_delta in warp_can_reach:
new_h, new_w = h + h_delta, w + w_delta
if new_h < 0 or new_h >= height or new_w < 0 or new_w >= width:
continue
if grid[new_h][new_w] == ".":
qu.append((new_h, new_w, warps + 1))
return -1
if __name__ == "__main__":
print(main())
| insert | 30 | 30 | 30 | 34 | TLE | |
p02579 | C++ | Time Limit Exceeded | // D - Wizard in Maze
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(x) (x).begin(), (x).end()
#define rp(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define irp(i, s, e) for (int i = (int)(s); i > (int)(e); --i)
using PR = pair<int, int>; // <h,w>
using T3 = tuple<int, int, int>; // <h,w,gid>
int main() {
int H, W;
cin >> H >> W;
int C[2], D[2];
cin >> C[0] >> C[1] >> D[0] >> D[1];
C[0]--, C[1]--, D[0]--, D[1]--;
string S[H];
rp(i, 0, H) cin >> S[i];
PR dhw[] = {//<h,w>
{1, 0}, {0, -1}, {0, 1}, {-1, 0}, {2, -2}, {2, -1},
{2, 0}, {2, 1}, {2, 2}, {1, -2}, {1, -1}, {1, 1},
{1, 2}, {0, -2}, {0, 2}, {-1, -2}, {-1, -1}, {-1, 1},
{-1, 2}, {-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2}};
vvi vis(H, vi(W, -1));
deque<T3> dq;
dq.push_back({C[0], C[1], 0});
while (dq.size()) {
int h, w, gid;
tie(h, w, gid) = dq.front();
dq.pop_front();
if (0 <= vis[h][w] and vis[h][w] <= gid)
continue;
// if(vis[h][w] >= 0) continue;
// if(vis[h][w] <= gid) continue;
if (S[h][w] == '#')
continue;
vis[h][w] = gid;
rp(i, 0, 4) {
int nx_h = h + dhw[i].first;
int nx_w = w + dhw[i].second;
if (nx_h < 0 || H <= nx_h)
continue;
if (nx_w < 0 || W <= nx_w)
continue;
dq.push_front({nx_h, nx_w, gid});
}
rp(i, 4, 24) {
int nx_h = h + dhw[i].first;
int nx_w = w + dhw[i].second;
if (nx_h < 0 || H <= nx_h)
continue;
if (nx_w < 0 || W <= nx_w)
continue;
dq.push_front({nx_h, nx_w, gid + 1});
}
}
// rp(h,0,H){
// rp(w,0,W){
// if(vis[h][w] < 0) cout<<"X ";
// else cout<<""<<vis[h][w]<<" ";
// }
// puts("");
// }
// puts("---");
cout << vis[D[0]][D[1]] << endl;
}
| // D - Wizard in Maze
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(x) (x).begin(), (x).end()
#define rp(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define irp(i, s, e) for (int i = (int)(s); i > (int)(e); --i)
using PR = pair<int, int>; // <h,w>
using T3 = tuple<int, int, int>; // <h,w,gid>
int main() {
int H, W;
cin >> H >> W;
int C[2], D[2];
cin >> C[0] >> C[1] >> D[0] >> D[1];
C[0]--, C[1]--, D[0]--, D[1]--;
string S[H];
rp(i, 0, H) cin >> S[i];
PR dhw[] = {//<h,w>
{1, 0}, {0, -1}, {0, 1}, {-1, 0}, {2, -2}, {2, -1},
{2, 0}, {2, 1}, {2, 2}, {1, -2}, {1, -1}, {1, 1},
{1, 2}, {0, -2}, {0, 2}, {-1, -2}, {-1, -1}, {-1, 1},
{-1, 2}, {-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2}};
vvi vis(H, vi(W, -1));
deque<T3> dq;
dq.push_back({C[0], C[1], 0});
while (dq.size()) {
int h, w, gid;
tie(h, w, gid) = dq.front();
dq.pop_front();
if (0 <= vis[h][w] and vis[h][w] <= gid)
continue;
// if(vis[h][w] >= 0) continue;
// if(vis[h][w] <= gid) continue;
if (S[h][w] == '#')
continue;
vis[h][w] = gid;
rp(i, 0, 4) {
int nx_h = h + dhw[i].first;
int nx_w = w + dhw[i].second;
if (nx_h < 0 || H <= nx_h)
continue;
if (nx_w < 0 || W <= nx_w)
continue;
dq.push_front({nx_h, nx_w, gid});
}
rp(i, 4, 24) {
int nx_h = h + dhw[i].first;
int nx_w = w + dhw[i].second;
if (nx_h < 0 || H <= nx_h)
continue;
if (nx_w < 0 || W <= nx_w)
continue;
dq.push_back({nx_h, nx_w, gid + 1});
}
}
// rp(h,0,H){
// rp(w,0,W){
// if(vis[h][w] < 0) cout<<"X ";
// else cout<<""<<vis[h][w]<<" ";
// }
// puts("");
// }
// puts("---");
cout << vis[D[0]][D[1]] << endl;
}
| replace | 58 | 59 | 58 | 59 | TLE | |
p02579 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <queue>
const int bufSize = 1e6;
#define DEBUG
inline char nc() {
#ifdef DEBUG
return getchar();
#endif
static char buf[bufSize], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2)
? EOF
: *p1++;
}
template <typename T> inline T read(T &r) {
static char c;
static int flag;
flag = 1, r = 0;
for (c = nc(); !isdigit(c); c = nc())
if (c == '-')
flag = -1;
for (; isdigit(c); c = nc())
r = r * 10 + c - 48;
return r *= flag;
}
const int maxn = 1e3 + 100;
const int maxm = 1e6 + 100;
const int inf = 1061109567;
int n, m;
char s[maxm];
bool mp[maxn][maxn];
int num[maxn][maxn];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int fa[maxm], cnt;
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
inline void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return;
fa[x] = y;
}
struct node {
int to, next;
} E[5 * maxm];
int head[maxm];
inline void add(const int &x, const int &y) {
static int tot = 0;
E[++tot].next = head[x], E[tot].to = y, head[x] = tot;
}
int dis[maxn * maxn];
int main() {
read(n), read(m);
int sx, sy, tx, ty;
read(sx), read(sy), read(tx), read(ty);
memset(dis, 0x3f, sizeof(dis));
for (int i = 1; i <= n; ++i) {
scanf("%s", s + 1);
for (int j = 1; j <= m; ++j)
if (s[j] == '.')
mp[i][j] = 1;
else
mp[i][j] = 0;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
num[i][j] = ++cnt, fa[cnt] = cnt;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (!mp[i][j])
continue;
for (int k = 0; k < 4; ++k) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < 1 || nx > n || ny < 1 || ny > m)
continue;
if (!mp[nx][ny])
continue;
unite(num[i][j], num[nx][ny]);
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
for (int x = std::max(i - 2, 1); x <= i + 2 && x <= n; ++x) {
for (int y = std::max(j - 2, 1); y <= j + 2 && y <= m; ++y) {
if (!mp[x][y])
continue;
if (find(num[x][y]) == find(num[i][j]))
continue;
add(find(num[x][y]), find(num[i][j]));
}
}
}
}
std::queue<int> q;
q.push(find(num[sx][sy]));
dis[find(num[sx][sy])] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int p = head[u]; p; p = E[p].next) {
int v = E[p].to;
if (dis[v] > dis[u] + 1)
dis[v] = dis[u] + 1, q.push(v);
}
}
if (dis[find(num[tx][ty])] < inf)
printf("%d\n", dis[find(num[tx][ty])]);
else
puts("-1");
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <queue>
const int bufSize = 1e6;
#define DEBUG
inline char nc() {
#ifdef DEBUG
return getchar();
#endif
static char buf[bufSize], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2)
? EOF
: *p1++;
}
template <typename T> inline T read(T &r) {
static char c;
static int flag;
flag = 1, r = 0;
for (c = nc(); !isdigit(c); c = nc())
if (c == '-')
flag = -1;
for (; isdigit(c); c = nc())
r = r * 10 + c - 48;
return r *= flag;
}
const int maxn = 2e3 + 100;
const int maxm = 2e6 + 100;
const int inf = 1061109567;
int n, m;
char s[maxm];
bool mp[maxn][maxn];
int num[maxn][maxn];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int fa[maxm], cnt;
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
inline void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return;
fa[x] = y;
}
struct node {
int to, next;
} E[5 * maxm];
int head[maxm];
inline void add(const int &x, const int &y) {
static int tot = 0;
E[++tot].next = head[x], E[tot].to = y, head[x] = tot;
}
int dis[maxn * maxn];
int main() {
read(n), read(m);
int sx, sy, tx, ty;
read(sx), read(sy), read(tx), read(ty);
memset(dis, 0x3f, sizeof(dis));
for (int i = 1; i <= n; ++i) {
scanf("%s", s + 1);
for (int j = 1; j <= m; ++j)
if (s[j] == '.')
mp[i][j] = 1;
else
mp[i][j] = 0;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
num[i][j] = ++cnt, fa[cnt] = cnt;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (!mp[i][j])
continue;
for (int k = 0; k < 4; ++k) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < 1 || nx > n || ny < 1 || ny > m)
continue;
if (!mp[nx][ny])
continue;
unite(num[i][j], num[nx][ny]);
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
for (int x = std::max(i - 2, 1); x <= i + 2 && x <= n; ++x) {
for (int y = std::max(j - 2, 1); y <= j + 2 && y <= m; ++y) {
if (!mp[x][y])
continue;
if (find(num[x][y]) == find(num[i][j]))
continue;
add(find(num[x][y]), find(num[i][j]));
}
}
}
}
std::queue<int> q;
q.push(find(num[sx][sy]));
dis[find(num[sx][sy])] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int p = head[u]; p; p = E[p].next) {
int v = E[p].to;
if (dis[v] > dis[u] + 1)
dis[v] = dis[u] + 1, q.push(v);
}
}
if (dis[find(num[tx][ty])] < inf)
printf("%d\n", dis[find(num[tx][ty])]);
else
puts("-1");
return 0;
} | replace | 27 | 29 | 27 | 29 | 0 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
int main() {
/* 頂点を探索するときに使う(右, 下, 左, 上) */
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int h, w;
int cw, ch;
int dw, dh;
std::cin >> h >> w >> cw >> ch >> dw >> dh;
--cw, --ch, --dw, --dh; // 0-indexed
std::vector<std::string> maze(h);
for (int i = 0; i < h; ++i)
std::cin >> maze[i];
const int INF = 1e9;
// 距離と状態を管理
std::vector<std::vector<int>> dist(h, std::vector<int>(w, INF));
std::deque<std::pair<int, int>> q;
// スタート地点のノード
q.push_front(std::make_pair(ch, cw));
dist[ch][cw] = 0;
while (!q.empty()) {
std::pair<int, int> v = q.front();
q.pop_front();
int ci = v.first;
int cj = v.second;
int d = dist[ci][cj]; // 現在頂点の始点からの距離
/* コスト0で行ける頂点を探索 */
for (int i = 0; i < 4; ++i) {
int ci_next = ci + dy[i];
int cj_next = cj + dx[i];
if (cj_next < 0 || cj_next >= w)
continue; // 横方向にはみ出てたらskip
if (ci_next < 0 || ci_next >= h)
continue; // 縦方向にはみ出てたらskip
if (maze[ci_next][cj_next] == '#')
continue; // 壁の場合はskip
if (dist[ci_next][cj_next] <= d)
continue; // 今見ている頂点と同じコスト以下ならskip
dist[ci_next][cj_next] = d;
q.push_front(std::make_pair(ci_next, cj_next));
}
// 5近傍を探索
for (int di = -2; di <= 2; ++di) {
for (int dj = -2; dj <= 2; ++dj) {
int ci_next = ci + di;
int cj_next = cj + dj;
if (cj_next < 0 || cj_next >= w)
continue; // 横方向にはみ出てたらskip
if (ci_next < 0 || ci_next >= h)
continue; // 縦方向にはみ出てたらskip
if (maze[ci_next][cj_next] == '#')
continue; // 壁の場合はskip
if (dist[ci_next][cj_next] <= d + 1)
continue; // 今見ている頂点と同じコスト以下ならskip
dist[ci_next][cj_next] = d + 1;
q.push_back(std::make_pair(ci_next, cj_next));
}
}
}
int ans = dist[dh][dw];
if (ans == INF)
ans = -1;
std::cout << ans << std::endl << std::flush;
return 0;
}
| #include <bits/stdc++.h>
int main() {
/* 頂点を探索するときに使う(右, 下, 左, 上) */
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int h, w;
int cw, ch;
int dw, dh;
std::cin >> h >> w >> ch >> cw >> dh >> dw;
--cw, --ch, --dw, --dh; // 0-indexed
std::vector<std::string> maze(h);
for (int i = 0; i < h; ++i)
std::cin >> maze[i];
const int INF = 1e9;
// 距離と状態を管理
std::vector<std::vector<int>> dist(h, std::vector<int>(w, INF));
std::deque<std::pair<int, int>> q;
// スタート地点のノード
q.push_front(std::make_pair(ch, cw));
dist[ch][cw] = 0;
while (!q.empty()) {
std::pair<int, int> v = q.front();
q.pop_front();
int ci = v.first;
int cj = v.second;
int d = dist[ci][cj]; // 現在頂点の始点からの距離
/* コスト0で行ける頂点を探索 */
for (int i = 0; i < 4; ++i) {
int ci_next = ci + dy[i];
int cj_next = cj + dx[i];
if (cj_next < 0 || cj_next >= w)
continue; // 横方向にはみ出てたらskip
if (ci_next < 0 || ci_next >= h)
continue; // 縦方向にはみ出てたらskip
if (maze[ci_next][cj_next] == '#')
continue; // 壁の場合はskip
if (dist[ci_next][cj_next] <= d)
continue; // 今見ている頂点と同じコスト以下ならskip
dist[ci_next][cj_next] = d;
q.push_front(std::make_pair(ci_next, cj_next));
}
// 5近傍を探索
for (int di = -2; di <= 2; ++di) {
for (int dj = -2; dj <= 2; ++dj) {
int ci_next = ci + di;
int cj_next = cj + dj;
if (cj_next < 0 || cj_next >= w)
continue; // 横方向にはみ出てたらskip
if (ci_next < 0 || ci_next >= h)
continue; // 縦方向にはみ出てたらskip
if (maze[ci_next][cj_next] == '#')
continue; // 壁の場合はskip
if (dist[ci_next][cj_next] <= d + 1)
continue; // 今見ている頂点と同じコスト以下ならskip
dist[ci_next][cj_next] = d + 1;
q.push_back(std::make_pair(ci_next, cj_next));
}
}
}
int ans = dist[dh][dw];
if (ans == INF)
ans = -1;
std::cout << ans << std::endl << std::flush;
return 0;
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define INF 1000000007
#define LINF (1LL << 62)
#define PI 3.14159265358979
typedef long long i64;
typedef pair<i64, i64> P;
inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int h, w, a, b, c, d;
string s[3030];
int m[3030][3030];
void solve() {
cin >> h >> w;
cin >> a >> b >> c >> d;
a--;
b--;
c--;
d--;
for (int i = 0; i < h; i++) {
cin >> s[i];
}
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int dd[5] = {-2, -1, 0, 1, 2};
priority_queue<pair<int, P>> q;
q.push({1, {a, b}});
while (!q.empty()) {
auto p = q.top();
q.pop();
int cnt = p.first;
int x = p.second.first, y = p.second.second;
for (int k = 0; k < 4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx < 0 || ny < 0 || nx >= h || ny >= w)
continue;
if (s[nx][ny] == '#')
continue;
if (m[nx][ny] != 0 && m[nx][ny] <= cnt)
continue;
m[nx][ny] = cnt;
q.push({cnt, {nx, ny}});
}
for (int i = 0; i < 5; i++) {
int nx = x + dd[i];
if (nx < 0 || nx >= h)
continue;
for (int j = 0; j < 5; j++) {
int ny = y + dd[j];
if (ny < 0 || ny >= w)
continue;
if (s[nx][ny] == '#')
continue;
if (m[nx][ny] != 0 && m[nx][ny] <= cnt + 1)
continue;
m[nx][ny] = cnt + 1;
q.push({cnt + 1, {nx, ny}});
}
}
}
cout << m[c][d] - 1 << endl;
}
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define INF 1000000007
#define LINF (1LL << 62)
#define PI 3.14159265358979
typedef long long i64;
typedef pair<i64, i64> P;
inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int h, w, a, b, c, d;
string s[3030];
int m[3030][3030];
void solve() {
cin >> h >> w;
cin >> a >> b >> c >> d;
a--;
b--;
c--;
d--;
for (int i = 0; i < h; i++) {
cin >> s[i];
}
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int dd[5] = {-2, -1, 0, 1, 2};
priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> q;
q.push({1, {a, b}});
while (!q.empty()) {
auto p = q.top();
q.pop();
int cnt = p.first;
int x = p.second.first, y = p.second.second;
for (int k = 0; k < 4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx < 0 || ny < 0 || nx >= h || ny >= w)
continue;
if (s[nx][ny] == '#')
continue;
if (m[nx][ny] != 0 && m[nx][ny] <= cnt)
continue;
m[nx][ny] = cnt;
q.push({cnt, {nx, ny}});
}
for (int i = 0; i < 5; i++) {
int nx = x + dd[i];
if (nx < 0 || nx >= h)
continue;
for (int j = 0; j < 5; j++) {
int ny = y + dd[j];
if (ny < 0 || ny >= w)
continue;
if (s[nx][ny] == '#')
continue;
if (m[nx][ny] != 0 && m[nx][ny] <= cnt + 1)
continue;
m[nx][ny] = cnt + 1;
q.push({cnt + 1, {nx, ny}});
}
}
}
cout << m[c][d] - 1 << endl;
}
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
} | replace | 44 | 45 | 44 | 45 | TLE | |
p02579 | C++ | Runtime Error | /* #region Head */
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
using vll = vc<ll>;
using vvll = vvc<ll>;
using vld = vc<ld>;
using vvld = vvc<ld>;
using vs = vc<string>;
using vvs = vvc<string>;
template <class T, class U> using um = unordered_map<T, U>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqa = priority_queue<T, vc<T>, greater<T>>;
template <class T> using us = unordered_set<T>;
#define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i))
#define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i))
#define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i))
#define REPD(i, m, n, d) \
for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d))
#define REPMD(i, m, n, d) \
for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d))
#define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define ALL(x) begin(x), end(x)
#define SIZE(x) ((ll)(x).size())
#define PERM(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define UNIQ(v) v.erase(unique(ALL(v)), v.end());
#define endl '\n'
#define sqrt sqrtl
#define floor floorl
#define log2 log2l
constexpr ll INF = 1'010'000'000'000'000'017LL;
constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.14159265358979323846;
template <typename T>
istream &operator>>(istream &is, vc<T> &vec) { // vector 入力
for (T &x : vec)
is >> x;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, vc<T> &vec) { // vector 出力 (for dump)
os << "{";
REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "" : ", ");
os << "}";
return os;
}
template <typename T>
ostream &operator>>(ostream &os, vc<T> &vec) { // vector 出力 (inline)
REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "\n" : " ");
return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &pair_var) { // pair 入力
is >> pair_var.first >> pair_var.second;
return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) { // pair 出力
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
// map, um, set, us 出力
template <class T> ostream &out_iter(ostream &os, T &map_var) {
os << "{";
REPI(itr, map_var) {
os << *itr;
auto itrcp = itr;
if (++itrcp != map_var.end())
os << ", ";
}
return os << "}";
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
return out_iter(os, map_var);
}
template <typename T, typename U>
ostream &operator<<(ostream &os, um<T, U> &map_var) {
os << "{";
REPI(itr, map_var) {
auto [key, value] = *itr;
os << "(" << key << ", " << value << ")";
auto itrcp = itr;
if (++itrcp != map_var.end())
os << ", ";
}
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
return out_iter(os, set_var);
}
template <typename T> ostream &operator<<(ostream &os, us<T> &set_var) {
return out_iter(os, set_var);
}
template <typename T> ostream &operator<<(ostream &os, pq<T> &pq_var) {
pq<T> pq_cp(pq_var);
os << "{";
if (!pq_cp.empty()) {
os << pq_cp.top(), pq_cp.pop();
while (!pq_cp.empty())
os << ", " << pq_cp.top(), pq_cp.pop();
}
return os << "}";
}
// dump
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) > 0)
DUMPOUT << ", ";
dump_func(move(tail)...);
}
// chmax (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
if (comp(xmax, x)) {
xmax = x;
return true;
}
return false;
}
// chmin (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
if (comp(x, xmin)) {
xmin = x;
return true;
}
return false;
}
// ローカル用
#define DEBUG_
#ifdef DEBUG_
#define DEB
#define dump(...) \
DUMPOUT << " " << string(#__VA_ARGS__) << ": " \
<< "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \
<< " ", \
dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif
struct AtCoderInitialize {
static constexpr int IOS_PREC = 15;
static constexpr bool AUTOFLUSH = false;
AtCoderInitialize() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(IOS_PREC);
if (AUTOFLUSH)
cout << unitbuf;
}
} ATCODER_INITIALIZE;
void Yn(bool p) { cout << (p ? "Yes" : "No") << endl; }
void YN(bool p) { cout << (p ? "YES" : "NO") << endl; }
/* #endregion */
/* #region Graph */
// エッジ(本来エッジは双方向だが,ここでは単方向で管理)
template <class weight_t, class flow_t> struct Edge {
ll src; // エッジ始点となる頂点
ll dst; // エッジ終点となる頂点
weight_t weight; // 重み
flow_t cap;
Edge() : src(0), dst(0), weight(0) {}
Edge(ll src, ll dst, weight_t weight) : src(src), dst(dst), weight(weight) {}
Edge(ll src, ll dst, weight_t weight, flow_t cap)
: src(src), dst(dst), weight(weight), cap(cap) {}
// Edge 標準出力
friend ostream &operator<<(ostream &os, Edge &edge) {
os << "(" << edge.src << " -> " << edge.dst << ", " << edge.weight << ")";
return os;
}
};
// 同じ頂点を始点とするエッジ集合
template <class weight_t, class flow_t>
class Node : public vc<Edge<weight_t, flow_t>> {
public:
ll idx;
Node() : vc<Edge<weight_t, flow_t>>() {}
// void add(int a, int b, weight_t w, flow_t cap) { this->emplace_back(a, b,
// w, cap); };
};
// graph[i] := 頂点 i を始点とするエッジ集合
template <class weight_t, class flow_t>
class Graph : public vc<Node<weight_t, flow_t>> {
public:
Graph() : vc<Node<weight_t, flow_t>>() {}
Graph(int n) : vc<Node<weight_t, flow_t>>(n) {
REP(i, 0, n)(*this)[i].idx = i;
}
// 単方向
void add_arc(int a, int b, weight_t w = 1, flow_t cap = 1) {
(*this)[a].emplace_back(a, b, w, cap);
}
// 双方向
void add_edge(int a, int b, weight_t w = 1, flow_t cap = 1) {
add_arc(a, b, w, cap), add_arc(b, a, w, cap);
}
};
// using Array = vc<Weight>;
// using Matrix = vc<Array>;
/* #endregion */
// Problem
void solve() {
ll h, w;
cin >> h >> w;
ll ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
--ch, --cw, --dh, --dw;
vc<string> s(h);
cin >> s;
char wall = '#';
const int dCol[4] = {1, 0, -1, 0};
const int dRow[4] = {0, 1, 0, -1};
vvll groupnums(h, vll(w, -1));
// auto dfs = [&](auto &&dfs, ll num, ll curRow, ll curCol) -> void {
// if (groupnums[curRow][curCol] == num) return; // 訪問済み
// if (s[curRow][curCol] == wall) return;
// groupnums[curRow][curCol] = num;
// // for (Edge<ll, ll> &edge : graph[idx]) dfs(dfs, num, edge.dst);
// REP(dir, 0, 4) {
// ll nextRow = curRow + dRow[dir];
// ll nextCol = curCol + dCol[dir];
// if (nextRow < 0 || nextRow >= h || nextCol < 0 || nextCol >= w)
// continue; if (s[nextRow][nextCol] == wall) continue;
// // if (dist[nextRow][nextCol] != INF) continue; // 訪問済み(BFS)
// dfs(dfs, num, nextRow, nextCol);
// }
// };
ll num = 0;
using node = pll;
// REP(i, 0, h) REP(j, 0, w) if (groupnums[i][j] == -1 && s[i][j] != wall)
// dfs(dfs, num++, i, j);
REP(sRow, 0, h)
REP(sCol, 0, w) if (groupnums[sRow][sCol] == -1 && s[sRow][sCol] != wall) {
groupnums[sRow][sCol] = num;
queue<node> que;
que.emplace(sRow, sCol);
while (!que.empty()) {
auto [curRow, curCol] = que.front();
que.pop();
// Dist curDist = dist[curRow][curCol];
// cuerrent から辿れる頂点をすべて調べる
REP(dir, 0, 4) {
ll nextRow = curRow + dRow[dir];
ll nextCol = curCol + dCol[dir];
if (nextRow < 0 || nextRow >= h || nextCol < 0 || nextCol >= w)
continue;
if (s[nextRow][nextCol] == wall)
continue;
if (groupnums[nextRow][nextCol] != -1)
continue; // 訪問済み(BFS)
// Dist nextDist = curDist + 1;
// if (chmin(dist[nextRow][nextCol], nextDist)) que.emplace(nextRow,
// nextCol); // ↑この書き方はダイクストラっぽい
groupnums[nextRow][nextCol] = num;
que.emplace(nextRow, nextCol);
}
}
num++;
}
// BFS 終了
// dump(groupnums, num); // num に連結成分数が入っている
if (groupnums[ch][cw] == groupnums[dh][dw]) {
cout << 0 << endl;
return;
}
// 1回以上ワープする
// vc<vc<bool>> warpable(num, vc<bool>(num, false));
// dump(groupnums);
set<pll> arcs;
// 左右の隣同士
REP(i, 0, h) REP(j, 0, w) if (groupnums[i][j] != -1) {
REPM(k, -2, 2) REPM(l, 0, 2) {
ll ii = i + k;
ll jj = j + l;
if (ii < 0 || jj < 0 || ii >= h || jj >= w)
continue;
if (groupnums[ii][jj] == -1)
continue;
if (groupnums[ii][jj] == groupnums[i][j])
continue;
// warpable[groupnums[i][j]][groupnums[ii][jj]] = true;
// warpable[groupnums[ii][jj]][groupnums[i][j]] = true;
arcs.emplace(groupnums[i][j], groupnums[ii][jj]);
arcs.emplace(groupnums[ii][jj], groupnums[i][j]);
}
// REPM(k, -1, 1) REPM(l, -1, 1) {
// if (k == 0 && l == 0) continue;
// }
}
// dump(warpable);
assert(false);
Graph<ll, ll> graph(num);
// REP(i, 0, num) REP(j, 0, num) if (warpable[i][j]) graph.add_arc(i, j);
for (const pll &p : arcs)
graph.add_arc(p.first, p.second);
ll idx1 = groupnums[ch][cw]; // 始点として選ぶ頂点
// if (SIZE(graph[idx1]) == 0) continue;
using Weight = ll;
using state = pair<Weight, ll>; // (始点からの最短経路長, ノード番号)
priority_queue<state, vc<state>, greater<state>>
que; // 昇順に並べ替え,小さい順に取り出す
vc<Weight> dist(num, INF);
dist[idx1] = 0;
que.emplace(0, idx1);
while (que.size()) {
// tie(d, v) = que.top();
state cur = que.top();
que.pop();
Weight d = cur.first;
ll v = cur.second;
if (dist[v] < d)
continue;
for (Edge<Weight, ll> edge : graph[v])
if (chmin(dist[edge.dst], dist[v] + edge.weight))
que.emplace(dist[edge.dst], edge.dst);
}
// ここまでで, idx1 を始点として各頂点までの距離が dist に入った
// ただし,到達不可能な頂点は距離が INF になっているので,
// chmax(ret, *max_element(ALL(dist))); は不可
if (dist[groupnums[dh][dw]] == INF) {
cout << -1 << endl;
} else {
cout << dist[groupnums[dh][dw]] << endl;
}
}
// entry point
int main() {
solve();
return 0;
}
| /* #region Head */
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
using vll = vc<ll>;
using vvll = vvc<ll>;
using vld = vc<ld>;
using vvld = vvc<ld>;
using vs = vc<string>;
using vvs = vvc<string>;
template <class T, class U> using um = unordered_map<T, U>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqa = priority_queue<T, vc<T>, greater<T>>;
template <class T> using us = unordered_set<T>;
#define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i))
#define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i))
#define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i))
#define REPD(i, m, n, d) \
for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d))
#define REPMD(i, m, n, d) \
for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d))
#define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define ALL(x) begin(x), end(x)
#define SIZE(x) ((ll)(x).size())
#define PERM(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define UNIQ(v) v.erase(unique(ALL(v)), v.end());
#define endl '\n'
#define sqrt sqrtl
#define floor floorl
#define log2 log2l
constexpr ll INF = 1'010'000'000'000'000'017LL;
constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.14159265358979323846;
template <typename T>
istream &operator>>(istream &is, vc<T> &vec) { // vector 入力
for (T &x : vec)
is >> x;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, vc<T> &vec) { // vector 出力 (for dump)
os << "{";
REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "" : ", ");
os << "}";
return os;
}
template <typename T>
ostream &operator>>(ostream &os, vc<T> &vec) { // vector 出力 (inline)
REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "\n" : " ");
return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &pair_var) { // pair 入力
is >> pair_var.first >> pair_var.second;
return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) { // pair 出力
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
// map, um, set, us 出力
template <class T> ostream &out_iter(ostream &os, T &map_var) {
os << "{";
REPI(itr, map_var) {
os << *itr;
auto itrcp = itr;
if (++itrcp != map_var.end())
os << ", ";
}
return os << "}";
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
return out_iter(os, map_var);
}
template <typename T, typename U>
ostream &operator<<(ostream &os, um<T, U> &map_var) {
os << "{";
REPI(itr, map_var) {
auto [key, value] = *itr;
os << "(" << key << ", " << value << ")";
auto itrcp = itr;
if (++itrcp != map_var.end())
os << ", ";
}
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
return out_iter(os, set_var);
}
template <typename T> ostream &operator<<(ostream &os, us<T> &set_var) {
return out_iter(os, set_var);
}
template <typename T> ostream &operator<<(ostream &os, pq<T> &pq_var) {
pq<T> pq_cp(pq_var);
os << "{";
if (!pq_cp.empty()) {
os << pq_cp.top(), pq_cp.pop();
while (!pq_cp.empty())
os << ", " << pq_cp.top(), pq_cp.pop();
}
return os << "}";
}
// dump
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) > 0)
DUMPOUT << ", ";
dump_func(move(tail)...);
}
// chmax (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
if (comp(xmax, x)) {
xmax = x;
return true;
}
return false;
}
// chmin (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
if (comp(x, xmin)) {
xmin = x;
return true;
}
return false;
}
// ローカル用
#define DEBUG_
#ifdef DEBUG_
#define DEB
#define dump(...) \
DUMPOUT << " " << string(#__VA_ARGS__) << ": " \
<< "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \
<< " ", \
dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif
struct AtCoderInitialize {
static constexpr int IOS_PREC = 15;
static constexpr bool AUTOFLUSH = false;
AtCoderInitialize() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(IOS_PREC);
if (AUTOFLUSH)
cout << unitbuf;
}
} ATCODER_INITIALIZE;
void Yn(bool p) { cout << (p ? "Yes" : "No") << endl; }
void YN(bool p) { cout << (p ? "YES" : "NO") << endl; }
/* #endregion */
/* #region Graph */
// エッジ(本来エッジは双方向だが,ここでは単方向で管理)
template <class weight_t, class flow_t> struct Edge {
ll src; // エッジ始点となる頂点
ll dst; // エッジ終点となる頂点
weight_t weight; // 重み
flow_t cap;
Edge() : src(0), dst(0), weight(0) {}
Edge(ll src, ll dst, weight_t weight) : src(src), dst(dst), weight(weight) {}
Edge(ll src, ll dst, weight_t weight, flow_t cap)
: src(src), dst(dst), weight(weight), cap(cap) {}
// Edge 標準出力
friend ostream &operator<<(ostream &os, Edge &edge) {
os << "(" << edge.src << " -> " << edge.dst << ", " << edge.weight << ")";
return os;
}
};
// 同じ頂点を始点とするエッジ集合
template <class weight_t, class flow_t>
class Node : public vc<Edge<weight_t, flow_t>> {
public:
ll idx;
Node() : vc<Edge<weight_t, flow_t>>() {}
// void add(int a, int b, weight_t w, flow_t cap) { this->emplace_back(a, b,
// w, cap); };
};
// graph[i] := 頂点 i を始点とするエッジ集合
template <class weight_t, class flow_t>
class Graph : public vc<Node<weight_t, flow_t>> {
public:
Graph() : vc<Node<weight_t, flow_t>>() {}
Graph(int n) : vc<Node<weight_t, flow_t>>(n) {
REP(i, 0, n)(*this)[i].idx = i;
}
// 単方向
void add_arc(int a, int b, weight_t w = 1, flow_t cap = 1) {
(*this)[a].emplace_back(a, b, w, cap);
}
// 双方向
void add_edge(int a, int b, weight_t w = 1, flow_t cap = 1) {
add_arc(a, b, w, cap), add_arc(b, a, w, cap);
}
};
// using Array = vc<Weight>;
// using Matrix = vc<Array>;
/* #endregion */
// Problem
void solve() {
ll h, w;
cin >> h >> w;
ll ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
--ch, --cw, --dh, --dw;
vc<string> s(h);
cin >> s;
char wall = '#';
const int dCol[4] = {1, 0, -1, 0};
const int dRow[4] = {0, 1, 0, -1};
vvll groupnums(h, vll(w, -1));
// auto dfs = [&](auto &&dfs, ll num, ll curRow, ll curCol) -> void {
// if (groupnums[curRow][curCol] == num) return; // 訪問済み
// if (s[curRow][curCol] == wall) return;
// groupnums[curRow][curCol] = num;
// // for (Edge<ll, ll> &edge : graph[idx]) dfs(dfs, num, edge.dst);
// REP(dir, 0, 4) {
// ll nextRow = curRow + dRow[dir];
// ll nextCol = curCol + dCol[dir];
// if (nextRow < 0 || nextRow >= h || nextCol < 0 || nextCol >= w)
// continue; if (s[nextRow][nextCol] == wall) continue;
// // if (dist[nextRow][nextCol] != INF) continue; // 訪問済み(BFS)
// dfs(dfs, num, nextRow, nextCol);
// }
// };
ll num = 0;
using node = pll;
// REP(i, 0, h) REP(j, 0, w) if (groupnums[i][j] == -1 && s[i][j] != wall)
// dfs(dfs, num++, i, j);
REP(sRow, 0, h)
REP(sCol, 0, w) if (groupnums[sRow][sCol] == -1 && s[sRow][sCol] != wall) {
groupnums[sRow][sCol] = num;
queue<node> que;
que.emplace(sRow, sCol);
while (!que.empty()) {
auto [curRow, curCol] = que.front();
que.pop();
// Dist curDist = dist[curRow][curCol];
// cuerrent から辿れる頂点をすべて調べる
REP(dir, 0, 4) {
ll nextRow = curRow + dRow[dir];
ll nextCol = curCol + dCol[dir];
if (nextRow < 0 || nextRow >= h || nextCol < 0 || nextCol >= w)
continue;
if (s[nextRow][nextCol] == wall)
continue;
if (groupnums[nextRow][nextCol] != -1)
continue; // 訪問済み(BFS)
// Dist nextDist = curDist + 1;
// if (chmin(dist[nextRow][nextCol], nextDist)) que.emplace(nextRow,
// nextCol); // ↑この書き方はダイクストラっぽい
groupnums[nextRow][nextCol] = num;
que.emplace(nextRow, nextCol);
}
}
num++;
}
// BFS 終了
// dump(groupnums, num); // num に連結成分数が入っている
if (groupnums[ch][cw] == groupnums[dh][dw]) {
cout << 0 << endl;
return;
}
// 1回以上ワープする
// vc<vc<bool>> warpable(num, vc<bool>(num, false));
// dump(groupnums);
set<pll> arcs;
// 左右の隣同士
REP(i, 0, h) REP(j, 0, w) if (groupnums[i][j] != -1) {
REPM(k, -2, 2) REPM(l, 0, 2) {
ll ii = i + k;
ll jj = j + l;
if (ii < 0 || jj < 0 || ii >= h || jj >= w)
continue;
if (groupnums[ii][jj] == -1)
continue;
if (groupnums[ii][jj] == groupnums[i][j])
continue;
// warpable[groupnums[i][j]][groupnums[ii][jj]] = true;
// warpable[groupnums[ii][jj]][groupnums[i][j]] = true;
arcs.emplace(groupnums[i][j], groupnums[ii][jj]);
arcs.emplace(groupnums[ii][jj], groupnums[i][j]);
}
// REPM(k, -1, 1) REPM(l, -1, 1) {
// if (k == 0 && l == 0) continue;
// }
}
// dump(warpable);
// assert(false);
Graph<ll, ll> graph(num);
// REP(i, 0, num) REP(j, 0, num) if (warpable[i][j]) graph.add_arc(i, j);
for (const pll &p : arcs)
graph.add_arc(p.first, p.second);
ll idx1 = groupnums[ch][cw]; // 始点として選ぶ頂点
// if (SIZE(graph[idx1]) == 0) continue;
using Weight = ll;
using state = pair<Weight, ll>; // (始点からの最短経路長, ノード番号)
priority_queue<state, vc<state>, greater<state>>
que; // 昇順に並べ替え,小さい順に取り出す
vc<Weight> dist(num, INF);
dist[idx1] = 0;
que.emplace(0, idx1);
while (que.size()) {
// tie(d, v) = que.top();
state cur = que.top();
que.pop();
Weight d = cur.first;
ll v = cur.second;
if (dist[v] < d)
continue;
for (Edge<Weight, ll> edge : graph[v])
if (chmin(dist[edge.dst], dist[v] + edge.weight))
que.emplace(dist[edge.dst], edge.dst);
}
// ここまでで, idx1 を始点として各頂点までの距離が dist に入った
// ただし,到達不可能な頂点は距離が INF になっているので,
// chmax(ret, *max_element(ALL(dist))); は不可
if (dist[groupnums[dh][dw]] == INF) {
cout << -1 << endl;
} else {
cout << dist[groupnums[dh][dw]] << endl;
}
}
// entry point
int main() {
solve();
return 0;
}
| replace | 331 | 332 | 331 | 332 | -6 | f74a617f-c4a6-43f6-b01a-15a30f52c569.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02579/C++/s664238343.cpp:290: void solve(): Assertion `false' failed.
|
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;
int main() {
ll H, W;
cin >> H >> W;
ll sh, sw, gh, gw;
cin >> sh >> sw;
cin >> gh >> gw;
sh--;
sw--;
gh--;
gw--;
// 道の一覧
vector<vector<bool>> roads(H, vector<bool>(W, false));
for (ll h = 0; h < H; h++) {
for (ll w = 0; w < W; w++) {
char c;
cin >> c;
while (c != '.' && c != '#') {
cin >> c;
}
if (c == '.') {
roads[h][w] = true;
}
}
}
vector<vector<bool>> done(H, vector<bool>(W, false));
vector<stack<pair<ll, ll>>> diststack(1 + (H * W) / 4);
ll d = 0;
diststack[0].push(make_pair(sh, sw));
while (d < (ll)diststack.size()) {
if (diststack[d].empty()) {
d++;
continue;
}
auto src = diststack[d].top();
diststack[d].pop();
ll h = src.first;
ll w = src.second;
if (h == gh && w == gw) {
cout << d << endl;
return 0;
}
if (done[h][w]) {
continue;
}
done[h][w] = true;
for (ll y = h - 2; y <= h + 2; y++) {
for (ll x = w - 2; x <= w + 2; x++) {
if (y >= 0 && y < H && x >= 0 && x < W && roads[y][x] && !done[y][x]) {
if (abs(x - w) + abs(y - h) == 1) {
diststack[d].push(make_pair(y, x));
} else {
diststack[d + 1].push(make_pair(y, x));
}
}
}
}
}
cout << -1 << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;
int main() {
ll H, W;
cin >> H >> W;
ll sh, sw, gh, gw;
cin >> sh >> sw;
cin >> gh >> gw;
sh--;
sw--;
gh--;
gw--;
// 道の一覧
vector<vector<bool>> roads(H, vector<bool>(W, false));
for (ll h = 0; h < H; h++) {
for (ll w = 0; w < W; w++) {
char c;
cin >> c;
while (c != '.' && c != '#') {
cin >> c;
}
if (c == '.') {
roads[h][w] = true;
}
}
}
vector<vector<bool>> done(H, vector<bool>(W, false));
vector<stack<pair<ll, ll>>> diststack(1 + (H * W) / 2);
ll d = 0;
diststack[0].push(make_pair(sh, sw));
while (d < (ll)diststack.size()) {
if (diststack[d].empty()) {
d++;
continue;
}
auto src = diststack[d].top();
diststack[d].pop();
ll h = src.first;
ll w = src.second;
if (h == gh && w == gw) {
cout << d << endl;
return 0;
}
if (done[h][w]) {
continue;
}
done[h][w] = true;
for (ll y = h - 2; y <= h + 2; y++) {
for (ll x = w - 2; x <= w + 2; x++) {
if (y >= 0 && y < H && x >= 0 && x < W && roads[y][x] && !done[y][x]) {
if (abs(x - w) + abs(y - h) == 1) {
diststack[d].push(make_pair(y, x));
} else {
diststack[d + 1].push(make_pair(y, x));
}
}
}
}
}
cout << -1 << endl;
}
| replace | 33 | 34 | 33 | 34 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath> //sqrt pow
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // dequef
#include <iostream> // cout, endl, cin
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define all(a) (a).begin(), (a).end()
#define mod 1000000007
using ll = long long;
using namespace std;
int movex[] = {0, 0, -1, 1};
int movey[] = {-1, 1, 0, 0};
int warpx[] = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
-2, -2, -2, 2, 2, 2, -1, 1, -1, 1};
int warpy[] = {-2, -2, -2, -2, -2, 2, 2, 2, 2, 2,
-1, 0, 1, -1, 0, 1, -1, -1, 1, 1};
int main() {
int inf = 100000000;
int h, w, ch, cw, dh, dw;
cin >> h >> w >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
vvi cnt(h, vi(w, inf));
rep(i, h) {
rep(j, w) {
char t;
cin >> t;
if (t == '#')
cnt[i][j] = -1;
}
}
deque<pii> que;
cnt[ch][cw] = 0;
que.push_back(make_pair(ch, cw));
while (que.size()) {
auto num = que.front();
que.pop_front();
int posy = num.first, posx = num.second;
rep(i, 4) {
int y = posy + movey[i], x = posx + movex[i];
if (y >= 0 && x >= 0 && y < h && x < w) {
if (cnt[y][x] > cnt[posy][posx]) {
cnt[y][x] = cnt[posy][posx];
que.push_front(make_pair(y, x));
}
}
}
rep(i, 20) {
int y = posy + warpy[i], x = posx + warpx[i];
if (y >= 0 && x >= 0 && y < h && x < w) {
if (cnt[y][x] > cnt[posy][posx] + 1) {
cnt[y][x] = cnt[posy][posx] + 1;
que.push_front(make_pair(y, x));
}
}
}
}
if (cnt[dh][dw] == inf)
cout << -1 << endl;
else
cout << cnt[dh][dw] << endl;
} | #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath> //sqrt pow
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // dequef
#include <iostream> // cout, endl, cin
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define all(a) (a).begin(), (a).end()
#define mod 1000000007
using ll = long long;
using namespace std;
int movex[] = {0, 0, -1, 1};
int movey[] = {-1, 1, 0, 0};
int warpx[] = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
-2, -2, -2, 2, 2, 2, -1, 1, -1, 1};
int warpy[] = {-2, -2, -2, -2, -2, 2, 2, 2, 2, 2,
-1, 0, 1, -1, 0, 1, -1, -1, 1, 1};
int main() {
int inf = 100000000;
int h, w, ch, cw, dh, dw;
cin >> h >> w >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
vvi cnt(h, vi(w, inf));
rep(i, h) {
rep(j, w) {
char t;
cin >> t;
if (t == '#')
cnt[i][j] = -1;
}
}
deque<pii> que;
cnt[ch][cw] = 0;
que.push_back(make_pair(ch, cw));
while (que.size()) {
auto num = que.front();
que.pop_front();
int posy = num.first, posx = num.second;
rep(i, 4) {
int y = posy + movey[i], x = posx + movex[i];
if (y >= 0 && x >= 0 && y < h && x < w) {
if (cnt[y][x] > cnt[posy][posx]) {
cnt[y][x] = cnt[posy][posx];
que.push_front(make_pair(y, x));
}
}
}
rep(i, 20) {
int y = posy + warpy[i], x = posx + warpx[i];
if (y >= 0 && x >= 0 && y < h && x < w) {
if (cnt[y][x] > cnt[posy][posx] + 1) {
cnt[y][x] = cnt[posy][posx] + 1;
que.push_back(make_pair(y, x));
}
}
}
}
if (cnt[dh][dw] == inf)
cout << -1 << endl;
else
cout << cnt[dh][dw] << endl;
} | replace | 71 | 72 | 71 | 72 | TLE | |
p02579 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
const ll MAX = 1001001;
const ll MOD = 1000000007;
struct edge {
ll to, cost;
};
typedef vector<vector<edge>> AdjList;
AdjList graph;
const ll INF = 1001001001001001001;
vector<ll> dist, prever;
void dijkstra(ll n, ll s) {
dist = vector<ll>(n, INF);
prever = vector<ll>(n, -1);
dist[s] = 0;
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(0, s));
while (que.size()) {
P p = que.top();
que.pop();
ll v = p.second;
if (dist[v] < p.first) {
continue;
}
for (ll i = 0; i < graph[v].size(); i++) {
edge e = graph[v][i];
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
prever[e.to] = v;
que.push(P(dist[e.to], e.to));
}
}
}
}
vector<ll> get_path(ll t) { // 頂点への最短路
vector<ll> path;
for (; t != -1; t = prever[t]) {
path.push_back(t);
}
reverse(path.begin(), path.end());
return path;
}
/*
graph=AdjList(n);
rep(i,n-1){
ll from,to,cost;
ll cin>>from>>to>>cost;
from--;to--;
graph[from].push_back((edge){to,cost});
graph[to].push_back((edge){from,cost});
}
*/
int main() {
ll h, w;
cin >> h >> w;
ll cx, cy, gx, gy;
cin >> cx >> cy >> gx >> gy;
cx--;
cy--;
gx--;
gy--;
graph = AdjList(h * w);
vector<vector<char>> s(h, vector<char>(w));
rep(i, h) {
rep(j, w) { cin >> s[i][j]; }
}
ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
rep(i, h) {
rep(j, w) {
rep(k, 4) {
if (s[i][j] == '#') {
continue;
}
ll nx = i + dx[k];
ll ny = j + dy[k];
if (0 <= nx && nx < h && 0 <= ny && ny < w) {
if (s[nx][ny] == '.') {
graph[i * w + j].push_back((edge){nx * w + ny, 0});
graph[nx * w + ny].push_back((edge){i * w + j, 0});
}
}
rep2(k, -2, 3) {
rep2(l, -2, 3) {
ll nx = i + k;
ll ny = j + l;
if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != '#') {
graph[i * w + j].push_back((edge){nx * w + ny, 1});
graph[nx * w + ny].push_back((edge){i * w + j, 1});
}
}
}
}
}
}
dijkstra(h * w, cx * w + cy);
if (dist[gx * w + gy] == INF) {
cout << -1 << endl;
} else {
cout << dist[gx * w + gy] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
const ll MAX = 1001001;
const ll MOD = 1000000007;
struct edge {
ll to, cost;
};
typedef vector<vector<edge>> AdjList;
AdjList graph;
const ll INF = 1001001001001001001;
vector<ll> dist, prever;
void dijkstra(ll n, ll s) {
dist = vector<ll>(n, INF);
prever = vector<ll>(n, -1);
dist[s] = 0;
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(0, s));
while (que.size()) {
P p = que.top();
que.pop();
ll v = p.second;
if (dist[v] < p.first) {
continue;
}
for (ll i = 0; i < graph[v].size(); i++) {
edge e = graph[v][i];
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
prever[e.to] = v;
que.push(P(dist[e.to], e.to));
}
}
}
}
vector<ll> get_path(ll t) { // 頂点への最短路
vector<ll> path;
for (; t != -1; t = prever[t]) {
path.push_back(t);
}
reverse(path.begin(), path.end());
return path;
}
/*
graph=AdjList(n);
rep(i,n-1){
ll from,to,cost;
ll cin>>from>>to>>cost;
from--;to--;
graph[from].push_back((edge){to,cost});
graph[to].push_back((edge){from,cost});
}
*/
int main() {
ll h, w;
cin >> h >> w;
ll cx, cy, gx, gy;
cin >> cx >> cy >> gx >> gy;
cx--;
cy--;
gx--;
gy--;
graph = AdjList(h * w);
vector<vector<char>> s(h, vector<char>(w));
rep(i, h) {
rep(j, w) { cin >> s[i][j]; }
}
ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
rep(i, h) {
rep(j, w) {
rep(k, 4) {
if (s[i][j] == '#') {
continue;
}
ll nx = i + dx[k];
ll ny = j + dy[k];
if (0 <= nx && nx < h && 0 <= ny && ny < w) {
if (s[nx][ny] == '.') {
graph[i * w + j].push_back((edge){nx * w + ny, 0});
graph[nx * w + ny].push_back((edge){i * w + j, 0});
}
}
rep2(k, -2, 3) {
rep2(l, -2, 3) {
if (abs(k) + abs(l) < 2) {
continue;
}
ll nx = i + k;
ll ny = j + l;
if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != '#') {
graph[i * w + j].push_back((edge){nx * w + ny, 1});
graph[nx * w + ny].push_back((edge){i * w + j, 1});
}
}
}
}
}
}
dijkstra(h * w, cx * w + cy);
if (dist[gx * w + gy] == INF) {
cout << -1 << endl;
} else {
cout << dist[gx * w + gy] << endl;
}
} | insert | 88 | 88 | 88 | 91 | MLE | |
p02579 | C++ | Time Limit Exceeded | #pragma region template
// clang-format off
#ifdef ONLINE_JUDGE
#pragma GCC target("avx2")
#pragma GCC optimize("tree-vectorize")
#pragma GCC diagnostic ignored "-Wunused-result"
#define here
#define com(msg)
#define obs(...)
#define see(vec)
#define local(x)
#define alter(x,y) y
#else
#define here cerr<<__func__<<"/"<<__LINE__<<": reached\n"
#define com(msg) cerr<<"{ "<<msg<< " }\n"
#define obs(...) observe(#__VA_ARGS__, __VA_ARGS__)
#define see(vec) do{cerr<<#vec<<": ";printv(#vec,vec);}while(0)
#define local(x) x
#define alter(x,y) x
#endif
#include <bits/stdc++.h>
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;i++)
#define brep(i,a,b) for(int i=(a),i##_len=(b);i<=i##_len;i++)
#define rrep(i,n) for(int i=(n)-1,now=0;i>=0;i--,now++)
#define rbrep(i,a,b) for(int i=(a),i##_len=(b),now=0;i>=i##_len;i--,now++)
#define xrep(i,n) for(int i=1,i##_len=(n);i<=i##_len;i++)
#define yes(n) cout<<((n)?YES:NO)<<"\n"
#define cco(...) prints(__VA_ARGS__)
#define Sort(v) sort((v).begin(),(v).end())
#define rSort(v) sort((v).rbegin(),(v).rend())
#define Rev(v) reverse((v).begin(),(v).end())
#define Unique(v) (v).erase(unique((v).begin(),(v).end()),(v).end())
#define in_range(x,a,b) ((a)<=(x)&&(x)<(b))
#define in_area(p,a,b) (0<=(p.first)&&0<=(p.second)&&(p.first)<(a)&&(p.second)<(b))
#define In_area(y,x,a,b) (0<=(y)&&0<=(x)&&(y)<(a)&&(x)<(b))
using namespace std;
using ll = long long;
using ld = long double;
using pt = pair<int, int>;
using ull = unsigned long long;
using str = string;
using vbl = vector<bool>;
using dqbl = deque<bool>;
using vint = vector<int>;
using vll = vector<long long>;
using vdb = vector<double>;
using vld = vector<long double>;
using vpt = vector<pair<int, int>>;
using vstr = vector<string>;
using vvdb = vector<vector<double>>;
using vvbl = vector<vector<bool>>;
using vdqbl = vector<deque<bool>>;
using vvint = vector<vector<int>>;
using vvll = vector<vector<long long>>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-9;
constexpr int dy[] = {1,0,-1,0,1,1,-1,-1};
constexpr int dx[] = {0,1,0,-1,-1,1,1,-1};
constexpr pt dv[] = {
{dy[0],dx[0]},
{dy[1],dx[1]},
{dy[2],dx[2]},
{dy[3],dx[3]},
{dy[4],dx[4]},
{dy[5],dx[5]},
{dy[6],dx[6]},
{dy[7],dx[7]}
};
// --- functions which take 1 argument --- //
template<class T> int sgn(const T &x){return (x>0)-(x<0);}
template<class S,class T=int> int digit(S x,const T &b=10){int r=1;while((x/=b)>=1)r++;return r;}
template<class T> double degToRad(const T &a){return a/180.0*M_PI;}
template<class T> double radToDeg(const T &a){return a/M_PI*180.0;}
template<class T> long long factorial(const T &n) {if(n==0)return 1;long long r=1;for(int i=2;i<=n;i++)r*=i;return r;}
template<class T> bool isPrime(const T &n){if(n<=1)return false;T i=2;while(i*i<=n){if(n%i==0&&n!=i)return false;i++;}return true;}
template<class T> map<T,T> factorize(T n){map<T,T>r;for(T i=2;i*i<=n;i++){while(n%i==0){r[i]++;n/=i;}}if(n!=1)r[n]=1;return r;}
template<class T> vector<T> divisor(const T &n){vector<T>r;for(T i=1;i*i<=n;i++){if(!(n%i)){r.emplace_back(i);if(i*i!=n)r.emplace_back(n/i);}}return r;}
// --- functions which take more than 1 argument --- //
template<class S,class T> [[maybe_unused]] bool chmax(S &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class S,class T> [[maybe_unused]] bool chmin(S &a,const T &b){if(a>b){a=b;return true;}return false;}
template<class S,class T> bool near(const S &a, const T &b){return abs(a-b)<EPS;}
template<class S,class T> long long bpow(S m,T n){long long r=1;while(n>0){if(n&1)r*=m;m*=m;n/=2;}return r;}
template<class S,class T> typename common_type<S,T>::type min(const S &m,const T &n){return min((typename common_type<S,T>::type)m,(typename common_type<S,T>::type)n);}
template<class S,class T> typename common_type<S,T>::type max(const S &m,const T &n){return max((typename common_type<S,T>::type)m,(typename common_type<S,T>::type)n);}
template<class S,class T> long long nPr(const S &n,const T &k){if(n<k||n<0||k<0)return 0;long long r=1;for(int i=n-k+1;i<=n;i++)r*=i;return r;}
template<class S,class T> long long nCr(const S &n,T k){if(n<k||n<0||k<0)return 0;long long r=1;k=min(k,n-k);for(int i=n-k+1;i<=n;i++)r*=i;return r/factorial(k);}
template<class T> void prints(const T &v){cout<<v<<"\n";}
template<class S,class... T> void prints(const S &v,const T&... w){cout<<v<<" ";prints(w...);}
// --- functions which take vector/deque as argument --- //
template<class T> void printd(const vector<T> &v,const string &d){for(int i=0,i_len=(int)v.size()-1;i<i_len;i++)cout<<v[i]<<d;cout<<*v.rbegin()<<"\n";}
template<class T> void printd(const vector<vector<T>> &v,const string &d){for(const auto &x:v)printD(x,d);}
template<class T> bool in(const T &k,const vector<T> &v) {return find(v.begin(),v.end(),k)!=v.end();}
bool in(const int &k,const vector<long long> &v){return find(v.begin(),v.end(),k)!=v.end();}
bool in(const long long &k,const vector<int> &v){return find(v.begin(),v.end(),k)!=v.end();}
bool in(const char &k,const string &v) {return find(v.begin(),v.end(),k)!=v.end();}
bool in(const char* k,const vector<string> &v) {return find(v.begin(),v.end(),k)!=v.end();}
template<class T> double veclen(const vector<T> &v){return sqrt(reduce(v.begin(),v.end(),0.0,[](T s,T v){return s+=v*v;}));}
template<class T> T min(const vector<T> &v){return *min_element(v.begin(),v.end());}
template<class T> T max(const vector<T> &v){return *max_element(v.begin(),v.end());}
template<class T> T sum(const vector<T> &v){return reduce(v.begin(),v.end(),(T)0);}
template<class T> T gcd(const vector<T> &v){T r=v[0];for(int i=1,i_len=v.size();i<i_len;i++)r=gcd(r,v[i]);return r;}
template<class T> T lcm(const vector<T> &v){T r=v[0];for(int i=1,i_len=v.size();i<i_len;i++)r=lcm(r,v[i]);return r;}
template<class T> T vectorAdd(const T &u,const T &v) {T r(u.size());for(int i=0,i_len=u.size();i<i_len;i++)r[i]=u[i]+v[i];return r;}
template<class T> T vectorSubtract(const T &u,const T &v){T r(u.size());for(int i=0,i_len=u.size();i<i_len;i++)r[i]=u[i]-v[i];return r;}
template<class T> T vectorMultiply(const T &u,const T &v){T r(u.size());for(int i=0,i_len=u.size();i<i_len;i++)r[i]=u[i]*v[i];return r;}
template<class T> T vectorDivide(const T &u,const T &v) {T r(u.size());for(int i=0,i_len=u.size();i<i_len;i++)r[i]=u[i]/v[i];return r;}
template<class T> T dotProduct(const deque<bool> &u,const vector<T> &v){T r=0;for(int i=0,i_len=u.size();i<i_len;i++)if(u[i])r+=v[i];return r;}
template<class S,class T> typename common_type<S,T>::type dotProduct(const vector<S> &u,const vector<T> &v){typename common_type<S,T>::type r=0;for(int i=0,i_len=u.size();i<i_len;i++)r+=u[i]*v[i];return r;}
template<class S,class T> void sortBySecond(vector<pair<S,T>> &v){sort(v.begin(),v.end(),[](auto &L,auto &R){if(L.second==R.second)return L.first<R.first;return L.second<R.second;});}
// --- functions which take set/map as argument --- //
template<class T> T min(const set<T> &v){return *min_element(v.begin(),v.end());}
template<class T> T max(const set<T> &v){return *max_element(v.begin(),v.end());}
template<class T> T sum(const set<T> &v){return reduce(v.begin(),v.end(),(T)0);}
template<class T> T gcd(const set<T> &v){T r=0;for(const T &x:v)r=(r==0)?x:gcd(r,x);return r;}
template<class T> T lcm(const set<T> &v){T r=0;for(const T &x:v)r=(r==0)?x:lcm(r,x);return r;}
template<class T> bool in(const T &k,const set<T> &v){return find(v.begin(),v.end(),k)!=v.end();}
template<class S,class T> pair<S,T> min(const map<S,T> &m){pair<S,T> r=*m.begin();for(const pair<S,T> &p:m)if(r.second>p.second)r=p;return r;}
template<class S,class T> pair<S,T> max(const map<S,T> &m){pair<S,T> r=*m.begin();for(const pair<S,T> &p:m)if(r.second<p.second)r=p;return r;}
// --- operators --- //
template<class T> istream& operator>>(istream &i,vector<T> &v){for(T &x:v)i>>x;return i;}
template<class T> ostream& operator<<(ostream &o,vector<T> &v){for(T &x:v)o<<x<<"\n";return o;}
template<class S,class T> istream& operator>>(istream &i,pair<S,T> &p){i>>p.first>>p.second;return i;}
template<class S,class T> ostream& operator<<(ostream &o,pair<S,T> &p){o<<p.first<<" "<<p.second<<"\n";return o;}
template<class S,class T> pair<S,T> operator+(const pair<S,T> &p,const pair<S,T> &q){return pair<S,T>{p.first+q.first,p.second+q.second};}
template<class S,class T> pair<S,T> operator-(const pair<S,T> &p,const pair<S,T> &q){return pair<S,T>{p.first-q.first,p.second-q.second};}
template<class S,class T> pair<S,T> operator*(const pair<S,T> &p,const pair<S,T> &q){return pair<S,T>{p.first*q.first,p.second*q.second};}
template<class S,class T> pair<S,T> operator/(const pair<S,T> &p,const pair<S,T> &q){return pair<S,T>{p.first/q.first,p.second/q.second};}
template<class S,class T> [[maybe_unused]] pair<S,T> operator--(pair<S,T> &p){return pair<S,T>{--p.first,--p.second};}
template<class S,class T> [[maybe_unused]] pair<S,T> operator++(pair<S,T> &p){return pair<S,T>{++p.first,++p.second};}
template<class S,class T> [[maybe_unused]] pair<S,T> operator+=(pair<S,T> &p,const pair<S,T> &q){return p=p+q;}
template<class S,class T> [[maybe_unused]] pair<S,T> operator-=(pair<S,T> &p,const pair<S,T> &q){return p=p-q;}
template<class S,class T> [[maybe_unused]] pair<S,T> operator*=(pair<S,T> &p,const pair<S,T> &q){return p=p*q;}
template<class S,class T> [[maybe_unused]] pair<S,T> operator/=(pair<S,T> &p,const pair<S,T> &q){return p=p/q;}
// --- functions for debugging --- //
template<class T> void observe(const char *n,T &&v) {cerr<<n<<": "<<v<<"\n";}
template<class S,class... T> void observe(const char *n,S &&v,T&&... w){const char *c=strchr(n+1,',');cerr.write(n,c-n)<<": "<<v<<" |";observe(c+1,w...);}
template<class T> void printv([[maybe_unused]] const char *n, const vector<T> &v){cerr<<"[ ";for(const T &x:v)cerr<<x<<" ";cerr<<"]\n";}
template<class T> void printv([[maybe_unused]] const char *n, const deque<T> &v) {cerr<<"[ ";for(const T &x:v)cerr<<x<<" ";cerr<<"]\n";}
template<class T> void printv([[maybe_unused]] const char *n, const set<T> &v) {cerr<<"[ ";for(const T &x:v)cerr<<x<<" ";cerr<<"]\n";}
template<class P> void printv([[maybe_unused]] const char *n, const P &p){cerr<<"{ "<<p.first<<" "<<p.second<<" } ";}
template<class T> void printv(const char *n, const vector<vector<T>> &v){bool f=0;for(const vector<T> &x:v){if(f)cerr<<string(strlen(n)+2,' ');f=1;printv<T>("",x);}}
template<class T> void printv(const char *n, const vector<deque<T>> &v) {bool f=0;for(const deque<T> &x:v){if(f)cerr<<string(strlen(n)+2,' ');f=1;printv<T>("",x);}}
template<class S,class T> void printv([[maybe_unused]] const char *n, const map<S,T> &m) {cerr<<"[ ";for(const pair<S,T> &p:m)cerr<<"{ "<<p.first<<", "<<p.second<<" } ";cerr<<"]\n";}
template<class S,class T> void printv([[maybe_unused]] const char *n, const vector<pair<S,T>> &v){cerr<<"[ ";for(const pair<S,T> &p:v)printv<pair<S,T>>("",p);cerr<<"]\n";}
template<class S,class T> void printv([[maybe_unused]] const char *n, const deque<pair<S,T>> &v){cerr<<"[ ";for(const pair<S,T> &p:v)printv<pair<S,T>>("",p);cerr<<"]\n";}
// clang-format on
#pragma endregion
int main() {
// Input
int H, W;
pair<int, int> C, D;
cin >> H >> W >> C >> D;
--C, --D;
vector<string> S(H);
cin >> S;
// 01-BFS
deque<pair<int, int>> BF;
vector<vector<int>> cost(H, vector<int>(W, INF));
BF.emplace_front(C);
cost[C.first][C.second] = 0;
while (!BF.empty()) {
pair<int, int> cur = BF.front();
BF.pop_front();
for (int d = 0; d < 4; d++) {
pair<int, int> nxt = cur + dv[d];
if (!in_area(nxt, H, W) || S[nxt.first][nxt.second] == '#' ||
cost[nxt.first][nxt.second] <= cost[cur.first][cur.second])
continue;
cost[nxt.first][nxt.second] = cost[cur.first][cur.second];
BF.emplace_front(nxt);
}
for (int di = -2; di <= 2; di++) {
for (int dj = -2; dj <= 2; dj++) {
pair<int, int> nxt = cur + pt{di, dj};
if (!in_area(nxt, H, W) || S[nxt.first][nxt.second] == '#' ||
cost[nxt.first][nxt.second] <= cost[cur.first][cur.second])
continue;
cost[nxt.first][nxt.second] = cost[cur.first][cur.second] + 1;
BF.emplace_front(nxt);
}
}
}
// Output
if (cost[D.first][D.second] == INF)
cout << -1 << "\n";
else
cout << cost[D.first][D.second] << "\n";
}
| #pragma region template
// clang-format off
#ifdef ONLINE_JUDGE
#pragma GCC target("avx2")
#pragma GCC optimize("tree-vectorize")
#pragma GCC diagnostic ignored "-Wunused-result"
#define here
#define com(msg)
#define obs(...)
#define see(vec)
#define local(x)
#define alter(x,y) y
#else
#define here cerr<<__func__<<"/"<<__LINE__<<": reached\n"
#define com(msg) cerr<<"{ "<<msg<< " }\n"
#define obs(...) observe(#__VA_ARGS__, __VA_ARGS__)
#define see(vec) do{cerr<<#vec<<": ";printv(#vec,vec);}while(0)
#define local(x) x
#define alter(x,y) x
#endif
#include <bits/stdc++.h>
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;i++)
#define brep(i,a,b) for(int i=(a),i##_len=(b);i<=i##_len;i++)
#define rrep(i,n) for(int i=(n)-1,now=0;i>=0;i--,now++)
#define rbrep(i,a,b) for(int i=(a),i##_len=(b),now=0;i>=i##_len;i--,now++)
#define xrep(i,n) for(int i=1,i##_len=(n);i<=i##_len;i++)
#define yes(n) cout<<((n)?YES:NO)<<"\n"
#define cco(...) prints(__VA_ARGS__)
#define Sort(v) sort((v).begin(),(v).end())
#define rSort(v) sort((v).rbegin(),(v).rend())
#define Rev(v) reverse((v).begin(),(v).end())
#define Unique(v) (v).erase(unique((v).begin(),(v).end()),(v).end())
#define in_range(x,a,b) ((a)<=(x)&&(x)<(b))
#define in_area(p,a,b) (0<=(p.first)&&0<=(p.second)&&(p.first)<(a)&&(p.second)<(b))
#define In_area(y,x,a,b) (0<=(y)&&0<=(x)&&(y)<(a)&&(x)<(b))
using namespace std;
using ll = long long;
using ld = long double;
using pt = pair<int, int>;
using ull = unsigned long long;
using str = string;
using vbl = vector<bool>;
using dqbl = deque<bool>;
using vint = vector<int>;
using vll = vector<long long>;
using vdb = vector<double>;
using vld = vector<long double>;
using vpt = vector<pair<int, int>>;
using vstr = vector<string>;
using vvdb = vector<vector<double>>;
using vvbl = vector<vector<bool>>;
using vdqbl = vector<deque<bool>>;
using vvint = vector<vector<int>>;
using vvll = vector<vector<long long>>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-9;
constexpr int dy[] = {1,0,-1,0,1,1,-1,-1};
constexpr int dx[] = {0,1,0,-1,-1,1,1,-1};
constexpr pt dv[] = {
{dy[0],dx[0]},
{dy[1],dx[1]},
{dy[2],dx[2]},
{dy[3],dx[3]},
{dy[4],dx[4]},
{dy[5],dx[5]},
{dy[6],dx[6]},
{dy[7],dx[7]}
};
// --- functions which take 1 argument --- //
template<class T> int sgn(const T &x){return (x>0)-(x<0);}
template<class S,class T=int> int digit(S x,const T &b=10){int r=1;while((x/=b)>=1)r++;return r;}
template<class T> double degToRad(const T &a){return a/180.0*M_PI;}
template<class T> double radToDeg(const T &a){return a/M_PI*180.0;}
template<class T> long long factorial(const T &n) {if(n==0)return 1;long long r=1;for(int i=2;i<=n;i++)r*=i;return r;}
template<class T> bool isPrime(const T &n){if(n<=1)return false;T i=2;while(i*i<=n){if(n%i==0&&n!=i)return false;i++;}return true;}
template<class T> map<T,T> factorize(T n){map<T,T>r;for(T i=2;i*i<=n;i++){while(n%i==0){r[i]++;n/=i;}}if(n!=1)r[n]=1;return r;}
template<class T> vector<T> divisor(const T &n){vector<T>r;for(T i=1;i*i<=n;i++){if(!(n%i)){r.emplace_back(i);if(i*i!=n)r.emplace_back(n/i);}}return r;}
// --- functions which take more than 1 argument --- //
template<class S,class T> [[maybe_unused]] bool chmax(S &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class S,class T> [[maybe_unused]] bool chmin(S &a,const T &b){if(a>b){a=b;return true;}return false;}
template<class S,class T> bool near(const S &a, const T &b){return abs(a-b)<EPS;}
template<class S,class T> long long bpow(S m,T n){long long r=1;while(n>0){if(n&1)r*=m;m*=m;n/=2;}return r;}
template<class S,class T> typename common_type<S,T>::type min(const S &m,const T &n){return min((typename common_type<S,T>::type)m,(typename common_type<S,T>::type)n);}
template<class S,class T> typename common_type<S,T>::type max(const S &m,const T &n){return max((typename common_type<S,T>::type)m,(typename common_type<S,T>::type)n);}
template<class S,class T> long long nPr(const S &n,const T &k){if(n<k||n<0||k<0)return 0;long long r=1;for(int i=n-k+1;i<=n;i++)r*=i;return r;}
template<class S,class T> long long nCr(const S &n,T k){if(n<k||n<0||k<0)return 0;long long r=1;k=min(k,n-k);for(int i=n-k+1;i<=n;i++)r*=i;return r/factorial(k);}
template<class T> void prints(const T &v){cout<<v<<"\n";}
template<class S,class... T> void prints(const S &v,const T&... w){cout<<v<<" ";prints(w...);}
// --- functions which take vector/deque as argument --- //
template<class T> void printd(const vector<T> &v,const string &d){for(int i=0,i_len=(int)v.size()-1;i<i_len;i++)cout<<v[i]<<d;cout<<*v.rbegin()<<"\n";}
template<class T> void printd(const vector<vector<T>> &v,const string &d){for(const auto &x:v)printD(x,d);}
template<class T> bool in(const T &k,const vector<T> &v) {return find(v.begin(),v.end(),k)!=v.end();}
bool in(const int &k,const vector<long long> &v){return find(v.begin(),v.end(),k)!=v.end();}
bool in(const long long &k,const vector<int> &v){return find(v.begin(),v.end(),k)!=v.end();}
bool in(const char &k,const string &v) {return find(v.begin(),v.end(),k)!=v.end();}
bool in(const char* k,const vector<string> &v) {return find(v.begin(),v.end(),k)!=v.end();}
template<class T> double veclen(const vector<T> &v){return sqrt(reduce(v.begin(),v.end(),0.0,[](T s,T v){return s+=v*v;}));}
template<class T> T min(const vector<T> &v){return *min_element(v.begin(),v.end());}
template<class T> T max(const vector<T> &v){return *max_element(v.begin(),v.end());}
template<class T> T sum(const vector<T> &v){return reduce(v.begin(),v.end(),(T)0);}
template<class T> T gcd(const vector<T> &v){T r=v[0];for(int i=1,i_len=v.size();i<i_len;i++)r=gcd(r,v[i]);return r;}
template<class T> T lcm(const vector<T> &v){T r=v[0];for(int i=1,i_len=v.size();i<i_len;i++)r=lcm(r,v[i]);return r;}
template<class T> T vectorAdd(const T &u,const T &v) {T r(u.size());for(int i=0,i_len=u.size();i<i_len;i++)r[i]=u[i]+v[i];return r;}
template<class T> T vectorSubtract(const T &u,const T &v){T r(u.size());for(int i=0,i_len=u.size();i<i_len;i++)r[i]=u[i]-v[i];return r;}
template<class T> T vectorMultiply(const T &u,const T &v){T r(u.size());for(int i=0,i_len=u.size();i<i_len;i++)r[i]=u[i]*v[i];return r;}
template<class T> T vectorDivide(const T &u,const T &v) {T r(u.size());for(int i=0,i_len=u.size();i<i_len;i++)r[i]=u[i]/v[i];return r;}
template<class T> T dotProduct(const deque<bool> &u,const vector<T> &v){T r=0;for(int i=0,i_len=u.size();i<i_len;i++)if(u[i])r+=v[i];return r;}
template<class S,class T> typename common_type<S,T>::type dotProduct(const vector<S> &u,const vector<T> &v){typename common_type<S,T>::type r=0;for(int i=0,i_len=u.size();i<i_len;i++)r+=u[i]*v[i];return r;}
template<class S,class T> void sortBySecond(vector<pair<S,T>> &v){sort(v.begin(),v.end(),[](auto &L,auto &R){if(L.second==R.second)return L.first<R.first;return L.second<R.second;});}
// --- functions which take set/map as argument --- //
template<class T> T min(const set<T> &v){return *min_element(v.begin(),v.end());}
template<class T> T max(const set<T> &v){return *max_element(v.begin(),v.end());}
template<class T> T sum(const set<T> &v){return reduce(v.begin(),v.end(),(T)0);}
template<class T> T gcd(const set<T> &v){T r=0;for(const T &x:v)r=(r==0)?x:gcd(r,x);return r;}
template<class T> T lcm(const set<T> &v){T r=0;for(const T &x:v)r=(r==0)?x:lcm(r,x);return r;}
template<class T> bool in(const T &k,const set<T> &v){return find(v.begin(),v.end(),k)!=v.end();}
template<class S,class T> pair<S,T> min(const map<S,T> &m){pair<S,T> r=*m.begin();for(const pair<S,T> &p:m)if(r.second>p.second)r=p;return r;}
template<class S,class T> pair<S,T> max(const map<S,T> &m){pair<S,T> r=*m.begin();for(const pair<S,T> &p:m)if(r.second<p.second)r=p;return r;}
// --- operators --- //
template<class T> istream& operator>>(istream &i,vector<T> &v){for(T &x:v)i>>x;return i;}
template<class T> ostream& operator<<(ostream &o,vector<T> &v){for(T &x:v)o<<x<<"\n";return o;}
template<class S,class T> istream& operator>>(istream &i,pair<S,T> &p){i>>p.first>>p.second;return i;}
template<class S,class T> ostream& operator<<(ostream &o,pair<S,T> &p){o<<p.first<<" "<<p.second<<"\n";return o;}
template<class S,class T> pair<S,T> operator+(const pair<S,T> &p,const pair<S,T> &q){return pair<S,T>{p.first+q.first,p.second+q.second};}
template<class S,class T> pair<S,T> operator-(const pair<S,T> &p,const pair<S,T> &q){return pair<S,T>{p.first-q.first,p.second-q.second};}
template<class S,class T> pair<S,T> operator*(const pair<S,T> &p,const pair<S,T> &q){return pair<S,T>{p.first*q.first,p.second*q.second};}
template<class S,class T> pair<S,T> operator/(const pair<S,T> &p,const pair<S,T> &q){return pair<S,T>{p.first/q.first,p.second/q.second};}
template<class S,class T> [[maybe_unused]] pair<S,T> operator--(pair<S,T> &p){return pair<S,T>{--p.first,--p.second};}
template<class S,class T> [[maybe_unused]] pair<S,T> operator++(pair<S,T> &p){return pair<S,T>{++p.first,++p.second};}
template<class S,class T> [[maybe_unused]] pair<S,T> operator+=(pair<S,T> &p,const pair<S,T> &q){return p=p+q;}
template<class S,class T> [[maybe_unused]] pair<S,T> operator-=(pair<S,T> &p,const pair<S,T> &q){return p=p-q;}
template<class S,class T> [[maybe_unused]] pair<S,T> operator*=(pair<S,T> &p,const pair<S,T> &q){return p=p*q;}
template<class S,class T> [[maybe_unused]] pair<S,T> operator/=(pair<S,T> &p,const pair<S,T> &q){return p=p/q;}
// --- functions for debugging --- //
template<class T> void observe(const char *n,T &&v) {cerr<<n<<": "<<v<<"\n";}
template<class S,class... T> void observe(const char *n,S &&v,T&&... w){const char *c=strchr(n+1,',');cerr.write(n,c-n)<<": "<<v<<" |";observe(c+1,w...);}
template<class T> void printv([[maybe_unused]] const char *n, const vector<T> &v){cerr<<"[ ";for(const T &x:v)cerr<<x<<" ";cerr<<"]\n";}
template<class T> void printv([[maybe_unused]] const char *n, const deque<T> &v) {cerr<<"[ ";for(const T &x:v)cerr<<x<<" ";cerr<<"]\n";}
template<class T> void printv([[maybe_unused]] const char *n, const set<T> &v) {cerr<<"[ ";for(const T &x:v)cerr<<x<<" ";cerr<<"]\n";}
template<class P> void printv([[maybe_unused]] const char *n, const P &p){cerr<<"{ "<<p.first<<" "<<p.second<<" } ";}
template<class T> void printv(const char *n, const vector<vector<T>> &v){bool f=0;for(const vector<T> &x:v){if(f)cerr<<string(strlen(n)+2,' ');f=1;printv<T>("",x);}}
template<class T> void printv(const char *n, const vector<deque<T>> &v) {bool f=0;for(const deque<T> &x:v){if(f)cerr<<string(strlen(n)+2,' ');f=1;printv<T>("",x);}}
template<class S,class T> void printv([[maybe_unused]] const char *n, const map<S,T> &m) {cerr<<"[ ";for(const pair<S,T> &p:m)cerr<<"{ "<<p.first<<", "<<p.second<<" } ";cerr<<"]\n";}
template<class S,class T> void printv([[maybe_unused]] const char *n, const vector<pair<S,T>> &v){cerr<<"[ ";for(const pair<S,T> &p:v)printv<pair<S,T>>("",p);cerr<<"]\n";}
template<class S,class T> void printv([[maybe_unused]] const char *n, const deque<pair<S,T>> &v){cerr<<"[ ";for(const pair<S,T> &p:v)printv<pair<S,T>>("",p);cerr<<"]\n";}
// clang-format on
#pragma endregion
int main() {
// Input
int H, W;
pair<int, int> C, D;
cin >> H >> W >> C >> D;
--C, --D;
vector<string> S(H);
cin >> S;
// 01-BFS
deque<pair<int, int>> BF;
vector<vector<int>> cost(H, vector<int>(W, INF));
BF.emplace_front(C);
cost[C.first][C.second] = 0;
while (!BF.empty()) {
pair<int, int> cur = BF.front();
BF.pop_front();
for (int d = 0; d < 4; d++) {
pair<int, int> nxt = cur + dv[d];
if (!in_area(nxt, H, W) || S[nxt.first][nxt.second] == '#' ||
cost[nxt.first][nxt.second] <= cost[cur.first][cur.second])
continue;
cost[nxt.first][nxt.second] = cost[cur.first][cur.second];
BF.emplace_front(nxt);
}
for (int di = -2; di <= 2; di++) {
for (int dj = -2; dj <= 2; dj++) {
pair<int, int> nxt = cur + pt{di, dj};
if (!in_area(nxt, H, W) || S[nxt.first][nxt.second] == '#' ||
cost[nxt.first][nxt.second] <= cost[cur.first][cur.second])
continue;
cost[nxt.first][nxt.second] = cost[cur.first][cur.second] + 1;
BF.emplace_back(nxt);
}
}
}
// Output
if (cost[D.first][D.second] == INF)
cout << -1 << "\n";
else
cout << cost[D.first][D.second] << "\n";
}
| replace | 201 | 202 | 201 | 202 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int a, b, k, x1, y_1, x2, y2, d[500005], v[1005][1005];
bool n[1005][1005], vi[500005];
char c;
struct st {
int w, v;
st(int V, int W) {
w = W;
v = V;
}
};
struct st1 {
int w, dd;
st1() {}
st1(int W, int DD) {
w = W;
dd = DD;
}
friend bool operator<(st1 x, st1 y) { return x.dd > y.dd; }
};
priority_queue<st1> q;
vector<st> ans[2505];
int Dijkstra(int s, int t) {
memset(d, 0x3f, sizeof(d));
memset(vi, 0, sizeof(vi));
d[s] = 0;
q.push(st1(s, 0));
while (q.size()) {
int x = q.top().w;
q.pop();
if (vi[x]) {
continue;
}
vi[x] = 1;
int siz = ans[x].size();
for (int i = 0; i < siz; i++) {
int vr = ans[x][i].v, w = ans[x][i].w;
if (d[vr] > d[x] + w) {
d[vr] = d[x] + w;
q.push(st1(vr, d[vr]));
}
}
}
return d[t];
}
void dfs(int x, int y) {
if (x > 1 && v[x - 1][y] == 0 && n[x - 1][y] == 0) {
n[x - 1][y] = 1;
v[x - 1][y] = k;
dfs(x - 1, y);
n[x - 1][y] = 0;
}
if (x < a && v[x + 1][y] == 0 && n[x + 1][y] == 0) {
n[x + 1][y] = 1;
v[x + 1][y] = k;
dfs(x + 1, y);
n[x + 1][y] = 0;
}
if (y > 1 && v[x][y - 1] == 0 && n[x][y - 1] == 0) {
n[x][y - 1] = 1;
v[x][y - 1] = k;
dfs(x, y - 1);
n[x][y - 1] = 0;
}
if (y < b && v[x][y + 1] == 0 && n[x][y + 1] == 0) {
n[x][y + 1] = 1;
v[x][y + 1] = k;
dfs(x, y + 1);
n[x][y + 1] = 0;
}
}
int main() {
scanf("%d %d\n", &a, &b);
scanf("%d %d\n", &x2, &y2);
scanf("%d %d", &x1, &y_1);
for (int i = 1; i <= a; i++) {
scanf("\n");
for (int j = 1; j <= b; j++) {
scanf("%c", &c);
if (c == '#')
n[i][j] = 1;
}
}
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
if (v[i][j] == 0 && n[i][j] == 0) {
k++;
v[i][j] = k;
n[i][j] = 1;
dfs(i, j);
n[i][j] = 0;
}
}
}
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
for (int i1 = max(1, i - 2); i1 <= min(a, i + 2); i1++) {
for (int j1 = max(1, j - 2); j1 <= min(b, j + 2); j1++) {
if (v[i][j] != 0 && v[i1][j1] != 0 && v[i][j] != v[i1][j1]) {
ans[v[i][j]].push_back(st(v[i1][j1], 1));
ans[v[i1][j1]].push_back(st(v[i][j], 1));
}
}
}
}
}
if (Dijkstra(v[x1][y_1], v[x2][y2]) == 0x3f3f3f3f) {
printf("-1");
} else
printf("%d", Dijkstra(v[x1][y_1], v[x2][y2]));
} | #include <bits/stdc++.h>
using namespace std;
int a, b, k, x1, y_1, x2, y2, d[500005], v[1005][1005];
bool n[1005][1005], vi[500005];
char c;
struct st {
int w, v;
st(int V, int W) {
w = W;
v = V;
}
};
struct st1 {
int w, dd;
st1() {}
st1(int W, int DD) {
w = W;
dd = DD;
}
friend bool operator<(st1 x, st1 y) { return x.dd > y.dd; }
};
priority_queue<st1> q;
vector<st> ans[500005];
int Dijkstra(int s, int t) {
memset(d, 0x3f, sizeof(d));
memset(vi, 0, sizeof(vi));
d[s] = 0;
q.push(st1(s, 0));
while (q.size()) {
int x = q.top().w;
q.pop();
if (vi[x]) {
continue;
}
vi[x] = 1;
int siz = ans[x].size();
for (int i = 0; i < siz; i++) {
int vr = ans[x][i].v, w = ans[x][i].w;
if (d[vr] > d[x] + w) {
d[vr] = d[x] + w;
q.push(st1(vr, d[vr]));
}
}
}
return d[t];
}
void dfs(int x, int y) {
if (x > 1 && v[x - 1][y] == 0 && n[x - 1][y] == 0) {
n[x - 1][y] = 1;
v[x - 1][y] = k;
dfs(x - 1, y);
n[x - 1][y] = 0;
}
if (x < a && v[x + 1][y] == 0 && n[x + 1][y] == 0) {
n[x + 1][y] = 1;
v[x + 1][y] = k;
dfs(x + 1, y);
n[x + 1][y] = 0;
}
if (y > 1 && v[x][y - 1] == 0 && n[x][y - 1] == 0) {
n[x][y - 1] = 1;
v[x][y - 1] = k;
dfs(x, y - 1);
n[x][y - 1] = 0;
}
if (y < b && v[x][y + 1] == 0 && n[x][y + 1] == 0) {
n[x][y + 1] = 1;
v[x][y + 1] = k;
dfs(x, y + 1);
n[x][y + 1] = 0;
}
}
int main() {
scanf("%d %d\n", &a, &b);
scanf("%d %d\n", &x2, &y2);
scanf("%d %d", &x1, &y_1);
for (int i = 1; i <= a; i++) {
scanf("\n");
for (int j = 1; j <= b; j++) {
scanf("%c", &c);
if (c == '#')
n[i][j] = 1;
}
}
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
if (v[i][j] == 0 && n[i][j] == 0) {
k++;
v[i][j] = k;
n[i][j] = 1;
dfs(i, j);
n[i][j] = 0;
}
}
}
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
for (int i1 = max(1, i - 2); i1 <= min(a, i + 2); i1++) {
for (int j1 = max(1, j - 2); j1 <= min(b, j + 2); j1++) {
if (v[i][j] != 0 && v[i1][j1] != 0 && v[i][j] != v[i1][j1]) {
ans[v[i][j]].push_back(st(v[i1][j1], 1));
ans[v[i1][j1]].push_back(st(v[i][j], 1));
}
}
}
}
}
if (Dijkstra(v[x1][y_1], v[x2][y2]) == 0x3f3f3f3f) {
printf("-1");
} else
printf("%d", Dijkstra(v[x1][y_1], v[x2][y2]));
} | replace | 22 | 23 | 22 | 23 | 0 | |
p02579 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int w_par[10000];
int w_rank[10000];
void w_init(int n) {
REP(i, n) {
w_par[i] = i;
w_rank[i] = 0;
}
}
int w_find(int x) {
if (w_par[x] == x) {
return x;
} else {
return w_par[x] = w_find(w_par[x]);
}
}
void w_unite(int x, int y) {
x = w_find(x);
y = w_find(y);
if (x == y)
return;
if (w_rank[x] < w_rank[y]) {
w_par[x] = y;
} else {
w_par[y] = x;
if (w_rank[x] == w_rank[y])
w_rank[x]++;
}
}
int main() {
int H, W;
cin >> H >> W;
int c[2], d[2];
cin >> c[0] >> c[1];
c[0]--;
c[1]--;
cin >> d[0] >> d[1];
d[0]--;
d[1]--;
vector<string> s(H);
REP(i, H) cin >> s[i];
vector<bool> isway(H * W, true);
w_init(H * W);
REP(i, H) {
REP(j, W) {
if (s[i][j] == '#') {
isway[i * W + j] = false;
continue;
}
if (i != 0 && s[i - 1][j] == '.')
w_unite(i * W + j, (i - 1) * W + j);
if (i != H - 1 && s[i + 1][j] == '.')
w_unite(i * W + j, (i + 1) * W + j);
if (j != 0 && s[i][j - 1] == '.')
w_unite(i * W + j, i * W + j - 1);
if (j != W - 1 && s[i][j + 1] == '.')
w_unite(i * W + j, i * W + j + 1);
}
}
if (w_find(c[0] * W + c[1]) == w_find(d[0] * W + d[1])) {
cout << 0 << endl;
return 0;
}
map<int, set<int>> g;
REP(i, H) {
REP(j, W) {
if (!isway[i * W + j])
continue;
for (int k = -2; k <= 2; k++) {
if (i + k < 0 || i + k >= H)
continue;
for (int l = -2; l <= 2; l++) {
if (j + l < 0 || j + l >= W)
continue;
int now = (i + k) * W + j + l;
if (!isway[now])
continue;
if (w_find(i * W + j) == w_find(now))
continue;
g[w_find(i * W + j)].insert(w_find(now));
g[w_find(now)].insert(w_find(i * W + j));
}
}
}
}
int start = w_find(c[0] * W + c[1]);
int goal = w_find(d[0] * W + d[1]);
map<int, int> count;
queue<int> q;
q.push(start);
count[start] = 0;
while (!q.empty()) {
int now = q.front();
q.pop();
for (auto x : g[now]) {
if (count[x] != 0)
continue;
if (x == goal) {
cout << count[now] + 1 << endl;
return 0;
}
count[x] = count[now] + 1;
q.push(x);
}
}
cout << -1 << endl;
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int w_par[1000000];
int w_rank[1000000];
void w_init(int n) {
REP(i, n) {
w_par[i] = i;
w_rank[i] = 0;
}
}
int w_find(int x) {
if (w_par[x] == x) {
return x;
} else {
return w_par[x] = w_find(w_par[x]);
}
}
void w_unite(int x, int y) {
x = w_find(x);
y = w_find(y);
if (x == y)
return;
if (w_rank[x] < w_rank[y]) {
w_par[x] = y;
} else {
w_par[y] = x;
if (w_rank[x] == w_rank[y])
w_rank[x]++;
}
}
int main() {
int H, W;
cin >> H >> W;
int c[2], d[2];
cin >> c[0] >> c[1];
c[0]--;
c[1]--;
cin >> d[0] >> d[1];
d[0]--;
d[1]--;
vector<string> s(H);
REP(i, H) cin >> s[i];
vector<bool> isway(H * W, true);
w_init(H * W);
REP(i, H) {
REP(j, W) {
if (s[i][j] == '#') {
isway[i * W + j] = false;
continue;
}
if (i != 0 && s[i - 1][j] == '.')
w_unite(i * W + j, (i - 1) * W + j);
if (i != H - 1 && s[i + 1][j] == '.')
w_unite(i * W + j, (i + 1) * W + j);
if (j != 0 && s[i][j - 1] == '.')
w_unite(i * W + j, i * W + j - 1);
if (j != W - 1 && s[i][j + 1] == '.')
w_unite(i * W + j, i * W + j + 1);
}
}
if (w_find(c[0] * W + c[1]) == w_find(d[0] * W + d[1])) {
cout << 0 << endl;
return 0;
}
map<int, set<int>> g;
REP(i, H) {
REP(j, W) {
if (!isway[i * W + j])
continue;
for (int k = -2; k <= 2; k++) {
if (i + k < 0 || i + k >= H)
continue;
for (int l = -2; l <= 2; l++) {
if (j + l < 0 || j + l >= W)
continue;
int now = (i + k) * W + j + l;
if (!isway[now])
continue;
if (w_find(i * W + j) == w_find(now))
continue;
g[w_find(i * W + j)].insert(w_find(now));
g[w_find(now)].insert(w_find(i * W + j));
}
}
}
}
int start = w_find(c[0] * W + c[1]);
int goal = w_find(d[0] * W + d[1]);
map<int, int> count;
queue<int> q;
q.push(start);
count[start] = 0;
while (!q.empty()) {
int now = q.front();
q.pop();
for (auto x : g[now]) {
if (count[x] != 0)
continue;
if (x == goal) {
cout << count[now] + 1 << endl;
return 0;
}
count[x] = count[now] + 1;
q.push(x);
}
}
cout << -1 << endl;
return 0;
} | replace | 30 | 32 | 30 | 32 | 0 | |
p02579 | C++ | Runtime Error | // #pragma GCC optimize "trapv"
//-D_GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define pm(m) \
for (auto itr = (m).begin(); itr != (m).end(); itr++) { \
itr->first << " " << itr->second << "\n"; \
}
#define int long long int
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define ii pair<int, int>
#define Max 100000 + 5
#define IO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define f first
#define s second
#define pb push_back
#define bug(x) \
cout << (x) << " "; \
exit(0);
int mod = 1000000007;
int mod1 = 998244353;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int power(int a, int b) {
if (b == 1)
return a;
if (a > mod)
a %= mod;
int r = 1;
while (b) {
if (b & 1)
r = r % mod * a % mod;
a = a % mod * a % mod;
b /= 2;
}
return r;
}
class uf {
vector<int> v;
vector<int> sz;
public:
uf(int n) {
v.resize(n + 1);
iota(v.begin() + 1, v.end(), 1);
sz.resize(n + 1, 1);
// 1 to sz
}
int root(int b) {
while (v[b] != b) {
b = v[b];
}
return b;
}
void join(int a, int b) {
// combine a and
int ra = root(a);
int rb = root(b);
if (ra == rb)
return;
if (sz[ra] < sz[rb])
swap(ra, rb);
v[rb] = ra;
sz[ra] += sz[rb];
}
bool same(int a, int b) {
// same set
return (root(a) == root(b));
}
int size(int a) { return sz[root(a)]; }
};
vector<int> matmul(vector<int> a, vector<int> b) {
vector<int> c(4, 0);
c[0] = (a[0] % mod * b[0] % mod + a[1] % mod * b[2] % mod) % mod;
c[1] = (a[0] % mod * b[1] % mod + a[1] % mod * b[3] % mod) % mod;
c[2] = (a[2] % mod * b[0] % mod + a[3] % mod * b[2] % mod) % mod;
c[3] = (a[2] % mod * b[1] % mod + a[3] % mod * b[3] % mod) % mod;
return c;
}
vector<int> id = {1, 0, 0, 1};
vector<int> g[500000 + 3];
vector<int> dis(500000 + 3, -1);
int32_t main() {
IO
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int n,
m;
cin >> n >> m;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int cx, cy, rx, ry;
cin >> cx >> cy >> rx >> ry;
cx--;
rx--;
cy--;
ry--;
vector<vector<char>> v(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cin >> v[i][j];
}
if (v[cx][cy] == '#' or v[rx][ry] == '#') {
cout << "-1";
exit(0);
}
uf tree(n * m);
vector<vector<bool>> vis(n, vector<bool>(m, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (v[i][j] == '.' and vis[i][j] == 0) {
// run a bfs
queue<ii> q;
q.push({i, j});
vis[i][j] = 1;
while (!q.empty()) {
ii fr = q.front();
q.pop();
int x = fr.f;
int y = fr.s;
for (int k = 0; k < 4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx >= 0 and nx < n and ny >= 0 and ny < m and !vis[nx][ny] and
v[nx][ny] == '.') {
q.push({nx, ny});
vis[nx][ny] = 1;
tree.join(i * m + j, nx * m + ny);
}
}
}
}
}
}
if (tree.root(cx * m + cy) == tree.root(rx * m + ry)) {
cout << 0;
exit(0);
}
// for(int i=0;i<n;i++){
// for(int j=0;j<m;j++){
// if(v[i][j]=='.')
// cout<<tree.root(i*m+j)<<" ";
// else cout<<"-1 ";
// }
// cout<<endl;
// }
// exit(0);
int dxx[] = {-2, -1, 0, 1, 2};
map<ii, bool> edge;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (v[i][j] == '.') {
int rb = tree.root(i * m + j);
// run window 5x5
for (int k = 0; k < 5; k++) {
for (int l = 0; l < 5; l++) {
int nx = i + dxx[k];
int ny = j + dxx[l];
if (nx >= 0 and nx < n and ny >= 0 and ny < m and
v[nx][ny] == '.') {
// cout<<"Valid "<<nx<<" "<<ny<<"\n";
int ra = tree.root(nx * m + ny);
if (tree.root(ra) != tree.root(rb)) {
if (edge.find({ra, rb}) == edge.end() and
edge.find({rb, ra}) == edge.end()) {
edge[{ra, rb}] = 1;
g[ra].pb(rb);
g[rb].pb(ra);
}
}
}
}
}
}
}
}
int ans = 0;
// final bfs
int rooti = tree.root(cx * m + cy);
int rootf = tree.root(rx * m + ry);
queue<int> q;
q.push(rooti);
dis[rooti] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : g[u]) {
if (dis[v] == -1) {
dis[v] = dis[u] + 1;
q.push(v);
}
}
}
cout << dis[rootf];
// for(auto x:edge){
// cout<<(x.f.f)<<" "<<(x.f.s)<<"\n";
// }
} | // #pragma GCC optimize "trapv"
//-D_GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define pm(m) \
for (auto itr = (m).begin(); itr != (m).end(); itr++) { \
itr->first << " " << itr->second << "\n"; \
}
#define int long long int
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define ii pair<int, int>
#define Max 100000 + 5
#define IO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define f first
#define s second
#define pb push_back
#define bug(x) \
cout << (x) << " "; \
exit(0);
int mod = 1000000007;
int mod1 = 998244353;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int power(int a, int b) {
if (b == 1)
return a;
if (a > mod)
a %= mod;
int r = 1;
while (b) {
if (b & 1)
r = r % mod * a % mod;
a = a % mod * a % mod;
b /= 2;
}
return r;
}
class uf {
vector<int> v;
vector<int> sz;
public:
uf(int n) {
v.resize(n + 1);
iota(v.begin() + 1, v.end(), 1);
sz.resize(n + 1, 1);
// 1 to sz
}
int root(int b) {
while (v[b] != b) {
b = v[b];
}
return b;
}
void join(int a, int b) {
// combine a and
int ra = root(a);
int rb = root(b);
if (ra == rb)
return;
if (sz[ra] < sz[rb])
swap(ra, rb);
v[rb] = ra;
sz[ra] += sz[rb];
}
bool same(int a, int b) {
// same set
return (root(a) == root(b));
}
int size(int a) { return sz[root(a)]; }
};
vector<int> matmul(vector<int> a, vector<int> b) {
vector<int> c(4, 0);
c[0] = (a[0] % mod * b[0] % mod + a[1] % mod * b[2] % mod) % mod;
c[1] = (a[0] % mod * b[1] % mod + a[1] % mod * b[3] % mod) % mod;
c[2] = (a[2] % mod * b[0] % mod + a[3] % mod * b[2] % mod) % mod;
c[3] = (a[2] % mod * b[1] % mod + a[3] % mod * b[3] % mod) % mod;
return c;
}
vector<int> id = {1, 0, 0, 1};
vector<int> g[1000000];
vector<int> dis(1000000, -1);
int32_t main() {
IO
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int n,
m;
cin >> n >> m;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int cx, cy, rx, ry;
cin >> cx >> cy >> rx >> ry;
cx--;
rx--;
cy--;
ry--;
vector<vector<char>> v(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cin >> v[i][j];
}
if (v[cx][cy] == '#' or v[rx][ry] == '#') {
cout << "-1";
exit(0);
}
uf tree(n * m);
vector<vector<bool>> vis(n, vector<bool>(m, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (v[i][j] == '.' and vis[i][j] == 0) {
// run a bfs
queue<ii> q;
q.push({i, j});
vis[i][j] = 1;
while (!q.empty()) {
ii fr = q.front();
q.pop();
int x = fr.f;
int y = fr.s;
for (int k = 0; k < 4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx >= 0 and nx < n and ny >= 0 and ny < m and !vis[nx][ny] and
v[nx][ny] == '.') {
q.push({nx, ny});
vis[nx][ny] = 1;
tree.join(i * m + j, nx * m + ny);
}
}
}
}
}
}
if (tree.root(cx * m + cy) == tree.root(rx * m + ry)) {
cout << 0;
exit(0);
}
// for(int i=0;i<n;i++){
// for(int j=0;j<m;j++){
// if(v[i][j]=='.')
// cout<<tree.root(i*m+j)<<" ";
// else cout<<"-1 ";
// }
// cout<<endl;
// }
// exit(0);
int dxx[] = {-2, -1, 0, 1, 2};
map<ii, bool> edge;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (v[i][j] == '.') {
int rb = tree.root(i * m + j);
// run window 5x5
for (int k = 0; k < 5; k++) {
for (int l = 0; l < 5; l++) {
int nx = i + dxx[k];
int ny = j + dxx[l];
if (nx >= 0 and nx < n and ny >= 0 and ny < m and
v[nx][ny] == '.') {
// cout<<"Valid "<<nx<<" "<<ny<<"\n";
int ra = tree.root(nx * m + ny);
if (tree.root(ra) != tree.root(rb)) {
if (edge.find({ra, rb}) == edge.end() and
edge.find({rb, ra}) == edge.end()) {
edge[{ra, rb}] = 1;
g[ra].pb(rb);
g[rb].pb(ra);
}
}
}
}
}
}
}
}
int ans = 0;
// final bfs
int rooti = tree.root(cx * m + cy);
int rootf = tree.root(rx * m + ry);
queue<int> q;
q.push(rooti);
dis[rooti] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : g[u]) {
if (dis[v] == -1) {
dis[v] = dis[u] + 1;
q.push(v);
}
}
}
cout << dis[rootf];
// for(auto x:edge){
// cout<<(x.f.f)<<" "<<(x.f.s)<<"\n";
// }
} | replace | 90 | 92 | 90 | 92 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
using namespace __gnu_pbds;
typedef tree<pair<ll, pair<ll, ll>>, null_type, less<pair<ll, pair<ll, ll>>>,
rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
#define me BOSS
#define bug() cout << "AIkahne aise" << endl
#define PI acos(-1)
/* Precode Start Here *\
Sieve
void sieve()
{
for(ll i=3; i<=2000; i+=2)
{
for(ll j=i*i; j<=4000000; j+=i)
A[j]=1;
}
v.push_back(2);
for(ll i=3; i<=4000000; i+=2)
if(A[i]==0)
v.push_back(i);
}
Power Function Modulo P
ll modPow(ll a, ll x, ll p) {
//calculates a^x mod p in logarithmic time.
long res = 1;
while(x > 0) {
if( x % 2 != 0) {
res = (res * a) % p;
}
a = (a * a) % p;
x /= 2;
}
return res;
}
factorial
ll fact(ll n)
{
ll ans=1;
for(ll i=1;i<=n;i++)
{
ans=(ans*i)%mod;
}
return ans;
}
Calculating nCr using Fermats law Theorem
ll modInverse(ll a, ll p) {
//calculates the modular multiplicative of a mod m.
//(assuming p is prime).
return modPow(a, p-2, p);
}
ll modBinomial(ll n, ll k, ll p) {
// calculates C(n,k) mod p (assuming p is prime).
ll numerator = 1; // n * (n-1) * ... * (n-k+1)
for (ll i=0; i<k; i++) {
numerator = (numerator * (n-i) ) % p;
}
ll denominator = 1; // k!
for (ll i=1; i<=k; i++) {
denominator = (denominator * i) % p;
}
// numerator / denominator mod p.
return ( numerator* modInverse(denominator,p) ) % p;
}
num of divisor and euler totient
ll phi[1000001],mark[1000001],divi[1000001];;
void divisorPhi(ll n)
{
for(int i=1;i<=n;i++)
phi[i]=i;
for(int i=2;i<=n;i++)
{
if(!mark[i])
{
for(int j=i;j<=n;j+=i)
{
mark[j]=1;
phi[j]*=(1-1/(double)i);
}
}
}
for(ll i=1;i<=n;i++)
{
for(ll j=i;j<=n;j+=i)
divi[j]++;
}
}
/* Precode Ends */
#define mod 1000000007
char A[1001][1001];
ll vis[1001][1001];
ll dis[1001][1001];
vector<ll> v;
map<ll, ll> myMap;
set<ll> mySet;
vector<pair<ll, ll>> vp;
priority_queue<ll> pq;
priority_queue<ll, vector<ll>, std::greater<ll>> first;
priority_queue<pair<ll, ll>> q;
ll po(ll a, ll n) {
ll ans = 1;
for (int i = 1; i <= n; i++)
ans *= a;
return ans;
}
ll r, c;
void go(int i, int j) {
q.push(make_pair(i, j));
vis[i][j] = 1;
dis[i][j] = 0;
while (!q.empty()) {
ll u = q.top().first;
ll v = q.top().second;
q.pop();
for (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 2; y++) {
// cout<<u+x<<" "<<v+y<<endl;
if (u + x > r || u + x < 1 || v + y > c || v + y < 1 ||
(x == 0 && y == 0))
continue;
if (vis[u + x][v + y] == 0 && A[u + x][v + y] == '.') {
vis[u + x][v + y] = 1;
if (abs(x) + abs(y) > 1)
dis[u + x][v + y] = dis[u][v] + 1;
else
dis[u + x][v + y] = dis[u][v];
q.push(make_pair(u + x, v + y));
} else if (A[u + x][v + y] == '.') {
if ((abs(x) + abs(y) > 1) && dis[u + x][v + y] > dis[u][v] + 1) {
dis[u + x][v + y] = dis[u][v] + 1;
q.push(make_pair(u + x, v + y));
} else if (dis[u][v] < dis[u + x][v + y] && (abs(x) + abs(y) <= 1)) {
dis[u + x][v + y] = dis[u][v];
q.push(make_pair(u + x, v + y));
}
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll sx, sy, dx, dy;
string s;
cin >> r >> c;
cin >> sx >> sy;
cin >> dx >> dy;
for (int i = 1; i <= r; i++) {
cin >> s;
for (int j = 1; j <= c; j++) {
A[i][j] = s[j - 1];
dis[i][j] = 99999999999999;
}
}
go(sx, sy);
// for(int i=1; i<=r; i++)
// {
// for(int j=1; j<=c; j++)
// {
// // cout<<A[i][j]<<" ";
// cout<<dis[i][j]<<" ";
// }
// cout<<endl;
// }
if (vis[dx][dy] == 0) {
cout << -1 << endl;
} else {
cout << dis[dx][dy] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
using namespace __gnu_pbds;
typedef tree<pair<ll, pair<ll, ll>>, null_type, less<pair<ll, pair<ll, ll>>>,
rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
#define me BOSS
#define bug() cout << "AIkahne aise" << endl
#define PI acos(-1)
/* Precode Start Here *\
Sieve
void sieve()
{
for(ll i=3; i<=2000; i+=2)
{
for(ll j=i*i; j<=4000000; j+=i)
A[j]=1;
}
v.push_back(2);
for(ll i=3; i<=4000000; i+=2)
if(A[i]==0)
v.push_back(i);
}
Power Function Modulo P
ll modPow(ll a, ll x, ll p) {
//calculates a^x mod p in logarithmic time.
long res = 1;
while(x > 0) {
if( x % 2 != 0) {
res = (res * a) % p;
}
a = (a * a) % p;
x /= 2;
}
return res;
}
factorial
ll fact(ll n)
{
ll ans=1;
for(ll i=1;i<=n;i++)
{
ans=(ans*i)%mod;
}
return ans;
}
Calculating nCr using Fermats law Theorem
ll modInverse(ll a, ll p) {
//calculates the modular multiplicative of a mod m.
//(assuming p is prime).
return modPow(a, p-2, p);
}
ll modBinomial(ll n, ll k, ll p) {
// calculates C(n,k) mod p (assuming p is prime).
ll numerator = 1; // n * (n-1) * ... * (n-k+1)
for (ll i=0; i<k; i++) {
numerator = (numerator * (n-i) ) % p;
}
ll denominator = 1; // k!
for (ll i=1; i<=k; i++) {
denominator = (denominator * i) % p;
}
// numerator / denominator mod p.
return ( numerator* modInverse(denominator,p) ) % p;
}
num of divisor and euler totient
ll phi[1000001],mark[1000001],divi[1000001];;
void divisorPhi(ll n)
{
for(int i=1;i<=n;i++)
phi[i]=i;
for(int i=2;i<=n;i++)
{
if(!mark[i])
{
for(int j=i;j<=n;j+=i)
{
mark[j]=1;
phi[j]*=(1-1/(double)i);
}
}
}
for(ll i=1;i<=n;i++)
{
for(ll j=i;j<=n;j+=i)
divi[j]++;
}
}
/* Precode Ends */
#define mod 1000000007
char A[1001][1001];
ll vis[1001][1001];
ll dis[1001][1001];
vector<ll> v;
map<ll, ll> myMap;
set<ll> mySet;
vector<pair<ll, ll>> vp;
priority_queue<ll> pq;
priority_queue<ll, vector<ll>, std::greater<ll>> first;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, std::greater<pair<ll, ll>>>
q;
ll po(ll a, ll n) {
ll ans = 1;
for (int i = 1; i <= n; i++)
ans *= a;
return ans;
}
ll r, c;
void go(int i, int j) {
q.push(make_pair(i, j));
vis[i][j] = 1;
dis[i][j] = 0;
while (!q.empty()) {
ll u = q.top().first;
ll v = q.top().second;
q.pop();
for (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 2; y++) {
// cout<<u+x<<" "<<v+y<<endl;
if (u + x > r || u + x < 1 || v + y > c || v + y < 1 ||
(x == 0 && y == 0))
continue;
if (vis[u + x][v + y] == 0 && A[u + x][v + y] == '.') {
vis[u + x][v + y] = 1;
if (abs(x) + abs(y) > 1)
dis[u + x][v + y] = dis[u][v] + 1;
else
dis[u + x][v + y] = dis[u][v];
q.push(make_pair(u + x, v + y));
} else if (A[u + x][v + y] == '.') {
if ((abs(x) + abs(y) > 1) && dis[u + x][v + y] > dis[u][v] + 1) {
dis[u + x][v + y] = dis[u][v] + 1;
q.push(make_pair(u + x, v + y));
} else if (dis[u][v] < dis[u + x][v + y] && (abs(x) + abs(y) <= 1)) {
dis[u + x][v + y] = dis[u][v];
q.push(make_pair(u + x, v + y));
}
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll sx, sy, dx, dy;
string s;
cin >> r >> c;
cin >> sx >> sy;
cin >> dx >> dy;
for (int i = 1; i <= r; i++) {
cin >> s;
for (int j = 1; j <= c; j++) {
A[i][j] = s[j - 1];
dis[i][j] = 99999999999999;
}
}
go(sx, sy);
// for(int i=1; i<=r; i++)
// {
// for(int j=1; j<=c; j++)
// {
// // cout<<A[i][j]<<" ";
// cout<<dis[i][j]<<" ";
// }
// cout<<endl;
// }
if (vis[dx][dy] == 0) {
cout << -1 << endl;
} else {
cout << dis[dx][dy] << endl;
}
return 0;
}
| replace | 114 | 115 | 114 | 116 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using pll = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, l, r) for (ll i = (l); i <= (ll)(r); i++)
#define rep3(i, l, r) for (ll i = (l); i >= (ll)(r); i--)
#define dup(x, y) (((x) + (y)-1) / (y)) // x/yの除算の切り上げ
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int inf = 1001001001;
const ll INF = 1LL << 60;
const ll mod = 1000000007; // 1000000007
const ld pi = acos(-1);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll h, w;
cin >> h >> w;
ll si, sj, ei, ej;
cin >> si >> sj >> ei >> ej;
si--;
sj--;
ei--;
ej--;
vector<string> s(h);
rep(i, h) cin >> s[i];
vector<vector<ll>> dist(h, vector<ll>(w, INF));
deque<pll> q;
dist[si][sj] = 0;
q.emplace_back(si, sj);
while (!q.empty()) {
ll i = q.front().first;
ll j = q.front().second;
ll d = dist[i][j];
q.pop_front();
rep(v, 4) {
ll ni = (i + di[v]);
ll nj = (j + dj[v]);
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d)
continue;
dist[ni][nj] = d;
q.emplace_back(ni, nj);
}
rep2(ei, -2, 2) {
rep2(ej, -2, 2) {
ll ni = (i + ei);
ll nj = (j + ej);
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= (d + 1))
continue;
dist[ni][nj] = (d + 1);
q.emplace_back(ni, nj);
}
}
}
ll ans = dist[ei][ej];
if (ans == INF)
ans = -1;
cout << ans << '\n';
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using pll = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, l, r) for (ll i = (l); i <= (ll)(r); i++)
#define rep3(i, l, r) for (ll i = (l); i >= (ll)(r); i--)
#define dup(x, y) (((x) + (y)-1) / (y)) // x/yの除算の切り上げ
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int inf = 1001001001;
const ll INF = 1LL << 60;
const ll mod = 1000000007; // 1000000007
const ld pi = acos(-1);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll h, w;
cin >> h >> w;
ll si, sj, ei, ej;
cin >> si >> sj >> ei >> ej;
si--;
sj--;
ei--;
ej--;
vector<string> s(h);
rep(i, h) cin >> s[i];
vector<vector<ll>> dist(h, vector<ll>(w, INF));
deque<pll> q;
dist[si][sj] = 0;
q.emplace_back(si, sj);
while (!q.empty()) {
ll i = q.front().first;
ll j = q.front().second;
ll d = dist[i][j];
q.pop_front();
rep(v, 4) {
ll ni = (i + di[v]);
ll nj = (j + dj[v]);
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= d)
continue;
dist[ni][nj] = d;
q.emplace_front(ni, nj);
}
rep2(ei, -2, 2) {
rep2(ej, -2, 2) {
ll ni = (i + ei);
ll nj = (j + ej);
if (ni < 0 || ni >= h || nj < 0 || nj >= w)
continue;
if (s[ni][nj] == '#')
continue;
if (dist[ni][nj] <= (d + 1))
continue;
dist[ni][nj] = (d + 1);
q.emplace_back(ni, nj);
}
}
}
ll ans = dist[ei][ej];
if (ans == INF)
ans = -1;
cout << ans << '\n';
} | replace | 66 | 67 | 66 | 67 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <algorithm> //sort,二分探索,など
#include <bitset> //固定長bit集合
#include <cmath> //pow,logなど
#include <complex> //複素数
#include <deque> //両端アクセスのキュー
#include <fstream> //ファイルストリーム(標準入力変更用)
#include <functional> //sortのgreater
#include <iomanip> //setprecision(浮動小数点の出力の誤差)
#include <iostream> //入出力
#include <iterator> //集合演算(積集合,和集合,差集合など)
#include <map> //map(辞書)
#include <numeric> //iota(整数列の生成),gcdとlcm(c++17)
#include <queue> //キュー
#include <set> //集合
#include <stack> //スタック
#include <string> //文字列
#include <unordered_map> //イテレータあるけど順序保持しないmap
#include <unordered_set> //イテレータあるけど順序保持しないset
#include <utility> //pair
#include <vector> //可変長配列
// 名前
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef map<string, int> msi;
typedef map<string, ll> msll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pllll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<string>> vvs;
typedef vector<vector<bool>> vvb;
// 定数
const ll MOD = 1000000007;
const ll INF = 1000000000000000000;
// マクロ
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, s, e) for (int i = s; i < e; i++)
#define repse(i, s, e) for (int i = s; i <= e; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define in(x1) cin >> x1
#define in2(x1, x2) cin >> x1 >> x2
#define in3(x1, x2, x3) cin >> x1 >> x2 >> x3
#define outl(x) cout << x << endl
#define out2l(x, y) cout << x << " " << y << endl
// よく使う関数
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// 標準入力をファイルに変更
// std::ifstream in("input.txt");
// std::cin.rdbuf(in.rdbuf());
int H, W;
in2(H, W);
pii c;
in2(c.first, c.second);
c.first--;
c.second--;
pii d;
in2(d.first, d.second);
d.first--;
d.second--;
vs S(H);
rep(i, H) in(S[i]);
vvll dist(H, vll(W, INF));
dist[c.first][c.second] = 0;
deque<pii> q;
q.emplace_back(c.first, c.second);
S[c.first][c.second] = '#';
int dh[4] = {-1, 0, 1, 0};
int dw[4] = {0, 1, 0, -1};
while (!q.empty()) {
auto p = q.front();
q.pop_front();
rep(i, 4) {
int h = p.first + dh[i];
int w = p.second + dw[i];
if (!(h >= 0 && h < H && w >= 0 && w < W))
continue;
if (S[h][w] == '#')
continue;
if (chmin(dist[h][w], dist[p.first][p.second])) {
q.emplace_front(h, w);
}
}
repse(i, -2, 2) {
repse(j, -2, 2) {
int h = p.first + i;
int w = p.second + j;
if (!(h >= 0 && h < H && w >= 0 && w < W))
continue;
if (S[h][w] == '#')
continue;
if (chmin(dist[h][w], dist[p.first][p.second] + 1)) {
q.emplace_front(h, w);
}
}
}
}
if (dist[d.first][d.second] == INF) {
outl(-1);
} else {
outl(dist[d.first][d.second]);
}
return 0;
} | #include <algorithm> //sort,二分探索,など
#include <bitset> //固定長bit集合
#include <cmath> //pow,logなど
#include <complex> //複素数
#include <deque> //両端アクセスのキュー
#include <fstream> //ファイルストリーム(標準入力変更用)
#include <functional> //sortのgreater
#include <iomanip> //setprecision(浮動小数点の出力の誤差)
#include <iostream> //入出力
#include <iterator> //集合演算(積集合,和集合,差集合など)
#include <map> //map(辞書)
#include <numeric> //iota(整数列の生成),gcdとlcm(c++17)
#include <queue> //キュー
#include <set> //集合
#include <stack> //スタック
#include <string> //文字列
#include <unordered_map> //イテレータあるけど順序保持しないmap
#include <unordered_set> //イテレータあるけど順序保持しないset
#include <utility> //pair
#include <vector> //可変長配列
// 名前
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef map<string, int> msi;
typedef map<string, ll> msll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pllll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<string>> vvs;
typedef vector<vector<bool>> vvb;
// 定数
const ll MOD = 1000000007;
const ll INF = 1000000000000000000;
// マクロ
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, s, e) for (int i = s; i < e; i++)
#define repse(i, s, e) for (int i = s; i <= e; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define in(x1) cin >> x1
#define in2(x1, x2) cin >> x1 >> x2
#define in3(x1, x2, x3) cin >> x1 >> x2 >> x3
#define outl(x) cout << x << endl
#define out2l(x, y) cout << x << " " << y << endl
// よく使う関数
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// 標準入力をファイルに変更
// std::ifstream in("input.txt");
// std::cin.rdbuf(in.rdbuf());
int H, W;
in2(H, W);
pii c;
in2(c.first, c.second);
c.first--;
c.second--;
pii d;
in2(d.first, d.second);
d.first--;
d.second--;
vs S(H);
rep(i, H) in(S[i]);
vvll dist(H, vll(W, INF));
dist[c.first][c.second] = 0;
deque<pii> q;
q.emplace_back(c.first, c.second);
S[c.first][c.second] = '#';
int dh[4] = {-1, 0, 1, 0};
int dw[4] = {0, 1, 0, -1};
while (!q.empty()) {
auto p = q.front();
q.pop_front();
rep(i, 4) {
int h = p.first + dh[i];
int w = p.second + dw[i];
if (!(h >= 0 && h < H && w >= 0 && w < W))
continue;
if (S[h][w] == '#')
continue;
if (chmin(dist[h][w], dist[p.first][p.second])) {
q.emplace_front(h, w);
}
}
repse(i, -2, 2) {
repse(j, -2, 2) {
int h = p.first + i;
int w = p.second + j;
if (!(h >= 0 && h < H && w >= 0 && w < W))
continue;
if (S[h][w] == '#')
continue;
if (chmin(dist[h][w], dist[p.first][p.second] + 1)) {
q.emplace_back(h, w);
}
}
}
}
if (dist[d.first][d.second] == INF) {
outl(-1);
} else {
outl(dist[d.first][d.second]);
}
return 0;
} | replace | 135 | 136 | 135 | 136 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
/// mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 1010;
int n, m;
char a[N][N];
int d[N][N];
bool valid(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= m); }
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
void solve() {
cin >> n >> m;
int x0, y0, x1, y1;
cin >> x0 >> y0;
cin >> x1 >> y1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
d[i][j] = 1e9;
}
}
set<pair<int, pair<int, int>>> s;
d[x1][y1] = 0;
s.insert({0, {x1, y1}});
while (!s.empty()) {
auto f = s.begin();
int x = f->second.first;
int y = f->second.second;
s.erase(s.begin());
cerr << x << ' ' << y << endl;
for (int i = 0; i < 4; i++) {
int X = x + dx[i];
int Y = y + dy[i];
if (valid(X, Y) && a[X][Y] == '.' && d[X][Y] > d[x][y]) {
s.erase({d[X][Y], {X, Y}});
d[X][Y] = d[x][y];
s.insert({d[X][Y], {X, Y}});
}
}
for (int X = x - 2; X <= x + 2; X++) {
for (int Y = y - 2; Y <= y + 2; Y++) {
if (valid(X, Y) && a[X][Y] == '.') {
if (d[X][Y] > d[x][y] + 1) {
s.erase({d[X][Y], {X, Y}});
d[X][Y] = d[x][y] + 1;
s.insert({d[X][Y], {X, Y}});
}
}
}
}
}
if (d[x0][y0] == 1e9)
cout << -1 << endl;
else
cout << d[x0][y0] << endl;
}
int main() {
#ifdef QWERTY
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case #" << i << ": ";
solve();
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
/// mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 1010;
int n, m;
char a[N][N];
int d[N][N];
bool valid(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= m); }
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
void solve() {
cin >> n >> m;
int x0, y0, x1, y1;
cin >> x0 >> y0;
cin >> x1 >> y1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
d[i][j] = 1e9;
}
}
set<pair<int, pair<int, int>>> s;
d[x1][y1] = 0;
s.insert({0, {x1, y1}});
while (!s.empty()) {
auto f = s.begin();
int x = f->second.first;
int y = f->second.second;
s.erase(s.begin());
// cerr << x << ' ' << y << endl;
for (int i = 0; i < 4; i++) {
int X = x + dx[i];
int Y = y + dy[i];
if (valid(X, Y) && a[X][Y] == '.' && d[X][Y] > d[x][y]) {
s.erase({d[X][Y], {X, Y}});
d[X][Y] = d[x][y];
s.insert({d[X][Y], {X, Y}});
}
}
for (int X = x - 2; X <= x + 2; X++) {
for (int Y = y - 2; Y <= y + 2; Y++) {
if (valid(X, Y) && a[X][Y] == '.') {
if (d[X][Y] > d[x][y] + 1) {
s.erase({d[X][Y], {X, Y}});
d[X][Y] = d[x][y] + 1;
s.insert({d[X][Y], {X, Y}});
}
}
}
}
}
if (d[x0][y0] == 1e9)
cout << -1 << endl;
else
cout << d[x0][y0] << endl;
}
int main() {
#ifdef QWERTY
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case #" << i << ": ";
solve();
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
} | replace | 39 | 40 | 39 | 40 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// マクロ
// forループ関係
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
// xにはvectorなどのコンテナ
#define ALL(x) x.begin(), x.end() // sortなどの引数を省略したい
#define SIZE(x) ll(x.size()) // sizeをsize_tからllに直しておく
#define LEN(x) ll(x.length()) // lengthをsize_tからllに直しておく
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
#define MOD 1000000007 // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange(素数列挙などで使用)
// 略記
#define PB emplace_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first // pairの一つ目の要素
#define S second // pairの二つ目の要素
#define Umap unordered_map
#define Uset unordered_set
bool is_ok(ll a, ll b, ll h, ll w) {
if (a < 0)
return false;
if (a >= h)
return false;
if (b < 0)
return false;
if (b >= w)
return false;
return true;
}
int main() {
ll h, w, ch, cw, dh, dw;
cin >> h >> w;
cin >> ch >> cw;
cin >> dh >> dw;
vector<string> s(h);
REP(i, h) cin >> s[i];
queue<pair<ll, ll>> q, temp, next;
vector<vector<ll>> num(h, vector<ll>(w, -1));
ch--;
cw--;
dh--;
dw--;
q.emplace(ch, cw);
temp.emplace(ch, cw);
num[ch][cw] = 0;
while (!temp.empty()) {
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v.F == dh && v.S == dw) {
cout << num[v.F][v.S];
return true;
}
if (is_ok(v.F - 1, v.S, h, w)) {
if (s[v.F - 1][v.S] == '.' && num[v.F - 1][v.S] == -1) {
q.emplace(v.F - 1, v.S);
temp.emplace(v.F - 1, v.S);
num[v.F - 1][v.S] = num[v.F][v.S];
}
}
if (is_ok(v.F + 1, v.S, h, w)) {
if (s[v.F + 1][v.S] == '.' && num[v.F + 1][v.S] == -1) {
q.emplace(v.F + 1, v.S);
temp.emplace(v.F + 1, v.S);
num[v.F + 1][v.S] = num[v.F][v.S];
}
}
if (is_ok(v.F, v.S - 1, h, w)) {
if (s[v.F][v.S - 1] == '.' && num[v.F][v.S - 1] == -1) {
q.emplace(v.F, v.S - 1);
temp.emplace(v.F, v.S - 1);
num[v.F][v.S - 1] = num[v.F][v.S];
}
}
if (is_ok(v.F, v.S + 1, h, w)) {
if (s[v.F][v.S + 1] == '.' && num[v.F][v.S + 1] == -1) {
q.emplace(v.F, v.S + 1);
temp.emplace(v.F, v.S + 1);
num[v.F][v.S + 1] = num[v.F][v.S];
}
}
}
while (!temp.empty()) {
auto v = temp.front();
temp.pop();
for (ll i = -2; i <= 2; i++) {
for (ll j = -2; j <= 2; j++) {
if (is_ok(v.F + i, v.S + j, h, w)) {
if (s[v.F + i][v.S + j] == '.' && num[v.F + i][v.S + j] == -1) {
q.emplace(v.F + i, v.S + j);
next.emplace(v.F + i, v.S + j);
num[v.F + i][v.S + j] = num[v.F][v.S] + 1;
}
}
}
}
}
// next -> temp
while (!next.empty()) {
auto v = next.front();
next.pop();
temp.emplace(v);
}
}
cout << -1;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// マクロ
// forループ関係
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
// xにはvectorなどのコンテナ
#define ALL(x) x.begin(), x.end() // sortなどの引数を省略したい
#define SIZE(x) ll(x.size()) // sizeをsize_tからllに直しておく
#define LEN(x) ll(x.length()) // lengthをsize_tからllに直しておく
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
#define MOD 1000000007 // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange(素数列挙などで使用)
// 略記
#define PB emplace_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first // pairの一つ目の要素
#define S second // pairの二つ目の要素
#define Umap unordered_map
#define Uset unordered_set
bool is_ok(ll a, ll b, ll h, ll w) {
if (a < 0)
return false;
if (a >= h)
return false;
if (b < 0)
return false;
if (b >= w)
return false;
return true;
}
int main() {
ll h, w, ch, cw, dh, dw;
cin >> h >> w;
cin >> ch >> cw;
cin >> dh >> dw;
vector<string> s(h);
REP(i, h) cin >> s[i];
queue<pair<ll, ll>> q, temp, next;
vector<vector<ll>> num(h, vector<ll>(w, -1));
ch--;
cw--;
dh--;
dw--;
q.emplace(ch, cw);
temp.emplace(ch, cw);
num[ch][cw] = 0;
while (!temp.empty()) {
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v.F == dh && v.S == dw) {
cout << num[v.F][v.S];
return 0;
}
if (is_ok(v.F - 1, v.S, h, w)) {
if (s[v.F - 1][v.S] == '.' && num[v.F - 1][v.S] == -1) {
q.emplace(v.F - 1, v.S);
temp.emplace(v.F - 1, v.S);
num[v.F - 1][v.S] = num[v.F][v.S];
}
}
if (is_ok(v.F + 1, v.S, h, w)) {
if (s[v.F + 1][v.S] == '.' && num[v.F + 1][v.S] == -1) {
q.emplace(v.F + 1, v.S);
temp.emplace(v.F + 1, v.S);
num[v.F + 1][v.S] = num[v.F][v.S];
}
}
if (is_ok(v.F, v.S - 1, h, w)) {
if (s[v.F][v.S - 1] == '.' && num[v.F][v.S - 1] == -1) {
q.emplace(v.F, v.S - 1);
temp.emplace(v.F, v.S - 1);
num[v.F][v.S - 1] = num[v.F][v.S];
}
}
if (is_ok(v.F, v.S + 1, h, w)) {
if (s[v.F][v.S + 1] == '.' && num[v.F][v.S + 1] == -1) {
q.emplace(v.F, v.S + 1);
temp.emplace(v.F, v.S + 1);
num[v.F][v.S + 1] = num[v.F][v.S];
}
}
}
while (!temp.empty()) {
auto v = temp.front();
temp.pop();
for (ll i = -2; i <= 2; i++) {
for (ll j = -2; j <= 2; j++) {
if (is_ok(v.F + i, v.S + j, h, w)) {
if (s[v.F + i][v.S + j] == '.' && num[v.F + i][v.S + j] == -1) {
q.emplace(v.F + i, v.S + j);
next.emplace(v.F + i, v.S + j);
num[v.F + i][v.S + j] = num[v.F][v.S] + 1;
}
}
}
}
}
// next -> temp
while (!next.empty()) {
auto v = next.front();
next.pop();
temp.emplace(v);
}
}
cout << -1;
return 0;
} | replace | 63 | 64 | 63 | 64 | 1 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define REP(i, j) for (int i = 0; i < j; i++)
#define FORN(i, j, k) for (int i = j; i < k; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define all(a) a.begin(), a.end()
using namespace std;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const int DX[25] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2,
2, 2, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2};
const int DY[25] = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0,
1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2};
int n, m;
int x, y, enx, eny, goal;
bool g[1005][1005];
int graph[1005][1005];
bool vis[1005][1005], used[500005];
vpii ver[500005];
int dis[500005] = {0};
int k = 0;
ll ans = 0;
bool infield(int a, int b) { return a >= 0 && b >= 0 && a < n && b < m; }
void bfs1(int a, int b) {
queue<pii> q;
q.push(mp(a, b));
while (!q.empty()) {
int _x = q.front().fi, _y = q.front().se;
q.pop();
vis[_x][_y] = 1;
graph[_x][_y] = k;
ver[k].pb(mp(_x, _y));
REP(i, 4) {
if ((!vis[_x + dx[i]][_y + dy[i]]) && g[_x + dx[i]][_y + dy[i]] &&
infield(_x + dx[i], _y + dy[i])) {
vis[_x + dx[i]][_y + dy[i]] = 1;
q.push(mp(_x + dx[i], _y + dy[i]));
}
}
}
}
bool bfs2(int pos) {
queue<int> q;
used[pos] = 1;
q.push(pos);
while (!q.empty()) {
int a = q.front();
q.pop();
REP(i, (int)ver[a].size()) {
int _x = ver[a][i].fi, _y = ver[a][i].se;
REP(j, 25) {
if (g[_x + DX[j]][_y + DY[j]] && infield(_x + DX[j], _y + DY[j])) {
if (graph[_x + DX[j]][_y + DY[j]] != a &&
!used[graph[_x + DX[j]][_y + DY[j]]]) {
q.push(graph[_x + DX[j]][_y + DY[j]]);
used[graph[_x + DX[j]][_y + DY[j]]] = 1;
dis[graph[_x + DX[j]][_y + DY[j]]] = dis[a] + 1;
}
}
}
}
}
}
int main(void) {
cin >> n >> m >> x >> y >> enx >> eny;
x--, y--, enx--, eny--;
REP(i, n) REP(j, m) graph[i][j] = -1;
REP(i, n) REP(j, m) {
char c;
cin >> c;
if (c == '.') {
g[i][j] = 1;
} else {
g[i][j] = 0;
}
}
REP(i, n) REP(j, m) {
if (!vis[i][j] && g[i][j]) {
bfs1(i, j);
k++;
}
}
memset(vis, false, sizeof(vis));
goal = graph[enx][eny];
if (goal == graph[x][y]) {
cout << 0;
return 0;
}
bfs2(graph[x][y]);
if (dis[goal] == 0)
cout << -1;
else
cout << dis[goal];
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define REP(i, j) for (int i = 0; i < j; i++)
#define FORN(i, j, k) for (int i = j; i < k; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define all(a) a.begin(), a.end()
using namespace std;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const int DX[25] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2,
2, 2, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2};
const int DY[25] = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0,
1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2};
int n, m;
int x, y, enx, eny, goal;
bool g[1005][1005];
int graph[1005][1005];
bool vis[1005][1005], used[500005];
vpii ver[500005];
int dis[500005] = {0};
int k = 0;
ll ans = 0;
bool infield(int a, int b) { return a >= 0 && b >= 0 && a < n && b < m; }
void bfs1(int a, int b) {
queue<pii> q;
q.push(mp(a, b));
while (!q.empty()) {
int _x = q.front().fi, _y = q.front().se;
q.pop();
vis[_x][_y] = 1;
graph[_x][_y] = k;
ver[k].pb(mp(_x, _y));
REP(i, 4) {
if ((!vis[_x + dx[i]][_y + dy[i]]) && g[_x + dx[i]][_y + dy[i]] &&
infield(_x + dx[i], _y + dy[i])) {
vis[_x + dx[i]][_y + dy[i]] = 1;
q.push(mp(_x + dx[i], _y + dy[i]));
}
}
}
}
void bfs2(int pos) {
queue<int> q;
used[pos] = 1;
q.push(pos);
while (!q.empty()) {
int a = q.front();
q.pop();
REP(i, (int)ver[a].size()) {
int _x = ver[a][i].fi, _y = ver[a][i].se;
REP(j, 25) {
if (g[_x + DX[j]][_y + DY[j]] && infield(_x + DX[j], _y + DY[j])) {
if (graph[_x + DX[j]][_y + DY[j]] != a &&
!used[graph[_x + DX[j]][_y + DY[j]]]) {
q.push(graph[_x + DX[j]][_y + DY[j]]);
used[graph[_x + DX[j]][_y + DY[j]]] = 1;
dis[graph[_x + DX[j]][_y + DY[j]]] = dis[a] + 1;
}
}
}
}
}
}
int main(void) {
cin >> n >> m >> x >> y >> enx >> eny;
x--, y--, enx--, eny--;
REP(i, n) REP(j, m) graph[i][j] = -1;
REP(i, n) REP(j, m) {
char c;
cin >> c;
if (c == '.') {
g[i][j] = 1;
} else {
g[i][j] = 0;
}
}
REP(i, n) REP(j, m) {
if (!vis[i][j] && g[i][j]) {
bfs1(i, j);
k++;
}
}
memset(vis, false, sizeof(vis));
goal = graph[enx][eny];
if (goal == graph[x][y]) {
cout << 0;
return 0;
}
bfs2(graph[x][y]);
if (dis[goal] == 0)
cout << -1;
else
cout << dis[goal];
return 0;
} | replace | 49 | 50 | 49 | 50 | 0 | |
p02579 | C++ | Runtime Error | #include <iostream>
#include <queue>
using namespace std;
int h, w, g[15][15], ans[15][15], vis[15][15], ch, cw, dh, dw;
string s;
int main() {
cin >> h >> w;
cin >> ch >> cw >> dh >> dw;
for (int i = 1; i <= h; i++) {
cin >> s;
for (int j = 1; j <= w; j++) {
if (s[j - 1] == '.')
g[i][j] = 1;
}
}
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
greater<pair<int, pair<int, int>>>>
pq;
pq.push({0, {ch, cw}});
while (!pq.empty()) {
int d = pq.top().first, x = pq.top().second.first,
y = pq.top().second.second;
pq.pop();
if (vis[x][y])
continue;
ans[x][y] = d;
vis[x][y] = 1;
for (int i = max(1, x - 2); i <= min(h, x + 2); i++) {
for (int j = max(1, y - 2); j <= min(w, y + 2); j++) {
if (!g[i][j] || vis[i][j])
continue;
if (abs(i - x) + abs(j - y) == 1) {
ans[i][j] = ans[x][y];
pq.push({ans[i][j], {i, j}});
} else {
ans[i][j] = ans[x][y] + 1;
pq.push({ans[i][j], {i, j}});
}
}
}
}
if (!vis[dh][dw])
cout << -1 << "\n";
else
cout << ans[dh][dw] << "\n";
}
| #include <iostream>
#include <queue>
using namespace std;
int h, w, g[1005][1005], ans[1005][1005], vis[1005][1005], ch, cw, dh, dw;
string s;
int main() {
cin >> h >> w;
cin >> ch >> cw >> dh >> dw;
for (int i = 1; i <= h; i++) {
cin >> s;
for (int j = 1; j <= w; j++) {
if (s[j - 1] == '.')
g[i][j] = 1;
}
}
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
greater<pair<int, pair<int, int>>>>
pq;
pq.push({0, {ch, cw}});
while (!pq.empty()) {
int d = pq.top().first, x = pq.top().second.first,
y = pq.top().second.second;
pq.pop();
if (vis[x][y])
continue;
ans[x][y] = d;
vis[x][y] = 1;
for (int i = max(1, x - 2); i <= min(h, x + 2); i++) {
for (int j = max(1, y - 2); j <= min(w, y + 2); j++) {
if (!g[i][j] || vis[i][j])
continue;
if (abs(i - x) + abs(j - y) == 1) {
ans[i][j] = ans[x][y];
pq.push({ans[i][j], {i, j}});
} else {
ans[i][j] = ans[x][y] + 1;
pq.push({ans[i][j], {i, j}});
}
}
}
}
if (!vis[dh][dw])
cout << -1 << "\n";
else
cout << ans[dh][dw] << "\n";
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int H, W, sh, sw, gh, gw;
bool Grid[1010][1010];
int id[1010][1010];
int64_t INF = (1LL << 62);
int64_t MAXN = 100010;
vector<vector<pair<int, int>>> Graph(MAXN);
int64_t dijkstra(int64_t start, int64_t goal) {
priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>,
greater<pair<int64_t, int64_t>>>
PQ;
vector<int64_t> Color(MAXN, 0), D(MAXN, INF);
D.at(start) = 0;
PQ.push(make_pair(0, start));
Color.at(start) = 1;
while (!PQ.empty()) {
pair<int64_t, int64_t> f = PQ.top();
PQ.pop();
int64_t u = f.second;
Color.at(u) = 2;
if (D.at(u) < f.first)
continue;
for (int i = 0; i < (int)(Graph.at(u).size()); i++) {
int64_t v = Graph.at(u).at(i).first;
if (Color.at(v) == 2)
continue;
if (D.at(v) > D.at(u) + Graph.at(u).at(i).second) {
D.at(v) = D.at(u) + Graph.at(u).at(i).second;
PQ.push(make_pair(D.at(v), v));
Color.at(v) = 1;
}
}
}
return D.at(goal);
}
void initId() {
int cnt = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
id[i][j] = cnt;
cnt++;
}
}
}
int main() {
cin >> H >> W >> sh >> sw >> gh >> gw;
char c;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> c;
Grid[i][j] = (c == '.');
}
}
initId();
vector<int> dh = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
vector<int> dw = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0,
1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2};
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (!Grid[i][j])
continue;
int cur = id[i][j];
for (int k = 0; k < 25; k++) {
if (dh[k] == 0 && dw[k] == 0)
continue;
int nexth = i + dh[k], nextw = j + dw[k];
if (nexth < 0 || nexth >= H || nextw < 0 || nextw >= W)
continue;
if (!Grid[nexth][nextw])
continue;
int nextv = id[nexth][nextw];
if (abs(dh[k]) + abs(dw[k]) == 1) {
Graph.at(cur).push_back({nextv, 0});
} else {
Graph.at(cur).push_back({nextv, 1});
}
}
}
}
int64_t ans = dijkstra(id[sh - 1][sw - 1], id[gh - 1][gw - 1]);
cout << (ans == INF ? -1 : ans) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int H, W, sh, sw, gh, gw;
bool Grid[1010][1010];
int id[1010][1010];
int64_t INF = (1LL << 62);
int64_t MAXN = 1000010;
vector<vector<pair<int, int>>> Graph(MAXN);
int64_t dijkstra(int64_t start, int64_t goal) {
priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>,
greater<pair<int64_t, int64_t>>>
PQ;
vector<int64_t> Color(MAXN, 0), D(MAXN, INF);
D.at(start) = 0;
PQ.push(make_pair(0, start));
Color.at(start) = 1;
while (!PQ.empty()) {
pair<int64_t, int64_t> f = PQ.top();
PQ.pop();
int64_t u = f.second;
Color.at(u) = 2;
if (D.at(u) < f.first)
continue;
for (int i = 0; i < (int)(Graph.at(u).size()); i++) {
int64_t v = Graph.at(u).at(i).first;
if (Color.at(v) == 2)
continue;
if (D.at(v) > D.at(u) + Graph.at(u).at(i).second) {
D.at(v) = D.at(u) + Graph.at(u).at(i).second;
PQ.push(make_pair(D.at(v), v));
Color.at(v) = 1;
}
}
}
return D.at(goal);
}
void initId() {
int cnt = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
id[i][j] = cnt;
cnt++;
}
}
}
int main() {
cin >> H >> W >> sh >> sw >> gh >> gw;
char c;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> c;
Grid[i][j] = (c == '.');
}
}
initId();
vector<int> dh = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
vector<int> dw = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0,
1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2};
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (!Grid[i][j])
continue;
int cur = id[i][j];
for (int k = 0; k < 25; k++) {
if (dh[k] == 0 && dw[k] == 0)
continue;
int nexth = i + dh[k], nextw = j + dw[k];
if (nexth < 0 || nexth >= H || nextw < 0 || nextw >= W)
continue;
if (!Grid[nexth][nextw])
continue;
int nextv = id[nexth][nextw];
if (abs(dh[k]) + abs(dw[k]) == 1) {
Graph.at(cur).push_back({nextv, 0});
} else {
Graph.at(cur).push_back({nextv, 1});
}
}
}
}
int64_t ans = dijkstra(id[sh - 1][sw - 1], id[gh - 1][gw - 1]);
cout << (ans == INF ? -1 : ans) << endl;
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
int H, W;
cin >> H >> W;
int ch, dh, cw, dw;
cin >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
vector<string> s(H);
for (int i = 0; i < H; ++i) {
cin >> s[i];
}
vector<vector<int>> map(H, vector<int>(W, -1));
map[ch][cw] = 0;
queue<pair<int, int>> q;
queue<pair<int, int>> qq;
q.push(make_pair(ch, cw));
qq.push(make_pair(ch, cw));
while (!q.empty()) {
set<pair<int, int>> qqvisit;
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
if (qqvisit.find(p) == qqvisit.end()) {
qq.push(p);
}
int warp = map[p.first][p.second];
if (p.first + 1 < H && s[p.first + 1][p.second] == '.' &&
map[p.first + 1][p.second] == -1) {
map[p.first + 1][p.second] = warp;
q.push(make_pair(p.first + 1, p.second));
if (qqvisit.find(make_pair(p.first + 1, p.second)) == qqvisit.end()) {
qq.push(make_pair(p.first + 1, p.second));
qqvisit.insert(make_pair(p.first + 1, p.second));
}
}
if (p.first - 1 >= 0 && s[p.first - 1][p.second] == '.' &&
map[p.first - 1][p.second] == -1) {
map[p.first - 1][p.second] = warp;
q.push(make_pair(p.first - 1, p.second));
if (qqvisit.find(make_pair(p.first - 1, p.second)) == qqvisit.end()) {
qq.push(make_pair(p.first - 1, p.second));
qqvisit.insert(make_pair(p.first - 1, p.second));
}
}
if (p.second + 1 < W && s[p.first][p.second + 1] == '.' &&
map[p.first][p.second + 1] == -1) {
map[p.first][p.second + 1] = warp;
q.push(make_pair(p.first, p.second + 1));
if (qqvisit.find(make_pair(p.first, p.second + 1)) == qqvisit.end()) {
qq.push(make_pair(p.first, p.second + 1));
qqvisit.insert(make_pair(p.first, p.second + 1));
}
}
if (p.second - 1 >= 0 && s[p.first][p.second - 1] == '.' &&
map[p.first][p.second - 1] == -1) {
map[p.first][p.second - 1] = warp;
q.push(make_pair(p.first, p.second - 1));
if (qqvisit.find(make_pair(p.first, p.second - 1)) == qqvisit.end()) {
qq.push(make_pair(p.first, p.second - 1));
qqvisit.insert(make_pair(p.first, p.second - 1));
}
}
}
set<pair<int, int>> qvisit;
while (!qq.empty()) {
pair<int, int> p = qq.front();
qq.pop();
int warp = map[p.first][p.second] + 1;
for (int h = -2; h <= 2; ++h) {
for (int w = -2; w <= 2; ++w) {
if (p.first + h >= 0 && p.first + h < H && p.second + w >= 0 &&
p.second + w < W && s[p.first + h][p.second + w] == '.' &&
map[p.first + h][p.second + w] == -1) {
map[p.first + h][p.second + w] = warp;
if (qvisit.find(make_pair(p.first + h, p.second + w)) ==
qvisit.end()) {
q.push(make_pair(p.first + h, p.second + w));
qvisit.insert(make_pair(p.first + h, p.second + w));
}
}
}
}
}
}
for (int h = 0; h < H; ++h) {
for (int w = 0; w < W; ++w) {
cerr << map[h][w] << " ";
}
cerr << endl;
}
cout << map[dh][dw] << endl;
return 0;
} | #include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
int H, W;
cin >> H >> W;
int ch, dh, cw, dw;
cin >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
vector<string> s(H);
for (int i = 0; i < H; ++i) {
cin >> s[i];
}
vector<vector<int>> map(H, vector<int>(W, -1));
map[ch][cw] = 0;
queue<pair<int, int>> q;
queue<pair<int, int>> qq;
q.push(make_pair(ch, cw));
qq.push(make_pair(ch, cw));
while (!q.empty()) {
set<pair<int, int>> qqvisit;
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
if (qqvisit.find(p) == qqvisit.end()) {
qq.push(p);
}
int warp = map[p.first][p.second];
if (p.first + 1 < H && s[p.first + 1][p.second] == '.' &&
map[p.first + 1][p.second] == -1) {
map[p.first + 1][p.second] = warp;
q.push(make_pair(p.first + 1, p.second));
if (qqvisit.find(make_pair(p.first + 1, p.second)) == qqvisit.end()) {
qq.push(make_pair(p.first + 1, p.second));
qqvisit.insert(make_pair(p.first + 1, p.second));
}
}
if (p.first - 1 >= 0 && s[p.first - 1][p.second] == '.' &&
map[p.first - 1][p.second] == -1) {
map[p.first - 1][p.second] = warp;
q.push(make_pair(p.first - 1, p.second));
if (qqvisit.find(make_pair(p.first - 1, p.second)) == qqvisit.end()) {
qq.push(make_pair(p.first - 1, p.second));
qqvisit.insert(make_pair(p.first - 1, p.second));
}
}
if (p.second + 1 < W && s[p.first][p.second + 1] == '.' &&
map[p.first][p.second + 1] == -1) {
map[p.first][p.second + 1] = warp;
q.push(make_pair(p.first, p.second + 1));
if (qqvisit.find(make_pair(p.first, p.second + 1)) == qqvisit.end()) {
qq.push(make_pair(p.first, p.second + 1));
qqvisit.insert(make_pair(p.first, p.second + 1));
}
}
if (p.second - 1 >= 0 && s[p.first][p.second - 1] == '.' &&
map[p.first][p.second - 1] == -1) {
map[p.first][p.second - 1] = warp;
q.push(make_pair(p.first, p.second - 1));
if (qqvisit.find(make_pair(p.first, p.second - 1)) == qqvisit.end()) {
qq.push(make_pair(p.first, p.second - 1));
qqvisit.insert(make_pair(p.first, p.second - 1));
}
}
}
set<pair<int, int>> qvisit;
while (!qq.empty()) {
pair<int, int> p = qq.front();
qq.pop();
int warp = map[p.first][p.second] + 1;
for (int h = -2; h <= 2; ++h) {
for (int w = -2; w <= 2; ++w) {
if (p.first + h >= 0 && p.first + h < H && p.second + w >= 0 &&
p.second + w < W && s[p.first + h][p.second + w] == '.' &&
map[p.first + h][p.second + w] == -1) {
map[p.first + h][p.second + w] = warp;
if (qvisit.find(make_pair(p.first + h, p.second + w)) ==
qvisit.end()) {
q.push(make_pair(p.first + h, p.second + w));
qvisit.insert(make_pair(p.first + h, p.second + w));
}
}
}
}
}
}
cout << map[dh][dw] << endl;
return 0;
} | delete | 100 | 107 | 100 | 100 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
struct Edge;
struct Node {
ll cost;
bool isClosed;
vector<Edge *> edges;
Node *last;
Node() {}
};
struct NODE {
Node *node;
ll costWhenPushed;
bool operator<(const NODE &rhs) const {
return costWhenPushed > rhs.costWhenPushed;
} // 逆にする
NODE(Node *n, ll cost) {
node = n;
costWhenPushed = cost;
}
};
struct Edge {
Node *node0;
Node *node1;
ll cost;
};
vector<Node> Nodes;
vector<Edge> Edges;
ll Dijkstra_Find(Node *s, Node *g) {
// 初期化
for (int i = 0; i < Nodes.size(); i++) {
Nodes[i].cost = &Nodes[i] == s ? 0 : LLONG_MAX / 2;
Nodes[i].isClosed = false;
Nodes[i].last = nullptr;
}
priority_queue<NODE> que;
que.push(NODE(s, 0));
while (!que.empty()) {
Node *n = que.top().node;
ll costWhenPushed = que.top().costWhenPushed;
que.pop();
if (n->cost < costWhenPushed) {
continue;
}
n->isClosed = true;
int size = (n->edges).size();
for (int i = 0; i < size; i++) {
Node *processNode = (n->edges)[i]->node0 == n ? (n->edges)[i]->node1
: (n->edges)[i]->node0;
if (processNode->isClosed) {
continue;
}
ll newCost = n->cost + (n->edges)[i]->cost;
if (newCost < processNode->cost) {
processNode->cost = newCost;
processNode->last = n;
que.push(NODE(processNode, newCost));
}
}
}
return g->cost;
}
int main() {
int wall = 99999999;
int H, W;
int Ch, Cw, Dh, Dw;
cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
static int Mass[1002][1002];
for (int i = 0; i < 1002; i++) {
for (int j = 0; j < 1002; j++) {
Mass[i][j] = wall;
}
}
int count = 0;
for (int h = 1; h <= H; h++) {
string line;
cin >> line;
for (int w = 1; w <= W; w++) {
if (line[w - 1] == '.') {
count++;
Mass[h][w] = count;
}
}
}
for (int step = 0; step < 500; step++) {
for (int h = 1; h <= H; h++) {
for (int w = 1; w <= W; w++) {
if (Mass[h][w] != wall) {
Mass[h][w] = min(Mass[h][w], Mass[h - 1][w]);
Mass[h][w] = min(Mass[h][w], Mass[h][w - 1]);
Mass[h][w] = min(Mass[h][w], Mass[h + 1][w]);
Mass[h][w] = min(Mass[h][w], Mass[h][w + 1]);
}
}
}
}
static int Mass2[1010][1010];
for (int i = 0; i < 1010; i++) {
for (int j = 0; j < 1010; j++) {
Mass2[i][j] = wall;
}
}
set<int> counter;
map<int, int> cor;
for (int h = 1; h <= H; h++) {
for (int w = 1; w <= W; w++) {
if (Mass[h][w] != wall) {
if (counter.find(Mass[h][w]) == counter.end()) {
counter.insert(Mass[h][w]);
cor[Mass[h][w]] = counter.size();
}
Mass2[h][w] = cor[Mass[h][w]];
} else {
Mass2[h][w] = wall;
}
}
}
/*
for(int h = 1; h <= H; h++){
for(int w = 1; w <= W; w++){
cout << Mass2[h][w] << " ";
}
cout << endl;
}
*/
for (int i = 0; i < counter.size() + 2; i++) {
Node node;
Nodes.push_back(node);
}
for (int i = 0; i < 1000 * 1000; i++) {
Edge edge;
Edges.push_back(edge);
}
int edgeCount = 0;
set<pair<int, int>> check;
for (int h = 1; h <= H; h++) {
for (int w = 1; w <= W; w++) {
if (Mass2[h][w] == wall) {
continue;
}
Node *node1 = &Nodes[Mass2[h][w]];
for (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 2; y++) {
if (x != 0 || y != 0) {
if (h + x >= 0 && h + x <= H && w + y >= 0 && w + y <= W) {
// cout << h << " " << w << " "<< h+x << " " << w+y << endl;
// cout << Mass2[h][w] << endl;
if (Mass2[h + x][w + y] == wall) {
// pass
} else if (Mass2[h][w] != Mass2[h + x][w + y]) {
if (check.find(
make_pair(min(Mass2[h][w], Mass2[h + x][w + y]),
max(Mass2[h][w], Mass2[h + x][w + y]))) !=
check.end()) {
continue;
}
Node *node2 = &Nodes[Mass2[h + x][w + y]];
Edge *edge12 = &Edges[edgeCount];
edge12->node0 = node1;
edge12->node1 = node2;
edge12->cost = 1;
node1->edges.push_back(edge12);
node2->edges.push_back(edge12);
edgeCount++;
check.insert(make_pair(min(Mass2[h][w], Mass2[h + x][w + y]),
max(Mass2[h][w], Mass2[h + x][w + y])));
}
}
}
}
}
}
}
ll ans = Dijkstra_Find(&Nodes[Mass2[Ch][Cw]], &Nodes[Mass2[Dh][Dw]]);
if (ans > 100010001000) {
ans = -1;
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
struct Edge;
struct Node {
ll cost;
bool isClosed;
vector<Edge *> edges;
Node *last;
Node() {}
};
struct NODE {
Node *node;
ll costWhenPushed;
bool operator<(const NODE &rhs) const {
return costWhenPushed > rhs.costWhenPushed;
} // 逆にする
NODE(Node *n, ll cost) {
node = n;
costWhenPushed = cost;
}
};
struct Edge {
Node *node0;
Node *node1;
ll cost;
};
vector<Node> Nodes;
vector<Edge> Edges;
ll Dijkstra_Find(Node *s, Node *g) {
// 初期化
for (int i = 0; i < Nodes.size(); i++) {
Nodes[i].cost = &Nodes[i] == s ? 0 : LLONG_MAX / 2;
Nodes[i].isClosed = false;
Nodes[i].last = nullptr;
}
priority_queue<NODE> que;
que.push(NODE(s, 0));
while (!que.empty()) {
Node *n = que.top().node;
ll costWhenPushed = que.top().costWhenPushed;
que.pop();
if (n->cost < costWhenPushed) {
continue;
}
n->isClosed = true;
int size = (n->edges).size();
for (int i = 0; i < size; i++) {
Node *processNode = (n->edges)[i]->node0 == n ? (n->edges)[i]->node1
: (n->edges)[i]->node0;
if (processNode->isClosed) {
continue;
}
ll newCost = n->cost + (n->edges)[i]->cost;
if (newCost < processNode->cost) {
processNode->cost = newCost;
processNode->last = n;
que.push(NODE(processNode, newCost));
}
}
}
return g->cost;
}
int main() {
int wall = 99999999;
int H, W;
int Ch, Cw, Dh, Dw;
cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
static int Mass[1002][1002];
for (int i = 0; i < 1002; i++) {
for (int j = 0; j < 1002; j++) {
Mass[i][j] = wall;
}
}
int count = 0;
for (int h = 1; h <= H; h++) {
string line;
cin >> line;
for (int w = 1; w <= W; w++) {
if (line[w - 1] == '.') {
count++;
Mass[h][w] = count;
}
}
}
for (int step = 0; step < 50; step++) {
for (int h = 1; h <= H; h++) {
for (int w = 1; w <= W; w++) {
if (Mass[h][w] != wall) {
Mass[h][w] = min(Mass[h][w], Mass[h - 1][w]);
Mass[h][w] = min(Mass[h][w], Mass[h][w - 1]);
Mass[h][w] = min(Mass[h][w], Mass[h + 1][w]);
Mass[h][w] = min(Mass[h][w], Mass[h][w + 1]);
}
}
}
}
static int Mass2[1010][1010];
for (int i = 0; i < 1010; i++) {
for (int j = 0; j < 1010; j++) {
Mass2[i][j] = wall;
}
}
set<int> counter;
map<int, int> cor;
for (int h = 1; h <= H; h++) {
for (int w = 1; w <= W; w++) {
if (Mass[h][w] != wall) {
if (counter.find(Mass[h][w]) == counter.end()) {
counter.insert(Mass[h][w]);
cor[Mass[h][w]] = counter.size();
}
Mass2[h][w] = cor[Mass[h][w]];
} else {
Mass2[h][w] = wall;
}
}
}
/*
for(int h = 1; h <= H; h++){
for(int w = 1; w <= W; w++){
cout << Mass2[h][w] << " ";
}
cout << endl;
}
*/
for (int i = 0; i < counter.size() + 2; i++) {
Node node;
Nodes.push_back(node);
}
for (int i = 0; i < 1000 * 1000; i++) {
Edge edge;
Edges.push_back(edge);
}
int edgeCount = 0;
set<pair<int, int>> check;
for (int h = 1; h <= H; h++) {
for (int w = 1; w <= W; w++) {
if (Mass2[h][w] == wall) {
continue;
}
Node *node1 = &Nodes[Mass2[h][w]];
for (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 2; y++) {
if (x != 0 || y != 0) {
if (h + x >= 0 && h + x <= H && w + y >= 0 && w + y <= W) {
// cout << h << " " << w << " "<< h+x << " " << w+y << endl;
// cout << Mass2[h][w] << endl;
if (Mass2[h + x][w + y] == wall) {
// pass
} else if (Mass2[h][w] != Mass2[h + x][w + y]) {
if (check.find(
make_pair(min(Mass2[h][w], Mass2[h + x][w + y]),
max(Mass2[h][w], Mass2[h + x][w + y]))) !=
check.end()) {
continue;
}
Node *node2 = &Nodes[Mass2[h + x][w + y]];
Edge *edge12 = &Edges[edgeCount];
edge12->node0 = node1;
edge12->node1 = node2;
edge12->cost = 1;
node1->edges.push_back(edge12);
node2->edges.push_back(edge12);
edgeCount++;
check.insert(make_pair(min(Mass2[h][w], Mass2[h + x][w + y]),
max(Mass2[h][w], Mass2[h + x][w + y])));
}
}
}
}
}
}
}
ll ans = Dijkstra_Find(&Nodes[Mass2[Ch][Cw]], &Nodes[Mass2[Dh][Dw]]);
if (ans > 100010001000) {
ans = -1;
}
cout << ans << endl;
return 0;
}
| replace | 113 | 114 | 113 | 114 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int NIL = -1;
const int INF = (1 << 21);
const long long MOD = 1e9 + 7;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
int main() {
int h, w, sx, sy, gx, gy;
cin >> h >> w >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
vector<string> field(h);
for (int i = 0; i < h; ++i)
cin >> field[i];
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<pair<int, int>> q;
q.emplace_back(make_pair(sx, sy));
dist[sx][sy] = 0;
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop_front();
int x = p.first;
int y = p.second;
for (int i = 0; i < 4; ++i) {
int nx = x + di[i];
int ny = y + dj[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w)
continue;
if (field[nx][ny] == '#')
continue;
if (dist[nx][ny] <= dist[x][y])
continue;
dist[nx][ny] = dist[x][y];
q.emplace_front(make_pair(nx, ny));
}
for (int i = -2; i <= 2; ++i) {
for (int j = -2; j <= 2; ++j) {
int nx = x + i;
int ny = y + j;
if (nx < 0 || nx >= h || ny < 0 || ny >= w)
continue;
if (field[nx][ny] == '#')
continue;
if (dist[nx][ny] <= dist[x][y] + 1)
continue;
dist[nx][ny] = dist[x][y] + 1;
q.emplace_front(make_pair(nx, ny));
}
}
}
if (dist[gx][gy] == INF)
cout << -1 << endl;
else
cout << dist[gx][gy] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int NIL = -1;
const int INF = (1 << 21);
const long long MOD = 1e9 + 7;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
int main() {
int h, w, sx, sy, gx, gy;
cin >> h >> w >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
vector<string> field(h);
for (int i = 0; i < h; ++i)
cin >> field[i];
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<pair<int, int>> q;
q.emplace_back(make_pair(sx, sy));
dist[sx][sy] = 0;
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop_front();
int x = p.first;
int y = p.second;
for (int i = 0; i < 4; ++i) {
int nx = x + di[i];
int ny = y + dj[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w)
continue;
if (field[nx][ny] == '#')
continue;
if (dist[nx][ny] <= dist[x][y])
continue;
dist[nx][ny] = dist[x][y];
q.emplace_front(make_pair(nx, ny));
}
for (int i = -2; i <= 2; ++i) {
for (int j = -2; j <= 2; ++j) {
int nx = x + i;
int ny = y + j;
if (nx < 0 || nx >= h || ny < 0 || ny >= w)
continue;
if (field[nx][ny] == '#')
continue;
if (dist[nx][ny] <= dist[x][y] + 1)
continue;
dist[nx][ny] = dist[x][y] + 1;
q.emplace_back(make_pair(nx, ny));
}
}
}
if (dist[gx][gy] == INF)
cout << -1 << endl;
else
cout << dist[gx][gy] << endl;
}
| replace | 66 | 67 | 66 | 67 | TLE | |
p02579 | C++ | Time Limit Exceeded | #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) \
sort(all((x))); \
(x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int h, w;
cin >> h >> w;
int sx, sy, fx, fy;
cin >> sx >> sy >> fx >> fy;
sx--;
sy--;
fx--;
fy--;
vector<string> m(h);
for (int i = 0; i < h; i++)
cin >> m[i];
vector<vector<int>> cost(h, vector<int>(w, 1e9 + 42));
cost[sx][sy] = 0;
deque<pair<int, int>> dq;
dq.push_front(mp(sx, sy));
int movex[4] = {-1, 0, 0, 1}, movey[4] = {0, -1, 1, 0};
int x, y;
while (!dq.empty()) {
tie(x, y) = dq.front();
dq.pop_front();
for (int mm = 0; mm < 4; mm++) {
int cx = x + movex[mm], cy = y + movey[mm];
if (cx >= 0 && cx < h && cy >= 0 && cy < w && m[cx][cy] == '.' &&
cost[cx][cy] > cost[x][y]) {
cost[cx][cy] = cost[x][y];
dq.push_front(mp(cx, cy));
}
}
for (int xx = x - 2; xx <= x + 2; xx++)
for (int yy = y - 2; yy <= y + 2; yy++) {
if (xx >= 0 && xx < h && yy >= 0 && yy < w && m[xx][yy] == '.' &&
cost[xx][yy] > cost[x][y] + 1) {
cost[xx][yy] = cost[x][y] + 1;
dq.push_front(mp(xx, yy));
}
}
}
cout << (cost[fx][fy] >= 1e9 ? -1 : cost[fx][fy]);
return 0;
}
/*
*/
| #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) \
sort(all((x))); \
(x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int h, w;
cin >> h >> w;
int sx, sy, fx, fy;
cin >> sx >> sy >> fx >> fy;
sx--;
sy--;
fx--;
fy--;
vector<string> m(h);
for (int i = 0; i < h; i++)
cin >> m[i];
vector<vector<int>> cost(h, vector<int>(w, 1e9 + 42));
cost[sx][sy] = 0;
deque<pair<int, int>> dq;
dq.push_front(mp(sx, sy));
int movex[4] = {-1, 0, 0, 1}, movey[4] = {0, -1, 1, 0};
int x, y;
while (!dq.empty()) {
tie(x, y) = dq.front();
dq.pop_front();
for (int mm = 0; mm < 4; mm++) {
int cx = x + movex[mm], cy = y + movey[mm];
if (cx >= 0 && cx < h && cy >= 0 && cy < w && m[cx][cy] == '.' &&
cost[cx][cy] > cost[x][y]) {
cost[cx][cy] = cost[x][y];
dq.push_front(mp(cx, cy));
}
}
for (int xx = x - 2; xx <= x + 2; xx++)
for (int yy = y - 2; yy <= y + 2; yy++) {
if (xx >= 0 && xx < h && yy >= 0 && yy < w && m[xx][yy] == '.' &&
cost[xx][yy] > cost[x][y] + 1) {
cost[xx][yy] = cost[x][y] + 1;
dq.push_back(mp(xx, yy));
}
}
}
cout << (cost[fx][fy] >= 1e9 ? -1 : cost[fx][fy]);
return 0;
}
/*
*/
| replace | 84 | 85 | 84 | 85 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.