#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <chrono>
#include <string>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <climits>
#include <bitset>
#define all(x) (x).begin(), (x).end()
#define vec(n) vll arr(n);
#define printarr(arr) for(auto i:arr)cout<<i<<" "; cout<<endl;
#define printdict(dict) for(auto i:dict)cout<<i.first<<": "<<i.second<<endl;
#define printadj(adj) for(ll i=0;i<n;i++){if(!adj[i].empty()){cout<<i<<": ";printarr(adj[i])}}
#define read(arr); for(ll i=0;i<arr.size();i++) cin>>arr[i];
#define readundirected(m) for(ll i=0;i<m;i++){ll a,b; cin>>a>>b; a--;b--; adj[a].pb(b);adj[b].pb(a);}
#define readdirected(m) for(ll i=0;i<m;i++){ll a,b; cin>>a>>b; a--;b--; adj[a].pb(b);}
#define readfunc(n) for(ll i=0;i<n;i++){ll a;cin>>a;a--;func_adj[i]=a;}
#define grid(n,m) for (ll i=1;i<=n;i++){for (ll j=1;j<=m;j++) cin>>graph[i][j];}
#define vll vector<ll>
#define sll set<ll>
#define msll multiset<ll>
#define qll queue<ll>
#define pll pair<ll,ll>
#define str string
#define pb push_back
#define ll long long
#define ld long double
using namespace std;
const str alph="abcdefghijklmnopqrstuvwxyz";
const str capalph="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll inf=2e5+1;
const ll graph_size=1000;
const ll mod=1e9+7;
const ll large=1e18;
const ll small=-1e18;
// Fast Input/Output
void fastio(){
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
}
// File Input/Output
str fileio(const string&filePath=__FILE__){
size_t lastSlash=filePath.find_last_of('/');
size_t lastDot=filePath.rfind('.');
return filePath.substr(lastSlash+1,lastDot-lastSlash-1);
}
// For Yes Or No Problems
str yes_or_no(bool test){
if (test){
return "YES";
}
return "NO";
}
ll n,m,q,s;
// Floodfill
char graph[graph_size+2][graph_size+2];
vector<pll> directions={{0,-1},{-1,0},{1,0},{0,1}};
bool floodfill_visited[graph_size+2][graph_size+2];
ll floodfill_bee_level[graph_size+2][graph_size+2];
vector<pll> hives;
void floodfill_bee_bfs(){
for (ll i=0;i<=n+1;i++){
for (ll j=0;j<=m+1;j++){
floodfill_bee_level[i][j]=large;
floodfill_visited[i][j]=0;
}
}
queue<pll> q;
for (auto i:hives){
q.push(i);
floodfill_bee_level[i.first][i.second]=0;
}
while (!q.empty()){
pll curr=q.front();
q.pop();
ll x=curr.first;
ll y=curr.second;
floodfill_visited[x][y]=1;
for (auto i:directions){
if (graph[x+i.first][y+i.second]!='T' and x+i.first>=1 and x+i.first<=n and y+i.second>=1 and y+i.second<=m and floodfill_bee_level[x+i.first][y+i.second]==large){
floodfill_bee_level[x+i.first][y+i.second]=floodfill_bee_level[x][y]+1;
q.push({x+i.first,y+i.second});
}
}
}
}
bool graph1[graph_size+2][graph_size+2];
ll floodfill_ans_level[graph_size+2][graph_size+2];
void floodfill_ans_bfs(pll coord,ll t){
for (ll i=0;i<=n+1;i++){
for (ll j=0;j<=m+1;j++){
floodfill_ans_level[i][j]=large;
floodfill_visited[i][j]=0;
}
}
ll x=coord.first;
ll y=coord.second;
queue<pll> q;
q.push({x,y});
floodfill_ans_level[x][y]=0;
while (!q.empty()){
pll curr=q.front();
q.pop();
x=curr.first;
y=curr.second;
floodfill_visited[x][y]=1;
for (auto i:directions){
if (graph[x+i.first][y+i.second]!='T' and (floodfill_ans_level[x][y]+1)/s+t<floodfill_bee_level[x+i.first][y+i.second] and x+i.first>=1 and x+i.first<=n and y+i.second>=1 and y+i.second<=m and floodfill_ans_level[x+i.first][y+i.second]==large){
floodfill_ans_level[x+i.first][y+i.second]=floodfill_ans_level[x][y]+1;
q.push({x+i.first,y+i.second});
}
}
}
}
// Binary Search
pll home,bear;
bool check(ll t){
if (t>=floodfill_bee_level[bear.first][bear.second]){
return 0;
}
floodfill_ans_bfs(bear,t);
bool test=0;
for (auto i:directions){
test=max(test,floodfill_visited[home.first+i.first][home.second+i.second]);
}
return test;
}
// Returns the first value in the range such that check(value)=True.
ll first_true(ll low,ll high){
while (low<high){
ll mid=(low+high)/2;
if (check(mid)){
high=mid;
}
else{
low=mid+1;
}
}
return low;
}
// Returns the last value in the range such that check(value)=True.
ll last_true(ll low,ll high){
while (low<high){
ll mid=(low+high+1)/2;
if (check(mid)){
low=mid;
}
else{
high=mid-1;
}
}
return low;
}
int main(){
// auto start_time=chrono::steady_clock::now();
fastio();
// str filename=fileio();
// ifstream cin(filename+".in");
// ofstream cout(filename+".out");
ll t=1;
// cin>>t;
while (t--){
cin>>n>>s;
m=n;
grid(n,m);
for (ll i=1;i<=n;i++){
for (ll j=1;j<=m;j++){
if (graph[i][j]=='H'){
hives.pb({i,j});
}
if (graph[i][j]=='M'){
bear={i,j};
}
if (graph[i][j]=='D'){
home={i,j};
}
}
}
floodfill_bee_bfs();
ll ans=last_true(0,large);
if (!check(0)){
ans--;
}
cout<<ans<<"\n";
}
// auto end_time=chrono::steady_clock::now();
// auto elapsed_time=chrono::duration_cast<chrono::milliseconds>(end_time-start_time);
// cout<<"Elapsed time: "<<elapsed_time.count()<<" milliseconds\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
87 ms |
17268 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
2 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
5 ms |
10844 KB |
Output is correct |
34 |
Correct |
5 ms |
10844 KB |
Output is correct |
35 |
Correct |
19 ms |
10844 KB |
Output is correct |
36 |
Correct |
7 ms |
10716 KB |
Output is correct |
37 |
Correct |
7 ms |
10844 KB |
Output is correct |
38 |
Correct |
25 ms |
10844 KB |
Output is correct |
39 |
Correct |
10 ms |
10844 KB |
Output is correct |
40 |
Correct |
10 ms |
10844 KB |
Output is correct |
41 |
Correct |
32 ms |
10908 KB |
Output is correct |
42 |
Correct |
9 ms |
12892 KB |
Output is correct |
43 |
Correct |
12 ms |
12948 KB |
Output is correct |
44 |
Correct |
42 ms |
12892 KB |
Output is correct |
45 |
Correct |
12 ms |
14952 KB |
Output is correct |
46 |
Correct |
12 ms |
15012 KB |
Output is correct |
47 |
Correct |
48 ms |
14952 KB |
Output is correct |
48 |
Correct |
14 ms |
14936 KB |
Output is correct |
49 |
Correct |
15 ms |
14940 KB |
Output is correct |
50 |
Correct |
59 ms |
14940 KB |
Output is correct |
51 |
Correct |
15 ms |
14940 KB |
Output is correct |
52 |
Correct |
18 ms |
14940 KB |
Output is correct |
53 |
Correct |
81 ms |
14800 KB |
Output is correct |
54 |
Correct |
17 ms |
15192 KB |
Output is correct |
55 |
Correct |
16 ms |
14816 KB |
Output is correct |
56 |
Correct |
81 ms |
14996 KB |
Output is correct |
57 |
Correct |
20 ms |
17052 KB |
Output is correct |
58 |
Correct |
20 ms |
16988 KB |
Output is correct |
59 |
Correct |
94 ms |
16988 KB |
Output is correct |
60 |
Correct |
24 ms |
16988 KB |
Output is correct |
61 |
Correct |
23 ms |
16988 KB |
Output is correct |
62 |
Correct |
102 ms |
16988 KB |
Output is correct |
63 |
Correct |
85 ms |
16884 KB |
Output is correct |
64 |
Correct |
158 ms |
17060 KB |
Output is correct |
65 |
Correct |
160 ms |
17056 KB |
Output is correct |
66 |
Correct |
103 ms |
16984 KB |
Output is correct |
67 |
Correct |
86 ms |
17040 KB |
Output is correct |
68 |
Correct |
39 ms |
16976 KB |
Output is correct |
69 |
Correct |
45 ms |
16988 KB |
Output is correct |
70 |
Correct |
31 ms |
16976 KB |
Output is correct |
71 |
Correct |
33 ms |
16852 KB |
Output is correct |
72 |
Correct |
35 ms |
16912 KB |
Output is correct |
73 |
Correct |
38 ms |
17472 KB |
Output is correct |
74 |
Correct |
89 ms |
17588 KB |
Output is correct |
75 |
Correct |
84 ms |
17640 KB |
Output is correct |
76 |
Correct |
63 ms |
17648 KB |
Output is correct |
77 |
Correct |
74 ms |
17376 KB |
Output is correct |
78 |
Correct |
91 ms |
17488 KB |
Output is correct |
79 |
Correct |
86 ms |
17460 KB |
Output is correct |
80 |
Correct |
61 ms |
17492 KB |
Output is correct |
81 |
Correct |
92 ms |
17580 KB |
Output is correct |
82 |
Correct |
65 ms |
17500 KB |
Output is correct |
83 |
Correct |
75 ms |
17484 KB |
Output is correct |
84 |
Correct |
104 ms |
17236 KB |
Output is correct |
85 |
Correct |
84 ms |
17500 KB |
Output is correct |
86 |
Correct |
81 ms |
17488 KB |
Output is correct |
87 |
Correct |
71 ms |
17488 KB |
Output is correct |
88 |
Correct |
125 ms |
17236 KB |
Output is correct |
89 |
Correct |
126 ms |
17328 KB |
Output is correct |
90 |
Correct |
130 ms |
17376 KB |
Output is correct |
91 |
Correct |
85 ms |
17244 KB |
Output is correct |
92 |
Correct |
124 ms |
17232 KB |
Output is correct |