#include<bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization("unroll-loops")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
// #pragma GCC optimize("no-stack-protector")
// #define ll __int128
#define ll long long
// #define ll int
#define f(i,a,b) for(int i=a;i<b;i++)
#define mod 1000000007
// #define mod 998244353
#define mp make_pair
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define ff first
#define ss second
#define rf(i,a,b) for(int i=a;i>=b;i--)
#define sc(a) scanf("%lld",&a)
#define pf printf
#define sz(a) (int)(a.size())
#define psf push_front
#define ppf pop_front
#define ppb pop_back
#define pb push_back
#define pq priority_queue
#define all(s) s.begin(),s.end()
#define sp(a) setprecision(a)
#define rz resize
#define ld long double
#define inf (ll)1e18
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define eb emplace_back
const double pi = acos(-1);
ll binpow(ll a, ll b){ll res=1;while(b!=0){if(b&1)res*=a;a*=a;b>>=1;}return res;}
// ll binpow(ll a, ll b, ll md){ll res=1;a%=mod;while(b!=0){if(b&1)res*=a,res%=md;a*=a,a%=md;b>>=1;}return res%md;}
using namespace std;
int n,m;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
vector<string> s;
bool check(int x, int y)
{
if(x>=0 && y>=0 && x<n && y<m && s[x][y]!='.')
return 1;
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("xortransform.in","r",stdin);
// freopen("xortransform.out","w",stdout);
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// #endif
int z=1;
// cin>>z;
f(i,1,z+1)
{
int ans=1;
cin>>n>>m;
s.rz(n);
f(i,0,n)
cin>>s[i];
vector<vector<int> > dis(n,vector<int> (m,INT_MAX));
deque<pair<int,int> > q;
q.psf({0,0});
dis[0][0]=1;
while(!q.empty())
{
auto temp=q.front();
q.pop_front();
int x=temp.ff,y=temp.ss;
f(i,0,4)
{
int xx=x+dx[i],yy=y+dy[i];
if(check(xx,yy))
{
if(s[xx][yy]!=s[x][y] && dis[xx][yy]>dis[x][y]+1)
{
dis[xx][yy]=1+dis[x][y];
ans=max(ans,dis[xx][yy]);
q.pb({xx,yy});
}
else if(s[xx][yy]==s[x][y] && dis[xx][yy]>dis[x][y])
{
dis[xx][yy]=dis[x][y];
ans=max(ans,dis[xx][yy]);
q.psf({xx,yy});
}
}
}
}
cout<<ans<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
1740 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
9 ms |
1484 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
716 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
6 ms |
836 KB |
Output is correct |
13 |
Correct |
2 ms |
716 KB |
Output is correct |
14 |
Correct |
2 ms |
716 KB |
Output is correct |
15 |
Correct |
13 ms |
1864 KB |
Output is correct |
16 |
Correct |
17 ms |
1848 KB |
Output is correct |
17 |
Correct |
9 ms |
1700 KB |
Output is correct |
18 |
Correct |
9 ms |
1484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
716 KB |
Output is correct |
2 |
Correct |
47 ms |
8908 KB |
Output is correct |
3 |
Correct |
233 ms |
82296 KB |
Output is correct |
4 |
Correct |
62 ms |
19788 KB |
Output is correct |
5 |
Correct |
158 ms |
46660 KB |
Output is correct |
6 |
Correct |
931 ms |
96340 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
48 ms |
8652 KB |
Output is correct |
14 |
Correct |
28 ms |
5324 KB |
Output is correct |
15 |
Correct |
17 ms |
5784 KB |
Output is correct |
16 |
Correct |
25 ms |
4044 KB |
Output is correct |
17 |
Correct |
122 ms |
21016 KB |
Output is correct |
18 |
Correct |
77 ms |
20832 KB |
Output is correct |
19 |
Correct |
62 ms |
19568 KB |
Output is correct |
20 |
Correct |
57 ms |
17996 KB |
Output is correct |
21 |
Correct |
143 ms |
47932 KB |
Output is correct |
22 |
Correct |
156 ms |
46292 KB |
Output is correct |
23 |
Correct |
248 ms |
40136 KB |
Output is correct |
24 |
Correct |
134 ms |
46752 KB |
Output is correct |
25 |
Correct |
422 ms |
82036 KB |
Output is correct |
26 |
Correct |
951 ms |
112924 KB |
Output is correct |
27 |
Correct |
900 ms |
107680 KB |
Output is correct |
28 |
Correct |
943 ms |
96304 KB |
Output is correct |
29 |
Correct |
943 ms |
93680 KB |
Output is correct |
30 |
Correct |
921 ms |
99148 KB |
Output is correct |
31 |
Correct |
718 ms |
53008 KB |
Output is correct |
32 |
Correct |
865 ms |
106028 KB |
Output is correct |