이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define MAX 4005
using namespace std;
char mat[MAX][MAX];
int bfs[MAX][MAX];
bool inqueue[MAX][MAX];
int dl[]={-1,0,1,0};
int dc[]={0,1,0,-1};
class cmp
{
public:
bool operator()(pair<int,int> a,pair<int,int> b)
{
return bfs[a.first][a.second]>bfs[b.first][b.second];
}
};
int main()
{
int n,m;
cin>>n>>m;
int i,j;
for(i=1;i<=n;++i)
for(j=1;j<=m;++j)
{
cin>>mat[i][j];
bfs[i][j]=INT_MAX;
}
bfs[1][1]=1;
priority_queue<pair<int,int>,vector<pair<int,int>>,cmp>pq;
pq.push({1,1});
inqueue[1][1]=1;
while(!pq.empty())
{
pair<int,int>nou=pq.top();
int l=nou.first;
int c=nou.second;
pq.pop();
inqueue[l][c]=0;
for(i=0;i<4;++i)
{
int lnou=l+dl[i];
int cnou=c+dc[i];
int pret=bfs[l][c]+(mat[l][c]!=mat[lnou][cnou]);
if(isalpha(mat[lnou][cnou]) && pret<bfs[lnou][cnou])
{
bfs[lnou][cnou]=pret;
if(!inqueue[lnou][cnou])
{
pq.push({lnou,cnou});
inqueue[lnou][cnou]=1;
}
}
}
}
int answer=-1;
for(i=1;i<=n;++i)
for(j=1;j<=m;++j)
if(isalpha(mat[i][j]))
answer=max(answer,bfs[i][j]);
cout<<answer;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |