이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize ("O1")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ll long long
#define int ll
#define pii pair<int, int>
#define io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define M_PI 3.14159265358979323846
#define all(v) v.begin(), v.end()
#define pss pair<string, string>
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define imp cout<<-1<<endl;
#define flu cout.flush();
#define Endl endl
const int N = 100009;
const int mod = 1e9+7;
char a[4005][4005];
int used[4005][4005];
int dx[]={1, -1, 0, 0, 1, -1, 1, -1};
int dy[]={0, 0, -1, 1, 1, -1, -1, 1};
int n, m;
char start;
void dfsr(int x, int y){
used[x][y]++;
for(int i=0; i<8; i++){
if(x+dx[i]>=0 and x+dx[i]<n and y+dy[i]>=0 and y+dy[i]<m){
if(used[x+dx[i]][y+dy[i]]==0 and a[x+dx[i]][y+dy[i]]==start){
dfsr(x+dx[i], y+dy[i]);
}
}
}
}
void dfsf(int x, int y){
used[x][y]++;
for(int i=0; i<8; i++){
if(x+dx[i]>=0 and x+dx[i]<n and y+dy[i]>=0 and y+dy[i]<m){
if(used[x+dx[i]][y+dy[i]]==0 and a[x+dx[i]][y+dy[i]]!=start){
dfsf(x+dx[i], y+dy[i]);
}
}
}
}
void solve(){
cin>>n>>m;
int cntf=0, cntr=0;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
char c;
cin>>c;
a[i][j]=c;
if(c=='F'){
cntf++;
}
else if(c=='R'){
cntr++;
}
}
}
int ans=0;
if(cntf>0){
ans++;
}
if(cntr>0){
ans++;
}
if(ans<2){
cout<<ans<<endl;
return;
}
start=a[0][0];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(a[i][j]=='.'){
a[i][j]=start;
}
}
}
int cnt=0, cnt2=0;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(a[i][j]==start){
if(used[i][j]==0){
dfsr(i, j);
cnt++;
}
}
else{
if(used[i][j]==0){
dfsf(i, j);
cnt2++;
}
}
}
}
if(cnt2>cnt){
cnt2=cnt;
}
cout<<cnt2+cnt<<endl;
/*
FRFF
FRRR
FFFF
FFRFFFFF
FFRRRFFF
FFFFFFFF
FFRRRFFR
FFFFFFFF
*/
}
signed main(){
io;
int t=1;
//cin>>t;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |