Submission #441338

#TimeUsernameProblemLanguageResultExecution timeMemory
441338WindChaserTracks in the Snow (BOI13_tracks)C++11
100 / 100
602 ms118736 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define pb push_back

void setIO(string name = "") { 
	ios_base::sync_with_stdio(0); cin.tie(0); 

	if(name.size()){
		freopen((name+".in").c_str(), "r", stdin); 
		freopen((name+".out").c_str(), "w", stdout);
	}
}

//tracks in the Snow

int h, w;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

string snow[4000];

int numAni[4000][4000];

bool valid(int x, int y)
{
    return (x > -1 && x < h && y > -1 && y < w && snow[x][y] != '.');
}

int main()
{
    setIO();

    cin >> h >> w;

    for(int i = 0; i < h; i++)
    {
        cin >> snow[i];
        
    }

    deque<pair<int, int>> q;

    q.push_back({0,0});
    numAni[0][0] = 1;
    int ans = 1;

    while(!q.empty())
    {
        pair<int, int> c = q.front();
        q.pop_front();

        int curAni = numAni[c.first][c.second];

        ans = max(ans, curAni);

        for(int i = 0; i < 4; i++)
        {
            int nx = c.first + dx[i];
            int ny = c.second + dy[i];

            if(valid(nx, ny) && numAni[nx][ny] == 0)
            {
                if(snow[nx][ny] == snow[c.first][c.second])
                {
                    numAni[nx][ny] = curAni;
                    q.push_front({nx,ny}); 
                }
                else
                {
                    numAni[nx][ny] = curAni + 1;
                    q.push_back({nx,ny}); 
                }
            }
        }
    }

    cout << ans;

    return 0;

}

Compilation message (stderr)

tracks.cpp: In function 'void setIO(std::string)':
tracks.cpp:11:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 |   freopen((name+".in").c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:12:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |   freopen((name+".out").c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...