이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// And you curse yourself for things you never done
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 4010, mod = 1e9 + 7, inf = 1e9 + 10;
int n, m;
string s[maxn];
int a[maxn][maxn];
bool mark[maxn][maxn];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
cin >> n >> m;
for(int i = 0; i < n; i++){
cin >> s[i];
}
assert(s[0][0] == s[n-1][m-1] && s[0][0] != '.');
char c = s[0][0];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
a[i][j] = (s[i][j] == '.' ? -1 : (s[i][j] != c));
}
}
queue<pii> q[2];
q[0].push({0,0});
mark[0][0] = 1;
int ans = 0;
while(sz(q[ans & 1])){
while(sz(q[ans & 1])){
pii p = q[ans & 1].front();
q[ans & 1].pop();
for(int i = 0; i < 4; i++){
int x = p.F + dx[i], y = p.S + dy[i];
if(x >= 0 && y >= 0 && x < n && y < m && a[x][y] != -1 && mark[x][y] == 0)
mark[x][y] = 1, q[a[x][y]].push({x, y});
}
}
ans++;
}
return cout << ans << endl, 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |