이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mxH=505, mxW=505;
int H, W;
char grid[mxH][mxW], rChange[4]={0, 1, 0, -1}, cChange[4]={1, 0, -1, 0};
bool vis[mxH][mxW];
int floodfill(int r, int c, char curr) {
int cnt=0;
stack<pair<int, int>> frontier;
frontier.push({r, c});
while(!frontier.empty()) {
r=frontier.top().first;
c=frontier.top().second;
frontier.pop();
if(r<0||r>=H||c<0||c>=W||grid[r][c]!=curr||vis[r][c]) {
continue;
}
++cnt;
vis[r][c]=1;
grid[r][c]=(grid[r][c]=='F'?'R' : 'F');
for(int i=0; i<4; ++i)
frontier.push({r+rChange[i], c+cChange[i]});
}
return cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> H >> W;
int tot=0;
for(int i=0; i<H; ++i) {
for(int j=0; j<W; ++j) {
cin >> grid[i][j];
if(grid[i][j]!='.')
++tot;
}
}
int ans=0;
while(1) {
memset(vis, 0, sizeof(vis));
if(tot==floodfill(0, 0, grid[0][0]))
break;
++ans;
}
cout << ans+1;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |