题解:P1713 麦当劳叔叔的难题

· · 题解

include <bits/stdc++.h>

            if (x1 < 1 || x1 > n || y1 < 1 || y1 > n || dis[x1][y1] != -1 || ban[x1][y1]) continue;
            dis[x1][y1] = dis[x][y] + 1;
            q.push( { x1,y1 } );
        }
    } 
};  bfs();
std::vector<int>v(n+2);
v[n+1] = 1; dp[1][n-1][v] = 1;
v[n+1] = 0; v[n] = 1; dp[1][n-1][v] = 1;

for (int i = 1; i <= n; ++ i) {
    for (int j = n - (i == 1); j >= 1; j --) {
        for (auto P:dp[i][j]) {
            auto S = P.first; int v = P.second;
            int p = S[j], q = S[j+1];
            if (p == 0 && q == 0) dp[i][j-1][S] = std::max(dp[i][j-1][S],v);
            if (ban[i][j]) continue;
            if (p == 0 && q == 0) {
                auto T = create(S,j);
                dp[i][j-1][T] = std::max(dp[i][j-1][T],v + 1);
            }
            if ((p != 0) + (q != 0) == 1) {
                dp[i][j-1][S] = std::max(dp[i][j-1][S],v + 1);
                auto T = trans(S,j);
                dp[i][j-1][T] = std::max(dp[i][j-1][T],v + 1);
            }
            if (p && q && p != q) {
                auto T = merge(S,j);
                dp[i][j-1][T] = std::max(dp[i][j-1][T],v + 1);
            }
        }
    } for (auto P:dp[i][0]) {
        auto S = P.first; int v = P.second;
        if (S[1] == 0) dp[i+1][n][shift(S)] = dp[i][0][S];
    }
}
std::vector<int>S1(n+2),S2(n+2);
S1[1] = 1; S2[2] = 1;
printf("%d",std::max(dp[n][1][S1],dp[n][1][S2]) - dis[n][1]);
return 0;

}**