# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1154938 | LOLOLO | Tracks in the Snow (BOI13_tracks) | C++20 | 2 ms | 328 KiB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define pb push_back
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
const int N = 4000;
char a[N + 3][N + 3];
int dp[N + 3][N + 3];
int n, m;
bool da[N + 3][N + 3];
ll ans = 0;
bool ingrid (int x, int y){
if (x>n || x<1 || y>m || y<1)
return false;
if (a[x][y] == '.')
return false;
return true;
}
void bfs01 (int x, int y){
bool k=true;
deque <pair<int, int>> dq;
dq.push_back({x, y});
char type = a[x][y];
if (a[x][y]=='F')
a[x][y]='R';
else
a[x][y]='F';
da[x][y]=1;
while (!dq.empty()){
int u = dq.front().f;
int v = dq.front().s;
dq.pop_front();
for (int z=0; z<=3; z++){
int nu = u + dx[z];
int nv = v + dy[z];
if (ingrid(nu, nv)){
if (a[nu][nv]==type){
dq.push_back({nu, nv});
a[nu][nv]=a[x][y];
da[nu][nv]=1;
if (nu==1 && nv==1){
ans++;
if (k)
return;
while (!dq.empty())
dq.pop_front();
k=true;
type = a[x][y];
if (a[x][y]=='F')
a[x][y]='R';
else
a[x][y]='F';
dq.push_back({x, y});
memset(da, 0, sizeof(da));
da[x][y]=1;
break;
}
}
else{
if (da[nu][nv]==0)
k=0;
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
freopen("TRACKS.INP", "r", stdin);
freopen("TRACKS.OUT", "w", stdout);
cin >> n >> m;
int x, y;
for (int i=1; i<=n; i++){
for (int j=1; j<=m; j++){
cin >> a[i][j];
}
}
bfs01(n, m);
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |