이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
typedef long double ld ;
#define FAST ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define pb push_back
#define pi pair<ll,ll>
#define pll pair<ll,ll>
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define firsst cout<<"First"<<endl;
#define seconnd cout<<"Second"<<endl;
#define tiie cout<<"Tie"<<endl;
#define fr(i,a,b) for(ll i = a;i < (ll)b;i++)
#define rfr(i,a,b) for(ll i = a;i > (ll)b;i--)
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define alll(x) ((x).begin()+1), (x).end()
#define MOD mod
#define endl '\n'
//#define uid(a, b) uniform_ll_distribution<ll>(a, b)(rng)
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 998244353 ;
void io(){ios::sync_with_stdio(false) ;cin.tie(NULL) ;
//freopen("tractor.in","r",stdin) ;freopen("tractor.out","w",stdout) ;
}
void dbg(vector<ll> tab){for(auto it : tab) cout<<it<<" ";cout<<endl;}
void dbgg(pi p){cout<<p.F<<" "<<p.S<<endl;}
void dbgpi(vector<pi> tab){for(auto it : tab) dbgg(it) ;}
template<class T> bool ckmax(T& a, const T& b){return a < b ? a = b, 1 : 0;}
template<class T> bool ckmin(T& a, const T& b){return a > b ? a = b, 1 : 0;}
template<class T> void add(T& a, const T& b){a = a + b ; if(a>mod) a-= mod ;}
void nop(){cout<<-1<<endl;return;}
const ll N = 4e3+5 , NAX = 1e6 +5 ;
char tab[N][N] ;
int vis[N][N] ;
int n , m ;
int di[] = {-1 , 0 , 1 , 0} ;
int dj[] = {0 , 1 , 0 , -1} ;
bool ok(int i , int j)
{
if(i<1 || i>n || j<1 || j>m) return false ;
return true ;
}
void dfs(int i , int j , char c)
{
if(vis[i][j]) return ;
vis[i][j] = 1 ;
for(int k = 0 ; k<4 ; k++){
int ii = di[k]+i ;
int jj = dj[k]+j ;
if(ok(ii,jj) && tab[ii][jj]==c){
dfs(ii,jj,c) ;
}
}
}
void solve()
{
cin>>n>>m ;
for(int i = 1 ; i<=n ; i++){
for(int j = 1 ; j<=m ; j++){
cin>>tab[i][j] ;
}
}
char c = tab[1][1] , ok = 0 , ans = 0 ;
if(c=='.'){
cout<<0<<endl;
return ;
}
for(int i = 1 ; i<=n ; i++){
for(int j = 1 ; j<=m ; j++){
if(c==tab[i][j] && !vis[i][j]){
ans++ ;
dfs(i , j , c) ;
}
if(tab[i][j]!=c && tab[i][j]!='.') ok = 1 ;
}
}
cout<<ans+ok<<endl;
}
int main()
{
io() ;
ll tt = 1 ;
// cin>>tt ;
while(tt--) solve() ;
return 0 ;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |