이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
#define bb back
#define ff first
#define ss second
using namespace std;
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("tmp.in", "r", stdin);
// freopen("tmp.out", "w", stdout);
int H, W;
cin >> H >> W;
vector<string> grid(H);
for (int i = 0; i < H; i++)
cin >> grid[i];
vector<vector<bool> > used(H, vector<bool>(W));
deque<pair<int, int> > q;
q.push_back({0, 0});
char last = '?';
int ans = 0;
pair<int, int> dir[4] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
while (!q.empty()) {
pair<int, int> now = q.front();
q.pop_front();
if (grid[now.ff][now.ss] != last) {
ans++;
last = grid[now.ff][now.ss];
}
for (pair<int, int> mv : dir) {
if (used[now.ff+mv.ff][now.ss+mv.ss]) continue;
if (now.ff+mv.ff < 0 || now.ff+mv.ff >= H) continue;
if (now.ss+mv.ss < 0 || now.ss+mv.ss >= W) continue;
if (grid[now.ff+mv.ff][now.ss+mv.ss] == '.') continue;
if (grid[now.ff+mv.ff][now.ss+mv.ss] == grid[now.ff][now.ss])
q.push_front({now.ff+mv.ff, now.ss+mv.ss});
else
q.push_back({now.ff+mv.ff, now.ss+mv.ss});
used[now.ff+mv.ff][now.ss+mv.ss] = 1;
}
}
cout << ans << '\n';
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |