QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#223717 | #1970. Social Distancing | Rabeya# | AC ✓ | 216ms | 18172kb | C++14 | 3.6kb | 2023-10-22 15:51:59 | 2023-10-22 15:52:00 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef unordered_map<int,int> umap;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define popcount __builtin_popcount
#define case cout<<"Case "<<__testcase-testcase<<": ";
vector<vii> d;
vector<vii> yes;
void dfs(int i,int j,int n,int m,int c){
if(i<0 || i>=n || j<0 || j>=m) return;
if(yes[i][j]>=0) return;
if(d[i][j]>=c) yes[i][j]=1;
else{
yes[i][j]=0;
return;
}
dfs(i-1,j,n,m,c);
dfs(i,j-1,n,m,c);
dfs(i+1,j,n,m,c);
dfs(i,j+1,n,m,c);
return;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int testcase=1;
//cin>>testcase;
int __testcase=testcase;
while(testcase--){
int n,m,i,j,k;
cin>>n>>m;
char s[n][m+10];
for(i=0;i<n;i++) cin>>s[i];
d.assign(n,vector<int>(m,1000));
vii x(n,1000), y(m,1000);
vector<vii> v;
int cnt=0, cntx=0;
int row[n][m];
for(int i=0;i<n;i++) for(int j=0;j<m;j++) row[i][j]=1000;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(s[i][j]=='*'){
d[i][j]=0;
x[i]=0;
y[j]=0;
cnt++;
row[i][j]=0;
}
else if(s[i][j]=='#') d[i][j]=0, cntx++;
else d[i][j]=1000;
if(s[i][j]=='S' || s[i][j]=='E') v.pb({i,j});
}
}
for(int i=0;i<n;i++){
for(int j=1;j<m;j++) row[i][j]=min(row[i][j-1]+1,row[i][j]);
for(int j=m-2;j>=0;j--) row[i][j]=min(row[i][j+1]+1,row[i][j]);
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
for(int k=0;k<n;k++){
d[i][j]=min(d[i][j],max(row[k][j],abs(k-i)));
}
}
}
// for(int i=0;i<n;i++) cout<<x[i]<<' ';
// cout<<endl;
// for(int j=0;j<m;j++) cout<<y[j]<<' ';
// cout<<endl;
//
// for(i=1;i<n;i++) x[i]=min(x[i], x[i-1]+1);
// for(i=n-2;i>=0;i--) x[i]=min(x[i], x[i+1]+1);
// for(j=1;j<m;j++) y[j]=min(y[j], y[j-1]+1);
// for(j=m-2;j>=0;j--) y[j]=min(y[j], y[j+1]+1);
//
//
//
//
//
//
//
//
//
// for(i=0;i<n;i++){
// for(j=0;j<m;j++){
// if(s[i][j]!='#') d[i][j]=max(x[i],y[j]);
// }
// }
//
// for(int i=0;i<n;i++){
// for(int j=0;j<m;j++) cout<<d[i][j]<<' ';
// cout<<endl;
// }
yes.resize(n,vii(m));
if(cnt==0){
for(i=0;i<n;i++) for(j=0;j<m;j++) yes[i][j]=-1;
dfs(v[0][0], v[0][1], n,m, 1);
if(yes[v[1][0]][v[1][1]] == -1) cout<<-1<<endl;
else
cout<<"safe"<<endl;
continue;
}
int l=1, r=500;
r=min(d[v[0][0]][v[0][1]], d[v[1][0]][v[1][1]]);
//cout<<d[v[0][0]][v[0][1]]<<" "<<d[v[1][0]][v[1][1]]<<endl;
int ans=1000;
while(l<=r){
int t=l+(r-l)/2;
for(i=0;i<n;i++) for(j=0;j<m;j++) yes[i][j]=-1;
dfs(v[0][0], v[0][1], n,m, t);
if(yes[v[1][0]][v[1][1]]==1) ans=t, l=t+1;
else r=t-1;
//cout<<l<<" "<<r<<endl;
}
if(ans==1000) cout<<-1<<endl;
else cout<<ans<<endl;
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
input:
4 5 .*#.. .*..E ..##. S....
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
6 8 .......E ........ ........ .....**. ........ S.......
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3672kb
input:
3 3 #E# ### #S#
output:
-1
result:
ok single line: '-1'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3892kb
input:
3 3 .S. *** .E.
output:
-1
result:
ok single line: '-1'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
3 3 S.. ... ..E
output:
safe
result:
ok single line: 'safe'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
2 2 S. .E
output:
safe
result:
ok single line: 'safe'
Test #7:
score: 0
Accepted
time: 1ms
memory: 3772kb
input:
50 50 .............##.........*....#...##....#.......... .##..........#...#............##.....#....#.....#. .........##...#....#.......#......#............... #..##..#.........*...#..........*..............#.. ....................*..#.....#.......#........#... ...#...............#................##....
output:
3
result:
ok single line: '3'
Test #8:
score: 0
Accepted
time: 158ms
memory: 9848kb
input:
495 496 S......................................................................................................................................................................................................................................................................................................
output:
13
result:
ok single line: '13'
Test #9:
score: 0
Accepted
time: 163ms
memory: 6324kb
input:
500 500 #.*#.##.**.#...#.*.*#.#.*.*#.#.*.##*.#.*..#..*...*##.#.##.#...**#*#.#*...#.#.......#*#...#...*#.#.*#.##..##...*.#.##.*#*..#...*##..##**...*#..##...##...#....*#..##*##....#.#**.#...##..#.##..#*.*#.*.....*#...##...###.##..#**....#####..*..#...#...*.....##*#*##.#..#*.*.*.*.#..*.#*#*#**.......#....
output:
-1
result:
ok single line: '-1'
Test #10:
score: 0
Accepted
time: 177ms
memory: 18172kb
input:
500 500 ....................................................................................................................................................................................S..................................................................................................................
output:
-1
result:
ok single line: '-1'
Test #11:
score: 0
Accepted
time: 169ms
memory: 6600kb
input:
500 500 *......................................................................................................................................................................................................................................................................................................
output:
8
result:
ok single line: '8'
Test #12:
score: 0
Accepted
time: 150ms
memory: 15304kb
input:
500 500 S###################################################################################################################################################################################################################################################################################################...
output:
safe
result:
ok single line: 'safe'
Test #13:
score: 0
Accepted
time: 216ms
memory: 16020kb
input:
499 499 .#.....#...#.....#.............##..............#....#.#...#..#......#......#.........................#.#.....#.....#....#..#.#........#....*...#......#..........................#...#......#..............#...*........#.......#...............................................#......................
output:
5
result:
ok single line: '5'
Test #14:
score: 0
Accepted
time: 151ms
memory: 8156kb
input:
500 500 S......................................................................................................................................................................................................................................................................................................
output:
38
result:
ok single line: '38'
Test #15:
score: 0
Accepted
time: 166ms
memory: 6508kb
input:
498 493 *......................................................................................................................................................................................................................................................................................................
output:
5
result:
ok single line: '5'