This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fs first
#define sc second
vector<string> arr;
int R,C,N;
pii s,e;
pii dir[] = {{0,1},{1,0},{0,-1},{-1,0}};
namespace N1{
vector<vector<int>> dist;
deque<pair<pii,int>> dq;
void GO(){
dist = vector<vector<int>>(R,vector<int>(C,R*C+1));
dist[s.fs][s.sc] = (arr[s.fs][s.sc] == '#');
dq.push_back(make_pair(s,dist[s.fs][s.sc]));
while(!dq.empty()){
auto now = dq.front().fs;
auto d = dq.front().sc;
dq.pop_front();
if(dist[now.fs][now.sc] != d)continue;
for(auto &dd:dir){
pii nxt = {now.fs+dd.fs,now.sc+dd.sc};
if(nxt.fs>=R||nxt.fs<0||nxt.sc>=C||nxt.sc<0)continue;
int w = (arr[nxt.fs][nxt.sc] == '#');
if(dist[nxt.fs][nxt.sc]>d+w){
dist[nxt.fs][nxt.sc] = d+w;
if(w==1)dq.push_back(make_pair(nxt,d+w));
else dq.push_front(make_pair(nxt,d+w));
}
}
}
cout<<dist[e.fs][e.sc]<<'\n';
return;
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>R>>C>>N;
cin>>s.fs>>s.sc>>e.fs>>e.sc;
arr = vector<string>(R);
for(int i = 0;i<R;i++)cin>>arr[i];
s.fs--,s.sc--,e.fs--,e.sc--;
if(N==1)N1::GO();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |