이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
#define rrep(i,a,b) for(int i=int(a);i>int(b);i--)
#define trav(a,v) for(auto& a: v)
#define sz(v) v.size()
#define all(v) v.begin(),v.end()
#define vi vector<int>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const long long inf = 1e15;
using namespace std;
vector<vector<ll>> grid;
vector<vector<ll>> dp;
ll m, r, c;
vector<vector<bool>> visited(1000, vector<bool>(1000));
vector<ll> edges(4);
ll dfs(ll x, ll y, bool first) {
if (first && grid[x][y] == inf)return inf;
if (y && first)if (grid[x][y - 1] <= edges[2])return inf;
if (y == c - 1)return 1;
if (grid[x][y] <= edges[2] && grid[x][y + 1] <= edges[0])return 1 + dfs(x, y + 1, 0);
else if (grid[x][y] <= edges[2])return 1;
else if (grid[x][y + 1] <= edges[0])return dfs(x, y + 1, 0);
return 1;
}
int main() {
string s;
cin >> m >> r >> c;
cin >> s;
grid.resize(r, vector<ll>(c));
dp.resize(r, vector<ll>(c, -1));
rep(i, 0, r) {
rep(j, 0, c) {
cin >> grid[i][j];
if (grid[i][j] == 0)grid[i][j] = inf;
}
}
while (s.size() < 1e5)s += s;//maybe slow
map<char, ll> mp;
mp['W'] = 0;
mp['N'] = 1;
mp['E'] = 2;
mp['S'] = 3;
ll curchar = -1;
ll cur = 0;
rep(i, 0, s.size()) {
if (curchar != mp[s[i]]) {
if (curchar != -1)edges[curchar] = max(edges[curchar], cur);
cur = 0;
curchar = mp[s[i]];
}
cur++;
}
//if there is an undirected edge between two nodes and merge them
//else take the node with that lacks an edge back
//if tere is no edges between two nodes then you have two different components, take the minimun of them
//after doing BFS in a compononet you may be able to add nodes , this may be a bit hard to implement
//the second answer is gonna be the number of nodes in a component
ll ans = inf;
ll howmany = 0;
rep(i, 0, r) {
rep(j, 0, c) {
if (ans > dfs(i, j, 1)) {
ans = min(ans, dfs(i, j, 1));
howmany = 1;
}
else if (ans == dfs(i, j, 1))howmany++;
}
}
cout << ans << endl << ans*howmany << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |