이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//#pragma GCC target("sse,sse2,avx2")
#pragma GCC optimize("unroll-loops,O3")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 4e3+20,mod = 998244353;
constexpr ll inf = 1e9+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k >>= 1;
}
return z;
}
int d[N][N];
string s[N];
int n,m;
pll ja[4] = {{1,0},{-1,0},{0,1},{0,-1}};
inline bool isval(int i,int j){
if (min(i,j) < 0 || i >= n || j >= m || s[i][j] == '.') return 0;
return 1;
}
int bfs(){
deque<pll> q;
q.pb({0,0});
d[0][0] = 1;
int mx = 0;
while (!q.empty()){
pll p = q.front();
q.pop_front();
int x = p.X,y = p.Y;
mx = max(mx,d[x][y]);
rep(i,0,4){
int x2 = x + ja[i].X,y2 = y + ja[i].Y;
if (isval(x2,y2) && !d[x2][y2]){
if (s[x2][y2] == s[x][y]){
d[x2][y2] = d[x][y];
q.push_front({x2,y2});
}
else{
d[x2][y2] = d[x][y] + 1;
q.pb({x2,y2});
}
}
}
}
return mx;
}
int main(){
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
rep(i,0,n) cin >> s[i];
cout << bfs() << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |