Submission #1154938

#TimeUsernameProblemLanguageResultExecution timeMemory
1154938LOLOLOTracks in the Snow (BOI13_tracks)C++20
0 / 100
2 ms328 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)

tracks.cpp: In function 'int main()':
tracks.cpp:84:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |     freopen("TRACKS.INP", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:85:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |     freopen("TRACKS.OUT", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...