이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define loop(_) for (int __ = 0; __ < (_); ++__)
#define pb push_back
#define f first
#define s second
#define sz(_) ((int)_.size())
#define all(_) _.begin(), _.end()
#define lb lower_bound
#define ub upper_bound
using ll = long long;
using ld = long double;
const int N = 800 + 7;
const ll mod = 1e9 + 7;
int n, m, k, Min, Cnt;
int a[N][N], b[N][N];
int mov[100000 + 8];
string st;
int col;
int viz[N][N];
int m1[4] = {1, -1, 0, 0}, m2[4] = {0, 0, 1, -1};
int mask[16] ;
bool chk(int A, int B)
{
int msk = 0 ;
for (int j = 0 ;j < 4; ++ j){
int nx = A + m1[j] ;
int ny = B + m2[j] ;
if(nx >= 0 &&nx < n && ny >= 0 && ny < m && viz[nx][ny] == col){
msk|=(1<<j) ;
}
}
return mask[msk] >= a[A][B];
}
int get(vi arr){
int best = 0;
int sum = 0;
for (int i = 0; i < k; ++i)
{
if (!arr[i])
break;
best++;
}
for (int i = k - 1; ~i; --i)
{
if (!arr[i])
break;
best++;
}
for (int i = 0; i < k; ++i)
{
if (!arr[i])
{
best = max(best, sum);
sum = 0;
}
else
++sum;
}
best = max(best, sum);
return best;
}
int solve(int x, int y)
{
++col;
queue<pii> qu;
qu.push({x, y});
int ret = 1;
viz[x][y] = col;
while (sz(qu))
{
int A = qu.front().f;
int B = qu.front().s;
qu.pop();
for (int j = 0; j < 4; ++j)
{
int nx = A + m1[j];
int ny = B + m2[j];
if (nx >= 0 && nx < n && ny >= 0 && ny < m)
{
if (viz[nx][ny] != col && a[nx][ny] != 0 && chk(nx, ny))
{
qu.push({nx, ny});
++ret;
viz[nx][ny] = col;
}
}
}
}
return ret;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
#endif
cin >> k >> n >> m;
cin >> st;
for (int i = 0; i < k; ++i)
{
if (st[i] == 'S')
mov[i] = 0;
else if (st[i] == 'N')
mov[i] = 1;
else if (st[i] == 'W')
mov[i] = 3;
else
mov[i] = 2;
}
for(int i = 0 ;i < (1<<4) ;++ i){
vi arr(k,0) ;
for(int j= 0 ;j < k ;++ j){
arr[j] = (i&(1<<mov[j])) ;
}
mask[i] = get(arr) ;
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> a[i][j];
}
}
Min = 1e9;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (!a[i][j])
continue;
int an = solve(i, j);
if (an < Min)
{
Min = an, Cnt = 1;
}
else if (an == Min)
++Cnt;
}
}
if(Min == 1e9)Min = 0 ;
cout << Min << "\n";
cout << Cnt << "\n";
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... |