Submission #1092558

#TimeUsernameProblemLanguageResultExecution timeMemory
1092558jerzykSandwich (JOI16_sandwich)C++17
100 / 100
891 ms3236 KiB
#include <bits/stdc++.h> //#pragma GCC optimize("Ofast","unroll-loops") //#pragma GCC target("popcnt") using namespace std; #define pb push_back #define st first #define nd second typedef long long ll; typedef long double ld; typedef unsigned long long ull; const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; const ll M = 1000LL * 1000LL * 1000LL + 7LL; const int N = 400 + 7; int n, m; int vis[N][N]; int il = 0; string s[N]; int answer[N][N]; queue<int> q; inline void Clean() { il = 0; for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) vis[i][j] = 0; } bool DFS(int r, int i, int j) { if(vis[i][j]) return vis[i][j] == 2; vis[i][j] = 2; ++il; ++il; if(r == 0) { if(i > 1 && DFS(0, i - 1, j)) return true; if(j > 1 && s[i][j] == 'Z') if(DFS((s[i][j - 1] != 'Z'), i, j - 1)) return true; if(j < m && s[i][j] == 'N' && DFS((s[i][j + 1] != 'N'), i, j + 1)) return true; }else { if(i < n && DFS(1, i + 1, j)) return true; if(j > 1 && s[i][j] == 'N' && DFS((s[i][j - 1] != 'Z'), i, j - 1)) return true; if(j < m && s[i][j] == 'Z' && DFS((s[i][j + 1] != 'N'), i, j + 1)) return true; } vis[i][j] = 1; return false; } void Solve() { cin >> n >> m; for(int i = 1; i <= n; ++i) { cin >> s[i]; s[i] = '#' + s[i]; for(int j = 1; j <= m; ++j) answer[i][j] = N * N * 4; } for(int j = 1; j <= m; ++j) { for(int i = 1; i <= n; ++i) { if(DFS(0, i, j)) break; answer[i][j] = min(answer[i][j], il); } Clean(); for(int i = n; i >= 1; --i) { if(DFS(1, i, j)) break; answer[i][j] = min(answer[i][j], il); } Clean(); } for(int i = 1; i <= n; ++i) { for(int j = 1; j <= m; ++j) { if(answer[i][j] == N * N * 4) answer[i][j] = -1; cout << answer[i][j] << " "; } cout << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //int t; cin >> t; //while(t--) Solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...