QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#104305#1970. Social Distancingzoker#AC ✓46ms6860kbC++173.7kb2023-05-10 00:38:262023-05-10 00:38:27

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-10 00:38:27]
  • Judged
  • Verdict: AC
  • Time: 46ms
  • Memory: 6860kb
  • [2023-05-10 00:38:26]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt")

using ll = long long int;
using ull = unsigned long long int;
using vi = vector<ll>;
using ii = pair<ll,ll>;
using vii = vector<ii>;
using ld = long double;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using ordered_set = tree < T, null_type, less<T>,
rb_tree_tag,
tree_order_statistics_node_update >;

#ifdef SA_DEBUG
template <class T>
void print(T a) {cerr << a << endl;}
template <class T, class... V> 
void print(T a, V... b) {cerr << a << ", "; print(b...);}
#define dbg(...) cerr << "[" << __LINE__ << "] " << #__VA_ARGS__ << " :: ", print(__VA_ARGS__)
#else
#define dbg(...) 7
#endif

#define eb emplace_back
#define fi first
#define se second

const ll INFL = 2e18;
const int INF = 1e9;
const double PI = acos(-1);
const ll mod = 1e9+7;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

template<class T, class V> 
ostream& operator << (ostream &s, pair<T, V> a){
	s << a.fi << ' ' << a.se; return s;
}

template<class T, class V> 
istream& operator >> (istream &s, pair<T, V> &a){
	s >> a.fi >> a.se; return s;
}

template<class T> 
ostream& operator << (ostream &s, vector<T> a){
	for(int i = 0; i < (int)a.size(); i++){
		if(i > 0)s << ' ' ; 
		s << a[i];
	} return s;
}

template<class T> 
istream& operator >> (istream &s, vector<T> &a){
	for(T &x : a)s >> x; 
	return s;
}

template<class T> 
void input(T a[], int l, int r, istream &s = cin){
	while(l <= r)s >> a[l++];
}

template<class T> 
void output(T a[], int l, int r, bool en = true, ostream &s = cout){
	while(l <= r){ s << a[l++];
		if(l <= r) s << ' ';
	} if(en)s << "\n";
}



const int N = 5e2+3, K = 26;
//====================================================================//
int g[N][N];
int val[N][N];
int ans[N][N];
string grid[N];
void testcase(){
	int n, m;
	cin >> n >> m;
	ii st, en;
	for(int i = 1; i <= n; i++){
		string s;
		cin >> s;
		
		for(int j = 1; j <= m; j++){
			g[i][j] = (s[j - 1] == '*');
			if(s[j - 1] == 'S')st = {i, j};
			if(s[j - 1] == 'E')en = {i, j};
			g[i][j] += g[i - 1][j];
			g[i][j] += g[i][j - 1];
			g[i][j] -= g[i - 1][j - 1];
		}
		grid[i] = "." + s;
	}
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			int lo = 0, hi = max(n, m);
			while(lo < hi){
				int mid = lo + hi >> 1;
				int Lr = max(1, i - mid);
				int Rr = min(n, i + mid);
				int Lc = max(1, j - mid);
				int Rc = min(m, j + mid);
				int cnt = g[Rr][Rc] - g[Rr][Lc - 1] - g[Lr - 1][Rc] + g[Lr - 1][Lc - 1];
				if(cnt)hi = mid;
				else lo = mid + 1;
			} 
			val[i][j] = lo;
		}
	}
	
	
	
	priority_queue<tuple<int, int, int>> pq;
	ans[st.fi][st.se] = val[st.fi][st.se];
	
	pq.emplace(val[st.fi][st.se], st.fi, st.se);
	int xi[] = {1, -1, 0, 0};
	int yi[] = {0, 0, -1, 1};
	while(!pq.empty()){
		auto [v, r, c] = pq.top();
		pq.pop();
		if(ans[r][c] > v)continue;
		for(int i = 0; i < 4; i++){
			int a = r + xi[i];
			int b = c + yi[i];
			if(a > 0 && b > 0 && a <= n && b <= m && grid[a][b] != '#' && ans[a][b] < min(v, val[a][b])){
				ans[a][b] = min(v, val[a][b]);
				pq.emplace(ans[a][b], a, b);
			}
		}
	}
	if(ans[en.fi][en.se] == 0)ans[en.fi][en.se] = -1;
	else if(g[n][m] == 0){
		cout << "safe\n";
		return;
	}
	cout << ans[en.fi][en.se] << "\n";
	
	
}





int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	
	int T = 1;
	//cin >> T;
	
	for(int qq = 1; qq <= T; ++qq){
		//cout << "Case #" << qq << ": ";
		testcase();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3444kb

input:

4 5
.*#..
.*..E
..##.
S....

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3396kb

input:

6 8
.......E
........
........
.....**.
........
S.......

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3368kb

input:

3 3
#E#
###
#S#

output:

-1

result:

ok single line: '-1'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3448kb

input:

3 3
.S.
***
.E.

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3396kb

input:

3 3
S..
...
..E

output:

safe

result:

ok single line: 'safe'

Test #6:

score: 0
Accepted
time: 2ms
memory: 5428kb

input:

2 2
S.
.E

output:

safe

result:

ok single line: 'safe'

Test #7:

score: 0
Accepted
time: 2ms
memory: 3736kb

input:

50 50
.............##.........*....#...##....#..........
.##..........#...#............##.....#....#.....#.
.........##...#....#.......#......#...............
#..##..#.........*...#..........*..............#..
....................*..#.....#.......#........#...
...#...............#................##....

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 24ms
memory: 6672kb

input:

495 496
S......................................................................................................................................................................................................................................................................................................

output:

13

result:

ok single line: '13'

Test #9:

score: 0
Accepted
time: 6ms
memory: 5724kb

input:

500 500
#.*#.##.**.#...#.*.*#.#.*.*#.#.*.##*.#.*..#..*...*##.#.##.#...**#*#.#*...#.#.......#*#...#...*#.#.*#.##..##...*.#.##.*#*..#...*##..##**...*#..##...##...#....*#..##*##....#.#**.#...##..#.##..#*.*#.*.....*#...##...###.##..#**....#####..*..#...#...*.....##*#*##.#..#*.*.*.*.#..*.#*#*#**.......#....

output:

-1

result:

ok single line: '-1'

Test #10:

score: 0
Accepted
time: 19ms
memory: 6116kb

input:

500 500
....................................................................................................................................................................................S..................................................................................................................

output:

-1

result:

ok single line: '-1'

Test #11:

score: 0
Accepted
time: 21ms
memory: 6624kb

input:

500 500
*......................................................................................................................................................................................................................................................................................................

output:

8

result:

ok single line: '8'

Test #12:

score: 0
Accepted
time: 7ms
memory: 6632kb

input:

500 500
S###################################################################################################################################################################################################################################################################################################...

output:

safe

result:

ok single line: 'safe'

Test #13:

score: 0
Accepted
time: 46ms
memory: 6860kb

input:

499 499
.#.....#...#.....#.............##..............#....#.#...#..#......#......#.........................#.#.....#.....#....#..#.#........#....*...#......#..........................#...#......#..............#...*........#.......#...............................................#......................

output:

5

result:

ok single line: '5'

Test #14:

score: 0
Accepted
time: 21ms
memory: 6676kb

input:

500 500
S......................................................................................................................................................................................................................................................................................................

output:

38

result:

ok single line: '38'

Test #15:

score: 0
Accepted
time: 29ms
memory: 6716kb

input:

498 493
*......................................................................................................................................................................................................................................................................................................

output:

5

result:

ok single line: '5'