# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
876211 | 2023-11-21T12:43:52 Z | vjudge1 | Tracks in the Snow (BOI13_tracks) | C++17 | 0 ms | 0 KB |
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back //emplace_back #define pp pop_back #define pii pair<ll, ll> //pair<int, int> #define all(x) (x).begin(),(x).end() #define mp(a,b) make_pair(a , b) #define lb lower_bound #define ub upper_bound #define sz(x) (ll)(x).size() #define F first #define S second #define bg(x) (x).begin() #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++) #define debug(x) cout << #x << " : " << x << endl #define endl (char)(10) #define arr(x) array<int , (x)> #define yes cout << "YES" << endl #define no cout << "NO" << endl #define FAST ios_base::sync_with_stdio(0);cin.tie(0); ll Sum(ll a , ll b , ll MOD) { a %= MOD; b %= MOD; a += b; return a % MOD; } ll Mul(ll a , ll b , ll MOD) { a %= MOD; b %= MOD; a *= b; return a % MOD; } ll Pow(ll a , ll b , ll MOD) { ll res = 1; while(b) { if((b & 1))res = Mul(res , a , MOD); a = Mul(a , a , MOD); b >>= 1; } return res; } ll Min(ll a , ll b) { if(a > b)return b; return a; } ll Max(ll a , ll b) { if(a > b)return a; return b; } ll Ceil(ll a , ll b) { if(a % b)return (a/b)+1; return a/b; } ///////////////////// //VALS const ll maxn = 4009; ll n,m; string s[maxn]; bool vis[maxn][maxn]; ll ans; ll x[4] = {1,-1,0,0}; ll y[4] = {0,0,1,-1}; ///////////////////// //FUNCTIONS void bfs(queue<pii> q1 = {mp(0,0)}, queue<pii> q2 = {}, ll d = 1) { if(q1.empty())return; ans = d; while(!q1.empty()) { pii p = q1.front(); q1.pop(); //debug(p.F); //debug(p.S); For(i,4) { if(p.F + x[i] < 0 or p.F + x[i] >= n or p.S + y[i] < 0 or p.S + y[i] >= m or s[p.F + x[i]][p.S + y[i]] == '.' or vis[p.F + x[i]][p.S + y[i]] == 1)continue; vis[p.F + x[i]][p.S + y[i]] = 1; bool flag = (s[p.F + x[i]][p.S + y[i]] == s[p.F][p.S]); //debug(i); //debug(flag); if(flag) q1.push(mp(p.F + x[i], p.S + y[i])); else q2.push(mp(p.F + x[i], p.S + y[i])); } } bfs(q2, q1, d+1); } ///////////////////// //SOLVE void solve() { cin >> n >> m; For(i,n)cin >> s[i]; vis[0][0] = 1; bfs(); cout << ans << endl; } ///////////////////// //MAIN int main() { FAST; ll t = 1; // cin >> t; while(t--) { solve(); } } ///////////////////// /* ZZZZZZZ A M M IIIIIII N N Z A A M M M M I NN N Z A A M M M M I N N N Z AAAAAAA M M M I N N N Z A A M M I N N N Z A A M M I N NN ZZZZZZZ A A M M IIIIIII N N TREE */