This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <random>
#include <string>
#include <deque>
#include <queue>
#include <unordered_set>
#include <functional>
#include <stack>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vll> vvll;
typedef vector<vld> vvld;
typedef vector<vvll> vvvll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef vector<pll> vpll;
typedef stack<pll> stpll;
typedef vector<stpll> vstpll;
typedef vector<pld> vpld;
typedef set<ll> sll;
typedef set<pll> spll;
typedef map<pll, ll> mpll_ll;
typedef map<ll, pll> mll_pll;
typedef string str;
typedef vector<str> vstr;
typedef vector<char> vc;
typedef vector<vc> vvc;
#define rep(x, a, b) for (ll x = a; x < b; x++)
#define rev_rep(x, a, b) for (ll x = a; x >= b; x--)
#define itr_rep(type_, x, b) for (type_::iterator x = b.begin(); x != b.end(); x++)
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) a.size()
#define resz(a, b) a.resize(b)
#define sort_all(a) sort(all(a))
#define pb(a) push_back(a)
#define fill_sz(a, b) fill_n(a.begin(), sz(a), b)
const ll INF = 1000000000;
ll H, W;
vvb fox;
vvb rab;
vvc board;
ll N = 1;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> H >> W;
resz(fox, H);
resz(rab, H);
resz(board, H);
rep(i, 0, H) {
resz(fox[i], W);
resz(rab[i], W);
resz(board[i], W);
fill_sz(fox[i], false);
fill_sz(rab[i], false);
}
rep(i, 0, H) {
rep(j, 0, W) {
cin >> board[i][j];
fox[i][j] = (board[i][j] == 'F');
rab[i][j] = (board[i][j] == 'R');
}
}
vstpll bfs;
vvll dst;
resz(bfs, H * W);
resz(dst, H);
rep(i, 0, H) {resz(dst[i], W); fill_sz(dst[i], INF);}
bfs[0].push(mp(0, 0));
dst[0][0] = 0;
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};
ll ptr = 0;
while (not bfs[ptr].empty()) {
pll nx = bfs[ptr].top();
bfs[ptr].pop();
if (dst[nx.first][nx.second] != ptr) continue;
N = ptr;
rep(i, 0, 4) {
pll nw = mp(nx.first + dx[i], nx.second + dy[i]);
if ((0 <= nw.first) and (nw.first < H) and (0 <= nw.second) and (nw.second < W)) {
if (board[nx.first][nx.second] == board[nw.first][nw.second]) {
if (dst[nx.first][nx.second] < dst[nw.first][nw.second]) {
dst[nw.first][nw.second] = dst[nx.first][nx.second];
bfs[dst[nw.first][nw.second]].push(nw);
}
} else if (board[nw.first][nw.second] != '.') {
if (dst[nx.first][nx.second] + 1 < dst[nw.first][nw.second]) {
dst[nw.first][nw.second] = dst[nx.first][nx.second] + 1;
bfs[dst[nw.first][nw.second]].push(nw);
}
}
}
}
if (bfs[ptr].empty()) ptr++;
}
cout << N + 1 << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |