// "Say:He is the Most Merciful,We have believed in him and upon him we have relied" [67:29]
//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx2")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<ll> vll;
typedef vector<int> vii;
typedef map<int,int> mpi;
typedef map<ll,ll> mpl;
typedef unordered_map<int,int> umpi;
typedef unordered_map<ll,ll> umpl;
#define ump unordered_map
#define mod 1000000007
#define inf 1000000000000000006
#define infi 1000000009
#define ff first
#define ss second
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl '\n'
#define pi acos(-1.0)
#define dec(n) fixed << setprecision(n)
#define N 200005
//#define int long long
int n,m,cc,d[1005][1005],mx,vis[1005][1005];
string s[1005];
int fx[]={+1,-1,0,0};
int fy[]={0,0,-1,+1};
bool isval(int x,int y){
if(x>=0 && x<n && y>=0 && y<m) return 1;
return 0;
}
void bfs(int sx,int sy){
d[sx][sy]=1;
vis[sx][sy]=1;
deque<pii> q;
q.pb({sx,sy});
while(!q.empty()){
int xx=q.front().ff,yy=q.front().ss;
q.pop_front();
for(int i=0;i<4;i++){
int x=xx+fx[i],y=yy+fy[i];
if(isval(x,y)==0 || vis[x][y]==1) continue;
vis[x][y]=1;
if(s[x][y]==s[xx][yy]){
d[x][y]=d[xx][yy];
mx=max(mx,d[x][y]);
q.push_front({x,y});
}
else if(s[x][y]!='*'){
d[x][y]=d[xx][yy]+1;
mx=max(mx,d[x][y]);
q.push_back({x,y});
}
}
}
}
void solve(){
cin >> n >> m;
for(int i=0;i<n;i++) cin >> s[i];
bfs(0,0);
cout << mx << endl;
}
signed main(){
fastio;
//srand(chrono::steady_clock::now().time_since_epoch().count());
int T=1,cs=0;
//cin >> T;
while(T--){
//cout << "Case " << ++cs << ":" << " " ;
solve();
}
}
Compilation message
zoo.cpp: In function 'int main()':
zoo.cpp:89:13: warning: unused variable 'cs' [-Wunused-variable]
89 | int T=1,cs=0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
1132 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1136 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
872 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
1132 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1136 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
872 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
13 ms |
10120 KB |
Output is correct |
17 |
Correct |
10 ms |
9932 KB |
Output is correct |
18 |
Correct |
11 ms |
10220 KB |
Output is correct |
19 |
Correct |
12 ms |
10096 KB |
Output is correct |
20 |
Correct |
10 ms |
9988 KB |
Output is correct |
21 |
Correct |
43 ms |
9676 KB |
Output is correct |
22 |
Correct |
40 ms |
9676 KB |
Output is correct |
23 |
Correct |
43 ms |
9804 KB |
Output is correct |
24 |
Correct |
46 ms |
10188 KB |
Output is correct |
25 |
Correct |
41 ms |
10060 KB |
Output is correct |
26 |
Correct |
40 ms |
9796 KB |
Output is correct |
27 |
Correct |
44 ms |
9812 KB |
Output is correct |
28 |
Correct |
40 ms |
9676 KB |
Output is correct |
29 |
Correct |
45 ms |
10188 KB |
Output is correct |
30 |
Correct |
42 ms |
9932 KB |
Output is correct |