# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
998326 | AtinaR | Mecho (IOI09_mecho) | C++14 | 159 ms | 5336 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAX=810;
const int INF=(1<<30);
int di[4]={0,0,-1,1};
int dj[4]={1,-1,0,0};
int n,s;
char mat[MAX][MAX];
int timebees[MAX][MAX];
bool visbees[MAX][MAX];
bool vis[MAX][MAX];
vector<pair<int,int> > hiveinitial;
vector<pair<int,int> > homeinitial;
int mi,mj;
int homei,homej;
bool validBees(int i, int j)
{
if(i<0 || i>=n || j<0 || j>=n || mat[i][j]=='T'||mat[i][j]=='D'|| mat[i][j]=='H'|| visbees[i][j])return false;
return true;
}
bool validBear(int i, int j)
{
if(i<0 || i>=n || j<0 || j>=n || mat[i][j]!='G' || vis[i][j])return false;
return true;
}
void bfsBees()
{
queue<pair<int,int> > q;
for(int i=0; i<hiveinitial.size(); i++)
{
int x=hiveinitial[i].first;
int y=hiveinitial[i].second;
q.push({x,y});
visbees[x][y]=true;
timebees[x][y]=0;
}
while(!q.empty())
{
pair<int,int> curr=q.front();
q.pop();
int x=curr.first;
int y=curr.second;
for(int k=0; k<4; k++)
{
int newi=x+di[k];
int newj=y+dj[k];
if(validBees(newi,newj))
{
q.push({newi,newj});
timebees[newi][newj]=timebees[x][y]+1;
visbees[newi][newj]=true;
}
}
}
return;
}
bool bearreached(int mechodis, int beedis)
{
return mechodis/s<beedis;
}
bool canReach(int secs)
{
queue<pair<int,int> > q;
q.push({mi,mj});
if(timebees[mi][mj]<=secs)
{
return false;
}
memset(vis,false,sizeof(vis));
vis[mi][mj]=true;
queue<int> steps;
steps.push(0);
while(!q.empty())
{
pair<int,int> curr=q.front();
int x=curr.first;
int y=curr.second;
int currstep=steps.front();
steps.pop();
q.pop();
for(int k=0; k<4; k++)
{
int newi=x+di[k];
int newj=y+dj[k];
if(mat[newi][newj]=='D')return true;
if(validBear(newi,newj))
{
if(bearreached(currstep+1,timebees[newi][newj]-secs))
{
q.push({newi,newj});
steps.push(currstep+1);
vis[newi][newj]=true;
}
}
}
}
for(int k=0; k<4; k++)
{
int newi=homei+di[k];
int newj=homej+dj[k];
if(validBear(newi,newj)&& vis[newi][newj])return true;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>s;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cin>>mat[i][j];
if(mat[i][j]=='H')
{
hiveinitial.push_back({i,j});
}
if(mat[i][j]=='M')
{
mi=i;
mj=j;
}
if(mat[i][j]=='D')
{
homei=i;
homej=j;
}
}
}
bfsBees();
int b=0;
int e=n*n;
while(b<=e)
{
int mid=(b+e)/2;
if(canReach(mid))
{
b=mid+1;
}
else
{
e=mid-1;
}
}
cout<<b-1<<endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |