# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
666585 |
2022-11-29T06:13:26 Z |
AmirH |
Robots (APIO13_robots) |
C++14 |
|
1 ms |
340 KB |
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<bitset>
#include<queue>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<unordered_map>
#include<list>
#include<utility>
#include<stack>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
#define cl clear
#define F first
#define S second
#define pb push_back
#define Sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define faster ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const int MAX_N = 5e2 + 10;
const ll INF = 1e18;
const int inf = 1e9;
void free() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int n, w, h;
char ch[MAX_N][MAX_N];
set<int> sat[MAX_N], sot[MAX_N];
vector<int> adj[MAX_N];
int heightst[MAX_N][2];
void bfs(int st, int ft, int d) {
int height[MAX_N];
bool mark[MAX_N];
for(int i = 1; i <= h * w; i++)
height[i] = inf, mark[i] = false;
queue<int> q;
q.push(st);
mark[st] = true;
height[st] = 0;
while(q.size()) {
int x = q.front();
q.pop();
for(auto i : adj[x])
if(!mark[i]) {
mark[i] = true;
q.push(i);
height[i] = height[x] + 1;
}
}
for(int i = 1; i <= h * w; i++)
heightst[i][d] = height[i];
}
int get(int x, int y) {
return (x - 1) * w + y;
}
void find(int x, int y) {
int rast = *sat[x].upper_bound(y) - 1;
int chap = *(--sat[x].upper_bound(y)) + 1;
int paiin = *(sot[y].upper_bound(x)) - 1;
int bala = *(--sot[y].upper_bound(x)) + 1;
if(rast != y) {
adj[get(x, y)].pb(get(x, rast));
}
if(chap != y) {
adj[get(x, y)].pb(get(x, chap));
}
if(paiin != x) {
adj[get(x, y)].pb(get(paiin, y));
}
if(bala != x) {
adj[get(x, y)].pb(get(bala, y));
}
}
int main() {
faster; //free();
cin >> n >> w >> h;
for(int i = 1; i <= h; i++)
sat[i].insert(0), sat[i].insert(w + 1);
for(int i = 1; i <= w; i++)
sot[i].insert(0), sot[i].insert(h + 1);
for(int i = 1; i <= h; i++){
for(int j = 1; j <= w; j++) {
cin >> ch[i][j];
if(ch[i][j] == 'x') {
sat[i].insert(j);
sot[j].insert(i);
}
}
}
int st, ft;
for(int i = 1; i <= h; i++) {
for(int j = 1; j <= w; j++) {
if(ch[i][j] != 'x')
find(i, j);
if(ch[i][j] == '1')
st = get(i, j);
if(ch[i][j] == '2')
ft = get(i, j);
}
}
// for(int i = 1; i <= h; i++) {
// for(int j = 1; j <= w; j++) {
// cout << "!! " << i << " " << j << '\n';
// for(auto k : adj[get(i, j)])
// cout << k << " ";
// cout << '\n';
// }
// }
// for(auto i : adj[st])
// cout << i << " ";
// cout << '\n';
// for(auto i : adj[ft])
// cout << i << " ";
// cout << '\n';
bfs(st,ft, 0);
bfs(ft, st, 1);
int res = inf;
for(int i = 1; i <= h; i++) {
for(int j = 1; j <= w; j++) {
if(ch[i][j] != 'x') {
res = min(res, heightst[get(i, j)][0] + heightst[get(i, j)][1]);
}
}
}
if(res == inf)
cout << -1;
else
cout << res;
}
Compilation message
robots.cpp: In function 'void free()':
robots.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp: In function 'int main()':
robots.cpp:127:8: warning: 'ft' may be used uninitialized in this function [-Wmaybe-uninitialized]
127 | bfs(ft, st, 1);
| ~~~^~~~~~~~~~~
robots.cpp:127:8: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |