#include <fstream>
#include <iostream>
#include <vector>
#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 qll queue<ll>
#define pll pair<ll,ll>
#define str string
#define pb push_back
#define ll long long
using namespace std;
const ll inf=2*1e5+1;
const ll graph_size=4000;
// 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);
}
ll n,m;
// Floodfill
char graph[graph_size+2][graph_size+2];
vector<pll> directions={{0,-1},{-1,0},{1,0},{0,1}};
vector<pll> path;
vector<pll> path1;
ll curr=0;
void floodfill_dfs(pll coord,char color){
ll x=coord.first;
ll y=coord.second;
if (graph[x][y]=='.' or graph[x][y]!=color){
return;
}
curr++;
graph[x][y]='.';
path1.pb({x,y});
for (auto i:directions){
floodfill_dfs({x+i.first,y+i.second},color);
}
}
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>>m;
grid(n,m);
ll tracks=0;
for (ll i=1;i<=n;i++){
for (ll j=1;j<=m;j++){
if (graph[i][j]!='.'){
tracks++;
}
}
}
for (ll i=0;i<=n;i++){
graph[i][0]='.';
graph[i][m+1]='.';
}
for (ll i=0;i<=m;i++){
graph[0][i]='.';
graph[n+1][i]='.';
}
char animal=graph[1][1];
ll ans=0;
path.pb({n,m});
while (curr<tracks){
ans++;
for (auto i:path){
for (auto j:directions){
floodfill_dfs({i.first+j.first,i.second+j.second},animal);
}
}
path=path1;
path1.clear();
if (animal=='F'){
animal='R';
}
else{
animal='F';
}
}
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 |
18 ms |
3780 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
14 ms |
7180 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
3 ms |
3932 KB |
Output is correct |
12 |
Correct |
7 ms |
3164 KB |
Output is correct |
13 |
Correct |
4 ms |
2664 KB |
Output is correct |
14 |
Correct |
4 ms |
2664 KB |
Output is correct |
15 |
Correct |
12 ms |
3176 KB |
Output is correct |
16 |
Correct |
15 ms |
3788 KB |
Output is correct |
17 |
Correct |
9 ms |
2944 KB |
Output is correct |
18 |
Correct |
10 ms |
7132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15708 KB |
Output is correct |
2 |
Correct |
45 ms |
8704 KB |
Output is correct |
3 |
Correct |
319 ms |
32752 KB |
Output is correct |
4 |
Correct |
72 ms |
12884 KB |
Output is correct |
5 |
Correct |
217 ms |
22416 KB |
Output is correct |
6 |
Correct |
960 ms |
391172 KB |
Output is correct |
7 |
Correct |
4 ms |
16472 KB |
Output is correct |
8 |
Correct |
5 ms |
15708 KB |
Output is correct |
9 |
Correct |
3 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
3 ms |
16220 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
62 ms |
8540 KB |
Output is correct |
14 |
Correct |
27 ms |
5724 KB |
Output is correct |
15 |
Correct |
20 ms |
5868 KB |
Output is correct |
16 |
Correct |
24 ms |
3420 KB |
Output is correct |
17 |
Correct |
114 ms |
13176 KB |
Output is correct |
18 |
Correct |
80 ms |
13232 KB |
Output is correct |
19 |
Correct |
78 ms |
12904 KB |
Output is correct |
20 |
Correct |
79 ms |
12532 KB |
Output is correct |
21 |
Correct |
205 ms |
22864 KB |
Output is correct |
22 |
Correct |
219 ms |
22376 KB |
Output is correct |
23 |
Correct |
255 ms |
19352 KB |
Output is correct |
24 |
Correct |
288 ms |
22500 KB |
Output is correct |
25 |
Correct |
336 ms |
32368 KB |
Output is correct |
26 |
Runtime error |
922 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Runtime error |
1069 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Correct |
987 ms |
390596 KB |
Output is correct |
29 |
Correct |
936 ms |
352444 KB |
Output is correct |
30 |
Correct |
1078 ms |
641536 KB |
Output is correct |
31 |
Correct |
683 ms |
30308 KB |
Output is correct |
32 |
Correct |
1105 ms |
920712 KB |
Output is correct |