Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize ("O3")
- #pragma GCC target("sse4")
- #include <bits/stdc++.h>
- #define f first
- #define s second
- #define pb push_back
- #define mp make_pair
- #define ts to_string
- #define ub upper_bound
- #define lb lower_bound
- const char nl = '\n';
- using namespace std;
- using ll = long long;
- using vi = vector<int>;
- using vl = vector<ll>;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using str = string;
- //The following debugging code was taken from Benjamin Qi's template :)
- str ts(char c) { return str(1,c); }
- str ts(bool b) { return b ? "true" : "false"; }
- str ts(const char* s) { return (str)s; }
- str ts(str s) { return s; }
- template<class A> str ts(complex<A> c) {
- stringstream ss; ss << c; return ss.str(); }
- str ts(vector<bool> v) {
- str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
- res += "}"; return res; }
- template<size_t SZ> str ts(bitset<SZ> b) {
- str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
- return res; }
- template<class A, class B> str ts(pair<A,B> p);
- template<class T> str ts(T v) {
- bool fst = 1; str res = "{";
- for (const auto& x: v) {
- if (!fst) res += ", ";
- fst = 0; res += ts(x);
- }
- res += "}"; return res;
- }
- template<class A, class B> str ts(pair<A,B> p) {
- return "("+ts(p.f)+", "+ts(p.s)+")"; }
- void DBG() { cerr << "]" << endl; }
- template<class H, class... T> void DBG(H h, T... t) {
- cerr << ts(h); if (sizeof...(t)) cerr << ", ";
- DBG(t...); }
- #ifdef LOCAL
- #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
- #else
- #define dbg(...) 0
- //freopen(".in", "r", stdin);
- //freopen(".out", "w", stdout);
- #endif
- //CODE STARTS HERE-----------------------------------------------------------------------------------------//
- const int N = 21;
- int n;
- char a[N][N];
- queue<array<int, 6>> bfs;
- int vis[N][N][N][N][4][4],d[N][N][N][N][4][4];
- int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0};
- array<int, 6> forward(array<int, 6> q){
- array<int,6> ne = q;
- ne[0] += dx[ne[4]];
- ne[1] += dy[ne[4]];
- ne[2] += dx[ne[5]];
- ne[3] += dy[ne[5]];
- if(a[ne[0]][ne[1]] == 'H' || ne[0] < 0 || ne[0] >= n || ne[1] < 0 || ne[1] >= n){
- ne[0] -= dx[ne[4]];
- ne[1] -= dy[ne[4]];
- }
- if(a[ne[2]][ne[3]] == 'H'|| ne[2] < 0 || ne[2] >= n || ne[3] < 0 || ne[3] >= n){
- ne[2] -= dx[ne[5]];
- ne[3] -= dy[ne[5]];
- }
- if(q[0] == 0 && q[1] == 0){
- ne[0] = q[0], ne[1] = q[1];
- }
- if(q[2] == 0 && q[3] == 0){
- ne[2] = q[2], ne[3] = q[3];
- }
- return ne;
- }
- vector<array<int, 6>> FIND(array<int,6> q){
- vector<array<int, 6>> ret;
- array<int, 6> ne = forward(q);
- if(!vis[ne[0]][ne[1]][ne[2]][ne[3]][ne[4]][ne[5]]){
- ret.pb(ne);
- vis[ne[0]][ne[1]][ne[2]][ne[3]][ne[4]][ne[5]] = 1;
- }
- ne = q;
- ne[4] = (q[4] + 1) % 4;
- ne[5] = (q[5] + 1) % 4;
- if(!vis[ne[0]][ne[1]][ne[2]][ne[3]][ne[4]][ne[5]]){
- ret.pb(ne);
- vis[ne[0]][ne[1]][ne[2]][ne[3]][ne[4]][ne[5]] = 1;
- }
- ne = q;
- ne[4] = (q[4] + 3) % 4;
- ne[5] = (q[5] + 3) % 4;
- if(!vis[ne[0]][ne[1]][ne[2]][ne[3]][ne[4]][ne[5]]){
- ret.pb(ne);
- vis[ne[0]][ne[1]][ne[2]][ne[3]][ne[4]][ne[5]] = 1;
- }
- // dbg(ret);
- return ret;
- }
- signed main(){
- cin.tie(nullptr);
- ios_base::sync_with_stdio(false);
- // ifstream cin("cownav.in");
- // ofstream cout("cownav.out");
- cin >> n;
- for(int i =0 ; i < n; i++){
- for(int j =0 ; j < n; j++){
- cin >> a[i][j];
- }
- }
- bfs.push({n-1,n-1,n-1,n-1,2,3});
- vis[n-1][n-1][n-1][n-1][2][3] = 1;
- memset(d, 0x3f, sizeof(d));
- d[n-1][n-1][n-1][n-1][2][3] = 0;
- //0=up,1=right,2=down,3=left
- while(!bfs.empty()){
- array<int, 6> cur = bfs.front();
- // dbg(cur,d[cur[0]][cur[1]][cur[2]][cur[3]][cur[4]][cur[5]]);
- bfs.pop();
- vector<array<int, 6>> remain = FIND(cur);
- for(auto x : remain){
- bfs.push(x);
- d[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]] = d[cur[0]][cur[1]][cur[2]][cur[3]][cur[4]][cur[5]] + 1;
- }
- }
- int ans = 1e9;
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 4; j++){
- ans = min(d[0][0][0][0][i][j],ans);
- }
- }
- cout << ans << nl;
- }
Add Comment
Please, Sign In to add comment