QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#212425 | #1970. Social Distancing | rfpermen# | AC ✓ | 10ms | 5760kb | C++14 | 2.1kb | 2023-10-13 15:47:33 | 2023-10-13 15:47:33 |
Judging History
answer
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
using namespace std;
#define ll long long
#define rep(i,n,N) for(int i = n; i<=N; ++i)
#define rap(i,n,N) for(int i = n; i>=N; --i)
#define For(i,n,N) for(int i = n; i< N; ++i)
#define rip(i,n,N,V) for(int i = n; i<=N; i+=V)
#define debug(x) cout << ">> " << #x <<" = " << x << endl
#define out(x) cout << "<> " << x << " " << y << endl
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define vi vector<int>
#define pii pair<int,int>
#define mems(x,y) memset(x,y,sizeof x)
#define all(x) x.begin(),x.end()
#define endl '\n'
const int MAX = 500 + 5;
const int dr[] = {0,0,1,-1,1,1,-1,-1};
const int dc[] = {1,-1,0,0,1,-1,1,-1};
int n,m,sr,sc,er,ec,r,c,nr,nc,vis[MAX][MAX],ans;
bool st[MAX][MAX];
char x[MAX][MAX];
queue<pii> q;
deque<pii> dq;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>m;
mems(x, '#');
mems(vis, -1);
rep(i,1,n)rep(j,1,m){
cin>>x[i][j];
if(x[i][j]=='E'){
er = i, ec = j;
}
else if(x[i][j]=='S'){
sr = i, sc = j;
}
else if(x[i][j]=='*'){
vis[i][j] = 0;
q.push({i,j});
}
}
if(q.empty()){
rep(i,1,n)rep(j,1,m)vis[i][j] = 1e9;
}
while(q.size()){
r = q.front().fi;
c = q.front().se;
q.pop();
rep(i,0,7){
nr = r+dr[i];
nc = c+dc[i];
if((nr>=1 && nc>=1 && nr<=n && nc<=m) && vis[nr][nc]==-1){
vis[nr][nc] = vis[r][c]+1;
q.push({nr, nc});
}
}
}
// rep(i,1,n){
// rep(j,1,m)cout<<vis[i][j]<<" ";
// cout<<endl;
// }
ans = vis[sr][sc];
dq.pf({sr, sc});
while(dq.size()){
r = dq.front().fi;
c = dq.front().se;
dq.pof();
if(st[r][c])continue;
st[r][c] = 1;
ans = min(ans, vis[r][c]);
if(r==er && c==ec){
if(ans > n*m)cout<<"safe\n";
else cout<<ans<<endl;
return 0;
}
rep(i,0,3){
nr = r+dr[i];
nc = c+dc[i];
if(x[nr][nc]=='#' || x[nr][nc]=='*' || st[nr][nc])continue;
if(vis[nr][nc]>=ans){
dq.pf({nr, nc});
}
else {
dq.pb({nr, nc});
}
}
}
cout<<"-1\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4680kb
input:
4 5 .*#.. .*..E ..##. S....
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 1ms
memory: 4664kb
input:
6 8 .......E ........ ........ .....**. ........ S.......
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 1ms
memory: 4740kb
input:
3 3 #E# ### #S#
output:
-1
result:
ok single line: '-1'
Test #4:
score: 0
Accepted
time: 0ms
memory: 4728kb
input:
3 3 .S. *** .E.
output:
-1
result:
ok single line: '-1'
Test #5:
score: 0
Accepted
time: 1ms
memory: 4696kb
input:
3 3 S.. ... ..E
output:
safe
result:
ok single line: 'safe'
Test #6:
score: 0
Accepted
time: 0ms
memory: 4796kb
input:
2 2 S. .E
output:
safe
result:
ok single line: 'safe'
Test #7:
score: 0
Accepted
time: 1ms
memory: 4684kb
input:
50 50 .............##.........*....#...##....#.......... .##..........#...#............##.....#....#.....#. .........##...#....#.......#......#............... #..##..#.........*...#..........*..............#.. ....................*..#.....#.......#........#... ...#...............#................##....
output:
3
result:
ok single line: '3'
Test #8:
score: 0
Accepted
time: 8ms
memory: 5032kb
input:
495 496 S......................................................................................................................................................................................................................................................................................................
output:
13
result:
ok single line: '13'
Test #9:
score: 0
Accepted
time: 9ms
memory: 5760kb
input:
500 500 #.*#.##.**.#...#.*.*#.#.*.*#.#.*.##*.#.*..#..*...*##.#.##.#...**#*#.#*...#.#.......#*#...#...*#.#.*#.##..##...*.#.##.*#*..#...*##..##**...*#..##...##...#....*#..##*##....#.#**.#...##..#.##..#*.*#.*.....*#...##...###.##..#**....#####..*..#...#...*.....##*#*##.#..#*.*.*.*.#..*.#*#*#**.......#....
output:
-1
result:
ok single line: '-1'
Test #10:
score: 0
Accepted
time: 9ms
memory: 4880kb
input:
500 500 ....................................................................................................................................................................................S..................................................................................................................
output:
-1
result:
ok single line: '-1'
Test #11:
score: 0
Accepted
time: 8ms
memory: 5100kb
input:
500 500 *......................................................................................................................................................................................................................................................................................................
output:
8
result:
ok single line: '8'
Test #12:
score: 0
Accepted
time: 5ms
memory: 4944kb
input:
500 500 S###################################################################################################################################################################################################################################################################################################...
output:
safe
result:
ok single line: 'safe'
Test #13:
score: 0
Accepted
time: 10ms
memory: 5112kb
input:
499 499 .#.....#...#.....#.............##..............#....#.#...#..#......#......#.........................#.#.....#.....#....#..#.#........#....*...#......#..........................#...#......#..............#...*........#.......#...............................................#......................
output:
5
result:
ok single line: '5'
Test #14:
score: 0
Accepted
time: 8ms
memory: 5016kb
input:
500 500 S......................................................................................................................................................................................................................................................................................................
output:
38
result:
ok single line: '38'
Test #15:
score: 0
Accepted
time: 8ms
memory: 5056kb
input:
498 493 *......................................................................................................................................................................................................................................................................................................
output:
5
result:
ok single line: '5'