Submission #1165134

#TimeUsernameProblemLanguageResultExecution timeMemory
1165134KasymKRobots (APIO13_robots)C++17
0 / 100
0 ms324 KiB
#include "bits/stdc++.h" using namespace std; #define ff first #define ss second #define all(v) v.begin(), v.end() #define ll long long #define pb push_back #define pii pair<int, int> #define pli pair<ll, int> #define pll pair<ll, ll> #define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i) #define wr puts("----------------") template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;} template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;} const int N = 13; const int INF = 1e9; char c[N][N]; int dis[N][N], dis2[N][N]; int main(){ int G, n, m; scanf("%d%d%d", &G, &m, &n); assert(G==2); pii F, S; for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j){ dis[i][j]=INF, dis2[i][j]=INF; scanf(" %c", &c[i][j]); if(c[i][j]=='1') F={i, j}; if(c[i][j]=='2') S={i, j}; } queue<pii> q; q.push(F); dis[F.ff][F.ss]=0; while(!q.empty()){ pii P=q.front(); q.pop(); // go right bool did=0; for(int y = P.ss+1; y <= m and !did; ++y) if(c[P.ff][y]=='#'){ did=1; if(umin(dis[P.ff][y-1], dis[P.ff][P.ss]+1)) q.push({P.ff, y-1}); } if(!did and umin(dis[P.ff][m], dis[P.ff][P.ss]+1)) q.push({P.ff, m}); // go left did=0; for(int y = P.ss-1; y >= 1 and !did; --y) if(c[P.ff][y]=='#'){ did=1; if(umin(dis[P.ff][y+1], dis[P.ff][P.ss]+1)) q.push({P.ff, y+1}); } if(!did and umin(dis[P.ff][1], dis[P.ff][P.ss]+1)) q.push({P.ff, 1}); // go down did=0; for(int x = P.ff+1; x <= n and !did; ++x) if(c[x][P.ss]=='#'){ did=1; if(umin(dis[x-1][P.ss], dis[P.ff][P.ss]+1)) q.push({x-1, P.ss}); } if(!did and umin(dis[n][P.ss], dis[P.ff][P.ss]+1)) q.push({n, P.ss}); // go up did=0; for(int x = P.ff-1; x >= 1 and !did; --x) if(c[x][P.ss]=='#'){ did=1; if(umin(dis[x+1][P.ss], dis[P.ff][P.ss]+1)) q.push({x+1, P.ss}); } if(!did and umin(dis[1][P.ss], dis[P.ff][P.ss]+1)) q.push({1, P.ss}); } assert(q.empty()); q.push(S); dis2[S.ff][S.ss]=0; while(!q.empty()){ pii P=q.front(); q.pop(); // go right bool did=0; for(int y = P.ss+1; y <= m and !did; ++y) if(c[P.ff][y]=='#'){ did=1; if(umin(dis2[P.ff][y-1], dis2[P.ff][P.ss]+1)) q.push({P.ff, y-1}); } if(!did and umin(dis2[P.ff][m], dis2[P.ff][P.ss]+1)) q.push({P.ff, m}); // go left did=0; for(int y = P.ss-1; y >= 1 and !did; --y) if(c[P.ff][y]=='#'){ did=1; if(umin(dis2[P.ff][y+1], dis2[P.ff][P.ss]+1)) q.push({P.ff, y+1}); } if(!did and umin(dis2[P.ff][1], dis2[P.ff][P.ss]+1)) q.push({P.ff, 1}); // go down did=0; for(int x = P.ff+1; x <= n and !did; ++x) if(c[x][P.ss]=='#'){ did=1; if(umin(dis2[x-1][P.ss], dis2[P.ff][P.ss]+1)) q.push({x-1, P.ss}); } if(!did and umin(dis2[n][P.ss], dis2[P.ff][P.ss]+1)) q.push({n, P.ss}); // go up did=0; for(int x = P.ff-1; x >= 1 and !did; --x) if(c[x][P.ss]=='#'){ did=1; if(umin(dis2[x+1][P.ss], dis2[P.ff][P.ss]+1)) q.push({x+1, P.ss}); } if(!did and umin(dis2[1][P.ss], dis2[P.ff][P.ss]+1)) q.push({1, P.ss}); } assert(q.empty()); int answer=INF; for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) if(c[i][j]!='#') umin(answer, dis[i][j]+dis2[i][j]); if(answer==INF){ puts("-1"); return 0; } printf("%d\n", answer); // wr; // for(int i = 1; i <= n; ++i){ // for(int j = 1; j <= m; ++j) // printf("%d ", dis2[i][j]); // puts(""); // } return 0; }

Compilation message (stderr)

robots.cpp: In function 'int main()':
robots.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |     scanf("%d%d%d", &G, &m, &n);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
robots.cpp:28:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |             scanf(" %c", &c[i][j]);
      |             ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...