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
p02802
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ac = 0; long long wa = 0; vector<long long> wrong(n, 0); vector<bool> isac(n, 0); for (int i = 0; i < m; ++i) { int p; string s; cin >> p >> s; if (!isac[p]) { if (s == "AC") { isac[p] = true; wa += wrong[p]; ac++; } else { wrong[p]++; }; }; } cout << ac << " " << wa << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ac = 0; long long wa = 0; vector<long long> wrong(n, 0); vector<bool> isac(n, 0); for (int i = 0; i < m; ++i) { int p; string s; cin >> p >> s; --p; if (!isac[p]) { if (s == "AC") { isac[p] = true; wa += wrong[p]; ac++; } else { wrong[p]++; }; }; } cout << ac << " " << wa << endl; return 0; }
insert
16
16
16
17
0
p02802
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { static int wa[10000]; static bool ac[10000]; int n, m; cin >> n >> m; int p, a = 0, w = 0; string s; for (int i = 0; i < m; i++) { cin >> p >> s; if (ac[p]) continue; if (s == "WA") wa[p]++; else { ac[p] = true; a++; w += wa[p]; } } cout << a << " " << w << endl; }
#include <bits/stdc++.h> using namespace std; int main() { static int wa[100001]; static bool ac[100001]; int n, m; cin >> n >> m; int p, a = 0, w = 0; string s; for (int i = 0; i < m; i++) { cin >> p >> s; if (ac[p]) continue; if (s == "WA") wa[p]++; else { ac[p] = true; a++; w += wa[p]; } } cout << a << " " << w << endl; }
replace
5
7
5
7
0
p02802
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<ll>; using vii = vector<vi>; using Pll = pair<ll, ll>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(v) v.begin(), v.end() #define sz(x) ((int)x.size()) #define pb push_back #define mp make_pair #define mt make_tuple #define F first #define S second const int MOD = 1000000007; template <class T> void print(const T &t) { cout << t << endl; } 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 gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { ll n, m; cin >> n >> m; ll count_ac = 0; ll count_wa = 0; vi p(m); vector<string> M(m); rep(i, m) { cin >> p[i] >> M[i]; } for (ll i = 1; i <= n; i++) { ll count_wai = 0; rep(j, m) { if (p[j] == i && M[j] == "WA") { count_wai++; } else if (p[j] == i && M[j] == "AC") { count_ac++; count_wa += count_wai; break; } } } cout << count_ac << " " << count_wa << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<ll>; using vii = vector<vi>; using Pll = pair<ll, ll>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(v) v.begin(), v.end() #define sz(x) ((int)x.size()) #define pb push_back #define mp make_pair #define mt make_tuple #define F first #define S second const int MOD = 1000000007; template <class T> void print(const T &t) { cout << t << endl; } 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 gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { ll n, m; cin >> n >> m; ll count_ac = 0; ll count_wa = 0; vi p(m); vector<string> M(m); vector<bool> a(n, true); vi b(n, 0); rep(i, m) { cin >> p[i] >> M[i]; p[i]--; } rep(i, m) { if (a[p[i]] == true && M[i] == "WA") { b[p[i]]++; } else if (a[p[i]] == true && M[i] == "AC") { a[p[i]] = false; count_ac++; count_wa += b[p[i]]; } } cout << count_ac << " " << count_wa << endl; }
replace
39
50
39
52
TLE
p02802
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; int main() { ll n, m; cin >> n >> m; vector<ll> p(n + 10); vector<ll> wa(n + 10); ll pen = 0; ll ans = 0; rep(i, m) { ll prob; string res; cin >> prob >> res; if (p[prob] == 1) { } else { if (res == "WA") { wa[prob] += 1; } else { ans += 1; p[prob] = 1; } } } rep(i, m + 1) { if (p[i] == 1) { pen += wa[i]; } } cout << ans << " " << pen << 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; int main() { ll n, m; cin >> n >> m; vector<ll> p(n + 10); vector<ll> wa(n + 10); ll pen = 0; ll ans = 0; rep(i, m) { ll prob; string res; cin >> prob >> res; if (p[prob] == 1) { } else { if (res == "WA") { wa[prob] += 1; } else { ans += 1; p[prob] = 1; } } } rep(i, n + 1) { if (p[i] == 1) { pen += wa[i]; } } cout << ans << " " << pen << endl; return 0; }
replace
26
27
26
27
0
p02802
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<bool> vb; #define pb push_back #define mp make_pair #define f first #define s second #define in insert #define FOR(i, a, b) for (int i = a; i < b; i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define speed \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); const int INF = 1e9 + 5; const int N = 2e5 + 314; void foo() { int n, m; cin >> n >> m; vi v(n + 1); vi cnt(n + 1); int ok = 0, wa = 0; for (int i = 0; i < m; i++) { int x; string c; cin >> x >> c; if (c == "AC") v[x] = 1; else { if (!v[i]) cnt[x]++; } } for (int i = 1; i <= n; i++) { if (v[i]) { ok++; wa += cnt[i]; } } cout << ok << " " << wa; } int main() { speed; int t = 1; // cin>>t; while (t--) foo(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<bool> vb; #define pb push_back #define mp make_pair #define f first #define s second #define in insert #define FOR(i, a, b) for (int i = a; i < b; i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define speed \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); const int INF = 1e9 + 5; const int N = 2e5 + 314; void foo() { int n, m; cin >> n >> m; vi v(n + 1); vi cnt(n + 1); int ok = 0, wa = 0; for (int i = 0; i < m; i++) { int x; string c; cin >> x >> c; if (c == "AC") v[x] = 1; else { if (!v[x]) cnt[x]++; } } for (int i = 1; i <= n; i++) { if (v[i]) { ok++; wa += cnt[i]; } } cout << ok << " " << wa; } int main() { speed; int t = 1; // cin>>t; while (t--) foo(); }
replace
33
34
33
34
0
p02802
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N, M, AC = 0, WA = 0; cin >> N >> M; vector<int> p(M), wa(M, 0); vector<string> S(M); vector<bool> flag(105000, false); for (int i = 0; i < M; ++i) { cin >> p.at(i) >> S.at(i); if (!flag.at(p.at(i))) { if (S.at(i) == "WA") { wa.at(p.at(i))++; } else if (S.at(i) == "AC") { ++AC; WA += wa.at(p.at(i)); flag.at(p.at(i)) = true; } } } cout << AC << " " << WA << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, M, AC = 0, WA = 0; cin >> N >> M; vector<int> p(M), wa(105000, 0); vector<string> S(M); vector<bool> flag(105000, false); for (int i = 0; i < M; ++i) { cin >> p.at(i) >> S.at(i); if (!flag.at(p.at(i))) { if (S.at(i) == "WA") { wa.at(p.at(i))++; } else if (S.at(i) == "AC") { ++AC; WA += wa.at(p.at(i)); flag.at(p.at(i)) = true; } } } cout << AC << " " << WA << endl; }
replace
6
7
6
7
0
p02802
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m, wans = 0, ans = 0; cin >> n >> m; vector<int> p(m), ac(n, 0), wa(n, 0); vector<string> s(m); rep(i, m) { cin >> p.at(i) >> s.at(i); if (ac.at(p.at(i) - 1) == 0) { if (s.at(i) == "WA") { wans++; wa.at(p.at(i))++; } if (s.at(i) == "AC") { ans++; ac.at(p.at(i) - 1)++; } } } rep(i, n) { if (ac.at(i) == 0) { wans -= wa.at(i); } } cout << ans << ' ' << wans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m, wans = 0, ans = 0; cin >> n >> m; vector<int> p(m), ac(n, 0), wa(n, 0); vector<string> s(m); rep(i, m) { cin >> p.at(i) >> s.at(i); if (ac.at(p.at(i) - 1) == 0) { if (s.at(i) == "WA") { wans++; wa.at(p.at(i) - 1)++; } if (s.at(i) == "AC") { ans++; ac.at(p.at(i) - 1)++; } } } rep(i, n) { if (ac.at(i) == 0) { wans -= wa.at(i); } } cout << ans << ' ' << wans << endl; return 0; }
replace
17
18
17
18
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 2) >= this->size() (which is 2)
p02802
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m, p, countWA = 0, countAC = 0; string s; cin >> n >> m; std::deque<int> deq(n + 1, 0); std::vector<vector<int>> vec(n, vector<int>(1, 0)); for (int i = 0; i < m; i++) { cin >> p >> s; if (deq[p] == 0 && s == "AC") { countAC++; countWA += vec[p][0]; deq[p] = 1; } else if (deq[p] == 0 && s == "WA") { vec[p][0]++; } } cout << countAC << " " << countWA << endl; }
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m, p, countWA = 0, countAC = 0; string s; cin >> n >> m; std::deque<int> deq(n + 1, 0); std::vector<vector<int>> vec(n + 1, vector<int>(1, 0)); for (int i = 0; i < m; i++) { cin >> p >> s; if (deq[p] == 0 && s == "AC") { countAC++; countWA += vec[p][0]; deq[p] = 1; } else if (deq[p] == 0 && s == "WA") { vec[p][0]++; } } cout << countAC << " " << countWA << endl; }
replace
7
8
7
8
-11
p02802
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main(void) { int n, m; cin >> n >> m; int ac = 0, wa = 0; vector<bool> AC(n); vector<int> WA(0); for (int i = 0; i < m; i++) { int p; string s; cin >> p >> s; p--; if (AC[p]) continue; if (s == "AC") { ac++; AC[p] = true; wa += WA[p]; } else { WA[p]++; } } cout << ac << " " << wa << endl; return 0; }
#include <iostream> #include <vector> using namespace std; int main(void) { int n, m; cin >> n >> m; int ac = 0, wa = 0; vector<bool> AC(n); vector<int> WA(n, 0); for (int i = 0; i < m; i++) { int p; string s; cin >> p >> s; p--; if (AC[p]) continue; if (s == "AC") { ac++; AC[p] = true; wa += WA[p]; } else { WA[p]++; } } cout << ac << " " << wa << endl; return 0; }
replace
8
9
8
9
-11
p02802
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int n, m; cin >> n >> m; vector<int> a(m); vector<string> b(m); int i; for (i = 0; i < m; i++) { cin >> a.at(i) >> b.at(i); } int count_1 = 0, count_2 = 0; vector<pair<bool, int>> is(n); rep(i, m) { if (b.at(i) == "AC") { is.at(a.at(i) - 1).first = true; } if (b.at(i) == "WA" && is.at(a.at(i) - 1).first == false) { is.at(a.at(i) - 1).second++; } } rep(i, n) { if (is.at(i).first) { count_1++; count_2 += is.at(a.at(i) - 1).second; } } cout << count_1 << " " << count_2; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int n, m; cin >> n >> m; vector<int> a(m); vector<string> b(m); int i; for (i = 0; i < m; i++) { cin >> a.at(i) >> b.at(i); } int count_1 = 0, count_2 = 0; vector<pair<bool, int>> is(n); rep(i, m) { if (b.at(i) == "AC") { is.at(a.at(i) - 1).first = true; } if (b.at(i) == "WA" && is.at(a.at(i) - 1).first == false) { is.at(a.at(i) - 1).second++; } } rep(i, n) { if (is.at(i).first) { count_1++; count_2 += is.at(i).second; } } cout << count_1 << " " << count_2; }
replace
25
26
25
26
0
p02802
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (long long i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef pair<ll, ll> P; const int INF = 1e9; const int MOD = 1000000007; int main() { ll n, m; cin >> n >> m; vector<ll> p(m); vector<string> s(m); rep(i, m) cin >> p[i] >> s[i]; vector<ll> num(m); ll sum1 = 0; ll sum2 = 0; rep(i, m) { if (s[i] == "AC") { if (num[p[i] - 1] >= 0) { sum2 += num[p[i] - 1]; num[p[i] - 1] = -1; sum1++; } } else { if (num[p[i] - 1] >= 0) num[p[i] - 1]++; } } cout << sum1 << " " << sum2 << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (long long i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef pair<ll, ll> P; const int INF = 1e9; const int MOD = 1000000007; int main() { ll n, m; cin >> n >> m; vector<ll> p(m); vector<string> s(m); rep(i, m) cin >> p[i] >> s[i]; vector<ll> num(n); ll sum1 = 0; ll sum2 = 0; rep(i, m) { if (s[i] == "AC") { if (num[p[i] - 1] >= 0) { sum2 += num[p[i] - 1]; num[p[i] - 1] = -1; sum1++; } } else { if (num[p[i] - 1] >= 0) num[p[i] - 1]++; } } cout << sum1 << " " << sum2 << endl; }
replace
14
15
14
15
0
p02802
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, x, N) for (long long i = x; i < (N); i++) using namespace std; int main() { long long N, M; cin >> N >> M; vector<long long> a(M); vector<string> b(M); vector<pair<long long, string>> p(M); rep(i, 0, M) { cin >> a[i] >> b[i]; p[i] = make_pair(a[i], b[i]); } vector<long long> flag(N), flag2(N); rep(i, 0, N) { flag[i] = 0; flag2[i] = 0; } rep(i, 0, M) { if (p[i].second == "AC") { flag2[p[i].first] = 1; } } long long count_ac = 0, count_wa = 0; rep(i, 0, M) { if (p[i].second == "AC" && flag[p[i].first] == 0) { flag[p[i].first] = 1; count_ac++; } else if (p[i].second == "WA" && flag[p[i].first] == 0 && flag2[p[i].first] == 1) { count_wa++; } } cout << count_ac << ' ' << count_wa << endl; }
#include <bits/stdc++.h> #define rep(i, x, N) for (long long i = x; i < (N); i++) using namespace std; int main() { long long N, M; cin >> N >> M; vector<long long> a(M); vector<string> b(M); vector<pair<long long, string>> p(M); rep(i, 0, M) { cin >> a[i] >> b[i]; p[i] = make_pair(a[i], b[i]); } long long H; H = max(N, M); vector<long long> flag(H), flag2(H); if (M > N) { rep(i, 0, M) { flag[i] = 0; flag2[i] = 0; } } else { rep(i, 0, N) { flag[i] = 0; flag2[i] = 0; } } rep(i, 0, M) { if (p[i].second == "AC") { flag2[p[i].first] = 1; } } long long count_ac = 0, count_wa = 0; rep(i, 0, M) { if (p[i].second == "AC" && flag[p[i].first] == 0) { flag[p[i].first] = 1; count_ac++; } else if (p[i].second == "WA" && flag[p[i].first] == 0 && flag2[p[i].first] == 1) { count_wa++; } } cout << count_ac << ' ' << count_wa << endl; }
replace
13
17
13
27
0
p02802
C++
Runtime Error
#include <iostream> using namespace std; int p[10007] = {}; bool ac[10007] = {}; int main() { int n, m, a; cin >> n >> m; string s; int pen = 0, corr = 0; for (int i = 0; i < m; i++) { cin >> a >> s; if (s == "AC") { if (!ac[a]) { pen += p[a]; ac[a] = 1; corr++; } } else { p[a]++; } } cout << corr << ' ' << pen << '\n'; }
#include <iostream> using namespace std; int p[100007] = {}; bool ac[100007] = {}; int main() { int n, m, a; cin >> n >> m; string s; int pen = 0, corr = 0; for (int i = 0; i < m; i++) { cin >> a >> s; if (s == "AC") { if (!ac[a]) { pen += p[a]; ac[a] = 1; corr++; } } else { p[a]++; } } cout << corr << ' ' << pen << '\n'; }
replace
3
5
3
5
0
p02802
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; //---------------------------------------------------------------------------------------------------------------------- typedef long long ll; typedef pair<int, int> pii; typedef pair<long long, long long> pll; #define mp make_pair #define pb push_back #define FOR(x, to) for (x = 0; x < (to); x++) #define FORR(x, arr) for (auto &x : arr) #define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++) #define ALL(a) (a.begin()), (a.end()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) //---------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------- template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ": " << p.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; for (int i = 0; i < (int)v.size(); i++) { if (i) os << ", "; os << v[i]; } os << "}"; return os; } // #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 //---------------------------------------------------------------------------------------------------------------------- int main() { fastio; long n, m; cin >> n >> m; vector<bool> ac(n, false); vector<long> pen(n, 0); string v; long p; long ca = 0; long wa = 0; for (long i = 0; i < m; i++) { cin >> p >> v; if (ac[p]) continue; if (v == "WA") pen[p]++; else { wa += pen[p]; ca++; ac[p] = true; } } cout << ca << " " << wa; return 0; }
#include <bits/stdc++.h> using namespace std; //---------------------------------------------------------------------------------------------------------------------- typedef long long ll; typedef pair<int, int> pii; typedef pair<long long, long long> pll; #define mp make_pair #define pb push_back #define FOR(x, to) for (x = 0; x < (to); x++) #define FORR(x, arr) for (auto &x : arr) #define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++) #define ALL(a) (a.begin()), (a.end()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) //---------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------- template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ": " << p.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; for (int i = 0; i < (int)v.size(); i++) { if (i) os << ", "; os << v[i]; } os << "}"; return os; } // #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 //---------------------------------------------------------------------------------------------------------------------- int main() { fastio; long n, m; cin >> n >> m; vector<bool> ac(n + 1, false); vector<long> pen(n + 1, 0); string v; long p; long ca = 0; long wa = 0; for (long i = 0; i < m; i++) { cin >> p >> v; if (ac[p]) continue; if (v == "WA") pen[p]++; else { wa += pen[p]; ca++; ac[p] = true; } } cout << ca << " " << wa; return 0; }
replace
61
63
61
63
0
p02802
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define fo(i, s, e) for (int i = (s); i < (e); i++) #define all(obj) (obj).begin(), (obj).end() /*卍卍卍 #define int long long 卍卍卍*/ signed main() { int n, m; cin >> n >> m; vector<vector<int>> acwa_pena(n, vector<int>(2, 0)); rep(i, m) { int no; string acwa; cin >> no >> acwa; if (acwa == "AC") acwa_pena[no][0] = 1; else if (acwa_pena[no][0] == 0) acwa_pena[no][1] += 1; } int ac = 0; int pena = 0; rep(i, n) { ac += acwa_pena[i][0]; if (acwa_pena[i][0]) pena += acwa_pena[i][1]; } cout << ac << " " << pena << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define fo(i, s, e) for (int i = (s); i < (e); i++) #define all(obj) (obj).begin(), (obj).end() /*卍卍卍 #define int long long 卍卍卍*/ signed main() { int n, m; cin >> n >> m; vector<vector<int>> acwa_pena(n, vector<int>(2, 0)); rep(i, m) { int no; string acwa; cin >> no >> acwa; if (acwa == "AC") acwa_pena[no - 1][0] = 1; else if (acwa_pena[no - 1][0] == 0) acwa_pena[no - 1][1] += 1; } int ac = 0; int pena = 0; rep(i, n) { ac += acwa_pena[i][0]; if (acwa_pena[i][0]) pena += acwa_pena[i][1]; } cout << ac << " " << pena << endl; }
replace
15
18
15
18
-11
p02802
C++
Runtime Error
#include <iostream> #include <string> #include <vector> using namespace std; int main() { int N, M; cin >> N >> M; int na = 0; vector<bool> V(N, false); vector<int> np(N, 0); for (int i = 0; i < M; ++i) { int p; string s; cin >> p >> s; --p; if (V[p]) continue; if (s == "AC") { V[p] = true; na++; } else { np[p]++; } } int sum = 0; for (int i = 0; i < M; ++i) { sum += np[i] * V[i]; } cout << na << ' ' << sum << endl; return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int main() { int N, M; cin >> N >> M; int na = 0; vector<bool> V(N, false); vector<int> np(N, 0); for (int i = 0; i < M; ++i) { int p; string s; cin >> p >> s; --p; if (V[p]) continue; if (s == "AC") { V[p] = true; na++; } else { np[p]++; } } int sum = 0; for (int i = 0; i < N; ++i) { sum += np[i] * V[i]; } cout << na << ' ' << sum << endl; return 0; }
replace
29
30
29
30
0
p02802
C++
Runtime Error
#include <cstring> #include <iostream> using namespace std; int main() { int penalties[10001] = {}, rez_w = 0, rez_a = 0, n, m, p; char ch[3]; cin >> n >> m; for (int i = 1; i <= m; ++i) { cin >> p >> ch; if (strcmp(ch, "WA") == 0 && penalties[p] != -1) penalties[p]++; else { if (penalties[p] != -1) { rez_w += penalties[p]; rez_a++; penalties[p] = -1; } } } cout << rez_a << ' ' << rez_w; return 0; }
#include <cstring> #include <iostream> using namespace std; int main() { int penalties[100001] = {}, rez_w = 0, rez_a = 0, n, m, p; char ch[3]; cin >> n >> m; for (int i = 1; i <= m; ++i) { cin >> p >> ch; if (strcmp(ch, "WA") == 0 && penalties[p] != -1) penalties[p]++; else { if (penalties[p] != -1) { rez_w += penalties[p]; rez_a++; penalties[p] = -1; } } } cout << rez_a << ' ' << rez_w; return 0; }
replace
5
6
5
6
0
p02802
C++
Runtime Error
#include <bits/stdc++.h> #include <numeric> using namespace std; using ll = long long; using pint = pair<int, int>; #define INF 99999999 #define rep(i, n) for (int(i) = 0, temp = (int)(n); (i) < temp; ++(i)) #define repi(i, start, end) \ for (int(i) = (start), temp = (int)(end); i < (end); ++(i)) #define rfor(v, x) for (const auto &(x) : (v)) // xは値を表す #define all(x) (x).begin(), (x).end() #define SORT(v, n) sort((v), (v) + (n)) #define vsort(v) sort((v).begin(), (v).end()) #define vfsort(v, lambda) sort(all((v)), (lambda)) #define vint vector<int> #define vvint vector<vector<int>> // vvint v(n,vint(n))のように宣言できる #define vin(v) \ rep(i, (v).size()) { cin >> (v)[i]; } // 最終奥義(使うときはINFも書き換えろ!!) // #define int long long 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 gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int ctoi(char c) { return c - '0'; } // lambda式 -> [&](int x, int y){return x<y;} // vector内の重複を削除-> v.erase(unique(all(v)),v.end()); signed main(void) { int n, m; cin >> n >> m; vector<pint> ps(m); vint sum(n, 0); vint wa(n, 0); rep(i, m) { int p; string s; cin >> p >> s; p--; if (sum[p] > 0) { continue; } else if (s == "WA") { wa[p]++; } else { sum[p]++; } } rep(i, m) if (sum[i] == 0) wa[i] = 0; cout << accumulate(all(sum), 0) << " " << accumulate(all(wa), 0) << endl; }
#include <bits/stdc++.h> #include <numeric> using namespace std; using ll = long long; using pint = pair<int, int>; #define INF 99999999 #define rep(i, n) for (int(i) = 0, temp = (int)(n); (i) < temp; ++(i)) #define repi(i, start, end) \ for (int(i) = (start), temp = (int)(end); i < (end); ++(i)) #define rfor(v, x) for (const auto &(x) : (v)) // xは値を表す #define all(x) (x).begin(), (x).end() #define SORT(v, n) sort((v), (v) + (n)) #define vsort(v) sort((v).begin(), (v).end()) #define vfsort(v, lambda) sort(all((v)), (lambda)) #define vint vector<int> #define vvint vector<vector<int>> // vvint v(n,vint(n))のように宣言できる #define vin(v) \ rep(i, (v).size()) { cin >> (v)[i]; } // 最終奥義(使うときはINFも書き換えろ!!) // #define int long long 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 gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int ctoi(char c) { return c - '0'; } // lambda式 -> [&](int x, int y){return x<y;} // vector内の重複を削除-> v.erase(unique(all(v)),v.end()); signed main(void) { int n, m; cin >> n >> m; vector<pint> ps(m); vint sum(n, 0); vint wa(n, 0); rep(i, m) { int p; string s; cin >> p >> s; p--; if (sum[p] > 0) { continue; } else if (s == "WA") { wa[p]++; } else { sum[p]++; } } rep(i, n) if (sum[i] == 0) wa[i] = 0; cout << accumulate(all(sum), 0) << " " << accumulate(all(wa), 0) << endl; }
replace
60
61
60
61
0
p02802
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template <typename T> bool mmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool mmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } #define dbg(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; int main() { lint n, m, p; cin >> n >> m; vector<lint> ac(n, 0), wa(n, 0); string s; REP(i, m) { cin >> p >> s; if (s == "AC") { ac[p] = 1; } else { if (ac[p] != 1) { wa[p]++; } } } lint a = 0, w = 0; REP(i, n) { a += ac[i]; if (ac[i] == 1) w += wa[i]; } cout << a << " " << w; }
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template <typename T> bool mmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool mmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } #define dbg(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; int main() { lint n, m, p; cin >> n >> m; vector<lint> ac(n, 0), wa(n, 0); string s; REP(i, m) { cin >> p >> s; p--; if (s == "AC") { ac[p] = 1; } else { if (ac[p] != 1) { wa[p]++; } } } lint a = 0, w = 0; REP(i, n) { a += ac[i]; if (ac[i] == 1) w += wa[i]; } cout << a << " " << w; }
insert
130
130
130
131
0
p02802
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<bool> y(n, true); vector<int> x(m, 0); int ac = 0, pen = 0; for (int i = 0; i < m; ++i) { int p; string s; cin >> p >> s; --p; if (y[p]) { if (s == "WA") { ++x[p]; } else { ++ac; y[p] = false; pen += x[p]; } } } cout << ac << " " << pen << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<bool> y(n, true); vector<int> x(n, 0); int ac = 0, pen = 0; for (int i = 0; i < m; ++i) { int p; string s; cin >> p >> s; --p; if (y[p]) { if (s == "WA") { ++x[p]; } else { ++ac; y[p] = false; pen += x[p]; } } } cout << ac << " " << pen << '\n'; return 0; }
replace
12
13
12
13
0
p02802
C++
Runtime Error
#include <iostream> #include <string> #include <vector> int main() { long long N, M; std::cin >> N >> M; std::vector<bool> flag(N, false); std::vector<long long> wa(N, 0); long long ac = 0; for (long long i = 0; i < M; i++) { long long p; std::string s; std::cin >> p >> s; if (s == "AC" && !flag[p]) { flag[p] = true; ac++; } else if (s == "WA" && !flag[p]) { wa[p]++; } } long long w = 0; for (long long i = 0; i < N; i++) { w += flag[i] ? wa[i] : 0; } std::cout << ac << " " << w << std::endl; return 0; }
#include <iostream> #include <string> #include <vector> int main() { long long N, M; std::cin >> N >> M; std::vector<bool> flag(N, false); std::vector<long long> wa(N, 0); long long ac = 0; for (long long i = 0; i < M; i++) { long long p; std::string s; std::cin >> p >> s; p--; if (s == "AC" && !flag[p]) { flag[p] = true; ac++; } else if (s == "WA" && !flag[p]) { wa[p]++; } } long long w = 0; for (long long i = 0; i < N; i++) { w += flag[i] ? wa[i] : 0; } std::cout << ac << " " << w << std::endl; return 0; }
insert
14
14
14
15
0
p02803
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; int N, M, ans; int main() { cin >> N >> M; vector<vector<int>> maze(N, vector<int>(M, 1)); for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { char a; cin >> a; if (a == '.') maze[i][j] = 0; } vector<vector<int>> G(N * M, vector<int>(0)); for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { if (i > 0 && maze[i][j] == 0 && maze[i - 1][j] == 0) { G[i * M + j].push_back((i - 1) * M + j); G[(i - 1) * M + j].push_back(i * M + j); } if (j > 0 && maze[i][j] == 0 && maze[i][j - 1] == 0) { G[i * N + j].push_back(i * N + j - 1); G[i * N + (j - 1)].push_back(i * N + j); } } ans = 0; for (int i = 0; i < N * M; i++) { if (G[i].size() == 0) continue; vector<int> dist(N * M, -1); queue<int> que; dist[i] = 0; que.push(i); while (!que.empty()) { int v = que.front(); que.pop(); for (int nv : G[v]) { if (dist[nv] != -1) continue; dist[nv] = dist[v] + 1; que.push(nv); } } sort(dist.begin(), dist.end()); if (dist[N * M - 1] > ans) ans = dist[N * M - 1]; } cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; int N, M, ans; int main() { cin >> N >> M; vector<vector<int>> maze(N, vector<int>(M, 1)); for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { char a; cin >> a; if (a == '.') maze[i][j] = 0; } vector<vector<int>> G(N * M, vector<int>(0)); for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { if (i > 0 && maze[i][j] == 0 && maze[i - 1][j] == 0) { G[i * M + j].push_back((i - 1) * M + j); G[(i - 1) * M + j].push_back(i * M + j); } if (j > 0 && maze[i][j] == 0 && maze[i][j - 1] == 0) { G[i * M + j].push_back(i * M + j - 1); G[i * M + (j - 1)].push_back(i * M + j); } } ans = 0; for (int i = 0; i < N * M; i++) { if (G[i].size() == 0) continue; vector<int> dist(N * M, -1); queue<int> que; dist[i] = 0; que.push(i); while (!que.empty()) { int v = que.front(); que.pop(); for (int nv : G[v]) { if (dist[nv] != -1) continue; dist[nv] = dist[v] + 1; que.push(nv); } } sort(dist.begin(), dist.end()); if (dist[N * M - 1] > ans) ans = dist[N * M - 1]; } cout << ans << endl; return 0; }
replace
27
29
27
29
0
p02803
C++
Time Limit Exceeded
/* Mbak Sana Dong Mbak Chaeyoung Dong */ #include <bits/stdc++.h> #define ll long long int using namespace std; int r, c; char grid[202][202]; int dist[22][22]; int main() { int n, i, j, k, x, y, ans, curr, temp, m; cin >> r >> c; getchar(); vector<pair<int, int>> vi; for (i = 1; i <= r; i++) { for (j = 1; j <= c; j++) { scanf("%c", &grid[i][j]); vi.push_back({i, j}); } getchar(); } int sz = vi.size(); int chaeng = 0; for (i = 0; i < sz; i++) { x = vi[i].first; y = vi[i].second; if (grid[x][y] == '#') continue; for (j = i + 1; j < sz; j++) { int endx = vi[j].first; int endy = vi[j].second; if (grid[endx][endy] == '#') continue; for (int a = 1; a <= r; a++) for (int b = 1; b <= c; b++) dist[a][b] = 1e9; set<pair<int, pair<int, int>>> s; s.insert({0, {x, y}}); dist[x][y] = 0; while (!s.empty()) { int now = (*s.begin()).first; int xx = (*s.begin()).second.first; int yy = (*s.begin()).second.second; s.erase(s.begin()); if (xx + 1 <= r) { if (grid[xx + 1][yy] == '.') { if (dist[xx + 1][yy] > now + 1) { dist[xx + 1][yy] = now + 1; s.insert({now + 1, {xx + 1, yy}}); } } } if (xx - 1 > 0) { if (grid[xx - 1][yy] == '.') { if (dist[xx - 1][yy] > now + 1) { dist[xx - 1][yy] = now + 1; s.insert({now + 1, {xx - 1, yy}}); } } } if (yy + 1 <= c) { if (grid[xx][yy + 1] == '.') { if (dist[xx][yy + 1] > now + 1) { dist[xx][yy + 1] = now + 1; s.insert({now + 1, {xx, yy + 1}}); } } } if (yy - 1 > 0) { if (grid[xx][yy - 1] == '.') { if (dist[xx][yy - 1] > now + 1) { dist[xx][yy - 1] = now + 1; s.insert({now + 1, {xx, yy - 1}}); } } } } if (dist[endx][endy] != 1e9) { chaeng = max(chaeng, dist[endx][endy]); } } } cout << chaeng << endl; return 0; }
/* Mbak Sana Dong Mbak Chaeyoung Dong */ #include <bits/stdc++.h> #define ll long long int using namespace std; int r, c; char grid[202][202]; int dist[22][22]; int main() { int n, i, j, k, x, y, ans, curr, temp, m; cin >> r >> c; getchar(); vector<pair<int, int>> vi; for (i = 1; i <= r; i++) { for (j = 1; j <= c; j++) { scanf("%c", &grid[i][j]); vi.push_back({i, j}); } getchar(); } int sz = vi.size(); int chaeng = 0; for (i = 0; i < sz; i++) { x = vi[i].first; y = vi[i].second; if (grid[x][y] == '#') continue; for (j = i + 1; j < sz; j++) { int endx = vi[j].first; int endy = vi[j].second; if (grid[endx][endy] == '#') continue; for (int a = 1; a <= r; a++) for (int b = 1; b <= c; b++) dist[a][b] = 1e9; set<pair<int, pair<int, int>>> s; s.insert({0, {x, y}}); dist[x][y] = 0; while (!s.empty()) { if (dist[endx][endy] < 1e9) break; int now = (*s.begin()).first; int xx = (*s.begin()).second.first; int yy = (*s.begin()).second.second; s.erase(s.begin()); if (xx + 1 <= r) { if (grid[xx + 1][yy] == '.') { if (dist[xx + 1][yy] > now + 1) { dist[xx + 1][yy] = now + 1; s.insert({now + 1, {xx + 1, yy}}); } } } if (xx - 1 > 0) { if (grid[xx - 1][yy] == '.') { if (dist[xx - 1][yy] > now + 1) { dist[xx - 1][yy] = now + 1; s.insert({now + 1, {xx - 1, yy}}); } } } if (yy + 1 <= c) { if (grid[xx][yy + 1] == '.') { if (dist[xx][yy + 1] > now + 1) { dist[xx][yy + 1] = now + 1; s.insert({now + 1, {xx, yy + 1}}); } } } if (yy - 1 > 0) { if (grid[xx][yy - 1] == '.') { if (dist[xx][yy - 1] > now + 1) { dist[xx][yy - 1] = now + 1; s.insert({now + 1, {xx, yy - 1}}); } } } } if (dist[endx][endy] != 1e9) { chaeng = max(chaeng, dist[endx][endy]); } } } cout << chaeng << endl; return 0; }
insert
41
41
41
43
TLE
p02803
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <set> #define int long long #define iris 1000000007 using namespace std; char arr[32][32]; int dis[32][32]; int id[32][32]; signed main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, i, j, k; cin >> n >> m; k = 0; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) id[i][j] = k++; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) cin >> arr[i][j]; for (i = 0; i < k; i++) for (j = 0; j < k; j++) if (i != j) dis[i][j] = dis[j][i] = 9999; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (i + 1 <= n) dis[id[i][j]][id[i + 1][j]] = dis[id[i + 1][j]][id[i][j]] = 1; if (i - 1 >= 1) dis[id[i][j]][id[i - 1][j]] = dis[id[i - 1][j]][id[i][j]] = 1; if (j + 1 <= m) dis[id[i][j]][id[i][j + 1]] = dis[id[i][j + 1]][id[i][j]] = 1; if (j - 1 >= 1) dis[id[i][j]][id[i][j - 1]] = dis[id[i][j - 1]][id[i][j]] = 1; } } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (arr[i][j] == '#') { if (i + 1 <= n) dis[id[i][j]][id[i + 1][j]] = dis[id[i + 1][j]][id[i][j]] = 9999; if (i - 1 >= 1) dis[id[i][j]][id[i - 1][j]] = dis[id[i - 1][j]][id[i][j]] = 9999; if (j + 1 <= m) dis[id[i][j]][id[i][j + 1]] = dis[id[i][j + 1]][id[i][j]] = 9999; if (j - 1 >= 1) dis[id[i][j]][id[i][j - 1]] = dis[id[i][j - 1]][id[i][j]] = 9999; } } } for (int ouo = 0; ouo < k; ouo++) { for (i = 0; i < k; i++) { for (j = 0; j < k; j++) { dis[i][j] = min(dis[i][j], dis[i][ouo] + dis[ouo][j]); } } } int ans = 0; for (i = 0; i < k; i++) { for (j = 0; j < k; j++) { if (dis[i][j] < 9999) { ans = max(ans, dis[i][j]); } } } cout << ans << '\n'; return 0; }
#include <algorithm> #include <iostream> #include <map> #include <set> #define int long long #define iris 1000000007 using namespace std; char arr[32][32]; int dis[1024][1024]; int id[32][32]; signed main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, i, j, k; cin >> n >> m; k = 0; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) id[i][j] = k++; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) cin >> arr[i][j]; for (i = 0; i < k; i++) for (j = 0; j < k; j++) if (i != j) dis[i][j] = dis[j][i] = 9999; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (i + 1 <= n) dis[id[i][j]][id[i + 1][j]] = dis[id[i + 1][j]][id[i][j]] = 1; if (i - 1 >= 1) dis[id[i][j]][id[i - 1][j]] = dis[id[i - 1][j]][id[i][j]] = 1; if (j + 1 <= m) dis[id[i][j]][id[i][j + 1]] = dis[id[i][j + 1]][id[i][j]] = 1; if (j - 1 >= 1) dis[id[i][j]][id[i][j - 1]] = dis[id[i][j - 1]][id[i][j]] = 1; } } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (arr[i][j] == '#') { if (i + 1 <= n) dis[id[i][j]][id[i + 1][j]] = dis[id[i + 1][j]][id[i][j]] = 9999; if (i - 1 >= 1) dis[id[i][j]][id[i - 1][j]] = dis[id[i - 1][j]][id[i][j]] = 9999; if (j + 1 <= m) dis[id[i][j]][id[i][j + 1]] = dis[id[i][j + 1]][id[i][j]] = 9999; if (j - 1 >= 1) dis[id[i][j]][id[i][j - 1]] = dis[id[i][j - 1]][id[i][j]] = 9999; } } } for (int ouo = 0; ouo < k; ouo++) { for (i = 0; i < k; i++) { for (j = 0; j < k; j++) { dis[i][j] = min(dis[i][j], dis[i][ouo] + dis[ouo][j]); } } } int ans = 0; for (i = 0; i < k; i++) { for (j = 0; j < k; j++) { if (dis[i][j] < 9999) { ans = max(ans, dis[i][j]); } } } cout << ans << '\n'; return 0; }
replace
9
10
9
10
0
p02803
C++
Time Limit Exceeded
// Author: πα #include <bits/stdc++.h> using namespace std; #define long uint32_t const int N = 22; char g[N][N]; long dp[N][N]; bool vis[N][N]; void dfs(int x, int y, long k = 0) { if (vis[x][y] or g[x][y] != '.') return; vis[x][y] = 1; dp[x][y] = min(dp[x][y], k); dfs(x + 1, y, k + 1); dfs(x - 1, y, k + 1); dfs(x, y + 1, k + 1); dfs(x, y - 1, k + 1); vis[x][y] = 0; } int main() { int n, m; long ans = 0; cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> g[i] + 1; for (int x = 1; x <= n; ++x) for (int y = 1; y <= m; ++y) if (g[x][y] == '.') { memset(dp, -1, sizeof(dp)); dfs(x, y); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (g[i][j] == '.') ans = max(ans, dp[i][j]); } cout << ans; return 0; }
// Author: πα #include <bits/stdc++.h> using namespace std; #define long uint32_t const int N = 22; char g[N][N]; long dp[N][N]; bool vis[N][N]; void dfs(int x, int y, long k = 0) { if (vis[x][y] or g[x][y] != '.' or dp[x][y] == k) return; vis[x][y] = 1; dp[x][y] = min(dp[x][y], k); dfs(x + 1, y, k + 1); dfs(x - 1, y, k + 1); dfs(x, y + 1, k + 1); dfs(x, y - 1, k + 1); vis[x][y] = 0; } int main() { int n, m; long ans = 0; cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> g[i] + 1; for (int x = 1; x <= n; ++x) for (int y = 1; y <= m; ++y) if (g[x][y] == '.') { memset(dp, -1, sizeof(dp)); dfs(x, y); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (g[i][j] == '.') ans = max(ans, dp[i][j]); } cout << ans; return 0; }
replace
12
13
12
13
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define _GLIBCXX_DEBUG #define fi first #define se second using ll = long long; using vi = vector<int>; using vll = vector<ll>; using vd = vector<double>; using vvi = vector<vi>; using vvll = vector<vll>; using vvd = vector<vd>; using vvvi = vector<vvi>; using vvvll = vector<vvll>; using vvvd = vector<vvd>; const double pi = acos(-1); const ll MOD = 1e9 + 7; const ll INF = (1LL << 60); int main() { int h, w; cin >> h >> w; char graph[h][w]; rep(i, h) rep(j, w) cin >> graph[i][j]; int ans = 0; rep(i, h) { rep(j, w) { if (graph[i][j] == '#') continue; else { vvi d(h, vi(w, 1000)); d[i][j] = 0; queue<pair<int, int>> q; q.push({i, j}); while (q.size() != 0) { auto p1 = q.front(); q.pop(); vector<pair<int, int>> move; move.push_back(make_pair(1, 0)); move.push_back(make_pair(-1, 0)); move.push_back(make_pair(0, 1)); move.push_back(make_pair(0, -1)); for (auto p : move) { int dx = p.fi, dy = p.se; int x = dx + p1.fi, y = dy + p1.se; if (x < 0 || y < 0 || x >= h || y >= w) continue; else if (graph[x][y] == '#') continue; else if (d[x][y] < d[p1.fi][p1.se] + 1) continue; else { d[x][y] = d[p1.fi][p1.se] + 1; q.push({x, y}); } } } rep(k, h) { rep(l, w) { if (d[k][l] != 1000) { ans = max(ans, d[k][l]); } } } } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define _GLIBCXX_DEBUG #define fi first #define se second using ll = long long; using vi = vector<int>; using vll = vector<ll>; using vd = vector<double>; using vvi = vector<vi>; using vvll = vector<vll>; using vvd = vector<vd>; using vvvi = vector<vvi>; using vvvll = vector<vvll>; using vvvd = vector<vvd>; const double pi = acos(-1); const ll MOD = 1e9 + 7; const ll INF = (1LL << 60); int main() { int h, w; cin >> h >> w; char graph[h][w]; rep(i, h) rep(j, w) cin >> graph[i][j]; int ans = 0; rep(i, h) { rep(j, w) { if (graph[i][j] == '#') continue; else { vvi d(h, vi(w, 1000)); d[i][j] = 0; queue<pair<int, int>> q; q.push({i, j}); while (q.size() != 0) { auto p1 = q.front(); q.pop(); vector<pair<int, int>> move; move.push_back(make_pair(1, 0)); move.push_back(make_pair(-1, 0)); move.push_back(make_pair(0, 1)); move.push_back(make_pair(0, -1)); for (auto p : move) { int dx = p.fi, dy = p.se; int x = dx + p1.fi, y = dy + p1.se; if (x < 0 || y < 0 || x >= h || y >= w) continue; else if (graph[x][y] == '#') continue; else if (d[x][y] <= d[p1.fi][p1.se] + 1) continue; else { d[x][y] = d[p1.fi][p1.se] + 1; q.push({x, y}); } } } rep(k, h) { rep(l, w) { if (d[k][l] != 1000) { ans = max(ans, d[k][l]); } } } } } } cout << ans << endl; return 0; }
replace
55
56
55
56
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<string> map(H); for (auto &s : map) cin >> s; auto bfs = [&](int s) { if (map[s / W][s % W] == '#') return -1; vector<int> d(H * W, numeric_limits<int>::max()); queue<pair<int, int>> qs; qs.emplace(s, 0); while (not qs.empty()) { auto q = qs.front(); qs.pop(); auto s = q.first; auto dist = q.second; if (d[s] <= dist) continue; auto go = [&](int t) { if (map[t / W][t % W] == '.' and d[t] > dist + 1) { qs.emplace(t, dist + 1); } }; // -1 if (s % W > 0) { go(s - 1); } // 1 if (s % W != W - 1) { go(s + 1); } // -W if (s / W > 0) { go(s - W); } // W if (s / W != H - 1) { go(s + W); } } auto res = 0; for (auto v : d) { if (v == numeric_limits<int>::max()) continue; res = max(res, v); } return res; }; auto ans = 0; for (int i = 0; i < H * W; i++) { ans = max(ans, bfs(i)); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<string> map(H); for (auto &s : map) cin >> s; auto bfs = [&](int s) { if (map[s / W][s % W] == '#') return -1; vector<int> d(H * W, numeric_limits<int>::max()); queue<pair<int, int>> qs; qs.emplace(s, 0); while (not qs.empty()) { auto q = qs.front(); qs.pop(); auto s = q.first; auto dist = q.second; if (d[s] <= dist) continue; d[s] = dist; auto go = [&](int t) { if (map[t / W][t % W] == '.' and d[t] > dist + 1) { qs.emplace(t, dist + 1); } }; // -1 if (s % W > 0) { go(s - 1); } // 1 if (s % W != W - 1) { go(s + 1); } // -W if (s / W > 0) { go(s - W); } // W if (s / W != H - 1) { go(s + W); } } auto res = 0; for (auto v : d) { if (v == numeric_limits<int>::max()) continue; res = max(res, v); } return res; }; auto ans = 0; for (int i = 0; i < H * W; i++) { ans = max(ans, bfs(i)); } cout << ans << endl; return 0; }
insert
27
27
27
28
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define fi first #define se second #define pb push_back #define all(v) v.begin(), v.end() #define repf(i, a, b) for (ll i = a; i < b; i++) #define repb(i, a, b) for (ll i = a; i >= b; i--) #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define mod 1000000007 #define MOD 998244353 using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; ll power(ll a, ll n, ll m) { ll ans = 1; while (n) { if (n & 1) ans = (ans * a) % m; n >>= 1; a = (a * a) % m; } return ans; } string s[20]; ll h, w; ll f(int sx, int sy, int ex, int ey) { vector<vector<ll>> dp(h, vector<ll>(w, -1)); queue<pair<int, int>> q; q.push({sx, sy}); dp[sx][sy] = 0; while (!q.empty()) { int x = q.front().fi; int y = q.front().se; q.pop(); if (x == ex && y == ey) return dp[x][y]; repf(i, 0, 4) { repf(j, 0, 4) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] == '.' && dp[nx][ny] == -1) { q.push({nx, ny}); dp[nx][ny] = dp[x][y] + 1; } } } } return -1; } int main() { IOS; cin >> h >> w; repf(i, 0, h) cin >> s[i]; ll ans = 0; repf(i, 0, h) { repf(j, 0, w) { if (s[i][j] == '#') continue; repf(k, 0, h) { repf(l, 0, w) { if (s[k][l] == '#') continue; ans = max(ans, f(i, j, k, l)); } } } } cout << ans << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define fi first #define se second #define pb push_back #define all(v) v.begin(), v.end() #define repf(i, a, b) for (ll i = a; i < b; i++) #define repb(i, a, b) for (ll i = a; i >= b; i--) #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define mod 1000000007 #define MOD 998244353 using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; ll power(ll a, ll n, ll m) { ll ans = 1; while (n) { if (n & 1) ans = (ans * a) % m; n >>= 1; a = (a * a) % m; } return ans; } string s[20]; ll h, w; ll f(int sx, int sy, int ex, int ey) { vector<vector<ll>> dp(h, vector<ll>(w, -1)); queue<pair<int, int>> q; q.push({sx, sy}); dp[sx][sy] = 0; while (!q.empty()) { int x = q.front().fi; int y = q.front().se; q.pop(); if (x == ex && y == ey) return dp[x][y]; repf(i, 0, 4) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] == '.' && dp[nx][ny] == -1) { q.push({nx, ny}); dp[nx][ny] = dp[x][y] + 1; } } } return -1; } int main() { IOS; cin >> h >> w; repf(i, 0, h) cin >> s[i]; ll ans = 0; repf(i, 0, h) { repf(j, 0, w) { if (s[i][j] == '#') continue; repf(k, 0, h) { repf(l, 0, w) { if (s[k][l] == '#') continue; ans = max(ans, f(i, j, k, l)); } } } } cout << ans << endl; }
replace
54
62
54
60
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long long ll; typedef pair<ll, ll> P; const ll mod = 1000000007; const ll LINF = 1LL << 60; const int INF = 1 << 30; int bfs(int N, int M, vector<vector<char>> field, int sx, int sy, int gx, int gy) { int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int d[N][M]; // 距離の配列 for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { d[i][j] = INF; // 初期化 } } queue<P> que; que.push(P(sx, sy)); d[sx][sy] = 0; // キューが空になるまでループ while (que.size()) { P p = que.front(); que.pop(); // 先頭を取り出す if (p.fs == gx && p.sc == gy) { break; // 探索終わり } for (int i = 0; i < 4; i++) { int nx = p.fs + dx[i], ny = p.sc + dy[i]; // nx,nyが進めるマスか、行ったことのないマスかどうかを判定 if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != '#' && d[nx][ny] == INF) { que.push(P(nx, ny)); // 追加 d[nx][ny] = d[p.fs][p.sc] + 1; // 距離を確定 } } } return d[gx][gy]; } int main() { int h, w, i, j; cin >> h; cin >> w; vector<vector<char>> field(h, vector<char>(w)); int res = 0; int sx, sy, gx, gy; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> field[i][j]; } } int ans = 0; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { if (field[i][j] == '.') { sx = i; sy = j; } for (int k = 0; k < h; k++) { for (int l = 0; l < w; l++) { if (field[k][l] == '.') { gx = k; gy = l; ans = max(ans, bfs(h, w, field, sx, sy, gx, gy)); } } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long long ll; typedef pair<ll, ll> P; const ll mod = 1000000007; const ll LINF = 1LL << 60; const int INF = 1 << 30; int bfs(int N, int M, vector<vector<char>> field, int sx, int sy, int gx, int gy) { int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int d[N][M]; // 距離の配列 for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { d[i][j] = INF; // 初期化 } } queue<P> que; que.push(P(sx, sy)); d[sx][sy] = 0; // キューが空になるまでループ while (que.size()) { P p = que.front(); que.pop(); // 先頭を取り出す if (p.fs == gx && p.sc == gy) { break; // 探索終わり } for (int i = 0; i < 4; i++) { int nx = p.fs + dx[i], ny = p.sc + dy[i]; // nx,nyが進めるマスか、行ったことのないマスかどうかを判定 if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != '#' && d[nx][ny] == INF) { que.push(P(nx, ny)); // 追加 d[nx][ny] = d[p.fs][p.sc] + 1; // 距離を確定 } } } return d[gx][gy]; } int main() { int h, w, i, j; cin >> h; cin >> w; vector<vector<char>> field(h, vector<char>(w)); int res = 0; int sx, sy, gx, gy; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { cin >> field[i][j]; } } int ans = 0; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { if (field[i][j] == '.') { sx = i; sy = j; } for (int k = 0; k < h; k++) { for (int l = 0; l < w; l++) { if (field[k][l] == '.' && field[i][j] == '.') { gx = k; gy = l; ans = max(ans, bfs(h, w, field, sx, sy, gx, gy)); } } } } } cout << ans << endl; }
replace
70
71
70
71
0
p02803
C++
Runtime Error
#include <iostream> #include <queue> #include <vector> using namespace std; int stepX[4] = {1, -1, 0, 0}; int stepY[4] = {0, 0, 1, -1}; bool inArea(int tx, int ty, int w, int h) { return (0 <= tx && tx < w && 0 <= ty && ty < h); } int getFar(vector<vector<bool>> &mat, int sh, int sw, int h, int w) { queue<int> que; vector<int> steps(h * w, h * w); vector<bool> checker(w * h); que.push(sh * w + sw); steps[sh * w + sw] = 0; checker[sh * w + sw] = true; int dist = 0; while (!que.empty()) { int num = que.front(); que.pop(); int tx = num % w; int ty = num / w; int stp = steps[ty * w + tx]; for (int i = 0; i < 4; ++i) { int ttx = tx + stepX[i]; int tty = ty + stepY[i]; if (inArea(ttx, tty, w, h) && mat[tty][ttx] && !checker[tty * w + ttx]) { que.push(tty * w + ttx); checker[tty * w + ttx] = true; steps[tty * w + ttx] = stp + 1; dist = max(dist, stp + 1); } } } return dist; } int main() { int h, w; cin >> h >> w; vector<vector<bool>> mat(h); for (int i = 0; i < h; ++i) { vector<bool> row(w); for (int j = 0; j < w; ++j) { char c; cin >> c; row[j] = c == '.'; } mat[i] = row; } int ans = 0; for (int i = 0; i < h * w; ++i) { int tx = i % w; int ty = i / w; if (!mat[ty][tx]) { continue; } ans = max(ans, getFar(mat, tx, ty, h, w)); } cout << ans << endl; }
#include <iostream> #include <queue> #include <vector> using namespace std; int stepX[4] = {1, -1, 0, 0}; int stepY[4] = {0, 0, 1, -1}; bool inArea(int tx, int ty, int w, int h) { return (0 <= tx && tx < w && 0 <= ty && ty < h); } int getFar(vector<vector<bool>> &mat, int sh, int sw, int h, int w) { queue<int> que; vector<int> steps(h * w, h * w); vector<bool> checker(w * h); que.push(sh * w + sw); steps[sh * w + sw] = 0; checker[sh * w + sw] = true; int dist = 0; while (!que.empty()) { int num = que.front(); que.pop(); int tx = num % w; int ty = num / w; int stp = steps[ty * w + tx]; for (int i = 0; i < 4; ++i) { int ttx = tx + stepX[i]; int tty = ty + stepY[i]; if (inArea(ttx, tty, w, h) && mat[tty][ttx] && !checker[tty * w + ttx]) { que.push(tty * w + ttx); checker[tty * w + ttx] = true; steps[tty * w + ttx] = stp + 1; dist = max(dist, stp + 1); } } } return dist; } int main() { int h, w; cin >> h >> w; vector<vector<bool>> mat(h); for (int i = 0; i < h; ++i) { vector<bool> row(w); for (int j = 0; j < w; ++j) { char c; cin >> c; row[j] = c == '.'; } mat[i] = row; } int ans = 0; for (int i = 0; i < h * w; ++i) { int tx = i % w; int ty = i / w; if (!mat[ty][tx]) { continue; } ans = max(ans, getFar(mat, ty, tx, h, w)); } cout << ans << endl; }
replace
60
61
60
61
0
p02803
C++
Runtime Error
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> using namespace std; vector<string> vec; vector<string> vec2; struct Node { int y; int x; int depth; }; queue<Node> que; int H, W; int sy, sx, gy, gx; int y, x, depth; vector<int> y_vec = {0, 0, -1, 1}; vector<int> x_vec = {-1, 1, 0, 0}; int bfs() { while (!que.empty()) { Node node = que.front(); que.pop(); y = node.y; x = node.x; depth = node.depth; if (y == gy && x == gx) return depth; for (int i = 0; i < 4; ++i) { Node next = {y + y_vec[i], x + x_vec[i], depth + 1}; if (0 <= next.y && next.y <= H - 1 && 0 <= next.x && next.x <= W - 1 && vec[next.y][next.x] == '.') { que.push(next); vec[next.y][next.x] = '#'; } } } return 0; } int main() { cin >> H >> W; vec.resize(H); for (int i = 0; i < H; ++i) { cin >> vec[i]; vec2[i] = vec[i]; } vector<pair<int, int>> sg; for (int h = 0; h < H; ++h) { for (int w = 0; w < W; ++w) { if (vec[h][w] == '.') { pair<int, int> p = make_pair(h, w); sg.push_back(p); } } } int res = -1; for (int i = 0; i < sg.size(); ++i) { sy = sg[i].first; sx = sg[i].second; for (int j = 1; j < sg.size(); ++j) { gy = sg[j].first; gx = sg[j].second; Node start = {sy, sx, 0}; queue<Node> empty; swap(que, empty); que.push(start); for (int i = 0; i < H; ++i) { vec[i] = vec2[i]; } res = max(res, bfs()); } } cout << res << endl; return 0; }
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> using namespace std; vector<string> vec; vector<string> vec2; struct Node { int y; int x; int depth; }; queue<Node> que; int H, W; int sy, sx, gy, gx; int y, x, depth; vector<int> y_vec = {0, 0, -1, 1}; vector<int> x_vec = {-1, 1, 0, 0}; int bfs() { while (!que.empty()) { Node node = que.front(); que.pop(); y = node.y; x = node.x; depth = node.depth; if (y == gy && x == gx) return depth; for (int i = 0; i < 4; ++i) { Node next = {y + y_vec[i], x + x_vec[i], depth + 1}; if (0 <= next.y && next.y <= H - 1 && 0 <= next.x && next.x <= W - 1 && vec[next.y][next.x] == '.') { que.push(next); vec[next.y][next.x] = '#'; } } } return 0; } int main() { cin >> H >> W; vec.resize(H); vec2.resize(H); for (int i = 0; i < H; ++i) { cin >> vec[i]; vec2[i] = vec[i]; } vector<pair<int, int>> sg; for (int h = 0; h < H; ++h) { for (int w = 0; w < W; ++w) { if (vec[h][w] == '.') { pair<int, int> p = make_pair(h, w); sg.push_back(p); } } } int res = -1; for (int i = 0; i < sg.size(); ++i) { sy = sg[i].first; sx = sg[i].second; for (int j = 1; j < sg.size(); ++j) { gy = sg[j].first; gx = sg[j].second; Node start = {sy, sx, 0}; queue<Node> empty; swap(que, empty); que.push(start); for (int i = 0; i < H; ++i) { vec[i] = vec2[i]; } res = max(res, bfs()); } } cout << res << endl; return 0; }
insert
52
52
52
53
-11
p02803
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <tuple> #include <utility> #include <vector> #define REP(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; using P = pair<ll, ll>; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false) template<typename T> void PPPPP(T t) { cerr << t; } template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); } #else #define dump(...) do{ } while(false) #endif template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; } template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; } template<typename T> void print(T a) { cout << a << endl; } template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); } template<typename T> istream &operator,(istream &in, T &t) { return in >> t; } class Range { using T = long long; class Iterator { T val, step, diff; public: Iterator(T v, T s, T d): val(v), step(s), diff(d) {} T operator*() { return val + diff; } bool operator!=(const Iterator &itr) { return step > 0 ? val < itr.val : val > itr.val; } Iterator operator++() { val += step; return *this; } }; Iterator _begin, _end; public: Range(T b, T e, T s, T d): _begin(b, s, d), _end(e, s, d) { if (s < 0) { _begin = Iterator(e - 1, s, d); _end = Iterator(b - 1, s, d); } } Range(T b, T e, T s): Range(b, e, s, 0) {} Range(T b, T e): Range(b, e, 1, 0) {} Range(T e): Range(0, e, 1, 0) {} Iterator begin() { return _begin; } Iterator end() { return _end; } }; // clang-format on #include <queue> using T = tuple<int, int, int>; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w; cin, h, w; auto mp = make_v(h + 2, w + 2, '#'); for (auto i : Range(0, h, 1, 1)) { for (auto j : Range(0, w, 1, 1)) { cin, mp[i][j]; } } const int d[5] = {0, 1, 0, -1, 0}; auto bfs = [&](int x, int y) -> int { const ll inf = 1LL << 60; auto cost = make_v(h + 2, w + 2, inf); queue<T> q; q.emplace(x + 1, y + 1, 0); cost[y + 1][x + 1] = 0; int ans = 0; while (q.size()) { int nx, ny, ncost; tie(nx, ny, ncost) = q.front(); q.pop(); if (cost[ny][nx] < ncost) continue; chmax(ans, ncost); cost[ny][nx] = ncost; for (ll k : Range(4)) { int dx = nx + d[k]; int dy = ny + d[k + 1]; if (mp[dy][dx] == '#') continue; q.emplace(dx, dy, ncost + 1); } } return ans; }; int ans = 0; for (auto i : Range(h)) { for (auto j : Range(w)) { if (mp[i + 1][j + 1] != '#') { chmax(ans, bfs(j, i)); } } } print(ans); return 0; }
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <tuple> #include <utility> #include <vector> #define REP(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; using P = pair<ll, ll>; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false) template<typename T> void PPPPP(T t) { cerr << t; } template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); } #else #define dump(...) do{ } while(false) #endif template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; } template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; } template<typename T> void print(T a) { cout << a << endl; } template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); } template<typename T> istream &operator,(istream &in, T &t) { return in >> t; } class Range { using T = long long; class Iterator { T val, step, diff; public: Iterator(T v, T s, T d): val(v), step(s), diff(d) {} T operator*() { return val + diff; } bool operator!=(const Iterator &itr) { return step > 0 ? val < itr.val : val > itr.val; } Iterator operator++() { val += step; return *this; } }; Iterator _begin, _end; public: Range(T b, T e, T s, T d): _begin(b, s, d), _end(e, s, d) { if (s < 0) { _begin = Iterator(e - 1, s, d); _end = Iterator(b - 1, s, d); } } Range(T b, T e, T s): Range(b, e, s, 0) {} Range(T b, T e): Range(b, e, 1, 0) {} Range(T e): Range(0, e, 1, 0) {} Iterator begin() { return _begin; } Iterator end() { return _end; } }; // clang-format on #include <queue> using T = tuple<int, int, int>; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w; cin, h, w; auto mp = make_v(h + 2, w + 2, '#'); for (auto i : Range(0, h, 1, 1)) { for (auto j : Range(0, w, 1, 1)) { cin, mp[i][j]; } } const int d[5] = {0, 1, 0, -1, 0}; auto bfs = [&](int x, int y) -> int { const ll inf = 1LL << 60; auto cost = make_v(h + 2, w + 2, inf); queue<T> q; q.emplace(x + 1, y + 1, 0); cost[y + 1][x + 1] = 0; int ans = 0; while (q.size()) { int nx, ny, ncost; tie(nx, ny, ncost) = q.front(); q.pop(); if (cost[ny][nx] < ncost) continue; chmax(ans, ncost); cost[ny][nx] = ncost; for (ll k : Range(4)) { int dx = nx + d[k]; int dy = ny + d[k + 1]; if (mp[dy][dx] == '#') continue; if (cost[dy][dx] > ncost + 1) { cost[dy][dx] = ncost + 1; q.emplace(dx, dy, ncost + 1); } } } return ans; }; int ans = 0; for (auto i : Range(h)) { for (auto j : Range(w)) { if (mp[i + 1][j + 1] != '#') { chmax(ans, bfs(j, i)); } } } print(ans); return 0; }
replace
88
89
88
92
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; const ll INF = 4e18; ll nodeno[30][30]; vector<ll> adj_list[30 * 30]; bool visited[30 * 30]; ll level[30 * 30]; void assign_nodeno(ll h, ll w) { ll k = 1; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { nodeno[i][j] = k; k++; } } } void bfs(ll s) { visited[s] = true; queue<ll> q; q.push(s); level[s] = 0; while (!q.empty()) { ll x = q.front(); q.pop(); for (auto it : adj_list[x]) { if (!visited[it]) { level[it] = level[x] + 1; visited[it] = true; q.push(it); } } } } void solve() { ll h, w; cin >> h >> w; assign_nodeno(h, w); memset(visited, false, sizeof(visited)); memset(visited, 0, sizeof(level)); char g[30][30]; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { cin >> g[i][j]; } } for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { if (i != 0) { if (g[i - 1][j] != '#') adj_list[nodeno[i][j]].push_back(nodeno[i - 1][j]); } if (i != h - 1) { if (g[i + 1][j] != '#') adj_list[nodeno[i][j]].push_back(nodeno[i + 1][j]); } if (j != 0) { if (g[i][j - 1] != '#') adj_list[nodeno[i][j]].push_back(nodeno[i][j - 1]); } if (j != w - 1) { if (g[i][j + 1] != '#') adj_list[nodeno[i][j]].push_back(nodeno[i][j + 1]); } } } ll ans = 0; for (ll p = 0; p < h; p++) { for (ll q = 0; q < w; q++) { if (g[p][q] != '#') { bfs(nodeno[p][q]); for (ll j = 1; j <= h * w; j++) { ans = max(ans, level[j]); } for (ll j = 1; j <= h * w; j++) { level[j] = 0; visited[j] = false; } } } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll T = 1, p = 0; // cin>>T; do { solve(); p++; } while (p < T); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; const ll INF = 4e18; ll nodeno[30][30]; vector<ll> adj_list[30 * 30]; bool visited[30 * 30]; ll level[30 * 30]; void assign_nodeno(ll h, ll w) { ll k = 1; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { nodeno[i][j] = k; k++; } } } void bfs(ll s) { visited[s] = true; queue<ll> q; q.push(s); level[s] = 0; while (!q.empty()) { ll x = q.front(); q.pop(); for (auto it : adj_list[x]) { if (!visited[it]) { level[it] = level[x] + 1; visited[it] = true; q.push(it); } } } } void solve() { ll h, w; cin >> h >> w; assign_nodeno(h, w); for (ll i = 1; i <= h * w; i++) { level[i] = 0; visited[i] = false; } char g[30][30]; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { cin >> g[i][j]; } } for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { if (i != 0) { if (g[i - 1][j] != '#') adj_list[nodeno[i][j]].push_back(nodeno[i - 1][j]); } if (i != h - 1) { if (g[i + 1][j] != '#') adj_list[nodeno[i][j]].push_back(nodeno[i + 1][j]); } if (j != 0) { if (g[i][j - 1] != '#') adj_list[nodeno[i][j]].push_back(nodeno[i][j - 1]); } if (j != w - 1) { if (g[i][j + 1] != '#') adj_list[nodeno[i][j]].push_back(nodeno[i][j + 1]); } } } ll ans = 0; for (ll p = 0; p < h; p++) { for (ll q = 0; q < w; q++) { if (g[p][q] != '#') { bfs(nodeno[p][q]); for (ll j = 1; j <= h * w; j++) { ans = max(ans, level[j]); } for (ll j = 1; j <= h * w; j++) { level[j] = 0; visited[j] = false; } } } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll T = 1, p = 0; // cin>>T; do { solve(); p++; } while (p < T); return 0; }
replace
38
40
38
42
0
p02803
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> int H, W; char map[20][20]; int dist_value; int dist[20][20]; void initdist() { dist_value = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { dist[i][j] = -1; } } return; } struct coord { int x; int y; }; int drow[4] = {1, -1, 0, 0}; int dcolumn[4] = {0, 0, 1, -1}; std::queue<coord> queue_next; coord last; void bfs(int start_i, int start_j) { queue_next.push((coord){start_i, start_j}); dist_value = 0; while (queue_next.size()) { std::queue<coord> queue(queue_next); // clear queue_next std::queue<coord> empty; std::swap(queue_next, empty); while (queue.size()) { int thisi = queue.front().x; int thisj = queue.front().y; last.x = thisi; last.y = thisj; queue.pop(); dist[thisi][thisj] = dist_value; for (int k = 0; k < 4; k++) { if (thisi + drow[k] >= 0 && thisi + drow[k] < H && thisj + dcolumn[k] >= 0 && thisj + dcolumn[k] < W) { if (dist[thisi + drow[k]][thisj + dcolumn[k]] == -1 && map[thisi + drow[k]][thisj + dcolumn[k]] != '#') { queue_next.push((coord){thisi + drow[k], thisj + dcolumn[k]}); } } } } dist_value++; } return; } int maxdist() { int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (map[i][j] == '#') { continue; } initdist(); bfs(i, j); ans = std::max(ans, dist_value - 1); } } return ans; } int main() { scanf("%d", &H); scanf("%d", &W); std::string S; for (int i = 0; i < H; i++) { std::cin >> S; for (int j = 0; j < W; j++) { map[i][j] = S[j]; } } int ans = maxdist(); printf("%d\n", ans); return 0; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> int H, W; char map[20][20]; int dist_value; int dist[20][20]; void initdist() { dist_value = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { dist[i][j] = -1; } } return; } struct coord { int x; int y; }; int drow[4] = {1, -1, 0, 0}; int dcolumn[4] = {0, 0, 1, -1}; std::queue<coord> queue_next; coord last; void bfs(int start_i, int start_j) { queue_next.push((coord){start_i, start_j}); dist_value = 0; while (queue_next.size()) { std::queue<coord> queue(queue_next); // clear queue_next std::queue<coord> empty; std::swap(queue_next, empty); while (queue.size()) { int thisi = queue.front().x; int thisj = queue.front().y; last.x = thisi; last.y = thisj; queue.pop(); dist[thisi][thisj] = dist_value; for (int k = 0; k < 4; k++) { if (thisi + drow[k] >= 0 && thisi + drow[k] < H && thisj + dcolumn[k] >= 0 && thisj + dcolumn[k] < W) { if (dist[thisi + drow[k]][thisj + dcolumn[k]] == -1 && map[thisi + drow[k]][thisj + dcolumn[k]] != '#') { queue_next.push((coord){thisi + drow[k], thisj + dcolumn[k]}); dist[thisi + drow[k]][thisj + dcolumn[k]] = -2; } } } } dist_value++; } return; } int maxdist() { int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (map[i][j] == '#') { continue; } initdist(); bfs(i, j); ans = std::max(ans, dist_value - 1); } } return ans; } int main() { scanf("%d", &H); scanf("%d", &W); std::string S; for (int i = 0; i < H; i++) { std::cin >> S; for (int j = 0; j < W; j++) { map[i][j] = S[j]; } } int ans = maxdist(); printf("%d\n", ans); return 0; }
insert
56
56
56
57
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define pb push_back #define mp make_pair using namespace std; const int inf = 1e18 + 7; const int N = 1e6 + 7; const int md = 1e9 + 7; int dist[900], vis[900]; char a[30][30]; int n, m; int val[30][30]; std::vector<int> g[30]; int ok(int i, int j) { if ((i >= 0 && i < n) && (j >= 0 && j < m)) { if (a[i][j] == '.') return 1; } return 0; } void bfs(int src) { std::queue<int> q; int from; q.push(src); vis[src] = 1; dist[src] = 0; while (!q.empty()) { from = q.front(); q.pop(); for (auto &to : g[from]) { if (vis[to]) continue; q.push(to); vis[to] = 1; dist[to] = dist[from] + 1; } } } void sol() { int nodes = 0; int ans = 0; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == '.') { val[i][j] = nodes++; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == '.') { if (ok(i + 1, j)) { g[val[i][j]].push_back(val[i + 1][j]); } if (ok(i, j - 1)) { g[val[i][j]].push_back(val[i][j - 1]); } if (ok(i - 1, j)) { g[val[i][j]].push_back(val[i - 1][j]); } if (ok(i, j + 1)) { g[val[i][j]].push_back(val[i][j + 1]); } } } } // dfs(0, 0); for (int i = 0; i < nodes; i++) { memset(dist, 0, sizeof(dist)); memset(vis, 0, sizeof(vis)); bfs(i); for (int j = 0; j < nodes; j++) { ans = max(ans, dist[j]); } } cout << ans << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1; // cin >> T; while(T--) sol(); return 0; }
#include <bits/stdc++.h> #define int long long #define pb push_back #define mp make_pair using namespace std; const int inf = 1e18 + 7; const int N = 1e6 + 7; const int md = 1e9 + 7; int dist[900], vis[900]; char a[30][30]; int n, m; int val[30][30]; std::vector<int> g[900]; int ok(int i, int j) { if ((i >= 0 && i < n) && (j >= 0 && j < m)) { if (a[i][j] == '.') return 1; } return 0; } void bfs(int src) { std::queue<int> q; int from; q.push(src); vis[src] = 1; dist[src] = 0; while (!q.empty()) { from = q.front(); q.pop(); for (auto &to : g[from]) { if (vis[to]) continue; q.push(to); vis[to] = 1; dist[to] = dist[from] + 1; } } } void sol() { int nodes = 0; int ans = 0; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == '.') { val[i][j] = nodes++; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == '.') { if (ok(i + 1, j)) { g[val[i][j]].push_back(val[i + 1][j]); } if (ok(i, j - 1)) { g[val[i][j]].push_back(val[i][j - 1]); } if (ok(i - 1, j)) { g[val[i][j]].push_back(val[i - 1][j]); } if (ok(i, j + 1)) { g[val[i][j]].push_back(val[i][j + 1]); } } } } // dfs(0, 0); for (int i = 0; i < nodes; i++) { memset(dist, 0, sizeof(dist)); memset(vis, 0, sizeof(vis)); bfs(i); for (int j = 0; j < nodes; j++) { ans = max(ans, dist[j]); } } cout << ans << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1; // cin >> T; while(T--) sol(); return 0; }
replace
12
13
12
13
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> #include <cmath> using namespace std; // typedef long long ll; typedef unsigned long long ll; // #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) ll(x.size()) typedef pair<int, int> P; // typedef pair<ll, ll> P; // const double INF = 1e10; // const ll INF = LONG_LONG_MAX; const ll INF = 1e15; const ll MINF = LONG_LONG_MIN; // const int INF = INT_MAX / 10; #define cmin(x, y) x = min(x, y) #define cmax(x, y) x = max(x, y) bool contain(set<int> &s, int a) { return s.find(a) != s.end(); } // ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt"); // ofstream outfile("log.txt"); // outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') // << rank << endl; // std::cout << std::bitset<8>(9); typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK; const int mod = 1000000007; // https://qiita.com/okaryo/items/8e6cd73f8a676b7a5d75 void warshall_floyd(int n, vector<vector<int>> &d) { for (int k = 0; k < n; k++) { // 経由する頂点 for (int i = 0; i < n; i++) { // 始点 for (int j = 0; j < n; j++) { // 終点 d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } } int main() { int h, w; cin >> h >> w; vector<vector<char>> maze(h, vector<char>(w)); rep(y, h) rep(x, w) { char c; cin >> c; maze[y][x] = c; } vector<vector<int>> distances(h * w, vector<int>(h * w, 1000)); auto toID = [&](int y, int x) { return y * w + x; }; vector<P> move = { {0, 1}, {1, 0}, {0, -1}, {-1, 0}, }; rep(y, h) rep(x, w) { if (maze[y][x] == '#') continue; distances[toID(x, y)][toID(x, y)] = 0; for (P p : move) { int cy = p.first; int cx = p.second; int ny = y + cy; int nx = x + cx; if (ny == -1) continue; if (nx == -1) continue; if (ny == h) continue; if (nx == w) continue; if (maze[y][x] == '.' && maze[ny][nx] == '.') { distances[toID(y, x)][toID(ny, nx)] = 1; distances[toID(ny, nx)][toID(y, x)] = 1; } } } warshall_floyd(w * h, distances); int ans = 0; rep(i, h * w) rep(j, h * w) { int d = distances[i][j]; if (d >= 1000) { continue; } cmax(ans, d); } cout << ans << endl; }
#include <bits/stdc++.h> #include <cmath> using namespace std; // typedef long long ll; typedef unsigned long long ll; // #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) ll(x.size()) typedef pair<int, int> P; // typedef pair<ll, ll> P; // const double INF = 1e10; // const ll INF = LONG_LONG_MAX; const ll INF = 1e15; const ll MINF = LONG_LONG_MIN; // const int INF = INT_MAX / 10; #define cmin(x, y) x = min(x, y) #define cmax(x, y) x = max(x, y) bool contain(set<int> &s, int a) { return s.find(a) != s.end(); } // ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt"); // ofstream outfile("log.txt"); // outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') // << rank << endl; // std::cout << std::bitset<8>(9); typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK; const int mod = 1000000007; // https://qiita.com/okaryo/items/8e6cd73f8a676b7a5d75 void warshall_floyd(int n, vector<vector<int>> &d) { for (int k = 0; k < n; k++) { // 経由する頂点 for (int i = 0; i < n; i++) { // 始点 for (int j = 0; j < n; j++) { // 終点 d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } } int main() { int h, w; cin >> h >> w; vector<vector<char>> maze(h, vector<char>(w)); rep(y, h) rep(x, w) { char c; cin >> c; maze[y][x] = c; } vector<vector<int>> distances(h * w, vector<int>(h * w, 1000)); auto toID = [&](int y, int x) { return y * w + x; }; vector<P> move = { {0, 1}, {1, 0}, {0, -1}, {-1, 0}, }; rep(y, h) rep(x, w) { if (maze[y][x] == '#') continue; distances[toID(y, x)][toID(y, x)] = 0; for (P p : move) { int cy = p.first; int cx = p.second; int ny = y + cy; int nx = x + cx; if (ny == -1) continue; if (nx == -1) continue; if (ny == h) continue; if (nx == w) continue; if (maze[y][x] == '.' && maze[ny][nx] == '.') { distances[toID(y, x)][toID(ny, nx)] = 1; distances[toID(ny, nx)][toID(y, x)] = 1; } } } warshall_floyd(w * h, distances); int ans = 0; rep(i, h * w) rep(j, h * w) { int d = distances[i][j]; if (d >= 1000) { continue; } cmax(ans, d); } cout << ans << endl; }
replace
68
69
68
69
0
p02803
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 h, w; vector<vector<int>> d; void make_way(int i, int j, int s, int t) { d[i * h + j][s * h + t] = 1; d[s * h + t][i * h + j] = 1; } int main() { cin >> h >> w; const int INF = 1 << 20; REP(i, h * w) { vector<int> tmp(h * w, INF); d.push_back(tmp); } vector<string> s(h); REP(i, h) { cin >> s[i]; } REP(i, h * w) d[i][i] = 0; REP(i, h) { REP(j, w) { if (s[i][j] == '#') continue; if (i != 0 && s[i - 1][j] == '.') make_way(i, j, i - 1, j); if (j != 0 && s[i][j - 1] == '.') make_way(i, j, i, j - 1); if (i != h - 1 && s[i + 1][j] == '.') make_way(i, j, i + 1, j); if (j != w - 1 && s[i][j + 1] == '.') make_way(i, j, i, j + 1); } } REP(k, h * w) { REP(i, h * w) { REP(j, h * w) { chmin(d[i][j], d[i][k] + d[k][j]); } } } int ans = 0; REP(i, h * w) { REP(j, h * w) { if (d[i][j] != INF) chmax(ans, d[i][j]); } } cout << ans << 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 h, w; vector<vector<int>> d; void make_way(int i, int j, int s, int t) { d[i * w + j][s * w + t] = 1; d[s * w + t][i * w + j] = 1; } int main() { cin >> h >> w; const int INF = 1 << 20; REP(i, h * w) { vector<int> tmp(h * w, INF); d.push_back(tmp); } vector<string> s(h); REP(i, h) { cin >> s[i]; } REP(i, h * w) d[i][i] = 0; REP(i, h) { REP(j, w) { if (s[i][j] == '#') continue; if (i != 0 && s[i - 1][j] == '.') make_way(i, j, i - 1, j); if (j != 0 && s[i][j - 1] == '.') make_way(i, j, i, j - 1); if (i != h - 1 && s[i + 1][j] == '.') make_way(i, j, i + 1, j); if (j != w - 1 && s[i][j + 1] == '.') make_way(i, j, i, j + 1); } } REP(k, h * w) { REP(i, h * w) { REP(j, h * w) { chmin(d[i][j], d[i][k] + d[k][j]); } } } int ans = 0; REP(i, h * w) { REP(j, h * w) { if (d[i][j] != INF) chmax(ans, d[i][j]); } } cout << ans << endl; return 0; }
replace
34
36
34
36
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int INF = 1e8; int H, W; int blankMap[20][20]; vector<pair<int, int>> steps = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int bfs(int initX, int initY) { int highestCost = 0; int simulateMap[20][20]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { simulateMap[i][j] = blankMap[i][j]; } } queue<tuple<int, int, int>> route; // cost, x, y; route.push(make_tuple(0, initX, initY)); while (!route.empty()) { int cost, x, y; tie(cost, x, y) = route.front(); route.pop(); simulateMap[y][x] = min(simulateMap[y][x], cost); highestCost = max(highestCost, cost); for (auto step : steps) { int dx = step.first; int dy = step.second; if (0 <= x + dx && x + dx < W && 0 <= y + dy && y + dy < H && cost + 1 < simulateMap[y + dy][x + dx]) { route.push(make_tuple(cost + 1, x + dx, y + dy)); } } } return highestCost; } int main() { cin >> H >> W; string S[H]; for (int i = 0; i < H; i++) { cin >> S[i]; for (int j = 0; j < W; j++) { blankMap[i][j] = (S[i][j] == '#') ? -1 : INF; } } int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (S[i][j] == '#') continue; ans = max(ans, bfs(j, i)); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e8; int H, W; int blankMap[20][20]; vector<pair<int, int>> steps = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int bfs(int initX, int initY) { int highestCost = 0; int simulateMap[20][20]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { simulateMap[i][j] = blankMap[i][j]; } } queue<tuple<int, int, int>> route; // cost, x, y; route.push(make_tuple(0, initX, initY)); while (!route.empty()) { int cost, x, y; tie(cost, x, y) = route.front(); route.pop(); if (simulateMap[y][x] <= cost) continue; simulateMap[y][x] = cost; highestCost = max(highestCost, cost); for (auto step : steps) { int dx = step.first; int dy = step.second; if (0 <= x + dx && x + dx < W && 0 <= y + dy && y + dy < H && cost + 1 < simulateMap[y + dy][x + dx]) { route.push(make_tuple(cost + 1, x + dx, y + dy)); } } } return highestCost; } int main() { cin >> H >> W; string S[H]; for (int i = 0; i < H; i++) { cin >> S[i]; for (int j = 0; j < W; j++) { blankMap[i][j] = (S[i][j] == '#') ? -1 : INF; } } int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (S[i][j] == '#') continue; ans = max(ans, bfs(j, i)); } } cout << ans << endl; return 0; }
replace
26
27
26
31
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define INF 100010001000 #define MOD 1000000007 #define int long long int #define maxn 100 #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, n) FOR(i, 0, n) typedef pair<int, int> P; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } int mpow(int N, int P, int M) // 繰り返し自乗法 { if (P == 0) return 1; if (P % 2 == 0) { int t = mpow(N, P / 2, M); return t * t % M; } return N * mpow(N, P - 1, M) % M; } int counter(int x) { // bit計算 if (x == 0) return 0; return counter(x >> 1) + (x & 1); } vector<bool> prime(maxn + 1, true); // 素数テーブル void prime_table(void) { prime[0] = false; prime[1] = false; for (int i = 2; i * i <= maxn; i++) { if (!prime[i]) { continue; } for (int j = i + i; j <= maxn; j += j + i) { prime[j] = false; } } } bool IsPrime(int num) { // 素数判定 if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) { return false; } } return true; } signed main() { int H, W; int ans = 0; cin >> H >> W; vector<string> S(H); rep(i, H) cin >> S[i]; rep(si, H) rep(sj, W) { if (S[si][sj] == '#') continue; vector<vector<int>> dist(H, vector<int>(W, INF)); queue<P> q; auto update = [&](int i, int j, int x) { if (dist[i][j] != INF) return; dist[i][j] = x; q.push(P(i, j)); }; update(si, sj, 0); while (!q.empty()) { int i = q.front().first; int j = q.front().second; q.pop(); rep(dis, 4) { int ni = i + dx[dis]; int nj = j + dy[dis]; if (ni < 0 || ni >= W || nj < 0 || nj >= H) continue; if (S[ni][nj] == '#') continue; update(ni, nj, dist[i][j] + 1); } } rep(i, H) rep(j, W) { if (dist[i][j] == INF) continue; ans = max(ans, dist[i][j]); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define INF 100010001000 #define MOD 1000000007 #define int long long int #define maxn 100 #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, n) FOR(i, 0, n) typedef pair<int, int> P; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } int mpow(int N, int P, int M) // 繰り返し自乗法 { if (P == 0) return 1; if (P % 2 == 0) { int t = mpow(N, P / 2, M); return t * t % M; } return N * mpow(N, P - 1, M) % M; } int counter(int x) { // bit計算 if (x == 0) return 0; return counter(x >> 1) + (x & 1); } vector<bool> prime(maxn + 1, true); // 素数テーブル void prime_table(void) { prime[0] = false; prime[1] = false; for (int i = 2; i * i <= maxn; i++) { if (!prime[i]) { continue; } for (int j = i + i; j <= maxn; j += j + i) { prime[j] = false; } } } bool IsPrime(int num) { // 素数判定 if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) { return false; } } return true; } signed main() { int H, W; int ans = 0; cin >> H >> W; vector<string> S(H); rep(i, H) cin >> S[i]; rep(si, H) rep(sj, W) { if (S[si][sj] == '#') continue; vector<vector<int>> dist(H, vector<int>(W, INF)); queue<P> q; auto update = [&](int i, int j, int x) { if (dist[i][j] != INF) return; dist[i][j] = x; q.push(P(i, j)); }; update(si, sj, 0); while (!q.empty()) { int i = q.front().first; int j = q.front().second; q.pop(); rep(dis, 4) { int ni = i + dx[dis]; int nj = j + dy[dis]; if (ni < 0 || ni >= H || nj < 0 || nj >= W) continue; if (S[ni][nj] == '#') continue; update(ni, nj, dist[i][j] + 1); } } rep(i, H) rep(j, W) { if (dist[i][j] == INF) continue; ans = max(ans, dist[i][j]); } } cout << ans << endl; }
replace
93
94
93
94
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef vector<vector<int>> Graph; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; int main() { int H, W; cin >> H >> W; vector<string> S(H); rep(i, H) cin >> S[i]; int ans = 0; rep(i, H) { rep(j, W) { if (S[i][j] == '#') continue; Graph dist(H, vector<int>(W, -1)); queue<pair<int, int>> que; dist[i][j] = 0; que.push(make_pair(i, j)); while (!que.empty()) { int x = que.front().first; int y = que.front().second; rep(dir, 4) { int n_x = x + dx[dir]; int n_y = y + dy[dir]; if (n_x < 0 || n_x >= H || n_y < 0 || n_y >= W) continue; if (S[n_x][n_y] == '#') continue; if (dist[n_x][n_y] == -1) { que.push(make_pair(n_x, n_y)); dist[n_x][n_y] = dist[x][y] + 1; } } } rep(ix, H) { rep(jy, W) { if (dist[ix][jy] == -1) continue; ans = max(ans, dist[ix][jy]); } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef vector<vector<int>> Graph; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; int main() { int H, W; cin >> H >> W; vector<string> S(H); rep(i, H) cin >> S[i]; int ans = 0; rep(i, H) { rep(j, W) { if (S[i][j] == '#') continue; Graph dist(H, vector<int>(W, -1)); queue<pair<int, int>> que; dist[i][j] = 0; que.push(make_pair(i, j)); while (!que.empty()) { int x = que.front().first; int y = que.front().second; que.pop(); rep(dir, 4) { int n_x = x + dx[dir]; int n_y = y + dy[dir]; if (n_x < 0 || n_x >= H || n_y < 0 || n_y >= W) continue; if (S[n_x][n_y] == '#') continue; if (dist[n_x][n_y] == -1) { que.push(make_pair(n_x, n_y)); dist[n_x][n_y] = dist[x][y] + 1; } } } rep(ix, H) { rep(jy, W) { if (dist[ix][jy] == -1) continue; ans = max(ans, dist[ix][jy]); } } } } cout << ans << endl; }
insert
29
29
29
30
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define int ll #define REP(i, n) for (int i = 0; i < n; ++i) #define ALL(name) name.begin(), name.end() #define SORT(name) sort(name.begin(), name.end()) #define ZERO(p) memset(p, 0, sizeof(p)) #define MINUS(p) memset(p, -1, sizeof(p)) #if 1 #define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__) #else #define DBG(fmt, ...) #endif const ll LLINF = (1LL << 60); const int INF = (1LL << 30); const double DINF = std::numeric_limits<double>::infinity(); const int MOD = 1000000007; #define MAX_N 500 int H, W; vector<string> S; int pos_to_id[MAX_N][MAX_N]; pair<int, int> id_to_pos[MAX_N]; int Dist[MAX_N][MAX_N]; int V; void connect(int i, int j) { if (i > 0) { int next_i = i - 1; int next_j = j; if (S[next_i][next_j] == '.') { Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1; Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1; } } if (i < H - 1) { int next_i = i + 1; int next_j = j; if (S[next_i][next_j] == '.') { Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1; Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1; } } if (j > 0) { int next_i = i; int next_j = j - 1; if (S[next_i][next_j] == '.') { Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1; Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1; } } if (j < W - 1) { int next_i = i; int next_j = j + 1; if (S[next_i][next_j] == '.') { Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1; Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1; } } } signed main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cin >> H >> W; S.resize(H); REP(i, H) { cin >> S[i]; } MINUS(pos_to_id); int node_size = 0; int root = -1; REP(i, H) { REP(j, W) { if (S[i][j] == '.') { pos_to_id[i][j] = node_size; id_to_pos[node_size] = make_pair(i, j); node_size++; } } } V = node_size; REP(i, V) { REP(j, V) { Dist[i][j] = (i == j) ? 0 : LLINF; } } REP(i, H) { REP(j, W) { connect(i, j); } } REP(k, V) { REP(i, V) { REP(j, V) { Dist[i][j] = min(Dist[i][j], Dist[i][k] + Dist[k][j]); } } } int ans = 0; REP(i, V) { REP(j, V) { ans = max(ans, Dist[i][j]); } } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define int ll #define REP(i, n) for (int i = 0; i < n; ++i) #define ALL(name) name.begin(), name.end() #define SORT(name) sort(name.begin(), name.end()) #define ZERO(p) memset(p, 0, sizeof(p)) #define MINUS(p) memset(p, -1, sizeof(p)) #if 1 #define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__) #else #define DBG(fmt, ...) #endif const ll LLINF = (1LL << 60); const int INF = (1LL << 30); const double DINF = std::numeric_limits<double>::infinity(); const int MOD = 1000000007; #define MAX_N 500 int H, W; vector<string> S; int pos_to_id[MAX_N][MAX_N]; pair<int, int> id_to_pos[MAX_N]; int Dist[MAX_N][MAX_N]; int V; void connect(int i, int j) { if (i > 0) { int next_i = i - 1; int next_j = j; if (S[next_i][next_j] == '.') { Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1; Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1; } } if (i < H - 1) { int next_i = i + 1; int next_j = j; if (S[next_i][next_j] == '.') { Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1; Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1; } } if (j > 0) { int next_i = i; int next_j = j - 1; if (S[next_i][next_j] == '.') { Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1; Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1; } } if (j < W - 1) { int next_i = i; int next_j = j + 1; if (S[next_i][next_j] == '.') { Dist[pos_to_id[i][j]][pos_to_id[next_i][next_j]] = 1; Dist[pos_to_id[next_i][next_j]][pos_to_id[i][j]] = 1; } } } signed main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cin >> H >> W; S.resize(H); REP(i, H) { cin >> S[i]; } MINUS(pos_to_id); int node_size = 0; int root = -1; REP(i, H) { REP(j, W) { if (S[i][j] == '.') { pos_to_id[i][j] = node_size; id_to_pos[node_size] = make_pair(i, j); node_size++; } } } V = node_size; REP(i, V) { REP(j, V) { Dist[i][j] = (i == j) ? 0 : LLINF; } } REP(i, H) { REP(j, W) { if (S[i][j] == '.') { connect(i, j); } } } REP(k, V) { REP(i, V) { REP(j, V) { Dist[i][j] = min(Dist[i][j], Dist[i][k] + Dist[k][j]); } } } int ans = 0; REP(i, V) { REP(j, V) { ans = max(ans, Dist[i][j]); } } printf("%lld\n", ans); return 0; }
replace
94
95
94
99
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using xy = pair<int, int>; using Graph = vector<vector<xy>>; int main() { int H, W; cin >> H >> W; vector<vector<char>> S(H); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char x; cin >> x; S.at(i).push_back(x); } } int dh[4] = {1, 0, -1, 0}; int dw[4] = {0, 1, 0, -1}; int Ans = -1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (S.at(i).at(j) != '#') { vector<vector<int>> d(H, vector<int>(W, -1)); queue<xy> que; d.at(i).at(j) = 0; xy p = make_pair(i, j); que.push(p); int An = 0; while (!que.empty()) { p = que.front(); que.pop(); for (int k = 0; k < 4; i++) { int nh = p.first + dh[k]; int nw = p.second + dw[k]; if (nh < 0 || nh >= H || nw < 0 || nw >= W) { continue; } if (S.at(nh).at(nw) == '#') { continue; } if (d.at(nh).at(nw) >= 0) { continue; } d.at(nh).at(nw) = d.at(p.first).at(p.second) + 1; An = d.at(nh).at(nw); que.push(make_pair(nh, nw)); } } if (An > Ans) { Ans = An; } } } } cout << Ans << endl; }
#include <bits/stdc++.h> using namespace std; using xy = pair<int, int>; using Graph = vector<vector<xy>>; int main() { int H, W; cin >> H >> W; vector<vector<char>> S(H); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char x; cin >> x; S.at(i).push_back(x); } } int dh[4] = {1, 0, -1, 0}; int dw[4] = {0, 1, 0, -1}; int Ans = -1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (S.at(i).at(j) != '#') { vector<vector<int>> d(H, vector<int>(W, -1)); queue<xy> que; d.at(i).at(j) = 0; xy p = make_pair(i, j); que.push(p); int An = 0; while (!que.empty()) { p = que.front(); que.pop(); for (int k = 0; k < 4; k++) { int nh = p.first + dh[k]; int nw = p.second + dw[k]; if (nh < 0 || nh >= H || nw < 0 || nw >= W) { continue; } if (S.at(nh).at(nw) == '#') { continue; } if (d.at(nh).at(nw) >= 0) { continue; } d.at(nh).at(nw) = d.at(p.first).at(p.second) + 1; An = d.at(nh).at(nw); que.push(make_pair(nh, nw)); } } if (An > Ans) { Ans = An; } } } } cout << Ans << endl; }
replace
34
35
34
35
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int h, w; cin >> h >> w; vector<string> S(h); rep(i, h) cin >> S.at(i); vector<int> dx = {0, -1, 0, 1}; vector<int> dy = {1, 0, -1, 0}; deque<pair<int, int>> next_masu; int ans = 1; rep(i, h) { rep(j, w) { if (S.at(i).at(j) == '#') continue; vector<vector<int>> cnt(h, vector<int>(w, 1000000000)); cnt.at(i).at(j) = 0; rep(k, 4) { int n_i, n_j; n_i = i + dy[k]; n_j = j + dx[k]; if (n_i < 0 || n_i > h - 1 || n_j < 0 || n_j > w - 1) continue; if (S.at(n_i).at(n_j) == '#') continue; next_masu.push_back(make_pair(n_i, n_j)); cnt.at(n_i).at(n_j) = 1; } while (!next_masu.empty()) { auto now_masu = next_masu.front(); next_masu.pop_front(); rep(k, 4) { int n_i, n_j; n_i = now_masu.first + dy[k]; n_j = now_masu.second + dx[k]; if (n_i < 0 || n_i > h - 1 || n_j < 0 || n_j > w - 1) continue; if (S.at(n_i).at(n_j) == '#') continue; if (cnt.at(n_i).at(n_j) <= cnt.at(now_masu.first).at(now_masu.second)) continue; next_masu.push_back(make_pair(n_i, n_j)); cnt.at(n_i).at(n_j) = cnt.at(now_masu.first).at(now_masu.second) + 1; ans = max(ans, cnt.at(n_i).at(n_j)); } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int h, w; cin >> h >> w; vector<string> S(h); rep(i, h) cin >> S.at(i); vector<int> dx = {0, -1, 0, 1}; vector<int> dy = {1, 0, -1, 0}; deque<pair<int, int>> next_masu; int ans = 1; rep(i, h) { rep(j, w) { if (S.at(i).at(j) == '#') continue; vector<vector<int>> cnt(h, vector<int>(w, 1000000000)); cnt.at(i).at(j) = 0; rep(k, 4) { int n_i, n_j; n_i = i + dy[k]; n_j = j + dx[k]; if (n_i < 0 || n_i > h - 1 || n_j < 0 || n_j > w - 1) continue; if (S.at(n_i).at(n_j) == '#') continue; next_masu.push_back(make_pair(n_i, n_j)); cnt.at(n_i).at(n_j) = 1; } while (!next_masu.empty()) { auto now_masu = next_masu.front(); next_masu.pop_front(); rep(k, 4) { int n_i, n_j; n_i = now_masu.first + dy[k]; n_j = now_masu.second + dx[k]; if (n_i < 0 || n_i > h - 1 || n_j < 0 || n_j > w - 1) continue; if (S.at(n_i).at(n_j) == '#') continue; if (cnt.at(n_i).at(n_j) <= cnt.at(now_masu.first).at(now_masu.second) + 1) continue; next_masu.push_back(make_pair(n_i, n_j)); cnt.at(n_i).at(n_j) = cnt.at(now_masu.first).at(now_masu.second) + 1; ans = max(ans, cnt.at(n_i).at(n_j)); } } } } cout << ans << endl; }
replace
45
46
45
47
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int ans = 0; int H, W; void bfs(vector<string> &S, int sy, int sx, int ey, int ex) { vector<int> vx = {0, 1, 0, -1}; vector<int> vy = {1, 0, -1, 0}; queue<int> quex; queue<int> quey; quex.push(sx); quey.push(sy); vector<vector<int>> dis(H, vector<int>(W, 1e9)); dis.at(sy).at(sx) = 0; while (!quex.empty() && !quey.empty()) { int x = quex.front(); int y = quey.front(); quex.pop(); quey.pop(); for (int i = 0; i < 4; i++) { int next_x = x + vx.at(i); int next_y = y + vy.at(i); if (next_x < 0 || next_x >= W || next_y < 0 || next_y >= H) continue; if (S.at(next_y).at(next_x) == '#') continue; if (dis.at(next_y).at(next_x) != 1e9) continue; quex.push(next_x); quey.push(next_y); dis.at(next_y).at(next_x) = dis.at(y).at(x) + 1; } } if (dis.at(ey).at(ex) != 1e9) ans = max(ans, dis.at(ey).at(ex)); } int main() { cin >> H >> W; vector<string> S(H); for (int i = 0; i < H; i++) cin >> S.at(i); for (int st_i = 0; st_i < H; st_i++) { for (int st_j = 0; st_j < W; st_j++) { for (int en_i = 0; en_i < H; en_i++) { for (int en_j = 0; en_j < W; en_j++) { if (S.at(st_i).at(st_j) == '#') continue; if (S.at(en_i).at(en_j) == '#') continue; bfs(S, st_i, st_j, en_i, en_j); } } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int ans = 0; int H, W; void bfs(vector<string> &S, int sy, int sx, int ey, int ex) { vector<int> vx = {0, 1, 0, -1}; vector<int> vy = {1, 0, -1, 0}; queue<int> quex; queue<int> quey; quex.push(sx); quey.push(sy); vector<vector<int>> dis(H, vector<int>(W, 1e9)); dis.at(sy).at(sx) = 0; while (!quex.empty() && !quey.empty()) { int x = quex.front(); int y = quey.front(); quex.pop(); quey.pop(); for (int i = 0; i < 4; i++) { int next_x = x + vx.at(i); int next_y = y + vy.at(i); if (next_x < 0 || next_x >= W || next_y < 0 || next_y >= H) continue; if (S.at(next_y).at(next_x) == '#') continue; if (dis.at(next_y).at(next_x) != 1e9) continue; quex.push(next_x); quey.push(next_y); dis.at(next_y).at(next_x) = dis.at(y).at(x) + 1; } } if (dis.at(ey).at(ex) != 1e9) ans = max(ans, dis.at(ey).at(ex)); } int main() { cin >> H >> W; vector<string> S(H); for (int i = 0; i < H; i++) cin >> S.at(i); for (int st_i = 0; st_i < H; st_i++) { for (int st_j = 0; st_j < W; st_j++) { for (int en_i = 0; en_i < H; en_i++) { for (int en_j = st_j; en_j < W; en_j++) { if (S.at(st_i).at(st_j) == '#') continue; if (S.at(en_i).at(en_j) == '#') continue; bfs(S, st_i, st_j, en_i, en_j); } } } } cout << ans << endl; return 0; }
replace
47
48
47
48
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int mp[23][23]; char str[23][23]; int book[23][23]; int ans; int gx, gy; int dx[5] = {-1, 1, 0, 0}; int dy[5] = {0, 0, -1, 1}; int n, m; int qx[4005], qy[4005], step[4005]; int bfs(int sx, int sy, int gx, int gy) { memset(book, 0, sizeof(book)); int head = 0, tail = 0; book[sx][sy] = 1; qx[head] = sx, qy[tail] = sy, step[tail++] = 0; while (head < tail) { int x = qx[head], y = qy[head]; if (x == gx && y == gy) return step[head]; int nowx, nowy; for (int i = 0; i < 4; i++) { nowx = x + dx[i], nowy = y + dy[i]; if (nowx < 0 || nowx >= n || nowy < 0 || nowy >= m) continue; if (book[nowx][nowy] == 0 && mp[nowx][nowy] == 0) { qx[tail] = nowx, qy[tail] = nowy, step[tail] = step[head] + 1; tail++; } } head++; } return 0; } int main() { int i, j; cin >> n >> m; for (i = 0; i < n; i++) { scanf("%s", str[i]); for (j = 0; j < m; j++) { mp[i][j] = str[i][j] == '#'; } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (mp[i][j]) continue; for (int l = 0; l < n; l++) { for (int k = 0; k < m; k++) { if (mp[l][k]) continue; ans = max(ans, bfs(i, j, l, k)); // printf("%d %d %d %d %d\n", i, j, l, k, // bfs(i,j,l,k)); } } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int mp[23][23]; char str[23][23]; int book[23][23]; int ans; int gx, gy; int dx[5] = {-1, 1, 0, 0}; int dy[5] = {0, 0, -1, 1}; int n, m; int qx[4005], qy[4005], step[4005]; int bfs(int sx, int sy, int gx, int gy) { memset(book, 0, sizeof(book)); int head = 0, tail = 0; book[sx][sy] = 1; qx[head] = sx, qy[tail] = sy, step[tail++] = 0; while (head < tail) { int x = qx[head], y = qy[head]; if (x == gx && y == gy) return step[head]; int nowx, nowy; for (int i = 0; i < 4; i++) { nowx = x + dx[i], nowy = y + dy[i]; if (nowx < 0 || nowx >= n || nowy < 0 || nowy >= m) continue; if (book[nowx][nowy] == 0 && mp[nowx][nowy] == 0) { book[nowx][nowy] = 1; qx[tail] = nowx, qy[tail] = nowy, step[tail] = step[head] + 1; tail++; } } head++; } return 0; } int main() { int i, j; cin >> n >> m; for (i = 0; i < n; i++) { scanf("%s", str[i]); for (j = 0; j < m; j++) { mp[i][j] = str[i][j] == '#'; } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (mp[i][j]) continue; for (int l = 0; l < n; l++) { for (int k = 0; k < m; k++) { if (mp[l][k]) continue; ans = max(ans, bfs(i, j, l, k)); // printf("%d %d %d %d %d\n", i, j, l, k, // bfs(i,j,l,k)); } } } } cout << ans << endl; return 0; }
insert
29
29
29
30
0
p02803
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; int I[20][20]; int D[400][400]; int main() { ios::sync_with_stdio(false); int H, W; string S[20]; cin >> H >> W; for (int i = 0; i < H; i++) { cin >> S[i]; } int N = 1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (S[i][j] == '.') { I[i][j] = N++; } } } int INF = 100000; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { D[i][j] = INF; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (S[i][j] == '.') { int k = I[i][j]; D[k][k] = 0; if (i > 0) { int l = I[i - 1][j]; D[k][l] = D[l][k] = 1; } if (j > 0) { int l = I[i][j - 1]; D[k][l] = D[l][k] = 1; } if (i < H - 1) { int l = I[i + 1][j]; D[k][l] = D[l][k] = 1; } if (j < W - 1) { int l = I[i][j + 1]; D[k][l] = D[l][k] = 1; } } } } for (int k = 1; k < N; k++) { for (int i = 1; i < N; i++) { for (int j = 1; j < N; j++) { D[i][j] = min(D[i][j], D[i][k] + D[k][j]); } } } int ret = 0; for (int i = 1; i < N; i++) { for (int j = 1; j < N; j++) { if (D[i][j] < INF) { ret = max(ret, D[i][j]); } } } cout << ret << endl; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int I[20][20]; int D[405][405]; int main() { ios::sync_with_stdio(false); int H, W; string S[20]; cin >> H >> W; for (int i = 0; i < H; i++) { cin >> S[i]; } int N = 1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (S[i][j] == '.') { I[i][j] = N++; } } } int INF = 100000; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { D[i][j] = INF; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (S[i][j] == '.') { int k = I[i][j]; D[k][k] = 0; if (i > 0) { int l = I[i - 1][j]; D[k][l] = D[l][k] = 1; } if (j > 0) { int l = I[i][j - 1]; D[k][l] = D[l][k] = 1; } if (i < H - 1) { int l = I[i + 1][j]; D[k][l] = D[l][k] = 1; } if (j < W - 1) { int l = I[i][j + 1]; D[k][l] = D[l][k] = 1; } } } } for (int k = 1; k < N; k++) { for (int i = 1; i < N; i++) { for (int j = 1; j < N; j++) { D[i][j] = min(D[i][j], D[i][k] + D[k][j]); } } } int ret = 0; for (int i = 1; i < N; i++) { for (int j = 1; j < N; j++) { if (D[i][j] < INF) { ret = max(ret, D[i][j]); } } } cout << ret << endl; }
replace
5
6
5
6
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define SORT(x) sort((x).begin(), (x).end()) #define ALL(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < n; i++) #define reps(i, m, n) for (ll i = m; i < n; i++) #define repr(i, m, n) for (ll i = m; i >= n; i--) #define de(x) cout << #x << "=" << x << endl; template <class T> bool maxi(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool mini(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define dame \ { \ cout << "-1" \ << "\n"; \ return; \ } #define INF2 1000000000000000037 #define INF 1000000007 #define MOD 1000000007 #define PI (acos(-1)) using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; //--GLOBAL--------------------------------- const ll MX = 2020, MY = 2020; bool map2d[MX][MY]; // 行けるならtrue ll depth[MX][MY]; ll dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0}; ll bfs(ll sx, ll sy) { if (!map2d[sx][sy]) return 0; rep(i, MX) rep(j, MY) depth[i][j] = 0; // 初期化する if (depth[sx][sy]) return 0; deque<P> dq; dq.emplace_back(sx, sy); depth[sx][sy] = 1; // departure: depth=1 while (!dq.empty()) { ll vx = dq.front().first, vy = dq.front().second; dq.pop_front(); rep(i, 4) { ll x = vx + dx[i], y = vy + dy[i]; if (x < 0 or x >= MX or y < 0 or y >= MY) continue; if (!map2d[x][y]) continue; if (depth[x][y] != 0) continue; depth[x][y] = depth[vx][vy] + 1; dq.emplace_back(x, y); } } ll ct = 0; rep(i, MX) rep(j, MY) maxi(ct, depth[i][j]); return ct - 1; // 行けたノード数を返す、深さなどでも可 } //--MAIN----------------------------------- void Main() { ll H, W; cin >> H >> W; rep(i, H) { rep(j, W) { char c; cin >> c; if (c == '.') map2d[i][j] = 1; } } ll ans = 0; rep(i, MX) rep(j, MY) maxi(ans, bfs(i, j)); cout << ans << "\n"; } //--START---------------------------------- int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); } //-----------------------------------------
#include <bits/stdc++.h> #define SORT(x) sort((x).begin(), (x).end()) #define ALL(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < n; i++) #define reps(i, m, n) for (ll i = m; i < n; i++) #define repr(i, m, n) for (ll i = m; i >= n; i--) #define de(x) cout << #x << "=" << x << endl; template <class T> bool maxi(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool mini(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define dame \ { \ cout << "-1" \ << "\n"; \ return; \ } #define INF2 1000000000000000037 #define INF 1000000007 #define MOD 1000000007 #define PI (acos(-1)) using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; //--GLOBAL--------------------------------- const ll MX = 20, MY = 20; bool map2d[MX][MY]; // 行けるならtrue ll depth[MX][MY]; ll dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0}; ll bfs(ll sx, ll sy) { if (!map2d[sx][sy]) return 0; rep(i, MX) rep(j, MY) depth[i][j] = 0; // 初期化する if (depth[sx][sy]) return 0; deque<P> dq; dq.emplace_back(sx, sy); depth[sx][sy] = 1; // departure: depth=1 while (!dq.empty()) { ll vx = dq.front().first, vy = dq.front().second; dq.pop_front(); rep(i, 4) { ll x = vx + dx[i], y = vy + dy[i]; if (x < 0 or x >= MX or y < 0 or y >= MY) continue; if (!map2d[x][y]) continue; if (depth[x][y] != 0) continue; depth[x][y] = depth[vx][vy] + 1; dq.emplace_back(x, y); } } ll ct = 0; rep(i, MX) rep(j, MY) maxi(ct, depth[i][j]); return ct - 1; // 行けたノード数を返す、深さなどでも可 } //--MAIN----------------------------------- void Main() { ll H, W; cin >> H >> W; rep(i, H) { rep(j, W) { char c; cin >> c; if (c == '.') map2d[i][j] = 1; } } ll ans = 0; rep(i, MX) rep(j, MY) maxi(ans, bfs(i, j)); cout << ans << "\n"; } //--START---------------------------------- int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); } //-----------------------------------------
replace
36
37
36
37
TLE
p02803
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; const int inf = 10000; int main() { int h, w; cin >> h >> w; vector<vector<char>> vec(h + 2, vector<char>(w + 2, '#')); for (int x = 1; x <= h; ++x) for (int y = 1; y <= w; ++y) cin >> vec[x][y]; vector<vector<int>> ans((h + 2) * (w + w), vector<int>((h + 2) * (w + 2), inf)); for (int x = 1; x <= h; ++x) { for (int y = 1; y <= w; ++y) { for (int d = 0; d < 4; ++d) { int dx = vector<int>({0, 0, -1, 1})[d]; int dy = vector<int>({-1, 1, 0, 0})[d]; int idx1 = (x) * (w + 2) + (y); int idx2 = (x + dx) * (w + 2) + (y + dy); if (vec[x][y] == '.' && vec[x + dx][y + dy] == '.') ans[idx1][idx2] = ans[idx2][idx1] = 1; ans[idx1][idx1] = 0; } } } for (int k = 1; k < (h + 2) * (w + 2); ++k) { for (int i = 1; i < (h + 2) * (w + 2); ++i) { for (int j = 1; j < (h + 2) * (w + 2); ++j) { int x1 = i / (w + 2); int y1 = i % (w + 2); int xm = k / (w + 2); int ym = k % (w + 2); int x2 = j / (w + 2); int y2 = j % (w + 2); int idx1 = x1 * (w + 2) + y1; int idxm = xm * (w + 2) + ym; int idx2 = x2 * (w + 2) + y2; if (ans[idx1][idxm] + ans[idxm][idx2] < ans[idx1][idx2]) ans[idx1][idx2] = ans[idx2][idx1] = ans[idx1][idxm] + ans[idxm][idx2]; } } } int vmax = 0; for (auto &seq : ans) { for (auto &v : seq) { if (v < inf && vmax < v) vmax = v; } } cout << vmax << endl; }
#include <iostream> #include <vector> using namespace std; const int inf = 10000; int main() { int h, w; cin >> h >> w; vector<vector<char>> vec(h + 2, vector<char>(w + 2, '#')); for (int x = 1; x <= h; ++x) for (int y = 1; y <= w; ++y) cin >> vec[x][y]; vector<vector<int>> ans((h + 2) * (w + 2), vector<int>((h + 2) * (w + 2), inf)); for (int x = 1; x <= h; ++x) { for (int y = 1; y <= w; ++y) { for (int d = 0; d < 4; ++d) { int dx = vector<int>({0, 0, -1, 1})[d]; int dy = vector<int>({-1, 1, 0, 0})[d]; int idx1 = (x) * (w + 2) + (y); int idx2 = (x + dx) * (w + 2) + (y + dy); if (vec[x][y] == '.' && vec[x + dx][y + dy] == '.') ans[idx1][idx2] = ans[idx2][idx1] = 1; ans[idx1][idx1] = 0; } } } for (int k = 1; k < (h + 2) * (w + 2); ++k) { for (int i = 1; i < (h + 2) * (w + 2); ++i) { for (int j = 1; j < (h + 2) * (w + 2); ++j) { int x1 = i / (w + 2); int y1 = i % (w + 2); int xm = k / (w + 2); int ym = k % (w + 2); int x2 = j / (w + 2); int y2 = j % (w + 2); int idx1 = x1 * (w + 2) + y1; int idxm = xm * (w + 2) + ym; int idx2 = x2 * (w + 2) + y2; if (ans[idx1][idxm] + ans[idxm][idx2] < ans[idx1][idx2]) ans[idx1][idx2] = ans[idx2][idx1] = ans[idx1][idxm] + ans[idxm][idx2]; } } } int vmax = 0; for (auto &seq : ans) { for (auto &v : seq) { if (v < inf && vmax < v) vmax = v; } } cout << vmax << endl; }
replace
16
17
16
17
0
p02803
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> int h, w; std::vector<std::vector<bool>> map; struct pairs { int first; int second; int depth; }; std::vector<std::pair<int, int>> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; inline int bfs(int start_h, int start_w) { int aaa = 0; std::vector<std::vector<bool>> seen(h + 2, std::vector<bool>(w + 2)); std::queue<pairs> que; seen[start_h][start_w] = true; que.push({start_h, start_w, 0}); while (que.size()) { auto cur = que.front(); que.pop(); aaa = std::max(cur.depth, aaa); for (int i = 0; i < 4; i++) { if (!seen[cur.first + dir[i].first][cur.second + dir[i].second] && map[cur.first + dir[i].first][cur.second + dir[i].second]) { que.push({cur.first + dir[i].first, cur.second + dir[i].second, cur.depth + 1}); seen[cur.first][cur.second] = true; } } } return aaa; } int main() { std::cin >> h >> w; map.resize(h + 2, std::vector<bool>(w + 2)); for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { char a; std::cin >> a; map[i][j] = a == '.'; } } int max = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (map[i][j]) max = std::max(bfs(i, j), max); } } std::cout << max << std::endl; }
#include <algorithm> #include <bits/stdc++.h> int h, w; std::vector<std::vector<bool>> map; struct pairs { int first; int second; int depth; }; std::vector<std::pair<int, int>> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; inline int bfs(int start_h, int start_w) { int aaa = 0; std::vector<std::vector<bool>> seen(h + 2, std::vector<bool>(w + 2)); std::queue<pairs> que; seen[start_h][start_w] = true; que.push({start_h, start_w, 0}); while (que.size()) { auto cur = que.front(); que.pop(); aaa = std::max(cur.depth, aaa); for (int i = 0; i < 4; i++) { if (!seen[cur.first + dir[i].first][cur.second + dir[i].second] && map[cur.first + dir[i].first][cur.second + dir[i].second]) { que.push({cur.first + dir[i].first, cur.second + dir[i].second, cur.depth + 1}); seen[cur.first + dir[i].first][cur.second + dir[i].second] = true; } } } return aaa; } int main() { std::cin >> h >> w; map.resize(h + 2, std::vector<bool>(w + 2)); for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { char a; std::cin >> a; map[i][j] = a == '.'; } } int max = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (map[i][j]) max = std::max(bfs(i, j), max); } } std::cout << max << std::endl; }
replace
27
28
27
28
TLE
p02803
C++
Time Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include "bits/stdc++.h" // #include <intrin.h> #define fore(i, a, b) for (int i = a; i < (b); i++) #define fr(i, a, b) for (int i = a - 1; i >= b; i--) #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define N 300005 #define inf 2000000000 #define MOD (ll)1e15 #define __builtin_popcount(x) __popcnt(x) using namespace std; typedef long long ll; typedef pair<int, int> pr; typedef pair<pair<int, int>, int> tup; inline void solve() { int n, m; cin >> n >> m; vector<vector<char>> g(n, vector<char>(m)); fore(i, 0, n) fore(j, 0, m) { cin >> g[i][j]; } int otv = 0; fore(i, 0, n) { fore(j, 0, m) { fore(k, 0, n) { fore(h, 0, m) { if (g[i][j] != '#' && g[k][h] != '#') { vector<vector<bool>> used(n, vector<bool>(m, 0)); queue<tup> q; q.push({{i, j}, 0}); while (q.size()) { tup v = q.front(); q.pop(); if (v.first.first < 0 || v.first.first >= n || v.first.second < 0 || v.first.second >= m || used[v.first.first][v.first.second] || g[v.first.first][v.first.second] == '#') { continue; } if (v.first.first == k && v.first.second == h) { otv = max(otv, v.second); break; } q.push({{v.first.first + 1, v.first.second}, v.second + 1}); q.push({{v.first.first - 1, v.first.second}, v.second + 1}); q.push({{v.first.first, v.first.second + 1}, v.second + 1}); q.push({{v.first.first, v.first.second - 1}, v.second + 1}); } } } } } } cout << otv; } int main() { fast; #if defined(_DEBUG) freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif solve(); }
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include "bits/stdc++.h" // #include <intrin.h> #define fore(i, a, b) for (int i = a; i < (b); i++) #define fr(i, a, b) for (int i = a - 1; i >= b; i--) #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define N 300005 #define inf 2000000000 #define MOD (ll)1e15 #define __builtin_popcount(x) __popcnt(x) using namespace std; typedef long long ll; typedef pair<int, int> pr; typedef pair<pair<int, int>, int> tup; inline void solve() { int n, m; cin >> n >> m; vector<vector<char>> g(n, vector<char>(m)); fore(i, 0, n) fore(j, 0, m) { cin >> g[i][j]; } int otv = 0; fore(i, 0, n) { fore(j, 0, m) { fore(k, 0, n) { fore(h, 0, m) { if (g[i][j] != '#' && g[k][h] != '#') { vector<vector<bool>> used(n, vector<bool>(m, 0)); queue<tup> q; q.push({{i, j}, 0}); while (q.size()) { tup v = q.front(); q.pop(); if (v.first.first < 0 || v.first.first >= n || v.first.second < 0 || v.first.second >= m || used[v.first.first][v.first.second] || g[v.first.first][v.first.second] == '#') { continue; } used[v.first.first][v.first.second] = 1; if (v.first.first == k && v.first.second == h) { otv = max(otv, v.second); break; } q.push({{v.first.first + 1, v.first.second}, v.second + 1}); q.push({{v.first.first - 1, v.first.second}, v.second + 1}); q.push({{v.first.first, v.first.second + 1}, v.second + 1}); q.push({{v.first.first, v.first.second - 1}, v.second + 1}); } } } } } } cout << otv; } int main() { fast; #if defined(_DEBUG) freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif solve(); }
insert
49
49
49
50
TLE
p02803
C++
Time Limit Exceeded
#include <iostream> #include <queue> #include <utility> #include <vector> using ll = long long; using lli = long long int; lli h, w; std::vector<std::string> vec; std::vector<std::vector<lli>> visited; std::string s; std::vector<int> x_s; std::vector<int> y_s; int main() { lli ans = 0; x_s = {0, 0, 1, -1}; y_s = {1, -1, 0, 0}; std::cin >> h >> w; vec.assign(h, "a"); for (int i = 0; i < h; i++) { std::cin >> s; vec[i] = s; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (vec[i][j] == '.') { visited.assign(h, std::vector<lli>(w, 0)); std::queue<std::vector<lli>> q; q.push({i, j, 0}); while (!q.empty()) { auto point = q.front(); lli x = point[1]; lli y = point[0]; if (point[2] > ans) { ans = point[2]; } visited[y][x] = 1; for (int k = 0; k < 4; k++) { if (y + y_s[k] >= 0 && x + x_s[k] >= 0 && y + y_s[k] < h && x + x_s[k] < w && vec[y + y_s[k]][x + x_s[k]] != '#' && visited[y + y_s[k]][x + x_s[k]] != 1) { q.push({y + y_s[k], x + x_s[k], point[2] + 1}); } } q.pop(); } } } } std::cout << ans << std::endl; }
#include <iostream> #include <queue> #include <utility> #include <vector> using ll = long long; using lli = long long int; lli h, w; std::vector<std::string> vec; std::vector<std::vector<lli>> visited; std::string s; std::vector<int> x_s; std::vector<int> y_s; int main() { lli ans = 0; x_s = {0, 0, 1, -1}; y_s = {1, -1, 0, 0}; std::cin >> h >> w; vec.assign(h, "a"); for (int i = 0; i < h; i++) { std::cin >> s; vec[i] = s; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (vec[i][j] == '.') { visited.assign(h, std::vector<lli>(w, 0)); std::queue<std::vector<lli>> q; q.push({i, j, 0}); while (!q.empty()) { auto point = q.front(); lli x = point[1]; lli y = point[0]; if (visited[y][x] == 1) { q.pop(); continue; } if (point[2] > ans) { ans = point[2]; } visited[y][x] = 1; for (int k = 0; k < 4; k++) { if (y + y_s[k] >= 0 && x + x_s[k] >= 0 && y + y_s[k] < h && x + x_s[k] < w && vec[y + y_s[k]][x + x_s[k]] != '#' && visited[y + y_s[k]][x + x_s[k]] != 1) { q.push({y + y_s[k], x + x_s[k], point[2] + 1}); } } q.pop(); } } } } std::cout << ans << std::endl; }
insert
35
35
35
39
TLE
p02803
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> using namespace std; using lint = long long; constexpr int MOD = 1000000007, INF = 1010101010; constexpr lint LINF = 1LL << 60; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (const auto &e : vec) os << e << (&e == &vec.back() ? "\n" : " "); return os; } #ifdef _DEBUG template <class T> void dump(const char *str, T &&h) { cerr << str << " = " << h << "\n"; }; template <class Head, class... Tail> void dump(const char *str, Head &&h, Tail &&...t) { while (*str != ',') cerr << *str++; cerr << " = " << h << "\n"; dump(str + (*(str + 1) == ' ' ? 2 : 1), t...); } #define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__) #else #define DMP(...) ((void)0) #endif struct pnt { int x, y; constexpr pnt(int x = 0, int y = 0) noexcept : x(x), y(y){}; constexpr bool operator==(const pnt &rhs) const noexcept { return x == rhs.x && y == rhs.y; } constexpr bool operator<(const pnt &rhs) const noexcept { return x < rhs.x || x == rhs.x && y < rhs.y; } constexpr pnt operator+(const pnt &rhs) const noexcept { return pnt(x + rhs.x, y + rhs.y); } constexpr pnt operator-(const pnt &rhs) const noexcept { return pnt(x - rhs.x, y - rhs.y); } constexpr pnt operator-() const noexcept { return pnt(-x, -y); } constexpr int operator*(const pnt &rhs) const noexcept { return x * rhs.x + y * rhs.y; } friend istream &operator>>(istream &is, pnt &p) { return is >> p.x >> p.y; } friend ostream &operator<<(ostream &os, const pnt &p) { return os << p.x << " " << p.y << "\n"; } constexpr pnt nor(const pnt &rhs) const noexcept { return pnt(rhs.y, -rhs.x); } template <class T> T &operator[](vector<vector<T>> &v) { return v[x][y]; } }; template <class T> inline bool chmax(T &a, const T b) { return a < b && (a = b, true); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<vector<char>> grid(H, vector<char>(W)); for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) cin >> grid[i][j]; DMP(grid); auto shortest_path = [&](pnt st, pnt go) { if (st[grid] == '#' || go[grid] == '#') return 0; queue<pnt> que; que.emplace(st); vector<vector<int>> used(H, vector<int>(W, INF)); st[used] = 0; while (!que.empty()) { pnt now = que.front(); que.pop(); if (now == go) continue; vector<pnt> dxdy{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; for (const auto &d : dxdy) { pnt next = now + d; if (0 <= next.x && next.x < H && 0 <= next.y && next.y < W && next[grid] == '.' && next[used] > now[used]) { que.emplace(next); next[used] = now[used] + 1; } } } DMP(used); return go[used]; }; int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = i; k < H; k++) { for (int l = 0; l < W; l++) { chmax(ans, shortest_path(pnt(i, j), pnt(k, l))); } } } } cout << ans << "\n"; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> using namespace std; using lint = long long; constexpr int MOD = 1000000007, INF = 1010101010; constexpr lint LINF = 1LL << 60; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (const auto &e : vec) os << e << (&e == &vec.back() ? "\n" : " "); return os; } #ifdef _DEBUG template <class T> void dump(const char *str, T &&h) { cerr << str << " = " << h << "\n"; }; template <class Head, class... Tail> void dump(const char *str, Head &&h, Tail &&...t) { while (*str != ',') cerr << *str++; cerr << " = " << h << "\n"; dump(str + (*(str + 1) == ' ' ? 2 : 1), t...); } #define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__) #else #define DMP(...) ((void)0) #endif struct pnt { int x, y; constexpr pnt(int x = 0, int y = 0) noexcept : x(x), y(y){}; constexpr bool operator==(const pnt &rhs) const noexcept { return x == rhs.x && y == rhs.y; } constexpr bool operator<(const pnt &rhs) const noexcept { return x < rhs.x || x == rhs.x && y < rhs.y; } constexpr pnt operator+(const pnt &rhs) const noexcept { return pnt(x + rhs.x, y + rhs.y); } constexpr pnt operator-(const pnt &rhs) const noexcept { return pnt(x - rhs.x, y - rhs.y); } constexpr pnt operator-() const noexcept { return pnt(-x, -y); } constexpr int operator*(const pnt &rhs) const noexcept { return x * rhs.x + y * rhs.y; } friend istream &operator>>(istream &is, pnt &p) { return is >> p.x >> p.y; } friend ostream &operator<<(ostream &os, const pnt &p) { return os << p.x << " " << p.y << "\n"; } constexpr pnt nor(const pnt &rhs) const noexcept { return pnt(rhs.y, -rhs.x); } template <class T> T &operator[](vector<vector<T>> &v) { return v[x][y]; } }; template <class T> inline bool chmax(T &a, const T b) { return a < b && (a = b, true); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector<vector<char>> grid(H, vector<char>(W)); for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) cin >> grid[i][j]; DMP(grid); auto shortest_path = [&](pnt st, pnt go) { if (st[grid] == '#' || go[grid] == '#') return 0; queue<pnt> que; que.emplace(st); vector<vector<int>> used(H, vector<int>(W, INF)); st[used] = 0; while (!que.empty()) { pnt now = que.front(); que.pop(); if (now == go) continue; vector<pnt> dxdy{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; for (const auto &d : dxdy) { pnt next = now + d; if (0 <= next.x && next.x < H && 0 <= next.y && next.y < W && next[grid] == '.' && next[used] == INF) { que.emplace(next); next[used] = now[used] + 1; } } } DMP(used); return go[used]; }; int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { for (int k = i; k < H; k++) { for (int l = 0; l < W; l++) { chmax(ans, shortest_path(pnt(i, j), pnt(k, l))); } } } } cout << ans << "\n"; return 0; }
replace
107
108
107
108
TLE
p02803
C++
Runtime Error
// D - Maze Master #include <bits/stdc++.h> using namespace std; #define rp(i, s, e) for (int i = (s); i < (e); ++i) using vi = vector<int>; using vvi = vector<vi>; // BFS (幅優先探索) // 文字列迷路Sについて、S[sh][sw]から各マスまでの最短コストをdist[h][w]に求める #define HMAX 20 #define WMAX 20 #define INF 100000 vvi dist(HMAX + 1, vi(WMAX + 1, INF)); // 外周を壁で囲む (-1:未訪問, 0~<INF:訪問済&コスト, INF:壁) void bfs(int sh, int sw) { int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0}; dist[sh][sw] = 0; // 最短コスト queue<int> qh, qw; qh.push(sh), qw.push(sw); while (!qh.empty()) { int ph = qh.front(), pw = qw.front(); qh.pop(), qw.pop(); rp(k, 0, 4) { // 四方向 int h = ph + dy[k], w = pw + dx[k]; if (dist[h][w] > -1) continue; // 壁・訪問済み dist[h][w] = dist[ph][pw] + 1; // 最短コスト qh.push(h), qw.push(w); } } } int main() { int H, W; cin >> H >> W; rp(h, 1, H + 1) { string s; cin >> s; rp(w, 1, W + 1) if (s[w - 1] == '.') dist[h][w] = -1; // 未訪問 } int ans = 0; vvi dist_temp = dist; rp(h, 1, H + 1) { rp(w, 1, W + 1) { if (dist_temp[h][w] < 0) { dist = dist_temp; bfs(h, w); for (vi v : dist) for (int d : v) ans = max(ans, d == INF ? -1 : d); // 壁は未訪問とみなす } } } cout << ans << endl; }
// D - Maze Master #include <bits/stdc++.h> using namespace std; #define rp(i, s, e) for (int i = (s); i < (e); ++i) using vi = vector<int>; using vvi = vector<vi>; // BFS (幅優先探索) // 文字列迷路Sについて、S[sh][sw]から各マスまでの最短コストをdist[h][w]に求める #define HMAX 20 #define WMAX 20 #define INF 100000 vvi dist(HMAX + 2, vi(WMAX + 2, INF)); // 外周を壁で囲む (-1:未訪問, 0~<INF:訪問済&コスト, INF:壁) void bfs(int sh, int sw) { int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0}; dist[sh][sw] = 0; // 最短コスト queue<int> qh, qw; qh.push(sh), qw.push(sw); while (!qh.empty()) { int ph = qh.front(), pw = qw.front(); qh.pop(), qw.pop(); rp(k, 0, 4) { // 四方向 int h = ph + dy[k], w = pw + dx[k]; if (dist[h][w] > -1) continue; // 壁・訪問済み dist[h][w] = dist[ph][pw] + 1; // 最短コスト qh.push(h), qw.push(w); } } } int main() { int H, W; cin >> H >> W; rp(h, 1, H + 1) { string s; cin >> s; rp(w, 1, W + 1) if (s[w - 1] == '.') dist[h][w] = -1; // 未訪問 } int ans = 0; vvi dist_temp = dist; rp(h, 1, H + 1) { rp(w, 1, W + 1) { if (dist_temp[h][w] < 0) { dist = dist_temp; bfs(h, w); for (vi v : dist) for (int d : v) ans = max(ans, d == INF ? -1 : d); // 壁は未訪問とみなす } } } cout << ans << endl; }
replace
12
14
12
14
0
p02803
C++
Time Limit Exceeded
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; const ll MOD = 1000000007; const long long INF = 1LL << 60; const double pi = acos(-1); int H, W; bool movable(vector<string> &maze, vector<vector<int>> &memo, int x, int y, int X, int Y) { if (X < 0 || X >= W || Y < 0 || Y >= H) { return false; } if (maze.at(Y).at(X) == '#') { return false; } if (memo.at(Y).at(X) != -1) { return false; } return true; } int main() { cin >> H >> W; int ans = 0; vector<string> maze(H); rep(i, H) { cin >> maze.at(i); } rep(i, H * W) { rep(j, H * W) { int sx = i % W, sy = i / W, gx = j % W, gy = j / W; if (maze.at(sy).at(sx) == '#' || maze.at(gy).at(gx) == '#') { continue; } queue<vector<int>> next; vector<vector<int>> memo(H, vector<int>(W, -1)); memo.at(sy).at(sx) = 0; next.push({sx, sy}); while (!next.empty()) { int x = next.front().at(0), y = next.front().at(1); // cout<<x<<" "<<y<<endl; next.pop(); if (x == gx && y == gy) { break; } if (movable(maze, memo, x, y, x + 1, y)) { memo.at(y).at(x + 1) = memo.at(y).at(x) + 1; next.push({x + 1, y}); } if (movable(maze, memo, x, y, x - 1, y)) { memo.at(y).at(x - 1) = memo.at(y).at(x) + 1; next.push({x - 1, y}); } if (movable(maze, memo, x, y, x, y + 1)) { memo.at(y + 1).at(x) = memo.at(y).at(x) + 1; next.push({x, y + 1}); } if (movable(maze, memo, x, y, x, y - 1)) { memo.at(y - 1).at(x) = memo.at(y).at(x) + 1; next.push({x, y - 1}); } } ans = max(ans, memo.at(gy).at(gx)); } } cout << ans << endl; return 0; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; const ll MOD = 1000000007; const long long INF = 1LL << 60; const double pi = acos(-1); int H, W; bool movable(vector<string> &maze, vector<vector<int>> &memo, int x, int y, int X, int Y) { if (X < 0 || X >= W || Y < 0 || Y >= H) { return false; } if (maze.at(Y).at(X) == '#') { return false; } if (memo.at(Y).at(X) != -1) { return false; } return true; } int main() { cin >> H >> W; int ans = 0; vector<string> maze(H); rep(i, H) { cin >> maze.at(i); } rep(i, H * W) { for (int j = 0; j < i; j++) { int sx = i % W, sy = i / W, gx = j % W, gy = j / W; if (maze.at(sy).at(sx) == '#' || maze.at(gy).at(gx) == '#') { continue; } queue<vector<int>> next; vector<vector<int>> memo(H, vector<int>(W, -1)); memo.at(sy).at(sx) = 0; next.push({sx, sy}); while (!next.empty()) { int x = next.front().at(0), y = next.front().at(1); // cout<<x<<" "<<y<<endl; next.pop(); if (x == gx && y == gy) { break; } if (movable(maze, memo, x, y, x + 1, y)) { memo.at(y).at(x + 1) = memo.at(y).at(x) + 1; next.push({x + 1, y}); } if (movable(maze, memo, x, y, x - 1, y)) { memo.at(y).at(x - 1) = memo.at(y).at(x) + 1; next.push({x - 1, y}); } if (movable(maze, memo, x, y, x, y + 1)) { memo.at(y + 1).at(x) = memo.at(y).at(x) + 1; next.push({x, y + 1}); } if (movable(maze, memo, x, y, x, y - 1)) { memo.at(y - 1).at(x) = memo.at(y).at(x) + 1; next.push({x, y - 1}); } } ans = max(ans, memo.at(gy).at(gx)); } } cout << ans << endl; return 0; }
replace
30
31
30
31
TLE
p02803
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <string> #include <vector> typedef long long ll; using namespace std; const int mod = 1000000007; int bfs(int c, int r); vector<vector<int>> M(20, vector<int>(20, 0)); int H, W; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; int main() { cin >> H >> W; vector<pair<int, int>> P; for (int i = 0; i < H; i++) { string S; cin >> S; for (int j = 0; j < W; j++) { if (S[j] == '.') { M[i][j] = 1; P.push_back(make_pair(i, j)); } } } /* for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << M[i][j] << ' '; } cout << '\n'; } */ int ans = 0; for (int i = 0; i < P.size(); i++) { int dist = bfs(P[i].first, P[i].second); ans = max(dist, ans); } cout << ans << '\n'; } int bfs(int c, int r) { vector<vector<int>> visited(20, vector<int>(20, -1)); queue<pair<int, int>> q; q.push(make_pair(c, r)); visited[c][r] = 0; int next_c, next_r; int max_dist = 0; pair<int, int> point; while (!q.empty()) { point = q.front(); q.pop(); for (int i = 0; i < 4; i++) { next_c = point.first + dx[i]; next_r = point.second + dy[i]; if (next_c < 0 || H < next_c || next_r < 0 || W < next_r) { continue; } if (visited[next_c][next_r] != -1) continue; if (M[next_c][next_r] == 0) continue; visited[next_c][next_r] = visited[point.first][point.second] + 1; q.push(make_pair(next_c, next_r)); max_dist = max(visited[next_c][next_r], max_dist); } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { max_dist = max(visited[i][j], max_dist); // cout << visited[i][j] << ' '; } // cout << '\n'; } // cout << "max_dist : " << max_dist << '\n'; return max_dist; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <string> #include <vector> typedef long long ll; using namespace std; const int mod = 1000000007; int bfs(int c, int r); vector<vector<int>> M(20, vector<int>(20, 0)); int H, W; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; int main() { cin >> H >> W; vector<pair<int, int>> P; for (int i = 0; i < H; i++) { string S; cin >> S; for (int j = 0; j < W; j++) { if (S[j] == '.') { M[i][j] = 1; P.push_back(make_pair(i, j)); } } } /* for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << M[i][j] << ' '; } cout << '\n'; } */ int ans = 0; for (int i = 0; i < P.size(); i++) { int dist = bfs(P[i].first, P[i].second); ans = max(dist, ans); } cout << ans << '\n'; } int bfs(int c, int r) { vector<vector<int>> visited(20, vector<int>(20, -1)); queue<pair<int, int>> q; q.push(make_pair(c, r)); visited[c][r] = 0; int next_c, next_r; int max_dist = 0; pair<int, int> point; while (!q.empty()) { point = q.front(); q.pop(); for (int i = 0; i < 4; i++) { next_c = point.first + dx[i]; next_r = point.second + dy[i]; if (next_c < 0 || H <= next_c || next_r < 0 || W <= next_r) { continue; } if (visited[next_c][next_r] != -1) continue; if (M[next_c][next_r] == 0) continue; visited[next_c][next_r] = visited[point.first][point.second] + 1; q.push(make_pair(next_c, next_r)); max_dist = max(visited[next_c][next_r], max_dist); } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { max_dist = max(visited[i][j], max_dist); // cout << visited[i][j] << ' '; } // cout << '\n'; } // cout << "max_dist : " << max_dist << '\n'; return max_dist; }
replace
73
74
73
74
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)n; ++i) const int INF = 1001001001; vector<int> dx = {-1, 0, 1, 0}; vector<int> dy = {0, 1, 0, -1}; int h, w; vector<string> s; vector<int> steps; void bfs(pair<int, int> st, pair<int, int> gl) { // stからの距離 vector<vector<int>> dist(h, vector<int>(w, INF)); queue<pair<int, int>> q; auto push = [&](pair<int, int> now, int d) { int y = now.first; int x = now.second; if (dist.at(y).at(x) != INF) return; dist.at(y).at(x) = d; q.push(now); }; push(st, 0); while (!q.empty()) { pair<int, int> now = q.front(); q.pop(); int y = now.first; int x = now.second; int d = dist.at(y).at(x); rep(i, 4) { int nowy = y + dy.at(i); int nowx = x + dx.at(i); if (nowy >= 0 && nowy < h && nowx >= 0 && nowx < w && s.at(nowy).at(nowx) != '#') { push(make_pair(nowy, nowx), d + 1); } } int gy = gl.first; int gx = gl.second; if (dist.at(gy).at(gx) != INF) { steps.push_back(dist.at(gy).at(gx)); } } } int main() { cin >> h >> w; s.resize(h); rep(i, h) cin >> s.at(i); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (s.at(i).at(j) == '.') { for (int k = 0; k < h; ++k) { for (int l = 0; l < w; ++l) { if (k == i && l == j) continue; // スタートとゴールは一致しない if (s.at(k).at(l) == '#') continue; // 壁はゴールにならない pair<int, int> st = make_pair(i, j); pair<int, int> gl = make_pair(k, l); bfs(st, gl); } } } } } sort(steps.begin(), steps.end()); cout << steps.back() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)n; ++i) const int INF = 1001001001; vector<int> dx = {-1, 0, 1, 0}; vector<int> dy = {0, 1, 0, -1}; int h, w; vector<string> s; vector<int> steps; void bfs(pair<int, int> st, pair<int, int> gl) { // stからの距離 vector<vector<int>> dist(h, vector<int>(w, INF)); queue<pair<int, int>> q; auto push = [&](pair<int, int> now, int d) { int y = now.first; int x = now.second; if (dist.at(y).at(x) != INF) return; dist.at(y).at(x) = d; q.push(now); }; push(st, 0); while (!q.empty()) { pair<int, int> now = q.front(); q.pop(); int y = now.first; int x = now.second; int d = dist.at(y).at(x); rep(i, 4) { int nowy = y + dy.at(i); int nowx = x + dx.at(i); if (nowy >= 0 && nowy < h && nowx >= 0 && nowx < w && s.at(nowy).at(nowx) != '#') { push(make_pair(nowy, nowx), d + 1); } } int gy = gl.first; int gx = gl.second; if (dist.at(gy).at(gx) != INF) { steps.push_back(dist.at(gy).at(gx)); return; } } } int main() { cin >> h >> w; s.resize(h); rep(i, h) cin >> s.at(i); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (s.at(i).at(j) == '.') { for (int k = 0; k < h; ++k) { for (int l = 0; l < w; ++l) { if (k == i && l == j) continue; // スタートとゴールは一致しない if (s.at(k).at(l) == '#') continue; // 壁はゴールにならない pair<int, int> st = make_pair(i, j); pair<int, int> gl = make_pair(k, l); bfs(st, gl); } } } } } sort(steps.begin(), steps.end()); cout << steps.back() << endl; return 0; }
insert
47
47
47
48
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define forn(i, a, n) for (int i = a; i < n; i++) int n, m; char a[21][21]; int d[401]; int dist[21][21]; int used[21][21]; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int res = -2e18; signed main() { ios::sync_with_stdio(false); cin.tie(0); // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); cin >> n >> m; forn(i, 0, n) forn(j, 0, m) cin >> a[i][j]; forn(i, 0, n) { forn(j, 0, m) { memset(dist, -1, sizeof(dist)); memset(used, 0, sizeof(used)); if (a[i][j] == '#') continue; int qh = 0, qt = 0; d[qt++] = i; d[qt++] = j; used[i][j] = 1; dist[i][j] = 0; while (qh < qt) { int x = d[qh++]; int y = d[qh++]; forn(k, 0, 4) { int nx = x + dx[k]; int ny = y + dy[k]; if (nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] == '#') continue; if (dist[nx][ny] == -1) { d[qt++] = nx; d[qt++] = ny; dist[nx][ny] = dist[x][y] + 1; } } } forn(r, 0, n) forn(c, 0, m) res = max(res, dist[r][c]); } } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define forn(i, a, n) for (int i = a; i < n; i++) int n, m; char a[21][21]; int d[40001]; int dist[21][21]; int used[21][21]; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int res = -2e18; signed main() { ios::sync_with_stdio(false); cin.tie(0); // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); cin >> n >> m; forn(i, 0, n) forn(j, 0, m) cin >> a[i][j]; forn(i, 0, n) { forn(j, 0, m) { memset(dist, -1, sizeof(dist)); memset(used, 0, sizeof(used)); if (a[i][j] == '#') continue; int qh = 0, qt = 0; d[qt++] = i; d[qt++] = j; used[i][j] = 1; dist[i][j] = 0; while (qh < qt) { int x = d[qh++]; int y = d[qh++]; forn(k, 0, 4) { int nx = x + dx[k]; int ny = y + dy[k]; if (nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] == '#') continue; if (dist[nx][ny] == -1) { d[qt++] = nx; d[qt++] = ny; dist[nx][ny] = dist[x][y] + 1; } } } forn(r, 0, n) forn(c, 0, m) res = max(res, dist[r][c]); } } cout << res << endl; return 0; }
replace
8
9
8
9
0
p02803
C++
Time Limit Exceeded
#include <algorithm> #include <cstring> #include <iostream> #include <queue> #include <stdio.h> #include <string> #include <vector> using namespace std; int main() { int H, W; cin >> H >> W; vector<vector<char>> field(H, vector<char>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char c; cin >> c; field[i][j] = c; } } vector<vector<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int cost_max = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { vector<vector<int>> visited(H, vector<int>(W)); pair<int, int> s(i, j); if (field[i][j] == '#') continue; queue<pair<int, pair<int, int>>> que; pair<int, pair<int, int>> p_(0, s); que.push(p_); while (que.size() > 0) { pair<int, pair<int, int>> q = que.front(); que.pop(); int cost = q.first; pair<int, int> pos = q.second; int x = pos.first; int y = pos.second; visited[x][y] = 1; for (int k = 0; k < directions.size(); k++) { vector<int> direction = directions[k]; int dx = direction[0]; int dy = direction[1]; int nx = x + dx; int ny = y + dy; int ncost = cost + 1; if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue; if (field[nx][ny] == '#') continue; if (field[nx][ny] == '.' && visited[nx][ny] == 0) { pair<int, int> np(nx, ny); pair<int, pair<int, int>> p(ncost, np); if (cost_max < ncost) cost_max = ncost; que.push(p); } } } } } cout << cost_max << endl; }
#include <algorithm> #include <cstring> #include <iostream> #include <queue> #include <stdio.h> #include <string> #include <vector> using namespace std; int main() { int H, W; cin >> H >> W; vector<vector<char>> field(H, vector<char>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char c; cin >> c; field[i][j] = c; } } vector<vector<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int cost_max = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { vector<vector<int>> visited(H, vector<int>(W)); pair<int, int> s(i, j); if (field[i][j] == '#') continue; queue<pair<int, pair<int, int>>> que; pair<int, pair<int, int>> p_(0, s); que.push(p_); while (que.size() > 0) { pair<int, pair<int, int>> q = que.front(); que.pop(); int cost = q.first; pair<int, int> pos = q.second; int x = pos.first; int y = pos.second; visited[x][y] = 1; for (int k = 0; k < directions.size(); k++) { vector<int> direction = directions[k]; int dx = direction[0]; int dy = direction[1]; int nx = x + dx; int ny = y + dy; int ncost = cost + 1; if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue; if (field[nx][ny] == '#') continue; if (field[nx][ny] == '.' && visited[nx][ny] == 0) { pair<int, int> np(nx, ny); pair<int, pair<int, int>> p(ncost, np); if (cost_max < ncost) cost_max = ncost; visited[nx][ny] = 1; que.push(p); } } } } } cout << cost_max << endl; }
insert
55
55
55
56
TLE
p02803
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> // #define cerr if(false)cerr #define watch(x) cerr << "> " << #x << ": " << x << "\n"; using namespace std; template <typename T> std::ostream &operator<<(std::ostream &out, vector<T> &v) { for (typename vector<T>::size_type i = 0; i < v.size(); ++i) out << v[i] << " "; out << "\n"; return out; } template <typename T, typename N> std::ostream &operator<<(std::ostream &out, vector<pair<T, N>> &v) { for (size_t i = 0; i < v.size(); ++i) out << "(" << v[i].first << ", " << v[i].second << ") "; out << "\n"; return out; } template <typename T> std::ostream &operator<<(std::ostream &out, vector<vector<T>> &v) { for (size_t i = 0; i < v.size(); ++i) { for (size_t j = 0; j < v[i].size(); ++j) { out << v[i][j] << " "; } out << "\n"; } return out; } using iPair = pair<int, int>; vector<vector<pair<int, int>>> graph; vector<int> dist; void add_edge(int u, int v, int wt) { graph[u].push_back(make_pair(v, wt)); graph[v].push_back(make_pair(u, wt)); } int longest_path_in_min_moves(int V, int src) { priority_queue<iPair, vector<iPair>, greater<iPair>> pq; pq.push(make_pair(0, src)); dist = vector<int>(V, INT_MAX); dist[src] = 0; while (!pq.empty()) { int u = pq.top().second; pq.pop(); for (auto x : graph[u]) { int v = x.first; int weight = x.second; if (dist[v] > dist[u] + weight) { dist[v] = dist[u] + weight; pq.push(make_pair(dist[v], v)); } } } int longest = 0; for (int i = 0; i < V; ++i) { if (dist[i] != INT_MAX) { longest = max(longest, dist[i]); } } return longest; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int h, w; cin >> h >> w; vector<string> maze(h); for (int i = 0; i < h; ++i) { cin >> maze[i]; } graph = vector<vector<pair<int, int>>>(h * w, vector<pair<int, int>>()); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (maze[i][j] == '.') { if (i > 0 && maze[i - 1][j] == '.') { add_edge(i * w + j, (i - 1) * w + j, 1); } if (i < h - 1 && maze[i + 1][j] == '.') { add_edge(i * w + j, (i + 1) * w + j, 1); } if (j > 0 && maze[i][j - 1] == '.') { add_edge(i * w + j, i * w + j - 1, 1); } if (j < w - 1 && maze[i][j + 1] == '.') { add_edge(i * w + j, i * w + j + 1, 1); } } } } int max_moves = -1; for (int i = 0; i < h * w; ++i) { for (int j = 0; j < h; ++j) { for (int k = 0; k < w; ++k) { max_moves = max(max_moves, longest_path_in_min_moves(h * w, i)); } } } cout << max_moves << "\n"; return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> // #define cerr if(false)cerr #define watch(x) cerr << "> " << #x << ": " << x << "\n"; using namespace std; template <typename T> std::ostream &operator<<(std::ostream &out, vector<T> &v) { for (typename vector<T>::size_type i = 0; i < v.size(); ++i) out << v[i] << " "; out << "\n"; return out; } template <typename T, typename N> std::ostream &operator<<(std::ostream &out, vector<pair<T, N>> &v) { for (size_t i = 0; i < v.size(); ++i) out << "(" << v[i].first << ", " << v[i].second << ") "; out << "\n"; return out; } template <typename T> std::ostream &operator<<(std::ostream &out, vector<vector<T>> &v) { for (size_t i = 0; i < v.size(); ++i) { for (size_t j = 0; j < v[i].size(); ++j) { out << v[i][j] << " "; } out << "\n"; } return out; } using iPair = pair<int, int>; vector<vector<pair<int, int>>> graph; vector<int> dist; void add_edge(int u, int v, int wt) { graph[u].push_back(make_pair(v, wt)); graph[v].push_back(make_pair(u, wt)); } int longest_path_in_min_moves(int V, int src) { priority_queue<iPair, vector<iPair>, greater<iPair>> pq; pq.push(make_pair(0, src)); dist = vector<int>(V, INT_MAX); dist[src] = 0; while (!pq.empty()) { int u = pq.top().second; pq.pop(); for (auto x : graph[u]) { int v = x.first; int weight = x.second; if (dist[v] > dist[u] + weight) { dist[v] = dist[u] + weight; pq.push(make_pair(dist[v], v)); } } } int longest = 0; for (int i = 0; i < V; ++i) { if (dist[i] != INT_MAX) { longest = max(longest, dist[i]); } } return longest; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int h, w; cin >> h >> w; vector<string> maze(h); for (int i = 0; i < h; ++i) { cin >> maze[i]; } graph = vector<vector<pair<int, int>>>(h * w, vector<pair<int, int>>()); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (maze[i][j] == '.') { if (i > 0 && maze[i - 1][j] == '.') { add_edge(i * w + j, (i - 1) * w + j, 1); } if (i < h - 1 && maze[i + 1][j] == '.') { add_edge(i * w + j, (i + 1) * w + j, 1); } if (j > 0 && maze[i][j - 1] == '.') { add_edge(i * w + j, i * w + j - 1, 1); } if (j < w - 1 && maze[i][j + 1] == '.') { add_edge(i * w + j, i * w + j + 1, 1); } } } } int max_moves = -1; for (int i = 0; i < h * w; ++i) { max_moves = max(max_moves, longest_path_in_min_moves(h * w, i)); } cout << max_moves << "\n"; return 0; }
replace
127
132
127
128
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define mod ((ll)1e9 + 7) #define inf ((ll)1e18 + 5) #define maxx ((ll)1e6 + 5) #define pb push_back #define endl "\n" #define time(i) \ cout << "\nTime Elapsed " << i << " : " << 1.0 * clock() / CLOCKS_PER_SEC \ << " sec\n"; #define usefile \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); #define fast \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define test \ ll t; \ cin >> t; \ while (t--) ll exp(ll x, ll y, ll p) { ll res = 1; while (y) { if (y % 2) res = (res * x % p) % p; x = (x * x) % p; y /= 2; } return res; } ll expm(ll x, ll y) { ll res = 1; while (y) { if (y % 2) res = (res * x % mod) % mod; x = (x * x) % mod; y /= 2; } return res; } /* bool sv[maxx]={0}; // returns 0 if a num is prime int sp[maxx]={0}; // gives smallest prime factor of the number void Sieve() {for(int i= 2; i< maxx; i+= 2) sv[i]=1,sp[i] = 2;sv[2]=0;for(ll i = 3; i < maxx; i += 2){if (!sv[i]){sp[i] = i;for (ll j = i; (j*i) < maxx; j += 2){if (!sv[j*i])sv[j*i] = true, sp[j*i] = i;}}}} ll fact[maxx]={0}; // returns factorial of that number void facto() {fact[0]=1;fact[1]=1;for(int i=2;i<maxx;i++)fact[i]=(fact[i-1]*i)%mod;} ll ncrm(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(expm(fact[r],mod-2)))%mod; res=(res*(expm(fact[n-r],mod-2)))%mod; return res;} ll inv[maxx]; // returns the modular inverse of number void inversify( ll m){inv[1]=1;for(int i=2;i<maxx;i++){inv[i]= m- m/i*inv[m%i]%m;}} ll ncr(ll n,ll r,ll m){int ans=1;for (int i = 0; i < r; i++){ans=ans*(n-i)%m*inv[i+1]%m;}return ans;} */ int adj[20][20]; int vis[20][20]; int dis[20][20]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int h, w; bool check(int x, int y) { if (x < 0 || x >= h || y < 0 || y >= w || adj[x][y] == 0) return 0; return 1; } int bfs(int sx, int sy) { queue<pair<int, int>> q; q.push({sx, sy}); memset(vis, 0, sizeof(vis)); memset(dis, 0, sizeof(dis)); dis[sx][sy] = 0; int maxDis = -2; while (q.size()) { pair<int, int> u = q.front(); q.pop(); vis[u.first][u.second] = 1; for (int i = 0; i < 4; i++) { int nx = u.first + dx[i]; int ny = u.second + dy[i]; if (check(nx, ny) && vis[nx][ny] != 1) { dis[nx][ny] = dis[u.first][u.second] + 1; maxDis = max(maxDis, dis[nx][ny]); q.push({nx, ny}); } } } return maxDis; } signed main() { fast; // usefile; // test { cin >> h >> w; char ch; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> ch; if (ch == '.') adj[i][j] = 1; } } int mx = -1; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (adj[i][j] == 1) mx = max(mx, bfs(i, j)); cout << mx; } // time(""); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define mod ((ll)1e9 + 7) #define inf ((ll)1e18 + 5) #define maxx ((ll)1e6 + 5) #define pb push_back #define endl "\n" #define time(i) \ cout << "\nTime Elapsed " << i << " : " << 1.0 * clock() / CLOCKS_PER_SEC \ << " sec\n"; #define usefile \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); #define fast \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define test \ ll t; \ cin >> t; \ while (t--) ll exp(ll x, ll y, ll p) { ll res = 1; while (y) { if (y % 2) res = (res * x % p) % p; x = (x * x) % p; y /= 2; } return res; } ll expm(ll x, ll y) { ll res = 1; while (y) { if (y % 2) res = (res * x % mod) % mod; x = (x * x) % mod; y /= 2; } return res; } /* bool sv[maxx]={0}; // returns 0 if a num is prime int sp[maxx]={0}; // gives smallest prime factor of the number void Sieve() {for(int i= 2; i< maxx; i+= 2) sv[i]=1,sp[i] = 2;sv[2]=0;for(ll i = 3; i < maxx; i += 2){if (!sv[i]){sp[i] = i;for (ll j = i; (j*i) < maxx; j += 2){if (!sv[j*i])sv[j*i] = true, sp[j*i] = i;}}}} ll fact[maxx]={0}; // returns factorial of that number void facto() {fact[0]=1;fact[1]=1;for(int i=2;i<maxx;i++)fact[i]=(fact[i-1]*i)%mod;} ll ncrm(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(expm(fact[r],mod-2)))%mod; res=(res*(expm(fact[n-r],mod-2)))%mod; return res;} ll inv[maxx]; // returns the modular inverse of number void inversify( ll m){inv[1]=1;for(int i=2;i<maxx;i++){inv[i]= m- m/i*inv[m%i]%m;}} ll ncr(ll n,ll r,ll m){int ans=1;for (int i = 0; i < r; i++){ans=ans*(n-i)%m*inv[i+1]%m;}return ans;} */ int adj[20][20]; int vis[20][20]; int dis[20][20]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int h, w; bool check(int x, int y) { if (x < 0 || x >= h || y < 0 || y >= w || adj[x][y] == 0) return 0; return 1; } int bfs(int sx, int sy) { queue<pair<int, int>> q; q.push({sx, sy}); memset(vis, 0, sizeof(vis)); memset(dis, 0, sizeof(dis)); dis[sx][sy] = 0; int maxDis = -2; while (q.size()) { pair<int, int> u = q.front(); q.pop(); vis[u.first][u.second] = 1; for (int i = 0; i < 4; i++) { int nx = u.first + dx[i]; int ny = u.second + dy[i]; if (check(nx, ny) && vis[nx][ny] != 1) { dis[nx][ny] = dis[u.first][u.second] + 1; maxDis = max(maxDis, dis[nx][ny]); q.push({nx, ny}); vis[nx][ny] = 1; } } } return maxDis; } signed main() { fast; // usefile; // test { cin >> h >> w; char ch; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> ch; if (ch == '.') adj[i][j] = 1; } } int mx = -1; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (adj[i][j] == 1) mx = max(mx, bfs(i, j)); cout << mx; } // time(""); return 0; }
insert
89
89
89
90
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; char a[22][22]; int n, m; struct pos { int x; int y; int step; }; pos make_pos(int a, int b, int c) { pos ret; ret.x = a; ret.y = b; ret.step = c; return ret; } int bfs(int sx, int sy, int ex, int ey) { bool vis[n + 2][m + 2]; for (int i = 0; i <= n + 1; ++i) for (int j = 0; j <= m + 1; ++j) if (a[i][j] == '#') vis[i][j] = true; else vis[i][j] = false; queue<pos> q; q.push(make_pos(sx, sy, 0)); vis[sx][sy] = true; int move[2][4] = {{0, 0, -1, 1}, {-1, 1, 0, 0}}; while (!q.empty()) { pos fr = q.front(); q.pop(); if (fr.x == ex && fr.y == ey) return fr.step; pos newpo; for (int i = 0; i < 4; ++i) { int nx = fr.x + move[0][i]; int ny = fr.y + move[1][i]; if (vis[nx][ny]) continue; vis[nx][ny] = true; newpo = make_pos(nx, ny, fr.step + 1); q.push(newpo); } } return 0; } int main() { int i, j, i1, i2, j1, j2; for (i = 0; i < 22; ++i) for (j = 0; j < 22; ++j) a[i][j] = '#'; cin >> n >> m; for (i = 1; i <= n; ++i) for (j = 1; j <= m; ++j) cin >> a[i][j]; int maxv = 0; for (i1 = 1; i1 <= n; ++i1) for (j1 = 1; j1 <= m; ++j1) { if (a[i1][j1] == '#') continue; for (i2 = 1; i2 <= n; ++i2) for (j2 = 1; j2 <= m; ++j2) { if (a[i2][j2] == '#') continue; if (i1 == i2 && j1 == j2) continue; maxv = max(maxv, bfs(i1, i2, j1, j2)); } } cout << maxv << endl; return 0; }
#include <bits/stdc++.h> using namespace std; char a[22][22]; int n, m; struct pos { int x; int y; int step; }; pos make_pos(int a, int b, int c) { pos ret; ret.x = a; ret.y = b; ret.step = c; return ret; } int bfs(int sx, int sy, int ex, int ey) { bool vis[n + 2][m + 2]; for (int i = 0; i <= n + 1; ++i) for (int j = 0; j <= m + 1; ++j) if (a[i][j] == '#') vis[i][j] = true; else vis[i][j] = false; queue<pos> q; q.push(make_pos(sx, sy, 0)); vis[sx][sy] = true; int move[2][4] = {{0, 0, -1, 1}, {-1, 1, 0, 0}}; while (!q.empty()) { pos fr = q.front(); q.pop(); if (fr.x == ex && fr.y == ey) return fr.step; pos newpo; for (int i = 0; i < 4; ++i) { int nx = fr.x + move[0][i]; int ny = fr.y + move[1][i]; if (vis[nx][ny]) continue; vis[nx][ny] = true; newpo = make_pos(nx, ny, fr.step + 1); q.push(newpo); } } return 0; } int main() { int i, j, i1, i2, j1, j2; for (i = 0; i < 22; ++i) for (j = 0; j < 22; ++j) a[i][j] = '#'; cin >> n >> m; for (i = 1; i <= n; ++i) for (j = 1; j <= m; ++j) cin >> a[i][j]; int maxv = 0; for (i1 = 1; i1 <= n; ++i1) for (j1 = 1; j1 <= m; ++j1) { if (a[i1][j1] == '#') continue; for (i2 = 1; i2 <= n; ++i2) for (j2 = 1; j2 <= m; ++j2) { if (a[i2][j2] == '#') continue; if (i1 == i2 && j1 == j2) continue; maxv = max(maxv, bfs(i1, j1, i2, j2)); } } cout << maxv << endl; return 0; }
replace
83
84
83
84
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> //JuniorMonster a.k.a Sho10 #define ll long long int #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #define all(a) (a).begin(), (a).end() #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define sz size #define f first #define s second #define pb push_back #define er erase #define in insert #define mp make_pair #define pi pair #define rc(s) return cout << s, 0 #define endl '\n' #define mod 1000000007 #define PI 3.14159265359 #define CODE_START \ ios_base::sync_with_stdio(); \ cin.tie(); \ cout.tie(); using namespace std; ll n, m, ans = 0, dist[21][21]; char a[21][21]; bool viz[21][21]; vector<pair<ll, ll>> v; int32_t main() { CODE_START; cin >> n >> m; for (ll i = 0; i < n; i++) for (ll j = 0; j < m; j++) { cin >> a[i][j]; if (a[i][j] == '.') { v.pb(mp(i, j)); } } for (ll i = 0; i < v.size(); i++) for (ll j = i + 1; j < v.size(); j++) { queue<ll> q1; queue<ll> q2; q1.push(v[i].f); q2.push(v[i].s); for (ll c = 0; c < v.size(); c++) { viz[v[c].f][v[c].s] = 0; } dist[v[i].f][v[i].s] = 0; while (!q1.empty()) { ll x, y; x = q1.front(); y = q2.front(); viz[x][y] = 1; if (x == v[j].f && y == v[j].s) { break; } if (x - 1 >= 0 && a[x - 1][y] == '.' && viz[x - 1][y] == 0) { q1.push(x - 1); q2.push(y); dist[x - 1][y] = dist[x][y] + 1; } if (x + 1 < n && a[x + 1][y] == '.' && viz[x + 1][y] == 0) { q1.push(x + 1); q2.push(y); dist[x + 1][y] = dist[x][y] + 1; } if (y - 1 >= 0 && a[x][y - 1] == '.' && viz[x][y - 1] == 0) { q1.push(x); q2.push(y - 1); dist[x][y - 1] = dist[x][y] + 1; } if (y + 1 < m && a[x][y + 1] == '.' && viz[x][y + 1] == 0) { q1.push(x); q2.push(y + 1); dist[x][y + 1] = dist[x][y] + 1; } q1.pop(); q2.pop(); } ans = max(ans, dist[v[j].f][v[j].s]); } cout << ans << endl; }
#include <bits/stdc++.h> //JuniorMonster a.k.a Sho10 #define ll long long int #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #define all(a) (a).begin(), (a).end() #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define sz size #define f first #define s second #define pb push_back #define er erase #define in insert #define mp make_pair #define pi pair #define rc(s) return cout << s, 0 #define endl '\n' #define mod 1000000007 #define PI 3.14159265359 #define CODE_START \ ios_base::sync_with_stdio(); \ cin.tie(); \ cout.tie(); using namespace std; ll n, m, ans = 0, dist[21][21]; char a[21][21]; bool viz[21][21]; vector<pair<ll, ll>> v; int32_t main() { CODE_START; cin >> n >> m; for (ll i = 0; i < n; i++) for (ll j = 0; j < m; j++) { cin >> a[i][j]; if (a[i][j] == '.') { v.pb(mp(i, j)); } } if (v.size() == n * m) { cout << n + m - 2 << endl; return 0; } for (ll i = 0; i < v.size(); i++) for (ll j = i + 1; j < v.size(); j++) { queue<ll> q1; queue<ll> q2; q1.push(v[i].f); q2.push(v[i].s); for (ll c = 0; c < v.size(); c++) { viz[v[c].f][v[c].s] = 0; } dist[v[i].f][v[i].s] = 0; while (!q1.empty()) { ll x, y; x = q1.front(); y = q2.front(); viz[x][y] = 1; if (x == v[j].f && y == v[j].s) { break; } if (x - 1 >= 0 && a[x - 1][y] == '.' && viz[x - 1][y] == 0) { q1.push(x - 1); q2.push(y); dist[x - 1][y] = dist[x][y] + 1; } if (x + 1 < n && a[x + 1][y] == '.' && viz[x + 1][y] == 0) { q1.push(x + 1); q2.push(y); dist[x + 1][y] = dist[x][y] + 1; } if (y - 1 >= 0 && a[x][y - 1] == '.' && viz[x][y - 1] == 0) { q1.push(x); q2.push(y - 1); dist[x][y - 1] = dist[x][y] + 1; } if (y + 1 < m && a[x][y + 1] == '.' && viz[x][y + 1] == 0) { q1.push(x); q2.push(y + 1); dist[x][y + 1] = dist[x][y] + 1; } q1.pop(); q2.pop(); } ans = max(ans, dist[v[j].f][v[j].s]); } cout << ans << endl; }
insert
37
37
37
41
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define vi vector<int> #define vll vector<ll> #define pll pair<ll, ll> #define pii pair<int, int> #define pb push_back #define ppb pop_back #define mp make_pair #define SIZE 25 ll H, W; string arr[SIZE]; ll vis[SIZE][SIZE]; ll bfs(ll a, ll b, ll x, ll y) { queue<pair<pll, ll>> q; q.push(mp(mp(a, b), 0)); for (ll i = 0; i < H; ++i) { for (ll j = 0; j < W; ++j) { vis[i][j] = false; if (arr[i][j] == '#') { vis[i][j] = true; } } } vis[a][b] = true; ll ans = -1; while (!q.empty()) { pair<pll, ll> u = q.front(); q.pop(); ll i = u.first.first; ll j = u.first.second; ll d = u.second; // cout<<i<<" "<<j<<" "<<d<<endl; if (i == x && j == y) { ans = d; // cout<<ans<<endl; break; } if (i + 1 < H && vis[i + 1][j] == false) { vis[i + 1][j] = true; q.push(mp(mp(i + 1, j), d + 1)); } if (j + 1 < W && vis[i][j + 1] == false) { vis[i][j + 1] = true; q.push(mp(mp(i, j + 1), d + 1)); } if (i - 1 >= 0 && vis[i - 1][j] == false) { vis[i - 1][j] = true; q.push(mp(mp(i - 1, j), d + 1)); } if (j - 1 >= 0 && vis[i][j - 1] == false) { vis[i][j - 1] = true; q.push(mp(mp(i, j - 1), d + 1)); } } return ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> H >> W; for (ll i = 0; i < H; ++i) { string str; cin >> str; arr[i] = str; } ll ans = 0; for (ll i = 0; i < H; ++i) { for (ll j = 0; j < W; ++j) { for (ll k = 0; k < H; ++k) { for (ll l = 0; l < W; ++l) { if (arr[i][j] != '#' && arr[k][l] != '#') { ans = max(ans, bfs(i, j, k, l)); // cout<<i<<","<<j<<" "<<k<<","<<l<<" "<<ans<<endl; } } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define vi vector<int> #define vll vector<ll> #define pll pair<ll, ll> #define pii pair<int, int> #define pb push_back #define ppb pop_back #define mp make_pair #define SIZE 25 ll H, W; string arr[SIZE]; ll vis[SIZE][SIZE]; ll bfs(ll a, ll b, ll x, ll y) { queue<pair<pll, ll>> q; q.push(mp(mp(a, b), 0)); for (ll i = 0; i < H; ++i) { for (ll j = 0; j < W; ++j) { vis[i][j] = false; if (arr[i][j] == '#') { vis[i][j] = true; } } } vis[a][b] = true; ll ans = -1; while (!q.empty()) { pair<pll, ll> u = q.front(); q.pop(); ll i = u.first.first; ll j = u.first.second; ll d = u.second; // cout<<i<<" "<<j<<" "<<d<<endl; if (i == x && j == y) { ans = d; // cout<<ans<<endl; break; } if (i + 1 < H && vis[i + 1][j] == false) { vis[i + 1][j] = true; q.push(mp(mp(i + 1, j), d + 1)); } if (j + 1 < W && vis[i][j + 1] == false) { vis[i][j + 1] = true; q.push(mp(mp(i, j + 1), d + 1)); } if (i - 1 >= 0 && vis[i - 1][j] == false) { vis[i - 1][j] = true; q.push(mp(mp(i - 1, j), d + 1)); } if (j - 1 >= 0 && vis[i][j - 1] == false) { vis[i][j - 1] = true; q.push(mp(mp(i, j - 1), d + 1)); } } return ans; } int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> H >> W; for (ll i = 0; i < H; ++i) { string str; cin >> str; arr[i] = str; } ll ans = 0; for (ll i = 0; i < H; ++i) { for (ll j = 0; j < W; ++j) { for (ll k = 0; k < H; ++k) { for (ll l = 0; l < W; ++l) { if (arr[i][j] != '#' && arr[k][l] != '#') { ans = max(ans, bfs(i, j, k, l)); // cout<<i<<","<<j<<" "<<k<<","<<l<<" "<<ans<<endl; } } } } } cout << ans << endl; }
replace
59
63
59
63
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; char maze[23][23]; bool visited[23][23]; int level[23][23]; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int h, w; int hx, hy; int bfs(int fx, int fy) { memset(visited, 0, sizeof(visited)); memset(level, 0, sizeof(level)); int mx = 0; queue<pair<int, int>> q; q.push({fx, fy}); visited[fx][fy] = 1; level[fx][fy] = 0; while (!q.empty()) { int px = q.front().first; int py = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int x = px + dx[i]; int y = py + dy[i]; if (!visited[x][y] && maze[x][y] == '.' && x >= 0 && y >= 0 && x < h && y < w) { level[x][y] = level[px][py] + 1; visited[x][y] = 1; q.push({x, y}); if (mx < level[x][y]) { mx = level[x][y]; hx = x; hy = y; } } } } return mx; } int main() { cin >> h >> w; int x, y; bool f = 1; int ans = 0; queue<pair<int, int>> nodes; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> maze[i][j]; if (f && maze[i][j] == '.') { nodes.push({i, j}); } } } for (int i = 0; !nodes.empty(); i++) { x = nodes.front().first; y = nodes.front().second; ans = max(ans, bfs(x, y)); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; char maze[23][23]; bool visited[23][23]; int level[23][23]; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int h, w; int hx, hy; int bfs(int fx, int fy) { memset(visited, 0, sizeof(visited)); memset(level, 0, sizeof(level)); int mx = 0; queue<pair<int, int>> q; q.push({fx, fy}); visited[fx][fy] = 1; level[fx][fy] = 0; while (!q.empty()) { int px = q.front().first; int py = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int x = px + dx[i]; int y = py + dy[i]; if (!visited[x][y] && maze[x][y] == '.' && x >= 0 && y >= 0 && x < h && y < w) { level[x][y] = level[px][py] + 1; visited[x][y] = 1; q.push({x, y}); if (mx < level[x][y]) { mx = level[x][y]; hx = x; hy = y; } } } } return mx; } int main() { cin >> h >> w; int x, y; bool f = 1; int ans = 0; queue<pair<int, int>> nodes; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> maze[i][j]; if (f && maze[i][j] == '.') { nodes.push({i, j}); } } } for (int i = 0; !nodes.empty(); i++) { x = nodes.front().first; y = nodes.front().second; nodes.pop(); ans = max(ans, bfs(x, y)); } cout << ans << endl; return 0; }
insert
62
62
62
63
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int H, W; vector<string> v; int d[30][30]; int main() { cin >> H >> W; for (int i = 0; i < H; i++) { string S; cin >> S; v.push_back(S); } for (int i = 0; i < H * W; i++) { for (int j = 0; j < H * W; j++) { int ix = i % W, iy = i / W; int jx = j % W, jy = j / W; if (abs(ix - jx) + abs(iy - jy) == 1 && v[iy][ix] == '.' && v[jy][jx] == '.') { d[i][j] = d[j][i] = 1; } else if (i == j) { d[i][j] = d[j][i] = 0; } else { d[i][j] = d[j][i] = INT_MAX / 3; } } } for (int k = 0; k < H * W; k++) { for (int i = 0; i < H * W; i++) { for (int j = 0; j < H * W; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (v[i][j] == '.') { for (int ii = 0; ii < H; ii++) { for (int jj = 0; jj < W; jj++) { if (v[ii][jj] == '.') { ans = max(ans, d[ii * W + jj][i * W + j]); } } } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int H, W; vector<string> v; int d[410][410]; int main() { cin >> H >> W; for (int i = 0; i < H; i++) { string S; cin >> S; v.push_back(S); } for (int i = 0; i < H * W; i++) { for (int j = 0; j < H * W; j++) { int ix = i % W, iy = i / W; int jx = j % W, jy = j / W; if (abs(ix - jx) + abs(iy - jy) == 1 && v[iy][ix] == '.' && v[jy][jx] == '.') { d[i][j] = d[j][i] = 1; } else if (i == j) { d[i][j] = d[j][i] = 0; } else { d[i][j] = d[j][i] = INT_MAX / 3; } } } for (int k = 0; k < H * W; k++) { for (int i = 0; i < H * W; i++) { for (int j = 0; j < H * W; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (v[i][j] == '.') { for (int ii = 0; ii < H; ii++) { for (int jj = 0; jj < W; jj++) { if (v[ii][jj] == '.') { ans = max(ans, d[ii * W + jj][i * W + j]); } } } } } } cout << ans << endl; }
replace
4
5
4
5
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // in-short-use macros #define ll long long int #define ld long double #define pb push_back #define mp make_pair #define ff first #define ss second #define mod 1000000007 // container-use typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<ll, ll> pll; typedef vector<pair<ll, ll>> vpl; #define all(c) (c).begin(), (c).end() #define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) #define present(c, x) ((c).find(x) != (c).end()) #define rep(i, a, b) for (int i = a; i <= b; i++) // main function here int a[25][25] = {0}; int n, m; int d[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; int bfs(int i, int j) { queue<pair<int, int>> q; int c = 0; int vis[25][25] = {0}, dis[25][25] = {0}; q.push({i, j}); vis[i][j] = 1; while (!q.empty()) { pair<int, int> el = q.front(); int x = el.ff, y = el.ss; for (int f = 0; f < 4; f++) { int newdx = d[f][0] + x, newdy = d[f][1] + y; if (newdx < 0 || newdx >= n || newdy < 0 || newdy >= m) continue; if (!vis[newdx][newdy] && a[newdx][newdy] != -1) { vis[newdx][newdy] = 1; dis[newdx][newdy] = dis[x][y] + 1; // cout<<newdx<<" "<<newdy<<" "<<dis[newdx][newdy]<<"\n"; c = max(c, dis[newdx][newdy]); q.push({newdx, newdy}); } } q.pop(); } return c; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("C:/Users/Saurav Kumar Singh/Desktop/cp/input.txt", "r", stdin); freopen("C:/Users/Saurav Kumar Singh/Desktop/cp/output.txt", "w", stdout); #endif cin >> n >> m; int q = 0; for (int i = 0; i < n; i++) { string c; cin >> c; for (int j = 0; j < m; j++) { if (c[j] == '#') a[i][j] = -1; } } // cout<<bfs(2,0); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == -1) continue; // cout<<bfs(i,j)<<"\n"; q = max(q, bfs(i, j)); } } cout << q << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; // in-short-use macros #define ll long long int #define ld long double #define pb push_back #define mp make_pair #define ff first #define ss second #define mod 1000000007 // container-use typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<ll, ll> pll; typedef vector<pair<ll, ll>> vpl; #define all(c) (c).begin(), (c).end() #define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++) #define present(c, x) ((c).find(x) != (c).end()) #define rep(i, a, b) for (int i = a; i <= b; i++) // main function here int a[25][25] = {0}; int n, m; int d[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; int bfs(int i, int j) { queue<pair<int, int>> q; int c = 0; int vis[25][25] = {0}, dis[25][25] = {0}; q.push({i, j}); vis[i][j] = 1; while (!q.empty()) { pair<int, int> el = q.front(); int x = el.ff, y = el.ss; for (int f = 0; f < 4; f++) { int newdx = d[f][0] + x, newdy = d[f][1] + y; if (newdx < 0 || newdx >= n || newdy < 0 || newdy >= m) continue; if (!vis[newdx][newdy] && a[newdx][newdy] != -1) { vis[newdx][newdy] = 1; dis[newdx][newdy] = dis[x][y] + 1; // cout<<newdx<<" "<<newdy<<" "<<dis[newdx][newdy]<<"\n"; c = max(c, dis[newdx][newdy]); q.push({newdx, newdy}); } } q.pop(); } return c; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; int q = 0; for (int i = 0; i < n; i++) { string c; cin >> c; for (int j = 0; j < m; j++) { if (c[j] == '#') a[i][j] = -1; } } // cout<<bfs(2,0); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == -1) continue; // cout<<bfs(i,j)<<"\n"; q = max(q, bfs(i, j)); } } cout << q << "\n"; return 0; }
replace
58
62
58
59
0
p02803
C++
Time Limit Exceeded
/* AuThOr GaRyMr */ #include <bits/stdc++.h> #define rb(a, b, c) for (int a = b; a <= c; ++a) #define rl(a, b, c) for (int a = b; a >= c; --a) #define LL long long #define IT iterator #define PB push_back #define II(a, b) make_pair(a, b) #define FIR first #define SEC second #define FREO freopen("check.out", "w", stdout) #define rep(a, b) for (int a = 0; a < b; ++a) #define KEEP while (1) #define SRAND \ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define random(a) rng() % a #define ALL(a) a.begin(), a.end() #define POB pop_back #define ff fflush(stdout) #define fastio ios::sync_with_stdio(false) using namespace std; const int INF = 0x3f3f3f3f; typedef pair<int, int> mp; typedef pair<mp, mp> superpair; int n, m; int v[22][22]; char maze[22][22]; int res = 0; bool check(int x, int y) { if (maze[x][y] == '.' && !v[x][y]) return 1; return 0; } void bfs(int now) { rb(i, 1, n) rb(j, 1, m) v[i][j] = 0; queue<mp> q; q.push(II(now, 0)); while (!q.empty()) { int now_ = q.front().FIR, step = q.front().SEC; q.pop(); res = max(res, step); int x, y; x = now_ / 100, y = now_ % 100; v[x][y] = 1; if (x != 1) { if (check(x - 1, y)) { q.push(II((x - 1) * 100 + y, step + 1)); } } if (x != n) { if (check(x + 1, y)) { q.push(II((x + 1) * 100 + y, step + 1)); } } if (y != 1) { if (check(x, y - 1)) { q.push(II((x)*100 + y - 1, step + 1)); } } if (y != m) { if (check(x, y + 1)) { q.push(II((x)*100 + y + 1, step + 1)); } } } } int main() { cin >> n >> m; rb(i, 1, n) rb(j, 1, m) cin >> maze[i][j]; rb(i, 1, n) rb(j, 1, m) { if (maze[i][j] == '.') bfs(i * 100 + j); } cout << res; return 0; }
/* AuThOr GaRyMr */ #include <bits/stdc++.h> #define rb(a, b, c) for (int a = b; a <= c; ++a) #define rl(a, b, c) for (int a = b; a >= c; --a) #define LL long long #define IT iterator #define PB push_back #define II(a, b) make_pair(a, b) #define FIR first #define SEC second #define FREO freopen("check.out", "w", stdout) #define rep(a, b) for (int a = 0; a < b; ++a) #define KEEP while (1) #define SRAND \ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define random(a) rng() % a #define ALL(a) a.begin(), a.end() #define POB pop_back #define ff fflush(stdout) #define fastio ios::sync_with_stdio(false) using namespace std; const int INF = 0x3f3f3f3f; typedef pair<int, int> mp; typedef pair<mp, mp> superpair; int n, m; int v[22][22]; char maze[22][22]; int res = 0; bool check(int x, int y) { if (maze[x][y] == '.' && !v[x][y]) return 1; return 0; } void bfs(int now) { rb(i, 1, n) rb(j, 1, m) v[i][j] = 0; queue<mp> q; q.push(II(now, 0)); while (!q.empty()) { int now_ = q.front().FIR, step = q.front().SEC; q.pop(); res = max(res, step); int x, y; x = now_ / 100, y = now_ % 100; if (v[x][y]) continue; v[x][y] = 1; if (x != 1) { if (check(x - 1, y)) { q.push(II((x - 1) * 100 + y, step + 1)); } } if (x != n) { if (check(x + 1, y)) { q.push(II((x + 1) * 100 + y, step + 1)); } } if (y != 1) { if (check(x, y - 1)) { q.push(II((x)*100 + y - 1, step + 1)); } } if (y != m) { if (check(x, y + 1)) { q.push(II((x)*100 + y + 1, step + 1)); } } } } int main() { cin >> n >> m; rb(i, 1, n) rb(j, 1, m) cin >> maze[i][j]; rb(i, 1, n) rb(j, 1, m) { if (maze[i][j] == '.') bfs(i * 100 + j); } cout << res; return 0; }
insert
45
45
45
47
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i) #define DEBUG(x) cout << #x << ": " << (x) << '\n' #define DEBUG_VEC(v) \ cout << #v << ":"; \ REP(i, v.size()) cout << ' ' << v[i]; \ cout << '\n' #define ALL(a) (a).begin(), (a).end() template <typename T> inline void CHMAX(T &a, const T b) { if (a < b) a = b; } template <typename T> inline void CHMIN(T &a, const T b) { if (a > b) a = b; } constexpr ll MOD = 1000000007ll; // constexpr ll MOD=998244353ll; #define FIX(a) ((a) % MOD + MOD) % MOD const double EPS = 1e-11; #define EQ0(x) (abs((x)) < EPS) #define EQ(a, b) (abs((a) - (b)) < EPS) int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; int main() { ios::sync_with_stdio(false); cin.tie(0); // cout<<setprecision(10)<<fixed; int h, w; cin >> h >> w; char s[30][30]; REP(i, w + 2) { s[0][i] = s[h + 1][i] = '#'; } REP(i, h + 2) { s[i][0] = s[i][w + 1] = '#'; } REP(i, h) { REP(j, w) { cin >> s[i + 1][j + 1]; } } int ans = 0; FOR(x, 1, h + 1) { FOR(y, 1, w + 1) { if (s[x][y] == '#') { continue; } FOR(a, 1, h + 1) { FOR(b, 1, w + 1) { if (s[a][b] == '#') { continue; } bool used[30][30]; int d[30][30]; FOR(i, 1, h + 1) { FOR(j, 1, w + 1) { used[i][j] = false; } } used[x][y] = true; d[x][y] = 0; queue<pii> que; que.push(pii(x, y)); while (que.size()) { pii uv = que.front(); que.pop(); int u = uv.first, v = uv.second; if (u == a && v == b) { break; } REP(i, 4) { int uu = u + dx[i], vv = v + dy[i]; if (s[uu][vv] == '#' || used[uu][vv]) { continue; } que.push(pii(uu, vv)); d[uu][vv] = d[u][v] + 1; } } CHMAX(ans, d[a][b]); } } } } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i) #define DEBUG(x) cout << #x << ": " << (x) << '\n' #define DEBUG_VEC(v) \ cout << #v << ":"; \ REP(i, v.size()) cout << ' ' << v[i]; \ cout << '\n' #define ALL(a) (a).begin(), (a).end() template <typename T> inline void CHMAX(T &a, const T b) { if (a < b) a = b; } template <typename T> inline void CHMIN(T &a, const T b) { if (a > b) a = b; } constexpr ll MOD = 1000000007ll; // constexpr ll MOD=998244353ll; #define FIX(a) ((a) % MOD + MOD) % MOD const double EPS = 1e-11; #define EQ0(x) (abs((x)) < EPS) #define EQ(a, b) (abs((a) - (b)) < EPS) int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; int main() { ios::sync_with_stdio(false); cin.tie(0); // cout<<setprecision(10)<<fixed; int h, w; cin >> h >> w; char s[30][30]; REP(i, w + 2) { s[0][i] = s[h + 1][i] = '#'; } REP(i, h + 2) { s[i][0] = s[i][w + 1] = '#'; } REP(i, h) { REP(j, w) { cin >> s[i + 1][j + 1]; } } int ans = 0; FOR(x, 1, h + 1) { FOR(y, 1, w + 1) { if (s[x][y] == '#') { continue; } FOR(a, 1, h + 1) { FOR(b, 1, w + 1) { if (s[a][b] == '#') { continue; } bool used[30][30]; int d[30][30]; FOR(i, 1, h + 1) { FOR(j, 1, w + 1) { used[i][j] = false; } } used[x][y] = true; d[x][y] = 0; queue<pii> que; que.push(pii(x, y)); while (que.size()) { pii uv = que.front(); que.pop(); int u = uv.first, v = uv.second; if (u == a && v == b) { break; } REP(i, 4) { int uu = u + dx[i], vv = v + dy[i]; if (s[uu][vv] == '#' || used[uu][vv]) { continue; } used[uu][vv] = true; que.push(pii(uu, vv)); d[uu][vv] = d[u][v] + 1; } } CHMAX(ans, d[a][b]); } } } } cout << ans << '\n'; return 0; }
insert
85
85
85
86
TLE
p02803
C++
Runtime Error
/* * ID: juryc * PROG: Maze Master * LANG: C++ */ #include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; vector<string> v(h); for (string &i : v) cin >> i; int val = h * w; vector<vector<int>> dp(val, vector<int>(val, 0x3f3f3f3f)); for (int i = 0; i < h * w; i++) dp[i][i] = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (v[i][j] == '#') continue; int arx[] = {-1, 1, 0, 0}; int ary[] = {0, 0, -1, 1}; for (int k = 0; k < 4; k++) { int nx = i + arx[k], ny = j + ary[k]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (v[nx][ny] == '#') dp[i * h + j][nx * h + ny] = 0x3f3f3f3f; else dp[i * h + j][nx * h + ny] = 1; } } } for (int k = 0; k < val; k++) { for (int i = 0; i < val; i++) { for (int j = 0; j < val; j++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); } } } int mx = 0; for (int i = 0; i < val; i++) for (int j = 0; j < val; j++) if (dp[i][j] != 0x3f3f3f3f) mx = max(mx, dp[i][j]); cout << mx << "\n"; return 0; }
/* * ID: juryc * PROG: Maze Master * LANG: C++ */ #include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; vector<string> v(h); for (string &i : v) cin >> i; int val = h * w; vector<vector<int>> dp(val, vector<int>(val, 0x3f3f3f3f)); for (int i = 0; i < h * w; i++) dp[i][i] = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (v[i][j] == '#') continue; int arx[] = {-1, 1, 0, 0}; int ary[] = {0, 0, -1, 1}; for (int k = 0; k < 4; k++) { int nx = i + arx[k], ny = j + ary[k]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (v[nx][ny] == '#') dp[i * w + j][nx * w + ny] = 0x3f3f3f3f; else { dp[i * w + j][nx * w + ny] = 1; } } } } for (int k = 0; k < val; k++) { for (int i = 0; i < val; i++) { for (int j = 0; j < val; j++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); } } } int mx = 0; for (int i = 0; i < val; i++) for (int j = 0; j < val; j++) if (dp[i][j] != 0x3f3f3f3f) mx = max(mx, dp[i][j]); cout << mx << "\n"; return 0; }
replace
28
31
28
32
0
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vector<T>>; using vl = vector<ll>; const ll MOD = 1e9 + 7; const ll INF = 1e14; const long double EPS = 1e-11; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (int i = (l); i < (r); i++) #define reper(i, l, r) for (int i = (r)-1; i >= (l); i--) void init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } //--------------------------------------------------------------------------------// int main() { init(); ll H, W; cin >> H >> W; vc<string> G(H); rep(i, H) cin >> G[i]; vl dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0}; ll ans = 0; queue<pair<ll, ll>> q; vvc<ll> road(H, vc<ll>(W, INF)); rep(i, H) rep(j, W) { if (G[i][j] == '#') continue; vvc<ll> dist; dist = road; dist[i][j] = 0; q.emplace(i, j); while (!q.empty()) { ll x, y; tie(x, y) = q.front(); q.pop(); rep(d, 4) { ll nx = x + dx[d], ny = y + dy[d]; if (nx < 0 || nx > H - 1 || ny < 0 || ny > W - 1) continue; if (G[nx][ny] == '#' || dist[nx][ny] <= dist[x][y]) continue; dist[nx][ny] = dist[x][y] + 1; q.emplace(nx, ny); } } ll tmp = 0; for (auto R : dist) for (auto r : R) if (r != INF) tmp = max(tmp, r); ans = max(ans, tmp); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vector<T>>; using vl = vector<ll>; const ll MOD = 1e9 + 7; const ll INF = 1e14; const long double EPS = 1e-11; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (int i = (l); i < (r); i++) #define reper(i, l, r) for (int i = (r)-1; i >= (l); i--) void init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } //--------------------------------------------------------------------------------// int main() { init(); ll H, W; cin >> H >> W; vc<string> G(H); rep(i, H) cin >> G[i]; vl dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0}; ll ans = 0; queue<pair<ll, ll>> q; vvc<ll> road(H, vc<ll>(W, INF)); rep(i, H) rep(j, W) { if (G[i][j] == '#') continue; vvc<ll> dist; dist = road; dist[i][j] = 0; q.emplace(i, j); while (!q.empty()) { ll x, y; tie(x, y) = q.front(); q.pop(); rep(d, 4) { ll nx = x + dx[d], ny = y + dy[d]; if (nx < 0 || nx > H - 1 || ny < 0 || ny > W - 1) continue; if (G[nx][ny] == '#' || dist[nx][ny] <= dist[x][y] + 1) continue; dist[nx][ny] = dist[x][y] + 1; q.emplace(nx, ny); } } ll tmp = 0; for (auto R : dist) for (auto r : R) if (r != INF) tmp = max(tmp, r); ans = max(ans, tmp); } cout << ans << endl; return 0; }
replace
55
56
55
56
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> P1; typedef pair<P, P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define mod 1000000007 #define fi first #define sc second #define rep(i, x) for (long long i = 0; i < x; i++) #define repn(i, x) for (long long i = 1; i <= x; i++) #define SORT(x) sort(x.begin(), x.end()) #define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end()) #define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin()) #define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin()) vector<pair<string, P>> vec; // vector<vector<int>> data(3, vector<int>(4)); #define MAX_N 301 #define INF 10000000000001 long long d[MAX_N][MAX_N]; long long N; void warshall_floyd() { for (int k = 0; k < N; k++) for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } int main() { long long H; long long W; scanf("%lld", &H); vector<string> S(H - 1 + 1); scanf("%lld", &W); for (int i = 0; i <= H - 1; i++) { cin >> S[i]; } for (int i = 0; i <= MAX_N - 1; i++) { for (int j = 0; j <= MAX_N - 1; j++) { if (i == j) d[i][j] = 0; else d[i][j] = INF; } } rep(i, H) { rep(j, W) { if (i > 0 && S[i][j] == '.' && S[i - 1][j] == '.') { ll f = i * W + j, t = (i - 1) * W + j; d[f][t] = 1; d[t][f] = 1; } if (j > 0 && S[i][j] == '.' && S[i][j - 1] == '.') { ll f = i * W + j, t = i * W + j - 1; d[f][t] = 1; d[t][f] = 1; } if (i < H - 1 && S[i][j] == '.' && S[i + 1][j] == '.') { ll f = i * W + j, t = (i + 1) * W + j; d[f][t] = 1; d[t][f] = 1; } if (j < W - 1 && S[i][j] == '.' && S[i][j + 1] == '.') { ll f = i * W + j, t = i * W + j + 1; d[f][t] = 1; d[t][f] = 1; } } } N = H * W; warshall_floyd(); ll res = 0; rep(i, H * W) { rep(j, H * W) { // cout << i/W << " " << i%W << " " << j/W << " " << j%W << " " << // d[i][j] << endl; if (d[i][j] < INF) res = max(res, d[i][j]); } } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> P1; typedef pair<P, P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define mod 1000000007 #define fi first #define sc second #define rep(i, x) for (long long i = 0; i < x; i++) #define repn(i, x) for (long long i = 1; i <= x; i++) #define SORT(x) sort(x.begin(), x.end()) #define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end()) #define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin()) #define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin()) vector<pair<string, P>> vec; // vector<vector<int>> data(3, vector<int>(4)); #define MAX_N 401 #define INF 10000000000001 long long d[MAX_N][MAX_N]; long long N; void warshall_floyd() { for (int k = 0; k < N; k++) for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } int main() { long long H; long long W; scanf("%lld", &H); vector<string> S(H - 1 + 1); scanf("%lld", &W); for (int i = 0; i <= H - 1; i++) { cin >> S[i]; } for (int i = 0; i <= MAX_N - 1; i++) { for (int j = 0; j <= MAX_N - 1; j++) { if (i == j) d[i][j] = 0; else d[i][j] = INF; } } rep(i, H) { rep(j, W) { if (i > 0 && S[i][j] == '.' && S[i - 1][j] == '.') { ll f = i * W + j, t = (i - 1) * W + j; d[f][t] = 1; d[t][f] = 1; } if (j > 0 && S[i][j] == '.' && S[i][j - 1] == '.') { ll f = i * W + j, t = i * W + j - 1; d[f][t] = 1; d[t][f] = 1; } if (i < H - 1 && S[i][j] == '.' && S[i + 1][j] == '.') { ll f = i * W + j, t = (i + 1) * W + j; d[f][t] = 1; d[t][f] = 1; } if (j < W - 1 && S[i][j] == '.' && S[i][j + 1] == '.') { ll f = i * W + j, t = i * W + j + 1; d[f][t] = 1; d[t][f] = 1; } } } N = H * W; warshall_floyd(); ll res = 0; rep(i, H * W) { rep(j, H * W) { // cout << i/W << " " << i%W << " " << j/W << " " << j%W << " " << // d[i][j] << endl; if (d[i][j] < INF) res = max(res, d[i][j]); } } cout << res << endl; return 0; }
replace
23
24
23
24
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = (int)1e9 + 7; const int INF = (int)1e9; const ll LINF = (ll)1e18; int main() { int h, w; cin >> h >> w; int n = h * w; string s[h]; for (int i = 0; i < h; i++) { cin >> s[i]; } int d[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = INF; } d[i][i] = 0; } const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == '#') continue; for (int k = 0; k < 4; k++) { int nx = i + dx[k]; int ny = j + dy[k]; if (nx < 0 || h <= nx || ny < 0 || w <= ny || s[nx][ny] == '#') continue; d[h * i + j][h * nx + ny] = 1; } } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } int ans = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (d[i][j] != INF) { ans = max(ans, d[i][j]); } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = (int)1e9 + 7; const int INF = (int)1e9; const ll LINF = (ll)1e18; int main() { int h, w; cin >> h >> w; int n = h * w; string s[h]; for (int i = 0; i < h; i++) { cin >> s[i]; } int d[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = INF; } d[i][i] = 0; } const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == '#') continue; for (int k = 0; k < 4; k++) { int nx = i + dx[k]; int ny = j + dy[k]; if (nx < 0 || h <= nx || ny < 0 || w <= ny || s[nx][ny] == '#') continue; d[w * i + j][w * nx + ny] = 1; } } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } int ans = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (d[i][j] != INF) { ans = max(ans, d[i][j]); } } } cout << ans << endl; }
replace
33
34
33
34
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; int h, w; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, -1, 0, 1}; vector<string> s; int bfs(int sx, int sy, int gx, int gy) { int d[w][h]; // スタート位置からの距離を持つ配列 // 配列を初期化しておく for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { d[i][j] = -1; } } queue<pair<int, int>> que; // スタート地点をキューに入れ、距離を0にする que.push(pair<int, int>(sx, sy)); d[sx][sy] = 0; while (!que.empty()) { pair<int, int> p = que.front(); que.pop(); // キューの先頭を取り出す if (p.first == gx && p.second == gy) break; // 取り出したものがゴールならば探索をやめる // 4方向をループ for (int i = 0; i < 4; ++i) { // 移動した後の点を(nx,ny)とする int nx = p.first + dx[i]; int ny = p.second + dy[i]; // 移動できる場所か判定 if (0 <= nx && nx < w && 0 <= ny && ny < h && s[ny][nx] == '.' && d[nx][ny] == -1) { // 移動できる場合はキューに入れて、その点の距離を設定 que.push(pair<int, int>(nx, ny)); d[nx][ny] = d[p.first][p.second] + 1; } } } int res = 0; for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { res = max(res, d[i][j]); } } return res; } int main() { cin >> h >> w; s.resize(h); for (int i = 0; i < h; ++i) { cin >> s[i]; } vector<pair<int, int>> p; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (s[i][j] == '.') { p.push_back(make_pair(i, j)); } } } int ans = 0; for (int i = 0; i < p.size(); ++i) { for (int j = 0; j < p.size(); ++j) { ans = max(ans, bfs(p[i].first, p[i].second, p[j].first, p[j].second)); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; int h, w; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, -1, 0, 1}; vector<string> s; int bfs(int sx, int sy, int gx, int gy) { int d[w][h]; // スタート位置からの距離を持つ配列 // 配列を初期化しておく for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { d[i][j] = -1; } } queue<pair<int, int>> que; // スタート地点をキューに入れ、距離を0にする que.push(pair<int, int>(sx, sy)); d[sx][sy] = 0; while (!que.empty()) { pair<int, int> p = que.front(); que.pop(); // キューの先頭を取り出す if (p.first == gx && p.second == gy) break; // 取り出したものがゴールならば探索をやめる // 4方向をループ for (int i = 0; i < 4; ++i) { // 移動した後の点を(nx,ny)とする int nx = p.first + dx[i]; int ny = p.second + dy[i]; // 移動できる場所か判定 if (0 <= nx && nx < w && 0 <= ny && ny < h && s[ny][nx] == '.' && d[nx][ny] == -1) { // 移動できる場合はキューに入れて、その点の距離を設定 que.push(pair<int, int>(nx, ny)); d[nx][ny] = d[p.first][p.second] + 1; } } } int res = 0; for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { res = max(res, d[i][j]); } } return res; } int main() { cin >> h >> w; s.resize(h); for (int i = 0; i < h; ++i) { cin >> s[i]; } vector<pair<int, int>> p; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (s[i][j] == '.') { p.push_back(make_pair(i, j)); } } } int ans = 0; for (int i = 0; i < p.size() - 1; ++i) { for (int j = i + 1; j < p.size(); ++j) { ans = max(ans, bfs(p[i].second, p[i].first, p[j].second, p[j].first)); } } cout << ans << endl; }
replace
85
88
85
88
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using int32 = int_fast32_t; using int64 = int_fast64_t; const int32 INF = 1e9; const int32 MOD = 1e9 + 7; const int64 LLINF = 1e18; #define YES(n) cout << ((n) ? "YES\n" : "NO\n") #define Yes(n) cout << ((n) ? "Yes\n" : "No\n") #define POSSIBLE cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n") #define ANS(n) cout << (n) << "\n" #define REP(i, n) for (int64 i = 0; i < (n); ++i) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define FORR(i, a, b) for (int64 i = (a); i >= (b); i--) #define ALL(obj) (obj).begin(), (obj).end() #define pii pair<int32, int32> #define pll pair<int64, int64> #define pb(a) push_back(a) #define mp make_pair int32 di[4] = {0, 1, 0, -1}; int32 dj[4] = {1, 0, -1, 0}; int main() { cin.tie(0); ios::sync_with_stdio(false); int32 h, w; cin >> h >> w; vector<string> s(h); REP(i, h) cin >> s[i]; vector<vector<int32>> dist; int32 ans = 0; REP(si, h) REP(sj, h) { queue<pii> que; que.emplace(si, sj); dist = vector<vector<int32>>(h, vector<int32>(w, INF)); dist[si][sj] = 0; while (!que.empty()) { int32 ci, cj; tie(ci, cj) = que.front(); que.pop(); REP(i, 4) { int32 ni = ci + di[i]; int32 nj = cj + dj[i]; if (ni < 0 || h <= ni) continue; if (nj < 0 || w <= nj) continue; if (s[ni][nj] == '#') continue; if (dist[ni][nj] > dist[ci][cj] + 1) { dist[ni][nj] = dist[ci][cj] + 1; que.emplace(ni, nj); } } } REP(i, h) REP(j, w) { if (dist[i][j] != INF && dist[i][j] > ans) { ans = dist[i][j]; } } } ANS(ans); return 0; }
#include <bits/stdc++.h> using namespace std; using int32 = int_fast32_t; using int64 = int_fast64_t; const int32 INF = 1e9; const int32 MOD = 1e9 + 7; const int64 LLINF = 1e18; #define YES(n) cout << ((n) ? "YES\n" : "NO\n") #define Yes(n) cout << ((n) ? "Yes\n" : "No\n") #define POSSIBLE cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n") #define ANS(n) cout << (n) << "\n" #define REP(i, n) for (int64 i = 0; i < (n); ++i) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define FORR(i, a, b) for (int64 i = (a); i >= (b); i--) #define ALL(obj) (obj).begin(), (obj).end() #define pii pair<int32, int32> #define pll pair<int64, int64> #define pb(a) push_back(a) #define mp make_pair int32 di[4] = {0, 1, 0, -1}; int32 dj[4] = {1, 0, -1, 0}; int main() { cin.tie(0); ios::sync_with_stdio(false); int32 h, w; cin >> h >> w; vector<string> s(h); REP(i, h) cin >> s[i]; vector<vector<int32>> dist; int32 ans = 0; REP(si, h) REP(sj, w) { if (s[si][sj] == '#') continue; queue<pii> que; que.emplace(si, sj); dist = vector<vector<int32>>(h, vector<int32>(w, INF)); dist[si][sj] = 0; while (!que.empty()) { int32 ci, cj; tie(ci, cj) = que.front(); que.pop(); REP(i, 4) { int32 ni = ci + di[i]; int32 nj = cj + dj[i]; if (ni < 0 || h <= ni) continue; if (nj < 0 || w <= nj) continue; if (s[ni][nj] == '#') continue; if (dist[ni][nj] > dist[ci][cj] + 1) { dist[ni][nj] = dist[ci][cj] + 1; que.emplace(ni, nj); } } } REP(i, h) REP(j, w) { if (dist[i][j] != INF && dist[i][j] > ans) { ans = dist[i][j]; } } } ANS(ans); return 0; }
replace
35
36
35
38
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; typedef long long ll; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<char> VC; typedef vector<double> VD; typedef vector<string> VS; typedef vector<ll> VLL; typedef vector<PLL> VP; const static int INF = 1000000000; const static int MOD = 1000000007; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; 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; } #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 rept(i, m, n) for (ll i = m; i < n; i++) #define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr) #define all(x) (x).begin(), (x).end() #define F first #define S second #define PF push_front #define PB push_back #define SORT(V) sort((V).begin(), (V).end()) #define RVERSE(V) reverse((V).begin(), (V).end()) #define paired make_pair #define PRINT(V) \ for (auto v : (V)) \ cout << v << " " // charを整数に int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } // 累積和 for (int i = 0; i < N; ++i) s[i+1] = s[i] + a[i]; void cum_sum(int N, vector<double> a, vector<double> &s) { for (int i = 0; i < N; i++) { s[i + 1] = s[i] + a[i]; } } // ユークリッドの控除法 ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // 最小公倍数 ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; // Be careful not to overflow } // 素数判定 bool is_prime(long long n) { if (n <= 1) return false; for (long long p = 2; p * p <= n; ++p) { if (n % p == 0) return false; } return true; } int getdigit(ll num) { unsigned digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } // 空白文字も入力 getline(cin, S); // 桁数指定 setprecision int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int H, W; int maxi = 0; cin >> H >> W; vector<string> S(H); rep(i, H) { cin >> S[i]; } for (int i = 0; i < H; i++) { for (int j = 0; j < S[i].size(); j++) { int sx, sy; if (S[i][j] == '.') { sx = i; sy = j; } vector<vector<int>> dist(H, vector<int>(W, -1)); queue<pair<int, int>> que; dist[sx][sy] = 0; que.push(make_pair(sx, sy)); while (!que.empty()) { auto v = que.front(); que.pop(); int vx = v.F; int vy = v.S; for (int k = 0; k < 4; k++) { int nx = vx + dx[k]; int ny = vy + dy[k]; if (nx < 0 || ny < 0 || nx >= H || ny >= W) continue; if (S[nx][ny] == '#') continue; if (dist[nx][ny] != -1) continue; dist[nx][ny] = dist[vx][vy] + 1; que.push(make_pair(nx, ny)); } } rep(l, H) { rep(m, S[l].size()) { if (S[l][m] != '#') { maxi = max(maxi, dist[l][m]); } } } } } cout << maxi << endl; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; typedef long long ll; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<char> VC; typedef vector<double> VD; typedef vector<string> VS; typedef vector<ll> VLL; typedef vector<PLL> VP; const static int INF = 1000000000; const static int MOD = 1000000007; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; 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; } #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 rept(i, m, n) for (ll i = m; i < n; i++) #define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr) #define all(x) (x).begin(), (x).end() #define F first #define S second #define PF push_front #define PB push_back #define SORT(V) sort((V).begin(), (V).end()) #define RVERSE(V) reverse((V).begin(), (V).end()) #define paired make_pair #define PRINT(V) \ for (auto v : (V)) \ cout << v << " " // charを整数に int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } // 累積和 for (int i = 0; i < N; ++i) s[i+1] = s[i] + a[i]; void cum_sum(int N, vector<double> a, vector<double> &s) { for (int i = 0; i < N; i++) { s[i + 1] = s[i] + a[i]; } } // ユークリッドの控除法 ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // 最小公倍数 ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; // Be careful not to overflow } // 素数判定 bool is_prime(long long n) { if (n <= 1) return false; for (long long p = 2; p * p <= n; ++p) { if (n % p == 0) return false; } return true; } int getdigit(ll num) { unsigned digit = 0; while (num != 0) { num /= 10; digit++; } return digit; } // 空白文字も入力 getline(cin, S); // 桁数指定 setprecision int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int H, W; int maxi = 0; cin >> H >> W; vector<string> S(H); rep(i, H) { cin >> S[i]; } for (int i = 0; i < H; i++) { for (int j = 0; j < S[i].size(); j++) { int sx, sy; if (S[i][j] == '.') { sx = i; sy = j; } else { continue; } vector<vector<int>> dist(H, vector<int>(W, -1)); queue<pair<int, int>> que; dist[sx][sy] = 0; que.push(make_pair(sx, sy)); while (!que.empty()) { auto v = que.front(); que.pop(); int vx = v.F; int vy = v.S; for (int k = 0; k < 4; k++) { int nx = vx + dx[k]; int ny = vy + dy[k]; if (nx < 0 || ny < 0 || nx >= H || ny >= W) continue; if (S[nx][ny] == '#') continue; if (dist[nx][ny] != -1) continue; dist[nx][ny] = dist[vx][vy] + 1; que.push(make_pair(nx, ny)); } } rep(l, H) { rep(m, S[l].size()) { if (S[l][m] != '#') { maxi = max(maxi, dist[l][m]); } } } } } cout << maxi << endl; }
insert
104
104
104
106
0
p02803
C++
Runtime Error
// --------------------------------------------------<TEMPLATE>-------------------------------------------------- // --------------------<optimizations>-------------------- #pragma GCC optimize("O3") //(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\ #pragma comment(linker, "/stack:200000000") //(UNCOMMENT WHEN NEEDED)\ #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")\ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // -------------------</optimizations>-------------------- // ---------------<Headers and namespaces>---------------- #include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <ratio> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace __gnu_pbds; using namespace __gnu_cxx; */ // ---------------</Headers and namespaces>--------------- // -----------------<Defines and typedefs>---------------- // typedef tree<int,null_type,less<int>,rb_tree_tag, \ tree_order_statistics_node_update> indexed_set; // order_of_key (val): returns the no. of values less than val // find_by_order (k): returns the iterator to kth largest element.(0-based) typedef long double LD; typedef long long ll; #define int ll #define pb push_back #define mp make_pair #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, a, b) for (int i = a; i < b; i++) #define REPD(i, n) for (int i = n - 1; i >= 0; i--) #define FORD(i, a, b) for (int i = a; i >= b; i--) #define remax(a, b) a = max(a, b) #define remin(a, b) a = min(a, b) #define all(v) v.begin(), v.end() typedef map<int, int> mii; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; typedef vector<pii> vpii; #define F first #define S second #define PQ(type) priority_queue<type> #define PQD(type) priority_queue<type, vector<type>, greater<type>> #define ITR ::iterator it #define WL(t) while (t--) #define SZ(x) ((int)(x).size()) #define runtime() ((double)clock() / CLOCKS_PER_SEC) #define TR(container, it) \ for (typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) #define sqr(x) ((x) * (x)) #define inrange(i, a, b) ((i >= min(a, b)) && (i <= max(a, b))) // -----<SCANF>----- #define sfi(x) scanf("%d", &x); #define sfi2(x, y) scanf("%d%d", &x, &y); #define sfi3(x, y, z) scanf("%d%d%d", &x, &y, &z); #define sfl(x) scanf("%lld", &x); #define sfl2(x, y) scanf("%lld%lld", &x, &y); #define sfl3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z); #define sfl4(x, y, z, x1) scanf("%lld%lld%lld%lld", &x, &y, &z, &x1); #define sfl5(x, y, z, x1, y1) \ scanf("%lld%lld%lld%lld%lld", &x, &y, &z, &x1, &y1); #define sfl6(x, y, z, x1, y1, z1) \ scanf("%lld%lld%lld%lld%lld%lld", &x, &y, &z, &x1, &y1, &z1); #define sfs(x) scanf("%s", x); #define sfs2(x, y) scanf("%s%s", x, y); #define sfs3(x, y, z) scanf("%s%s%s", x, y, z); // ----</SCANF>----- // ----<PRINTF>----- #define pfi(x) printf("%d\n", x); #define pfi2(x, y) printf("%d %d\n", x, y); #define pfi3(x, y, z) printf("%d %d %d\n", x, y, z); #define pfl(x) printf("%lld\n", x); #define pfl2(x, y) printf("%lld %lld\n", x, y); #define pfl3(x, y, z) printf("%lld %lld %lld\n", x, y, z); #define pfs(x) printf("%s\n", x); #define pfs2(x, y) printf("%s %s\n", x, y); #define pfs3(x, y, z) printf("%s %s %s\n", x, y, z); #define pwe(x) printf("%lld ", x); // print without end // ----</PRINTF>---- #define FLSH fflush(stdout) #define fileIO(name) \ freopen(name ".in", "r", stdin); \ freopen(name ".out", "w", stdout); #define PRECISION(x) cout << fixed << setprecision(x); #define FAST_IO \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); // ----------------</Defines and typedefs>---------------- // -------------------<Debugging stuff>------------------- #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 // ------------------</Debugging stuff>------------------- // ------------------------<Consts>----------------------- const int MAXN = 1000005; const int SQRTN = 1003; const int LOGN = 22; const double PI = acos(-1); #ifdef int const int INF = 1e16; #else const int INF = 1e9; #endif const int MOD = 1000000007; const int FMOD = 998244353; const double eps = 1e-9; // -----------------------</Consts>----------------------- // -------------------------<RNG>------------------------- mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count()); #define SHUF(v) shuffle(all(v), RNG); // Use mt19937_64 for 64 bit random numbers. // ------------------------</RNG>------------------------- // -----------------<Modular Arithmetic>------------------ template <typename T> T gcd(T a, T b) { return (b ? __gcd(a, b) : a); } template <typename T> T lcm(T a, T b) { return (a * (b / gcd(a, b))); } int add(int a, int b, int c = MOD) { int res = a + b; return (res >= c ? res - c : res); } int mod_neg(int a, int b, int c = MOD) { int res; if (abs(a - b) < c) res = a - b; else res = (a - b) % c; return (res < 0 ? res + c : res); } int mul(int a, int b, int c = MOD) { ll res = (ll)a * b; return (res >= c ? res % c : res); } ll mulmod(ll a, ll b, ll m = MOD) { ll q = (ll)(((LD)a * (LD)b) / (LD)m); ll r = a * b - q * m; if (r > m) r %= m; if (r < 0) r += m; return r; } template <typename T> T expo(T e, T n) { T x = 1, p = e; while (n) { if (n & 1) x = x * p; p = p * p; n >>= 1; } return x; } template <typename T> T power(T e, T n, T m = MOD) { T x = 1, p = e; while (n) { if (n & 1) x = mul(x, p, m); p = mul(p, p, m); n >>= 1; } return x; } template <typename T> T extended_euclid(T a, T b, T &x, T &y) { T xx = 0, yy = 1; y = 0; x = 1; while (b) { T q = a / b, t = b; b = a % b; a = t; t = xx; xx = x - q * xx; x = t; t = yy; yy = y - q * yy; y = t; } return a; } template <typename T> T mod_inverse(T a, T n = MOD) { T x, y, z = 0; T d = extended_euclid(a, n, x, y); return (d > 1 ? -1 : mod_neg(x, z, n)); } // -----------------</Modular Arithmetic>----------------- // --------------------------------------------------</TEMPLATE>-------------------------------------------------- void solvethetestcase(); signed main() { // (UNCOMMENT FOR CIN/COUT) \ FAST_IO PRECISION(10) int t = 1; // (UNCOMMENT FOR MULTIPLE TEST CASES) \ sfl(t); FOR(testcase, 1, t + 1) { // (UNCOMMENT FOR CODEJAM) \ printf("Case #%lld: ",testcase); solvethetestcase(); } } int n, m; char a[25][25]; int dist[505][505]; int conv(int i, int j) { i--; j--; return i * m + j; } void solvethetestcase() { sfl2(n, m) REP(i, 25) { REP(j, 25) { a[i][j] = '#'; } } FOR(i, 1, n + 1) { FOR(j, 1, m + 1) { cin >> a[i][j]; } } REP(i, 405) { REP(j, 405) { dist[i][j] = INF; } if (a[i / m + 1][(i % m) + 1] == '.') dist[i][i] = 0; } FOR(i, 1, n + 1) { FOR(j, 1, m + 1) { if (a[i][j] == '.' and a[i + 1][j] == '.') { dist[conv(i, j)][conv(i + 1, j)] = 1; dist[conv(i + 1, j)][conv(i, j)] = 1; } if (a[i][j] == '.' and a[i][j + 1] == '.') { dist[conv(i, j)][conv(i, j + 1)] = 1; dist[conv(i, j + 1)][conv(i, j)] = 1; } } } REP(k, 405) { REP(i, 405) { REP(j, 405) { remin(dist[i][j], dist[i][k] + dist[k][j]); } } } int ans = 0; REP(i, 405) { REP(j, 405) { if (dist[i][j] != INF) remax(ans, dist[i][j]); } } pfl(ans) }
// --------------------------------------------------<TEMPLATE>-------------------------------------------------- // --------------------<optimizations>-------------------- #pragma GCC optimize("O3") //(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\ #pragma comment(linker, "/stack:200000000") //(UNCOMMENT WHEN NEEDED)\ #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")\ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // -------------------</optimizations>-------------------- // ---------------<Headers and namespaces>---------------- #include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <ratio> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace __gnu_pbds; using namespace __gnu_cxx; */ // ---------------</Headers and namespaces>--------------- // -----------------<Defines and typedefs>---------------- // typedef tree<int,null_type,less<int>,rb_tree_tag, \ tree_order_statistics_node_update> indexed_set; // order_of_key (val): returns the no. of values less than val // find_by_order (k): returns the iterator to kth largest element.(0-based) typedef long double LD; typedef long long ll; #define int ll #define pb push_back #define mp make_pair #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, a, b) for (int i = a; i < b; i++) #define REPD(i, n) for (int i = n - 1; i >= 0; i--) #define FORD(i, a, b) for (int i = a; i >= b; i--) #define remax(a, b) a = max(a, b) #define remin(a, b) a = min(a, b) #define all(v) v.begin(), v.end() typedef map<int, int> mii; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; typedef vector<pii> vpii; #define F first #define S second #define PQ(type) priority_queue<type> #define PQD(type) priority_queue<type, vector<type>, greater<type>> #define ITR ::iterator it #define WL(t) while (t--) #define SZ(x) ((int)(x).size()) #define runtime() ((double)clock() / CLOCKS_PER_SEC) #define TR(container, it) \ for (typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) #define sqr(x) ((x) * (x)) #define inrange(i, a, b) ((i >= min(a, b)) && (i <= max(a, b))) // -----<SCANF>----- #define sfi(x) scanf("%d", &x); #define sfi2(x, y) scanf("%d%d", &x, &y); #define sfi3(x, y, z) scanf("%d%d%d", &x, &y, &z); #define sfl(x) scanf("%lld", &x); #define sfl2(x, y) scanf("%lld%lld", &x, &y); #define sfl3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z); #define sfl4(x, y, z, x1) scanf("%lld%lld%lld%lld", &x, &y, &z, &x1); #define sfl5(x, y, z, x1, y1) \ scanf("%lld%lld%lld%lld%lld", &x, &y, &z, &x1, &y1); #define sfl6(x, y, z, x1, y1, z1) \ scanf("%lld%lld%lld%lld%lld%lld", &x, &y, &z, &x1, &y1, &z1); #define sfs(x) scanf("%s", x); #define sfs2(x, y) scanf("%s%s", x, y); #define sfs3(x, y, z) scanf("%s%s%s", x, y, z); // ----</SCANF>----- // ----<PRINTF>----- #define pfi(x) printf("%d\n", x); #define pfi2(x, y) printf("%d %d\n", x, y); #define pfi3(x, y, z) printf("%d %d %d\n", x, y, z); #define pfl(x) printf("%lld\n", x); #define pfl2(x, y) printf("%lld %lld\n", x, y); #define pfl3(x, y, z) printf("%lld %lld %lld\n", x, y, z); #define pfs(x) printf("%s\n", x); #define pfs2(x, y) printf("%s %s\n", x, y); #define pfs3(x, y, z) printf("%s %s %s\n", x, y, z); #define pwe(x) printf("%lld ", x); // print without end // ----</PRINTF>---- #define FLSH fflush(stdout) #define fileIO(name) \ freopen(name ".in", "r", stdin); \ freopen(name ".out", "w", stdout); #define PRECISION(x) cout << fixed << setprecision(x); #define FAST_IO \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); // ----------------</Defines and typedefs>---------------- // -------------------<Debugging stuff>------------------- #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 // ------------------</Debugging stuff>------------------- // ------------------------<Consts>----------------------- const int MAXN = 1000005; const int SQRTN = 1003; const int LOGN = 22; const double PI = acos(-1); #ifdef int const int INF = 1e16; #else const int INF = 1e9; #endif const int MOD = 1000000007; const int FMOD = 998244353; const double eps = 1e-9; // -----------------------</Consts>----------------------- // -------------------------<RNG>------------------------- mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count()); #define SHUF(v) shuffle(all(v), RNG); // Use mt19937_64 for 64 bit random numbers. // ------------------------</RNG>------------------------- // -----------------<Modular Arithmetic>------------------ template <typename T> T gcd(T a, T b) { return (b ? __gcd(a, b) : a); } template <typename T> T lcm(T a, T b) { return (a * (b / gcd(a, b))); } int add(int a, int b, int c = MOD) { int res = a + b; return (res >= c ? res - c : res); } int mod_neg(int a, int b, int c = MOD) { int res; if (abs(a - b) < c) res = a - b; else res = (a - b) % c; return (res < 0 ? res + c : res); } int mul(int a, int b, int c = MOD) { ll res = (ll)a * b; return (res >= c ? res % c : res); } ll mulmod(ll a, ll b, ll m = MOD) { ll q = (ll)(((LD)a * (LD)b) / (LD)m); ll r = a * b - q * m; if (r > m) r %= m; if (r < 0) r += m; return r; } template <typename T> T expo(T e, T n) { T x = 1, p = e; while (n) { if (n & 1) x = x * p; p = p * p; n >>= 1; } return x; } template <typename T> T power(T e, T n, T m = MOD) { T x = 1, p = e; while (n) { if (n & 1) x = mul(x, p, m); p = mul(p, p, m); n >>= 1; } return x; } template <typename T> T extended_euclid(T a, T b, T &x, T &y) { T xx = 0, yy = 1; y = 0; x = 1; while (b) { T q = a / b, t = b; b = a % b; a = t; t = xx; xx = x - q * xx; x = t; t = yy; yy = y - q * yy; y = t; } return a; } template <typename T> T mod_inverse(T a, T n = MOD) { T x, y, z = 0; T d = extended_euclid(a, n, x, y); return (d > 1 ? -1 : mod_neg(x, z, n)); } // -----------------</Modular Arithmetic>----------------- // --------------------------------------------------</TEMPLATE>-------------------------------------------------- void solvethetestcase(); signed main() { // (UNCOMMENT FOR CIN/COUT) \ FAST_IO PRECISION(10) int t = 1; // (UNCOMMENT FOR MULTIPLE TEST CASES) \ sfl(t); FOR(testcase, 1, t + 1) { // (UNCOMMENT FOR CODEJAM) \ printf("Case #%lld: ",testcase); solvethetestcase(); } } int n, m; char a[25][25]; int dist[505][505]; int conv(int i, int j) { i--; j--; return i * m + j; } void solvethetestcase() { sfl2(n, m) REP(i, 25) { REP(j, 25) { a[i][j] = '#'; } } FOR(i, 1, n + 1) { FOR(j, 1, m + 1) { cin >> a[i][j]; } } REP(i, 405) { REP(j, 405) { dist[i][j] = INF; } if ((i / m + 1 <= n) and (i % m + 1 <= m) and a[i / m + 1][(i % m) + 1] == '.') dist[i][i] = 0; } FOR(i, 1, n + 1) { FOR(j, 1, m + 1) { if (a[i][j] == '.' and a[i + 1][j] == '.') { dist[conv(i, j)][conv(i + 1, j)] = 1; dist[conv(i + 1, j)][conv(i, j)] = 1; } if (a[i][j] == '.' and a[i][j + 1] == '.') { dist[conv(i, j)][conv(i, j + 1)] = 1; dist[conv(i, j + 1)][conv(i, j)] = 1; } } } REP(k, 405) { REP(i, 405) { REP(j, 405) { remin(dist[i][j], dist[i][k] + dist[k][j]); } } } int ans = 0; REP(i, 405) { REP(j, 405) { if (dist[i][j] != INF) remax(ans, dist[i][j]); } } pfl(ans) }
replace
294
295
294
296
0
p02803
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <tuple> #include <utility> #include <vector> #define ll long long #define itn int #define endl '\n' #define co(ans) cout << ans << endl #define COYE cout << "YES" << endl #define COYe cout << "Yes" << endl #define COye cout << "yes" << endl #define CONO cout << "NO" << endl #define CONo cout << "No" << endl #define COno cout << "no" << endl #define FORE(i, a) for (auto &i : a) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FFOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) FFOR(i, 1, n) #define PB push_back #define MP make_pair #define ALL(V) (V).begin(), (V).end() #define SORT(V) sort((V).begin(), (V).end()) #define REVERSE(V) reverse((V).begin(), (V).end()) #define EACH(V, i) \ for (typeof((V).begin()) i = (V).begin(); i != (V).end(); ++i) #define equals(a, b) (fabs((a) - (b)) < EPS) #define INF ((1LL << 62) - (1LL << 31)) #define EPS 1e-10 #define PI 3.141592653589793238 #define MOD 1000000007 #define MAX 5100000 using namespace std; using Edge = pair<ll, ll>; using Graph = vector<vector<int>>; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } 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; } typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int main() { ll H, W, ans = 0; cin >> H >> W; char S[H][W]; REP(i, H) { REP(j, W) { cin >> S[i][j]; } } REP(i, H) { REP(j, W) { if (S[i][j] == '#') continue; ll DIS[H][W]; bool CAN[H][W]; REP(k, H) { REP(l, W) { DIS[k][l] = MAX; CAN[k][l] = false; } } DIS[i][j] = 0; queue<pair<ll, ll>> Q; Q.push(MP(i, j)); while (!Q.empty()) { auto it = Q.front(); Q.pop(); ll vx = it.first, vy = it.second; CAN[vx][vy] = true; REP(k, 4) { ll nx = vx + dx[k], ny = vy + dy[k]; if (nx < 0 || H <= nx || ny < 0 || W <= ny) continue; if (S[nx][ny] == '#' || CAN[nx][ny]) continue; chmin(DIS[nx][ny], DIS[vx][vy] + 1); Q.push(MP(nx, ny)); } } REP(k, H) { REP(l, W) { if (DIS[k][l] != MAX) chmax(ans, DIS[k][l]); } } } } co(ans); return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <tuple> #include <utility> #include <vector> #define ll long long #define itn int #define endl '\n' #define co(ans) cout << ans << endl #define COYE cout << "YES" << endl #define COYe cout << "Yes" << endl #define COye cout << "yes" << endl #define CONO cout << "NO" << endl #define CONo cout << "No" << endl #define COno cout << "no" << endl #define FORE(i, a) for (auto &i : a) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define FFOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) FFOR(i, 1, n) #define PB push_back #define MP make_pair #define ALL(V) (V).begin(), (V).end() #define SORT(V) sort((V).begin(), (V).end()) #define REVERSE(V) reverse((V).begin(), (V).end()) #define EACH(V, i) \ for (typeof((V).begin()) i = (V).begin(); i != (V).end(); ++i) #define equals(a, b) (fabs((a) - (b)) < EPS) #define INF ((1LL << 62) - (1LL << 31)) #define EPS 1e-10 #define PI 3.141592653589793238 #define MOD 1000000007 #define MAX 5100000 using namespace std; using Edge = pair<ll, ll>; using Graph = vector<vector<int>>; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } 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; } typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int main() { ll H, W, ans = 0; cin >> H >> W; char S[H][W]; REP(i, H) { REP(j, W) { cin >> S[i][j]; } } REP(i, H) { REP(j, W) { if (S[i][j] == '#') continue; ll DIS[H][W]; bool CAN[H][W]; REP(k, H) { REP(l, W) { DIS[k][l] = MAX; CAN[k][l] = false; } } DIS[i][j] = 0; queue<pair<ll, ll>> Q; Q.push(MP(i, j)); while (!Q.empty()) { auto it = Q.front(); Q.pop(); ll vx = it.first, vy = it.second; CAN[vx][vy] = true; REP(k, 4) { ll nx = vx + dx[k], ny = vy + dy[k]; if (nx < 0 || H <= nx || ny < 0 || W <= ny) continue; if (S[nx][ny] == '#' || CAN[nx][ny]) continue; chmin(DIS[nx][ny], DIS[vx][vy] + 1); Q.push(MP(nx, ny)); CAN[nx][ny] = true; } } REP(k, H) { REP(l, W) { if (DIS[k][l] != MAX) chmax(ans, DIS[k][l]); } } } } co(ans); return 0; }
insert
128
128
128
129
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int height, width; cin >> height >> width; vector<string> field(height); for (int h = 0; h < height; ++h) cin >> field[h]; int sx, sy, gx, gy; int ans = 0; for (int h = 0; h < height; ++h) { for (int w = 0; w < width; ++w) { if (field[h][w] == '.') { sx = h; sy = w; } vector<vector<int>> dist(height, vector<int>(width, -1)); dist[sx][sy] = 0; // スタートを 0 に int dis = 0; queue<pair<int, int>> que; que.push(make_pair(sx, sy)); // スタートを push while (!que.empty()) { pair<int, int> current_pos = que.front(); // キューの先頭を見る (C++ ではこれをしても pop しない) int x = current_pos.first; int y = current_pos.second; que.pop(); // キューから pop を忘れずに for (int direction = 0; direction < 4; ++direction) { int next_x = x + dx[direction]; int next_y = y + dy[direction]; if (next_x < 0 || next_x >= height || next_y < 0 || next_y >= width) continue; // 場外アウトならダメ if (field[next_x][next_y] == '#') continue; // 壁はダメ // まだ見ていない頂点なら push if (dist[next_x][next_y] == -1) { que.push(make_pair(next_x, next_y)); dist[next_x][next_y] = dist[x][y] + 1; // (next_x, next_y) の距離も更新 dis = max(dis, dist[next_x][next_y]); } } } ans = max(ans, dis); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int height, width; cin >> height >> width; vector<string> field(height); for (int h = 0; h < height; ++h) cin >> field[h]; int sx, sy, gx, gy; int ans = 0; for (int h = 0; h < height; ++h) { for (int w = 0; w < width; ++w) { if (field[h][w] == '.') { sx = h; sy = w; } else { continue; } vector<vector<int>> dist(height, vector<int>(width, -1)); dist[sx][sy] = 0; // スタートを 0 に int dis = 0; queue<pair<int, int>> que; que.push(make_pair(sx, sy)); // スタートを push while (!que.empty()) { pair<int, int> current_pos = que.front(); // キューの先頭を見る (C++ ではこれをしても pop しない) int x = current_pos.first; int y = current_pos.second; que.pop(); // キューから pop を忘れずに for (int direction = 0; direction < 4; ++direction) { int next_x = x + dx[direction]; int next_y = y + dy[direction]; if (next_x < 0 || next_x >= height || next_y < 0 || next_y >= width) continue; // 場外アウトならダメ if (field[next_x][next_y] == '#') continue; // 壁はダメ // まだ見ていない頂点なら push if (dist[next_x][next_y] == -1) { que.push(make_pair(next_x, next_y)); dist[next_x][next_y] = dist[x][y] + 1; // (next_x, next_y) の距離も更新 dis = max(dis, dist[next_x][next_y]); } } } ans = max(ans, dis); } } cout << ans << endl; }
insert
20
20
20
22
0
p02803
C++
Runtime Error
// --------------------<optimizations>-------------------- #pragma GCC optimize("O3") //(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\ #pragma comment(linker, "/stack:200000000") //(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)\ #pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") #include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define mp make_pair #define pb push_back #define eb emplace_back #define pii pair<ll, ll> #define vpii vector<pair<ll, ll>> #define F first #define S second #define ld long double #define built __builtin_popcountll #define mst(a, i) memset(a, i, sizeof(a)) #define all(x) x.begin(), x.end() #define itit(it, a) for (auto it = (a).begin(); it != (a).end(); it++) #define rep(i, a, b) for (ll i = a; i < b; i++) #define repr(i, a, b) for (ll i = a; i > b; i--) #define reprr(i, a, b) for (ll i = a; i >= b; i--) #define pi 3.14159265358979323846264338327950288419716939937510582097494459230 ll max3(ll x, ll y, ll z) { return max(max(x, y), z); } ll min3(ll x, ll y, ll z) { return min(min(x, y), z); } const ll N = 1e5 + 10, M = 2e5 + 10, M2 = 1e6 + 10, mod = 1e9 + 7, inf = 1e17 + 10; const int INF = 1e9 + 7; void add(int &a, int b) { a += b; if (a >= mod) { a -= mod; } } #define trace(...) \ cerr << "Line:" << __LINE__ << " "; \ __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << 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...); } int X[] = {0, 1, 0, -1}; int Y[] = {-1, 0, 1, 0}; // assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z) // is(65-90) and 32 for space ll power(ll x, ll n) { ll result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % mod; x = ((x % mod) * (x % mod)) % mod; n = n / 2; } return result; } int h, w; string s[21]; int vis[21][21], ways[21][21], dist[21][21]; int bfs(int x, int y) { mst(vis, 0); queue<pair<int, int>> q; q.push({x, y}); vis[x][y] = 1; dist[x][y] = 0; int maxi = 0; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int nx = x + X[i]; int ny = y + Y[i]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (s[nx][ny] == '#') continue; if (!vis[nx][ny]) { dist[nx][ny] = dist[x][y] + 1; maxi = max(maxi, dist[nx][ny]); vis[nx][ny] = 1; ways[nx][ny] += ways[x][y]; q.push({nx, ny}); } else { if (dist[x][y] + 1 == dist[nx][ny]) ways[nx][ny] += ways[x][y]; } } } return maxi; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> h >> w; rep(i, 0, h) { cin >> s[i]; } int ans = 0; rep(i, 0, h) { rep(j, 0, w) { if (s[i][j] == '.') { ans = max(ans, bfs(i, j)); } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define mp make_pair #define pb push_back #define eb emplace_back #define pii pair<ll, ll> #define vpii vector<pair<ll, ll>> #define F first #define S second #define ld long double #define built __builtin_popcountll #define mst(a, i) memset(a, i, sizeof(a)) #define all(x) x.begin(), x.end() #define itit(it, a) for (auto it = (a).begin(); it != (a).end(); it++) #define rep(i, a, b) for (ll i = a; i < b; i++) #define repr(i, a, b) for (ll i = a; i > b; i--) #define reprr(i, a, b) for (ll i = a; i >= b; i--) #define pi 3.14159265358979323846264338327950288419716939937510582097494459230 ll max3(ll x, ll y, ll z) { return max(max(x, y), z); } ll min3(ll x, ll y, ll z) { return min(min(x, y), z); } const ll N = 1e5 + 10, M = 2e5 + 10, M2 = 1e6 + 10, mod = 1e9 + 7, inf = 1e17 + 10; const int INF = 1e9 + 7; void add(int &a, int b) { a += b; if (a >= mod) { a -= mod; } } #define trace(...) \ cerr << "Line:" << __LINE__ << " "; \ __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << 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...); } int X[] = {0, 1, 0, -1}; int Y[] = {-1, 0, 1, 0}; // assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z) // is(65-90) and 32 for space ll power(ll x, ll n) { ll result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % mod; x = ((x % mod) * (x % mod)) % mod; n = n / 2; } return result; } int h, w; string s[21]; int vis[21][21], ways[21][21], dist[21][21]; int bfs(int x, int y) { mst(vis, 0); queue<pair<int, int>> q; q.push({x, y}); vis[x][y] = 1; dist[x][y] = 0; int maxi = 0; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int nx = x + X[i]; int ny = y + Y[i]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (s[nx][ny] == '#') continue; if (!vis[nx][ny]) { dist[nx][ny] = dist[x][y] + 1; maxi = max(maxi, dist[nx][ny]); vis[nx][ny] = 1; ways[nx][ny] += ways[x][y]; q.push({nx, ny}); } else { if (dist[x][y] + 1 == dist[nx][ny]) ways[nx][ny] += ways[x][y]; } } } return maxi; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> h >> w; rep(i, 0, h) { cin >> s[i]; } int ans = 0; rep(i, 0, h) { rep(j, 0, w) { if (s[i][j] == '.') { ans = max(ans, bfs(i, j)); } } } cout << ans; return 0; }
delete
0
11
0
0
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; const int MOD = 1000000007; const int INF = numeric_limits<int>::max() / 2; typedef pair<int, int> P; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int H, W, sx, sy; cin >> H >> W; char s[H][W]; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { cin >> s[i][j]; } } int ans = 0; for (int m = 0; m < H; m++) { for (int n = 0; n < W; n++) { if (s[m][n] == '#') continue; sx = m; sy = n; int dist[25][25]; fill((int *)dist, (int *)(dist + H * W), INF); queue<P> que; dist[sx][sy] = 0; que.push(P(sx, sy)); while (!que.empty()) { P p = que.front(); que.pop(); int x = p.first; int y = p.second; for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue; if (s[nx][ny] == '#') continue; if (dist[nx][ny] > dist[x][y] + 1) { dist[nx][ny] = dist[x][y] + 1; que.push(P(nx, ny)); ans = max(ans, dist[nx][ny]); } } } } } cout << ans << endl; }
#include <bits/stdc++.h> #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; const int MOD = 1000000007; const int INF = numeric_limits<int>::max() / 2; typedef pair<int, int> P; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int H, W, sx, sy; cin >> H >> W; char s[H][W]; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { cin >> s[i][j]; } } int ans = 0; for (int m = 0; m < H; m++) { for (int n = 0; n < W; n++) { if (s[m][n] == '#') continue; sx = m; sy = n; int dist[25][25]; fill((int *)dist, (int *)(dist + H), INF); queue<P> que; dist[sx][sy] = 0; que.push(P(sx, sy)); while (!que.empty()) { P p = que.front(); que.pop(); int x = p.first; int y = p.second; for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue; if (s[nx][ny] == '#') continue; if (dist[nx][ny] > dist[x][y] + 1) { dist[nx][ny] = dist[x][y] + 1; que.push(P(nx, ny)); ans = max(ans, dist[nx][ny]); } } } } } cout << ans << endl; }
replace
32
33
32
33
0
p02803
C++
Time Limit Exceeded
#include <algorithm> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; #define FOR(i, n) for (int i = 0; i < (int)n; i++) #define all(v) v.begin(), v.end() #define F first #define S second typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int MOD = 1e9 + 7; const int INF = 1e9 + 7; int n, m; string g[21]; bool vis[21][21]; bool valid(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m && !vis[x][y] && g[x][y] == '.'; } int bfs(int x, int y) { int ans = 0; queue<pair<pii, int>> q; q.push({{x, y}, 0}); while (!q.empty()) { pii atual = q.front().F; vis[atual.F][atual.S] = true; int d = q.front().S; q.pop(); if (valid(atual.F + 1, atual.S)) q.push({{atual.F + 1, atual.S}, d + 1}); if (valid(atual.F - 1, atual.S)) q.push({{atual.F - 1, atual.S}, d + 1}); if (valid(atual.F, atual.S + 1)) q.push({{atual.F, atual.S + 1}, d + 1}); if (valid(atual.F, atual.S - 1)) q.push({{atual.F, atual.S - 1}, d + 1}); ans = max(ans, d); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; FOR(i, n) cin >> g[i]; int ans = 0; FOR(i, n) { FOR(j, m) { if (g[i][j] == '.') { memset(vis, false, sizeof(vis)); ans = max(ans, bfs(i, j)); } } } cout << ans << '\n'; return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; #define FOR(i, n) for (int i = 0; i < (int)n; i++) #define all(v) v.begin(), v.end() #define F first #define S second typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int MOD = 1e9 + 7; const int INF = 1e9 + 7; int n, m; string g[21]; bool vis[21][21]; bool valid(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m && !vis[x][y] && g[x][y] == '.'; } int bfs(int x, int y) { int ans = 0; queue<pair<pii, int>> q; q.push({{x, y}, 0}); while (!q.empty()) { pii atual = q.front().F; // cout << atual.F << ' ' << atual.S << '\n'; if (vis[atual.F][atual.S]) { q.pop(); continue; } vis[atual.F][atual.S] = true; int d = q.front().S; q.pop(); if (valid(atual.F + 1, atual.S)) q.push({{atual.F + 1, atual.S}, d + 1}); if (valid(atual.F - 1, atual.S)) q.push({{atual.F - 1, atual.S}, d + 1}); if (valid(atual.F, atual.S + 1)) q.push({{atual.F, atual.S + 1}, d + 1}); if (valid(atual.F, atual.S - 1)) q.push({{atual.F, atual.S - 1}, d + 1}); ans = max(ans, d); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; FOR(i, n) cin >> g[i]; int ans = 0; FOR(i, n) { FOR(j, m) { if (g[i][j] == '.') { memset(vis, false, sizeof(vis)); ans = max(ans, bfs(i, j)); } } } cout << ans << '\n'; return 0; }
insert
34
34
34
39
TLE
p02803
C++
Runtime Error
// 各スタート地点からDFS // 探索が終わるまでの距離をカウント // 最も長かったスタート地点とそのゴール地点を返す // bfsの引数:現在の場所/前の場所 // グローバル:W/H/S/mxdist // 任意のスタートから任意の位置までの距離 : dist #include <bits/stdc++.h> using namespace std; int H, W, mxdist = 0; vector<vector<char>> S(20, vector<char>(20)); typedef pair<int, int> P; vector<int> dx = {0, 1, 0, -1}; vector<int> dy = {1, 0, -1, 0}; // 返り値は無して良い void bfs(P pos, P ppos, int dist, vector<vector<int>> &_checked) { int px = ppos.second, py = ppos.first; _checked.at(pos.first).at(pos.second) = min(_checked.at(pos.first).at(pos.second), dist); dist++; // cout << " " << pos.second << " " << pos.first << endl; for (int i = 0; i < 4; i++) { int x = pos.second + dx.at(i), y = pos.first + dy.at(i); if (y >= 0 && x >= 0 && y < H && x < W && dist < _checked.at(y).at(x)) { if (!(x == px && y == py) && S.at(y).at(x) == '.') bfs(P(y, x), pos, dist, _checked); } } /*if(mxdist < _checked.at(pos.first).at(pos.second)){ mxdist = _checked.at(pos.first).at(pos.second); }*/ } int main() { int i, j; cin >> H >> W; for (i = 0; i < H; i++) { for (j = 0; j < W; j++) { cin >> S.at(i).at(j); } } for (i = 0; i < H; i++) { for (j = 0; j < W; j++) { vector<vector<int>> checked(H, vector<int>(W, 401)); if (S.at(i).at(j) == '#') continue; bfs(P(i, j), P(-1, -1), 0, checked); for (int _i = 0; _i < H; _i++) { for (int _j = 0; _j < H; _j++) { if (mxdist <= checked.at(_i).at(_j) && checked.at(_i).at(_j) != 401) mxdist = checked.at(_i).at(_j); } } } } cout << mxdist << endl; }
// 各スタート地点からDFS // 探索が終わるまでの距離をカウント // 最も長かったスタート地点とそのゴール地点を返す // bfsの引数:現在の場所/前の場所 // グローバル:W/H/S/mxdist // 任意のスタートから任意の位置までの距離 : dist #include <bits/stdc++.h> using namespace std; int H, W, mxdist = 0; vector<vector<char>> S(20, vector<char>(20)); typedef pair<int, int> P; vector<int> dx = {0, 1, 0, -1}; vector<int> dy = {1, 0, -1, 0}; // 返り値は無して良い void bfs(P pos, P ppos, int dist, vector<vector<int>> &_checked) { int px = ppos.second, py = ppos.first; _checked.at(pos.first).at(pos.second) = min(_checked.at(pos.first).at(pos.second), dist); dist++; // cout << " " << pos.second << " " << pos.first << endl; for (int i = 0; i < 4; i++) { int x = pos.second + dx.at(i), y = pos.first + dy.at(i); if (y >= 0 && x >= 0 && y < H && x < W && dist < _checked.at(y).at(x)) { if (!(x == px && y == py) && S.at(y).at(x) == '.') bfs(P(y, x), pos, dist, _checked); } } /*if(mxdist < _checked.at(pos.first).at(pos.second)){ mxdist = _checked.at(pos.first).at(pos.second); }*/ } int main() { int i, j; cin >> H >> W; for (i = 0; i < H; i++) { for (j = 0; j < W; j++) { cin >> S.at(i).at(j); } } for (i = 0; i < H; i++) { for (j = 0; j < W; j++) { vector<vector<int>> checked(H, vector<int>(W, 401)); if (S.at(i).at(j) == '#') continue; bfs(P(i, j), P(-1, -1), 0, checked); for (int _i = 0; _i < H; _i++) { for (int _j = 0; _j < W; _j++) { if (mxdist <= checked.at(_i).at(_j) && checked.at(_i).at(_j) != 401) mxdist = checked.at(_i).at(_j); } } } } cout << mxdist << endl; }
replace
60
61
60
61
0
p02803
Python
Runtime Error
from scipy.sparse.csgraph import dijkstra, csgraph_from_dense import numpy as np H, W = map(int, input().split()) S = [input() for _ in range(H)] v_id_dict = {} dista = np.ones((20, 20)) * np.inf for i in range(H): for j in range(W): if S[i][j] == ".": v_id_dict[i, j] = len(v_id_dict) if (i > 0) and (S[i - 1][j] == "."): dista[v_id_dict[(i, j)], v_id_dict[(i - 1, j)]] = 1 if (j > 0) and (S[i][j - 1] == "."): dista[v_id_dict[(i, j)], v_id_dict[(i, j - 1)]] = 1 dist_mat = dijkstra( csgraph_from_dense(dista[: len(v_id_dict), : len(v_id_dict)]), directed=False, unweighted=False, ) print(int(dist_mat.max()))
from scipy.sparse.csgraph import dijkstra, csgraph_from_dense import numpy as np H, W = map(int, input().split()) S = [input() for _ in range(H)] v_id_dict = {} dista = np.ones((400, 400)) * np.inf for i in range(H): for j in range(W): if S[i][j] == ".": v_id_dict[i, j] = len(v_id_dict) if (i > 0) and (S[i - 1][j] == "."): dista[v_id_dict[(i, j)], v_id_dict[(i - 1, j)]] = 1 if (j > 0) and (S[i][j - 1] == "."): dista[v_id_dict[(i, j)], v_id_dict[(i, j - 1)]] = 1 dist_mat = dijkstra( csgraph_from_dense(dista[: len(v_id_dict), : len(v_id_dict)]), directed=False, unweighted=False, ) print(int(dist_mat.max()))
replace
7
8
7
8
ModuleNotFoundError: No module named 'scipy'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02803/Python/s307435936.py", line 1, in <module> from scipy.sparse.csgraph import dijkstra, csgraph_from_dense ModuleNotFoundError: No module named 'scipy'
p02803
Python
Runtime Error
import os import sys import numpy as np from scipy.sparse.csgraph._shortest_path import floyd_warshall if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(10**9) INF = float("inf") IINF = 10**18 MOD = 10**9 + 7 # MOD = 998244353 H, W = list(map(int, sys.stdin.buffer.readline().split())) S = [sys.stdin.buffer.readline().decode().rstrip() for _ in range(H)] def to_i(h, w): return h * W + w N = H * W graph = [[0] * N for _ in range(N)] for h in range(H): for w in range(W): if S[h][w] == ".": if 0 <= h - 1 < H and S[h - 1][w] == ".": graph[to_i(h - 1, w)][to_i(h, w)] = 1 graph[to_i(h, w)][to_i(h - 1, w)] = 1 if 0 <= h + 1 < H and S[h + 1][w] == ".": graph[to_i(h + 1, w)][to_i(h, w)] = 1 graph[to_i(h, w)][to_i(h + 1, w)] = 1 if 0 <= w - 1 < W and S[h][w - 1] == ".": graph[to_i(h, w - 1)][to_i(h, w)] = 1 graph[to_i(h, w)][to_i(h, w - 1)] = 1 if 0 <= w + 1 < H and S[h][w + 1] == ".": graph[to_i(h, w + 1)][to_i(h, w)] = 1 graph[to_i(h, w)][to_i(h, w + 1)] = 1 dist = floyd_warshall(graph, directed=False) dist[dist == np.inf] = 0 print(int(dist.max()))
import os import sys import numpy as np from scipy.sparse.csgraph._shortest_path import floyd_warshall if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(10**9) INF = float("inf") IINF = 10**18 MOD = 10**9 + 7 # MOD = 998244353 H, W = list(map(int, sys.stdin.buffer.readline().split())) S = [sys.stdin.buffer.readline().decode().rstrip() for _ in range(H)] def to_i(h, w): return h * W + w N = H * W graph = [[0] * N for _ in range(N)] for h in range(H): for w in range(W): if S[h][w] == ".": if 0 <= h - 1 < H and S[h - 1][w] == ".": graph[to_i(h - 1, w)][to_i(h, w)] = 1 graph[to_i(h, w)][to_i(h - 1, w)] = 1 if 0 <= h + 1 < H and S[h + 1][w] == ".": graph[to_i(h + 1, w)][to_i(h, w)] = 1 graph[to_i(h, w)][to_i(h + 1, w)] = 1 if 0 <= w - 1 < W and S[h][w - 1] == ".": graph[to_i(h, w - 1)][to_i(h, w)] = 1 graph[to_i(h, w)][to_i(h, w - 1)] = 1 if 0 <= w + 1 < W and S[h][w + 1] == ".": graph[to_i(h, w + 1)][to_i(h, w)] = 1 graph[to_i(h, w)][to_i(h, w + 1)] = 1 dist = floyd_warshall(graph, directed=False) dist[dist == np.inf] = 0 print(int(dist.max()))
replace
37
38
37
38
TLE
p02803
Python
Runtime Error
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall H, W = map(int, input().split()) S = [input() for _ in range(H)] g = [[0] * (H * W) for _ in range(H * W)] for y in range(H): for x in range(W): if S[y][x] == "#": continue if y - 1 >= 0 and S[y - 1][x] != "#": g[y * W + x][(y - 1) * W + x] = 1 if y + 1 < H and S[y + 1][x] != "#": g[y * W + x][(y + 1) * W + x] = 1 if x - 1 >= 0 and S[y][x - 1] != "#": g[y * W + x][y * W + x - 1] = 1 if x + 1 < W and S[y][x + 1] != "#": g[y * W + x][y * W + x + 1] = 1 g = floyd_warshall(csgraph_from_dense(g)) result = 0 for i in range(H * W): for j in range(H * W): if g[i][j] == 0: continue result = max(result, g[i][j]) print(int(result))
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall H, W = map(int, input().split()) S = [input() for _ in range(H)] g = [[0] * (H * W) for _ in range(H * W)] for y in range(H): for x in range(W): if S[y][x] == "#": continue if y - 1 >= 0 and S[y - 1][x] != "#": g[y * W + x][(y - 1) * W + x] = 1 if y + 1 < H and S[y + 1][x] != "#": g[y * W + x][(y + 1) * W + x] = 1 if x - 1 >= 0 and S[y][x - 1] != "#": g[y * W + x][y * W + x - 1] = 1 if x + 1 < W and S[y][x + 1] != "#": g[y * W + x][y * W + x + 1] = 1 g = floyd_warshall(csgraph_from_dense(g)) result = 0 for i in range(H * W): for j in range(H * W): if g[i][j] == float("inf"): continue result = max(result, g[i][j]) print(int(result))
replace
23
24
23
24
ModuleNotFoundError: No module named 'scipy'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02803/Python/s337120294.py", line 1, in <module> from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall ModuleNotFoundError: No module named 'scipy'
p02803
Python
Runtime Error
H, W = map(int, input().split()) S = [input() for _ in range(H)] def f(i, j): t = [[-1] * W for _ in range(W)] t[i][j] = 0 q = [(i, j)] while q: y, x = q.pop(0) if y - 1 >= 0 and S[y - 1][x] != "#" and t[y - 1][x] == -1: t[y - 1][x] = t[y][x] + 1 q.append((y - 1, x)) if y + 1 < H and S[y + 1][x] != "#" and t[y + 1][x] == -1: t[y + 1][x] = t[y][x] + 1 q.append((y + 1, x)) if x - 1 >= 0 and S[y][x - 1] != "#" and t[y][x - 1] == -1: t[y][x - 1] = t[y][x] + 1 q.append((y, x - 1)) if x + 1 < W and S[y][x + 1] != "#" and t[y][x + 1] == -1: t[y][x + 1] = t[y][x] + 1 q.append((y, x + 1)) return max(max(tt) for tt in t) result = 0 for i in range(H): for j in range(W): if S[i][j] != "#": result = max(result, f(i, j)) print(result)
H, W = map(int, input().split()) S = [input() for _ in range(H)] def f(i, j): t = [[-1] * W for _ in range(H)] t[i][j] = 0 q = [(i, j)] while q: y, x = q.pop(0) if y - 1 >= 0 and S[y - 1][x] != "#" and t[y - 1][x] == -1: t[y - 1][x] = t[y][x] + 1 q.append((y - 1, x)) if y + 1 < H and S[y + 1][x] != "#" and t[y + 1][x] == -1: t[y + 1][x] = t[y][x] + 1 q.append((y + 1, x)) if x - 1 >= 0 and S[y][x - 1] != "#" and t[y][x - 1] == -1: t[y][x - 1] = t[y][x] + 1 q.append((y, x - 1)) if x + 1 < W and S[y][x + 1] != "#" and t[y][x + 1] == -1: t[y][x + 1] = t[y][x] + 1 q.append((y, x + 1)) return max(max(tt) for tt in t) result = 0 for i in range(H): for j in range(W): if S[i][j] != "#": result = max(result, f(i, j)) print(result)
replace
5
6
5
6
0
p02803
Python
Time Limit Exceeded
from itertools import product def solve(string): h, w, *s = string.split() h, w = map(int, [h, w]) s = ["#" + _s + "#" for _s in s] s = ["#" * (w + 2)] + s + ["#" * (w + 2)] ans = 0 d = [(1, 0), (-1, 0), (0, 1), (0, -1)] for _h, _w in product(range(1, h + 1), range(1, w + 1)): if s[_h][_w] == "#": continue c = [[h * w] * (w + 2) for _ in range(h + 2)] c[_h][_w] = 0 q = list() for x, y in d: if s[_h + x][_w + y] == ".": q.append((_h + x, _w + y, _h, _w)) while q: cx, cy, px, py = q.pop() if c[px][py] + 1 >= c[cx][cy]: continue c[cx][cy] = c[px][py] + 1 for x, y in d: nx = cx + x ny = cy + y if s[nx][ny] == ".": q.append((nx, ny, cx, cy)) for i, j in product(range(1, h + 1), range(1, w + 1)): ans = max(ans, c[i][j] * (s[i][j] == ".")) return str(ans) if __name__ == "__main__": import sys print(solve(sys.stdin.read().strip()))
from itertools import product def solve(string): h, w, *s = string.split() h, w = map(int, [h, w]) s = ["#" + _s + "#" for _s in s] s = ["#" * (w + 2)] + s + ["#" * (w + 2)] ans = 0 d = [(1, 0), (-1, 0), (0, 1), (0, -1)] for _h, _w in product(range(1, h + 1), range(1, w + 1)): if s[_h][_w] == "#": continue c = [[h * w] * (w + 2) for _ in range(h + 2)] c[_h][_w] = 0 q = list() for x, y in d: if s[_h + x][_w + y] == ".": q.append((_h + x, _w + y, _h, _w)) if len(q) > 2: continue while q: cx, cy, px, py = q.pop() if c[px][py] + 1 >= c[cx][cy]: continue c[cx][cy] = c[px][py] + 1 for x, y in d: nx = cx + x ny = cy + y if s[nx][ny] == ".": q.append((nx, ny, cx, cy)) for i, j in product(range(1, h + 1), range(1, w + 1)): ans = max(ans, c[i][j] * (s[i][j] == ".")) return str(ans) if __name__ == "__main__": import sys print(solve(sys.stdin.read().strip()))
insert
19
19
19
21
TLE
p02803
C++
Time Limit Exceeded
/* confirm 0LL and 1LL confirm cornercases such as 0 confirm times of cin < 10^6 */ #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; using Vec = vector<ll>; using VecP = vector<P>; using VecB = vector<bool>; using VecC = vector<char>; using VecD = vector<double>; using VecS = vector<string>; using VecVec = vector<Vec>; using Tree = vector<VecP>; template <typename T> using Vec1 = vector<T>; template <typename T> using Vec2 = vector<vector<T>>; #define REP(i, m, n) for (ll(i) = (m); (i) < (n); ++(i)) #define REPN(i, m, n) for (ll(i) = (m); (i) <= (n); ++(i)) #define REPR(i, m, n) for (ll(i) = (m)-1; (i) >= (n); --(i)) #define REPNR(i, m, n) for (ll(i) = (m); (i) >= (n); --(i)) #define rep(i, n) REP(i, 0, n) #define repn(i, n) REPN(i, 1, n) #define repr(i, n) REPR(i, n, 0) #define repnr(i, n) REPNR(i, n, 1) #define all(s) (s).begin(), (s).end() #define pb push_back #define mp make_pair #define mt make_tuple #define fs first #define sc second template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> void co(const T n) { cout << n << endl; } template <typename T> void cosp(const T n) { cout << n << ' '; } void coVec(const Vec &v) { for (ll i : v) cosp(i); cout << endl; } void sonic() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } void setp(const ll n) { cout << fixed << setprecision(n); } const ll INF = 1e9 + 1; const ll LINF = 1e18 + 1; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const double PI = acos(-1); const double EPS = 1e-9; ll h, w; VecS s; VecVec dist; void init() { rep(i, h) fill(all(dist[i]), LINF); } void wfs(ll x, ll y) { queue<P> que; que.push({x * w + y, 0}); while (!que.empty()) { P p = que.front(); que.pop(); ll x1 = p.fs / w, y1 = p.fs % w; dist[x1][y1] = p.sc; if (x1 - 1 >= 0 && s[x1 - 1][y1] == '.' && dist[x1 - 1][y1] == LINF) que.push({(x1 - 1) * w + y1, p.sc + 1}); if (y1 - 1 >= 0 && s[x1][y1 - 1] == '.' && dist[x1][y1 - 1] == LINF) que.push({x1 * w + y1 - 1, p.sc + 1}); if (x1 + 1 < h && s[x1 + 1][y1] == '.' && dist[x1 + 1][y1] == LINF) que.push({(x1 + 1) * w + y1, p.sc + 1}); if (y1 + 1 < w && s[x1][y1 + 1] == '.' && dist[x1][y1 + 1] == LINF) que.push({x1 * w + y1 + 1, p.sc + 1}); } } ll maxDist() { ll res = 0; rep(i, h) { rep(j, w) if (dist[i][j] != LINF) chmax(res, dist[i][j]); } return res; } int main(void) { cin >> h >> w; s.resize(h); rep(i, h) cin >> s[i]; dist.resize(h); rep(i, h) dist[i].resize(w); ll ans = 0; rep(i, h) { rep(j, w) if (s[i][j] == '.') { init(); wfs(i, j); chmax(ans, maxDist()); } } co(ans); return 0; }
/* confirm 0LL and 1LL confirm cornercases such as 0 confirm times of cin < 10^6 */ #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; using Vec = vector<ll>; using VecP = vector<P>; using VecB = vector<bool>; using VecC = vector<char>; using VecD = vector<double>; using VecS = vector<string>; using VecVec = vector<Vec>; using Tree = vector<VecP>; template <typename T> using Vec1 = vector<T>; template <typename T> using Vec2 = vector<vector<T>>; #define REP(i, m, n) for (ll(i) = (m); (i) < (n); ++(i)) #define REPN(i, m, n) for (ll(i) = (m); (i) <= (n); ++(i)) #define REPR(i, m, n) for (ll(i) = (m)-1; (i) >= (n); --(i)) #define REPNR(i, m, n) for (ll(i) = (m); (i) >= (n); --(i)) #define rep(i, n) REP(i, 0, n) #define repn(i, n) REPN(i, 1, n) #define repr(i, n) REPR(i, n, 0) #define repnr(i, n) REPNR(i, n, 1) #define all(s) (s).begin(), (s).end() #define pb push_back #define mp make_pair #define mt make_tuple #define fs first #define sc second template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> void co(const T n) { cout << n << endl; } template <typename T> void cosp(const T n) { cout << n << ' '; } void coVec(const Vec &v) { for (ll i : v) cosp(i); cout << endl; } void sonic() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } void setp(const ll n) { cout << fixed << setprecision(n); } const ll INF = 1e9 + 1; const ll LINF = 1e18 + 1; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const double PI = acos(-1); const double EPS = 1e-9; ll h, w; VecS s; VecVec dist; void init() { rep(i, h) fill(all(dist[i]), LINF); } void wfs(ll x, ll y) { queue<P> que; que.push({x * w + y, 0}); while (!que.empty()) { P p = que.front(); que.pop(); ll x1 = p.fs / w, y1 = p.fs % w; dist[x1][y1] = p.sc; if (x1 - 1 >= 0 && s[x1 - 1][y1] == '.' && dist[x1 - 1][y1] == LINF) que.push({(x1 - 1) * w + y1, p.sc + 1}); if (y1 - 1 >= 0 && s[x1][y1 - 1] == '.' && dist[x1][y1 - 1] == LINF) que.push({x1 * w + y1 - 1, p.sc + 1}); if (x1 + 1 < h && s[x1 + 1][y1] == '.' && dist[x1 + 1][y1] == LINF) que.push({(x1 + 1) * w + y1, p.sc + 1}); if (y1 + 1 < w && s[x1][y1 + 1] == '.' && dist[x1][y1 + 1] == LINF) que.push({x1 * w + y1 + 1, p.sc + 1}); } } ll maxDist() { ll res = 0; rep(i, h) { rep(j, w) if (dist[i][j] != LINF) chmax(res, dist[i][j]); } return res; } int main(void) { cin >> h >> w; s.resize(h); rep(i, h) cin >> s[i]; dist.resize(h); rep(i, h) dist[i].resize(w); bool flg = true; rep(i, h) rep(j, w) if (s[i][j] == '#') { flg = false; break; } if (flg) { co(w + h - 2); return 0; } ll ans = 0; rep(i, h) { rep(j, w) if (s[i][j] == '.') { init(); wfs(i, j); chmax(ans, maxDist()); } } co(ans); return 0; }
insert
105
105
105
114
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; const int INF = 10000; int d[20][20]; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; char maze[20][20]; int h, w, hw; int l = 0; bool flg = true; int bfs(int sx, int sy) { int hwc = 0; queue<P> que; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) d[i][j] = INF; que.push(P(sx, sy)); d[sx][sy] = 0; while (hw > hwc) { P p = que.front(); que.pop(); for (int i = 0; i < 4; i++) { int nx = p.first + dx[i], ny = p.second + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] != '#' && d[nx][ny] == INF) { que.push(P(nx, ny)); d[nx][ny] = d[p.first][p.second] + 1; if (l < d[nx][ny]) { l = d[nx][ny]; } } } hwc++; } return l; } int main() { string s; cin >> h >> w; for (int i = 0; i < h; i++) { cin >> s; for (int j = 0; j < w; j++) { maze[i][j] = s[j]; } } hw = h * w; int ans = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (maze[i][j] == '.') ans = max(ans, bfs(i, j)); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; const int INF = 10000; int d[20][20]; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; char maze[20][20]; int h, w, hw; int l = 0; bool flg = true; int bfs(int sx, int sy) { int hwc = 0; queue<P> que; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) d[i][j] = INF; que.push(P(sx, sy)); d[sx][sy] = 0; while (hw > hwc && que.size() > 0) { P p = que.front(); que.pop(); for (int i = 0; i < 4; i++) { int nx = p.first + dx[i], ny = p.second + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] != '#' && d[nx][ny] == INF) { que.push(P(nx, ny)); d[nx][ny] = d[p.first][p.second] + 1; if (l < d[nx][ny]) { l = d[nx][ny]; } } } hwc++; } return l; } int main() { string s; cin >> h >> w; for (int i = 0; i < h; i++) { cin >> s; for (int j = 0; j < w; j++) { maze[i][j] = s[j]; } } hw = h * w; int ans = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (maze[i][j] == '.') ans = max(ans, bfs(i, j)); cout << ans; return 0; }
replace
18
19
18
19
0
p02803
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; int x[] = {0, 1, 0, -1}; int y[] = {1, 0, -1, 0}; bool M[21][21]; int H, W; bool valid(int r, int c) { if (r < H && r >= 0 && c < W && c >= 0) return true; else return false; } bool v[21][21]; int BFS(int r, int c) { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { v[i][j] = 0; } } queue<pair<int, int>> q; q.push({r, c}); v[r][c] = 1; q.push({-1, -1}); int cnt = 0; while (!q.empty()) { pair<int, int> p = q.front(); q.pop(); if (p.first == -1) { cnt++; if (!q.empty()) { q.push({-1, -1}); continue; } else { break; } } r = p.first; c = p.second; for (int i = 0; i < 4; i++) { int R = r + x[i]; int C = c + y[i]; if (valid(R, C) && M[R][C] && !v[R][C]) { q.push({R, C}); v[r][c] = 1; } } } return cnt - 1; } int main() { cin >> H >> W; char c; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> c; if (c == '.') M[i][j] = 1; } } int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (M[i][j]) { int x = BFS(i, j); ans = max(ans, x); } } } cout << ans << endl; return 0; }
#include "bits/stdc++.h" using namespace std; int x[] = {0, 1, 0, -1}; int y[] = {1, 0, -1, 0}; bool M[21][21]; int H, W; bool valid(int r, int c) { if (r < H && r >= 0 && c < W && c >= 0) return true; else return false; } bool v[21][21]; int BFS(int r, int c) { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { v[i][j] = 0; } } queue<pair<int, int>> q; q.push({r, c}); v[r][c] = 1; q.push({-1, -1}); int cnt = 0; while (!q.empty()) { pair<int, int> p = q.front(); q.pop(); if (p.first == -1) { cnt++; if (!q.empty()) { q.push({-1, -1}); continue; } else { break; } } r = p.first; c = p.second; for (int i = 0; i < 4; i++) { int R = r + x[i]; int C = c + y[i]; if (valid(R, C) && M[R][C] && !v[R][C]) { q.push({R, C}); v[R][C] = 1; } } } return cnt - 1; } int main() { cin >> H >> W; char c; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> c; if (c == '.') M[i][j] = 1; } } int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (M[i][j]) { int x = BFS(i, j); ans = max(ans, x); } } } cout << ans << endl; return 0; }
replace
45
46
45
46
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int H, W; char c[21][21]; int check[21][21]; int i, j; queue<pair<int, int>> que; cin >> H >> W; int ans = 0; for (i = 0; i < H; i++) for (j = 0; j < W; j++) cin >> c[i][j]; for (i = 0; i < H; i++) for (j = 0; j < W; j++) { if (c[i][j] == '#') continue; else { for (int k = 0; k < H; k++) for (int l = 0; l < W; l++) check[k][l] = -1e9; check[i][j] = 0; que.push(pair<int, int>(i, j)); while (!que.empty()) { int x = que.front().first; int y = que.front().second; int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; que.pop(); for (int k = 0; k < 4; k++) { if (c[x + dx[k]][y + dy[k]] == '.' && check[x + dx[k]][y + dy[k]] < check[x][y] + 1) { check[x + dx[k]][y + dy[k]] = check[x][y] + 1; que.push(pair<int, int>(x + dx[k], y + dy[k])); } } } for (int k = 0; k < H; k++) for (int l = 0; l < W; l++) if (check[k][l] > ans) ans = check[k][l]; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int H, W; char c[21][21]; int check[21][21]; int i, j; queue<pair<int, int>> que; cin >> H >> W; int ans = 0; for (i = 0; i < H; i++) for (j = 0; j < W; j++) cin >> c[i][j]; for (i = 0; i < H; i++) for (j = 0; j < W; j++) { if (c[i][j] == '#') continue; else { for (int k = 0; k < H; k++) for (int l = 0; l < W; l++) check[k][l] = -1e9; check[i][j] = 0; que.push(pair<int, int>(i, j)); while (!que.empty()) { int x = que.front().first; int y = que.front().second; int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; que.pop(); for (int k = 0; k < 4; k++) { if (c[x + dx[k]][y + dy[k]] == '.' && check[x + dx[k]][y + dy[k]] == -1e9) { check[x + dx[k]][y + dy[k]] = check[x][y] + 1; que.push(pair<int, int>(x + dx[k], y + dy[k])); } } } for (int k = 0; k < H; k++) for (int l = 0; l < W; l++) if (check[k][l] > ans) ans = check[k][l]; } } cout << ans << endl; }
replace
32
33
32
33
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ln(a) cout << (a) << endl #define debug(a, n) \ rep(hoge, n) printf("%lld%c", a[hoge], hoge == (n)-1 ? '\n' : ' '); #define all(a) (a).begin(), (a).end() #define pb push_back #define INF 1e9 #define LINF 1e18 using namespace std; using ll = long long; using P = pair<int, int>; using LP = pair<ll, ll>; signed main() { int h, w; cin >> h >> w; string *maze = new string[h]; rep(i, h) { cin >> maze[i]; } int ans = 0; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; rep(i, h) { rep(j, w) { rep(k, h) { rep(l, w) { if (maze[i][j] == '#' || maze[k][l] == '#') continue; bool **used = new bool *[h]; rep(m, h) { used[m] = new bool[w]; fill(used[m], used[m] + w, false); } used[i][j] = true; queue<pair<P, int>> que; que.push(make_pair(P(i, j), 0)); while (!que.empty()) { P p = que.front().first; int d = que.front().second; que.pop(); if (p.first == k && p.second == l) { ans = max(ans, d); break; } rep(m, 4) { int nx = p.first + dx[m]; int ny = p.second + dy[m]; if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == '.') { que.push(make_pair(P(nx, ny), d + 1)); } } } } } } } ln(ans); }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ln(a) cout << (a) << endl #define debug(a, n) \ rep(hoge, n) printf("%lld%c", a[hoge], hoge == (n)-1 ? '\n' : ' '); #define all(a) (a).begin(), (a).end() #define pb push_back #define INF 1e9 #define LINF 1e18 using namespace std; using ll = long long; using P = pair<int, int>; using LP = pair<ll, ll>; signed main() { int h, w; cin >> h >> w; string *maze = new string[h]; rep(i, h) { cin >> maze[i]; } int ans = 0; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; rep(i, h) { rep(j, w) { if (maze[i][j] == '#') continue; bool **used = new bool *[h]; rep(m, h) { used[m] = new bool[w]; fill(used[m], used[m] + w, false); } used[i][j] = true; queue<pair<P, int>> que; que.push(make_pair(P(i, j), 0)); while (!que.empty()) { P p = que.front().first; int d = que.front().second; que.pop(); rep(m, 4) { int nx = p.first + dx[m]; int ny = p.second + dy[m]; if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == '.' && !used[nx][ny]) { used[nx][ny] = true; que.push(make_pair(P(nx, ny), d + 1)); ans = max(ans, d + 1); } } } } } ln(ans); }
replace
26
54
26
48
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int h, w; vector<vector<char>> s; vector<vector<bool>> sb; vector<int> dx = {-1, 0, 1, 0}; vector<int> dy = {0, -1, 0, 1}; struct xydata { int x, y, d; }; void refresh() { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { sb.at(i).at(j) = false; } } } int bfs(int sx, int sy, int gx, int gy) { xydata first; first.x = sx; first.y = sy; first.d = 0; queue<xydata> que; que.push(first); int depth = 0; while (!que.empty()) { xydata now = que.front(); que.pop(); if (now.x == gx && now.y == gy) { depth = now.d; break; } for (int i = 0; i < 4; ++i) { xydata next; next.x = now.x + dx.at(i); next.y = now.y + dy.at(i); if (next.x >= 0 && next.x < w && next.y >= 0 && next.y < h) { if (s.at(next.y).at(next.x) == '.' && !sb.at(next.y).at(next.x)) { next.d = now.d + 1; sb.at(now.y).at(now.x) = true; que.push(next); } } } } return depth; } int main() { cin >> h >> w; s = vector<vector<char>>(h, vector<char>(w, '.')); sb = vector<vector<bool>>(h, vector<bool>(w, false)); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { cin >> s.at(i).at(j); } } int maxlen = 0; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (s.at(i).at(j) == '.') { for (int k = 0; k < h; ++k) { for (int l = 0; l < w; ++l) { if (i != k || j != l) { if (s.at(k).at(l) == '.') { // cout << j << i << l << k << " "; int len = bfs(j, i, l, k); // cout << len << endl; maxlen = max(maxlen, len); refresh(); } } } } } } } cout << maxlen << endl; }
#include <bits/stdc++.h> using namespace std; int h, w; vector<vector<char>> s; vector<vector<bool>> sb; vector<int> dx = {-1, 0, 1, 0}; vector<int> dy = {0, -1, 0, 1}; struct xydata { int x, y, d; }; void refresh() { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { sb.at(i).at(j) = false; } } } int bfs(int sx, int sy, int gx, int gy) { xydata first; first.x = sx; first.y = sy; first.d = 0; queue<xydata> que; que.push(first); int depth = 0; while (!que.empty()) { xydata now = que.front(); que.pop(); if (now.x == gx && now.y == gy) { depth = now.d; break; } for (int i = 0; i < 4; ++i) { xydata next; next.x = now.x + dx.at(i); next.y = now.y + dy.at(i); if (next.x >= 0 && next.x < w && next.y >= 0 && next.y < h) { if (s.at(next.y).at(next.x) == '.' && !sb.at(next.y).at(next.x)) { next.d = now.d + 1; sb.at(next.y).at(next.x) = true; que.push(next); } } } } return depth; } int main() { cin >> h >> w; s = vector<vector<char>>(h, vector<char>(w, '.')); sb = vector<vector<bool>>(h, vector<bool>(w, false)); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { cin >> s.at(i).at(j); } } int maxlen = 0; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (s.at(i).at(j) == '.') { for (int k = 0; k < h; ++k) { for (int l = 0; l < w; ++l) { if (i != k || j != l) { if (s.at(k).at(l) == '.') { // cout << j << i << l << k << " "; int len = bfs(j, i, l, k); // cout << len << endl; maxlen = max(maxlen, len); refresh(); } } } } } } } cout << maxlen << endl; }
replace
43
44
43
44
TLE
p02803
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int h, w; vector<vector<char>> s; vector<vector<int>> sb; vector<int> dx = {-1, 0, 1, 0}; vector<int> dy = {0, -1, 0, 1}; struct xydata { int x, y; }; int bfs(int sx, int sy, int gx, int gy) { sb = vector<vector<int>>(h, vector<int>(w, 1000)); xydata first; first.x = sx; first.y = sy; queue<xydata> que; que.push(first); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { sb.at(i).at(j) = 1000; } } sb.at(first.y).at(first.x) = 0; while (!que.empty()) { xydata now = que.front(); que.pop(); if (now.x == gx && now.y == gy) { break; } for (int i = 0; i < 4; ++i) { xydata next; next.x = now.x + dx.at(i); next.y = now.y + dy.at(i); if (next.x >= 0 && next.x < w && next.y >= 0 && next.y < h) { if (s.at(next.y).at(next.x) == '.') { if (sb.at(now.y).at(now.x) + 1 <= sb.at(next.y).at(next.x)) { sb.at(next.y).at(next.x) = sb.at(now.y).at(now.x) + 1; que.push(next); } } } } } return sb.at(gy).at(gx); } int main() { cin >> h >> w; s = vector<vector<char>>(h, vector<char>(w, '.')); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { cin >> s.at(i).at(j); } } int maxlen = 0; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (s.at(i).at(j) == '.') { for (int k = 0; k < h; ++k) { for (int l = 0; l < w; ++l) { if (i == k && j == l) { } else { if (s.at(k).at(l) == '.') { // cout << j << i << l << k << " "; int len = bfs(j, i, l, k); // cout << len << endl; maxlen = max(maxlen, len); } } } } } } } cout << maxlen << endl; }
#include <bits/stdc++.h> using namespace std; int h, w; vector<vector<char>> s; vector<vector<int>> sb; vector<int> dx = {-1, 0, 1, 0}; vector<int> dy = {0, -1, 0, 1}; struct xydata { int x, y; }; int bfs(int sx, int sy, int gx, int gy) { sb = vector<vector<int>>(h, vector<int>(w, 1000)); xydata first; first.x = sx; first.y = sy; queue<xydata> que; que.push(first); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { sb.at(i).at(j) = 1000; } } sb.at(first.y).at(first.x) = 0; while (!que.empty()) { xydata now = que.front(); que.pop(); if (now.x == gx && now.y == gy) { break; } for (int i = 0; i < 4; ++i) { xydata next; next.x = now.x + dx.at(i); next.y = now.y + dy.at(i); if (next.x >= 0 && next.x < w && next.y >= 0 && next.y < h) { if (s.at(next.y).at(next.x) == '.' && sb.at(next.y).at(next.x) == 1000) { sb.at(next.y).at(next.x) = sb.at(now.y).at(now.x) + 1; que.push(next); } } } } return sb.at(gy).at(gx); } int main() { cin >> h >> w; s = vector<vector<char>>(h, vector<char>(w, '.')); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { cin >> s.at(i).at(j); } } int maxlen = 0; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (s.at(i).at(j) == '.') { for (int k = 0; k < h; ++k) { for (int l = 0; l < w; ++l) { if (i == k && j == l) { } else { if (s.at(k).at(l) == '.') { // cout << j << i << l << k << " "; int len = bfs(j, i, l, k); // cout << len << endl; maxlen = max(maxlen, len); } } } } } } } cout << maxlen << endl; }
replace
37
42
37
41
TLE
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int INF = 1e9 + 7; #define MAX_V 300 struct edge { int to; int cost; }; int V; vector<edge> G[MAX_V]; int d[MAX_V][MAX_V]; void warshall_floyd() { for (int i = 0; i < V; i++) { fill(d[i], d[i] + V, INF); d[i][i] = 0; for (int j = 0; j < G[i].size(); j++) { edge e = G[i][j]; d[i][e.to] = e.cost; } } for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } } int main() { int h, w; cin >> h >> w; int s[h][w]; V = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { char c; cin >> c; if (c == '.') { s[i][j] = V++; } else { s[i][j] = -1; } } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] != -1) { edge e; e.cost = 1; if (i - 1 >= 0 && s[i - 1][j] != -1) { e.to = s[i - 1][j]; G[s[i][j]].push_back(e); } if (i + 1 < h && s[i + 1][j] != -1) { e.to = s[i + 1][j]; G[s[i][j]].push_back(e); } if (j - 1 >= 0 && s[i][j - 1] != -1) { e.to = s[i][j - 1]; G[s[i][j]].push_back(e); } if (j + 1 < w && s[i][j + 1] != -1) { e.to = s[i][j + 1]; G[s[i][j]].push_back(e); } } } } warshall_floyd(); int ans = 0; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { ans = max(d[i][j], ans); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int INF = 1e9 + 7; #define MAX_V 401 struct edge { int to; int cost; }; int V; vector<edge> G[MAX_V]; int d[MAX_V][MAX_V]; void warshall_floyd() { for (int i = 0; i < V; i++) { fill(d[i], d[i] + V, INF); d[i][i] = 0; for (int j = 0; j < G[i].size(); j++) { edge e = G[i][j]; d[i][e.to] = e.cost; } } for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } } int main() { int h, w; cin >> h >> w; int s[h][w]; V = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { char c; cin >> c; if (c == '.') { s[i][j] = V++; } else { s[i][j] = -1; } } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] != -1) { edge e; e.cost = 1; if (i - 1 >= 0 && s[i - 1][j] != -1) { e.to = s[i - 1][j]; G[s[i][j]].push_back(e); } if (i + 1 < h && s[i + 1][j] != -1) { e.to = s[i + 1][j]; G[s[i][j]].push_back(e); } if (j - 1 >= 0 && s[i][j - 1] != -1) { e.to = s[i][j - 1]; G[s[i][j]].push_back(e); } if (j + 1 < w && s[i][j + 1] != -1) { e.to = s[i][j + 1]; G[s[i][j]].push_back(e); } } } } warshall_floyd(); int ans = 0; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { ans = max(d[i][j], ans); } } cout << ans << endl; return 0; }
replace
4
5
4
5
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> #define INF 1001001001 #define PI 3.141592653589793 #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; int H; int W; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; void display_matrix(vector<vector<int>> &matrix) { int x, y; cout << "-----------------" << endl; for (y = 0; y < H; y++) { for (x = 0; x < W; x++) { cout << setw(3) << matrix[y][x]; } cout << endl; } cout << "-----------------" << endl; } int bfs(vector<vector<int>> &maze) { int x, y; int dmax = 0; int start_x; int start_y; vector<vector<int>> dist; queue<pair<int, int>> que; for (start_y = 0; start_y < H; start_y++) { for (start_x = 0; start_x < W; start_x++) { dist.clear(); dist.resize(H, vector<int>(W, -1)); if (maze[start_y][start_x]) continue; dist[start_y][start_x] = 0; que.emplace(start_x, start_y); while (!que.empty()) { x = que.front().first; y = que.front().second; que.pop(); rep(i, 4) { if (!(0 <= x + dx[i] && x + dx[i] < W && 0 <= y + dy[i] && y + dy[i] < H)) continue; if (dist[y + dy[i]][x + dx[i]] >= 0) continue; if (maze[y + dy[i]][x + dx[i]]) continue; que.emplace(x + dx[i], y + dy[i]); dist[y + dy[i]][x + dx[i]] = dist[y][x] + 1; } } rep(x, W) rep(y, H) dmax = max(dmax, dist[y][x]); } } return dmax; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); int i, k; int x, y; int ans = 0; int ctr = 0; char input; cin >> H >> W; vector<vector<int>> maze(W, vector<int>(H, 0)); for (y = 0; y < H; y++) { for (x = 0; x < W; x++) { cin >> input; if (input == '#') maze[y][x] = 1; else maze[y][x] = 0; } } ans = max(ans, bfs(maze)); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define INF 1001001001 #define PI 3.141592653589793 #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; int H; int W; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; void display_matrix(vector<vector<int>> &matrix) { int x, y; cout << "-----------------" << endl; for (y = 0; y < H; y++) { for (x = 0; x < W; x++) { cout << setw(3) << matrix[y][x]; } cout << endl; } cout << "-----------------" << endl; } int bfs(vector<vector<int>> &maze) { int x, y; int dmax = 0; int start_x; int start_y; vector<vector<int>> dist; queue<pair<int, int>> que; for (start_y = 0; start_y < H; start_y++) { for (start_x = 0; start_x < W; start_x++) { dist.clear(); dist.resize(H, vector<int>(W, -1)); if (maze[start_y][start_x]) continue; dist[start_y][start_x] = 0; que.emplace(start_x, start_y); while (!que.empty()) { x = que.front().first; y = que.front().second; que.pop(); rep(i, 4) { if (!(0 <= x + dx[i] && x + dx[i] < W && 0 <= y + dy[i] && y + dy[i] < H)) continue; if (dist[y + dy[i]][x + dx[i]] >= 0) continue; if (maze[y + dy[i]][x + dx[i]]) continue; que.emplace(x + dx[i], y + dy[i]); dist[y + dy[i]][x + dx[i]] = dist[y][x] + 1; } } rep(x, W) rep(y, H) dmax = max(dmax, dist[y][x]); } } return dmax; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); int i, k; int x, y; int ans = 0; int ctr = 0; char input; cin >> H >> W; vector<vector<int>> maze(H, vector<int>(W, 0)); for (y = 0; y < H; y++) { for (x = 0; x < W; x++) { cin >> input; if (input == '#') maze[y][x] = 1; else maze[y][x] = 0; } } ans = max(ans, bfs(maze)); cout << ans << endl; return 0; }
replace
79
80
79
80
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; const int INF = 0x3fffffff; int h, w; vector<string> s; vector<vector<int>> bfs(int x, int y) { vector<vector<int>> cost(h, vector<int>(h, INF)); cost[x][y] = 0; queue<pair<int, int>> q; q.emplace(x, y); while (q.size()) { int x, y; tie(x, y) = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int x2 = x + dx[i], y2 = y + dy[i]; if (x2 < 0 || x2 >= h || y2 < 0 || y2 >= w || s[x2][y2] == '#') continue; if (cost[x2][y2] > cost[x][y] + 1) { cost[x2][y2] = cost[x][y] + 1; q.emplace(x2, y2); } } } return cost; } int main() { cin >> h >> w; s.resize(h); for (auto &&i : s) cin >> i; int ans = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (s[i][j] != '#') { auto cost = bfs(i, j); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (s[i][j] != '#' && ans < cost[i][j]) ans = cost[i][j]; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; const int INF = 0x3fffffff; int h, w; vector<string> s; vector<vector<int>> bfs(int x, int y) { vector<vector<int>> cost(h, vector<int>(w, INF)); cost[x][y] = 0; queue<pair<int, int>> q; q.emplace(x, y); while (q.size()) { int x, y; tie(x, y) = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int x2 = x + dx[i], y2 = y + dy[i]; if (x2 < 0 || x2 >= h || y2 < 0 || y2 >= w || s[x2][y2] == '#') continue; if (cost[x2][y2] > cost[x][y] + 1) { cost[x2][y2] = cost[x][y] + 1; q.emplace(x2, y2); } } } return cost; } int main() { cin >> h >> w; s.resize(h); for (auto &&i : s) cin >> i; int ans = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (s[i][j] != '#') { auto cost = bfs(i, j); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (s[i][j] != '#' && ans < cost[i][j]) ans = cost[i][j]; } cout << ans << endl; }
replace
8
9
8
9
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define endl "\n" #define PI 3.14159265358979 #define MOD 1000000007 // = 10^9 + 7 using namespace std; using ll = long long; const int BLOCK = 100000; struct Path { int x, y, cost; Path(int _x, int _y, int _cost) { x = _x; y = _y; cost = _cost; } }; void solve() { int h, w; cin >> h >> w; vector<vector<int>> initial_maze(h, vector<int>(w, -1)); string s; for (int i = 0; i < h; ++i) { cin >> s; for (int j = 0; j < w; ++j) { initial_maze[i][j] = ((s[j] == '.') ? -1 : BLOCK); } } vector<vector<int>> maxPathCost(initial_maze); for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { if (initial_maze[j][i] == BLOCK) { continue; } vector<vector<int>> maze(initial_maze); queue<Path> que; Path p(i, j, 0); maze[i][j] = max(0, maze[i][j]); que.push(p); int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; while (!que.empty()) { Path now = que.front(); que.pop(); for (int k = 0; k < 4; ++k) { int next_x = now.x + dx[k]; int next_y = now.y + dy[k]; if (next_x < 0 || w <= next_x || next_y < 0 || h <= next_y) { continue; } if (maze[next_y][next_x] == -1) { Path next(next_x, next_y, now.cost + 1); maze[next.y][next.x] = next.cost; que.push(next); } // cout << "come " << next.x << ", " << next.y << " " << next.cost << // endl; } } for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { maxPathCost[y][x] = max(maze[y][x], maxPathCost[y][x]); } } } } int ans = 0; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { // cout << x << ", " << y << " : " << maxPathCost[y][x] << endl; if (maxPathCost[y][x] == BLOCK) { continue; } ans = max(ans, maxPathCost[y][x]); } } cout << ans; } int main() { fastio; solve(); return 0; }
#include <bits/stdc++.h> #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define endl "\n" #define PI 3.14159265358979 #define MOD 1000000007 // = 10^9 + 7 using namespace std; using ll = long long; const int BLOCK = 100000; struct Path { int x, y, cost; Path(int _x, int _y, int _cost) { x = _x; y = _y; cost = _cost; } }; void solve() { int h, w; cin >> h >> w; vector<vector<int>> initial_maze(h, vector<int>(w, -1)); string s; for (int i = 0; i < h; ++i) { cin >> s; for (int j = 0; j < w; ++j) { initial_maze[i][j] = ((s[j] == '.') ? -1 : BLOCK); } } vector<vector<int>> maxPathCost(initial_maze); for (int i = 0; i < w; ++i) { for (int j = 0; j < h; ++j) { if (initial_maze[j][i] == BLOCK) { continue; } vector<vector<int>> maze(initial_maze); queue<Path> que; Path p(i, j, 0); maze[j][i] = max(0, maze[j][i]); que.push(p); int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; while (!que.empty()) { Path now = que.front(); que.pop(); for (int k = 0; k < 4; ++k) { int next_x = now.x + dx[k]; int next_y = now.y + dy[k]; if (next_x < 0 || w <= next_x || next_y < 0 || h <= next_y) { continue; } if (maze[next_y][next_x] == -1) { Path next(next_x, next_y, now.cost + 1); maze[next.y][next.x] = next.cost; que.push(next); } // cout << "come " << next.x << ", " << next.y << " " << next.cost << // endl; } } for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { maxPathCost[y][x] = max(maze[y][x], maxPathCost[y][x]); } } } } int ans = 0; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { // cout << x << ", " << y << " : " << maxPathCost[y][x] << endl; if (maxPathCost[y][x] == BLOCK) { continue; } ans = max(ans, maxPathCost[y][x]); } } cout << ans; } int main() { fastio; solve(); return 0; }
replace
49
50
49
50
0
p02803
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define pb push_back #define all(v) (v).begin(), (v).end() #define fi first #define se second #define sz(x) ((int)(x).size()) using ll = long long; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define MOD 1000000007 const ll INF = 1e18; 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 dy[4] = {-1, 0, 1, 0}, dx[4] = {0, 1, 0, -1}; int h, w; vector<string> s(h); int bfs(int sy, int sx) { queue<pii> q; vector<vector<int>> dist(h, vector<int>(w, -1)); dist[sy][sx] = 0; q.push(pii(sy, sx)); while (!q.empty()) { pii now = q.front(); q.pop(); int y = now.fi, x = now.se; rep(i, 4) { int ny = y + dy[i], nx = x + dx[i]; if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue; if (s[ny][nx] == '#') continue; if (dist[ny][nx] == -1) { q.push(pii(ny, nx)); dist[ny][nx] = dist[y][x] + 1; } } } int ret = 0; rep(i, h) rep(j, w) ret = max(ret, dist[i][j]); return ret; } int main(int argc, char const *argv[]) { int ans = 0; cin >> h >> w; rep(i, h) cin >> s[i]; rep(i, h) rep(j, w) { if (s[i][j] == '#') continue; int res = bfs(i, j); ans = max(ans, res); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define pb push_back #define all(v) (v).begin(), (v).end() #define fi first #define se second #define sz(x) ((int)(x).size()) using ll = long long; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define MOD 1000000007 const ll INF = 1e18; 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 dy[4] = {-1, 0, 1, 0}, dx[4] = {0, 1, 0, -1}; int h, w; string s[20]; int bfs(int sy, int sx) { queue<pii> q; vector<vector<int>> dist(h, vector<int>(w, -1)); dist[sy][sx] = 0; q.push(pii(sy, sx)); while (!q.empty()) { pii now = q.front(); q.pop(); int y = now.fi, x = now.se; rep(i, 4) { int ny = y + dy[i], nx = x + dx[i]; if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue; if (s[ny][nx] == '#') continue; if (dist[ny][nx] == -1) { q.push(pii(ny, nx)); dist[ny][nx] = dist[y][x] + 1; } } } int ret = 0; rep(i, h) rep(j, w) ret = max(ret, dist[i][j]); return ret; } int main(int argc, char const *argv[]) { int ans = 0; cin >> h >> w; rep(i, h) cin >> s[i]; rep(i, h) rep(j, w) { if (s[i][j] == '#') continue; int res = bfs(i, j); ans = max(ans, res); } cout << ans << endl; return 0; }
replace
30
31
30
31
-11
p02803
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #include <queue> #include <stdio.h> #include <vector> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<double> vd; typedef pair<int, int> P; int di[] = {1, -1, 0, 0}; int dj[] = {0, 0, 1, -1}; const int INF = 1001001001; int main() { int h, w; cin >> h >> w; vector<string> g(h); rep(i, h) cin >> g[i]; // init graph vector<vi> dist(h * w, vi(h * w, INF)); rep(i, h * w) dist[i][i] = 0; rep(i, h) rep(j, w) { if (g[i][j] == '#') continue; rep(k, 4) { int dy = i + di[k]; int dx = j + dj[k]; if (dy < 0 or dy >= h or dx < 0 or dx >= w) continue; if (g[dy][dx] == '#') continue; dist[(i * h) + j][(dy * h) + dx] = 1; } } rep(k, h * w) { rep(i, h * w) { rep(j, h * w) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } int ans = 0; rep(i, h * w) { rep(j, h * w) { if (dist[i][j] != INF) ans = max(ans, dist[i][j]); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include <iostream> #include <queue> #include <stdio.h> #include <vector> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<double> vd; typedef pair<int, int> P; int di[] = {1, -1, 0, 0}; int dj[] = {0, 0, 1, -1}; const int INF = 1001001001; int main() { int h, w; cin >> h >> w; vector<string> g(h); rep(i, h) cin >> g[i]; // init graph vector<vi> dist(h * w, vi(h * w, INF)); rep(i, h * w) dist[i][i] = 0; rep(i, h) rep(j, w) { if (g[i][j] == '#') continue; rep(k, 4) { int dy = i + di[k]; int dx = j + dj[k]; if (dy < 0 or dy >= h or dx < 0 or dx >= w) continue; if (g[dy][dx] == '#') continue; dist[(i * w) + j][(dy * w) + dx] = 1; } } rep(k, h * w) { rep(i, h * w) { rep(j, h * w) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } int ans = 0; rep(i, h * w) { rep(j, h * w) { if (dist[i][j] != INF) ans = max(ans, dist[i][j]); } } cout << ans << endl; return 0; }
replace
39
40
39
40
0