이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const long long MAX=4001;
long long n,m;
char mat[MAX][MAX];
long long di[4]={0,0,-1,1};
long long dj[4]={1,-1,0,0};
long long vis[MAX][MAX];
bool valid_move(long long i, long long j)
{
if(i<0 || i>=n || j<0 || j>=m || vis[i][j]!=0 || mat[i][j]=='.')return false;
return true;
}
int main()
{
cin>>n>>m;
for(long long i=0; i<n; i++)
{
for(long long j=0; j<m; j++)
{
cin>>mat[i][j];
}
}
deque<pair<long long,long long> > q;
long long res=0;
q.push_back({0,0});
memset(vis,0,sizeof(vis));
vis[0][0]=1;
while(q.size())
{
pair<long long,long long> curr=q.front();
q.pop_front();
res=max(res,vis[curr.first][curr.second]);
for(long long k=0; k<4; k++)
{
long long newi=curr.first+di[k];
long long newj=curr.second+dj[k];
if(!valid_move(newi,newj))continue;
if(mat[newi][newj]==mat[curr.first][curr.second])
{
q.push_front({newi,newj});
vis[newi][newj]=vis[curr.first][curr.second];
}
else
{
q.push_back({newi,newj});
vis[newi][newj]=vis[curr.first][curr.second]+1;
}
}
}
cout<<res<<endl;
return 0;
}
/*
5 8
FFR.....
.FRRR...
.FFFFF..
..RRRFFR
.....FFF*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |