1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <cctype> #include <vector> #include <cmath> #include <queue> #define pii pair<int,int> #define mp make_pair #define ll long long using namespace std; const int N = 1e6 + 10; int a[10][10],f[N],qwq[10][10]; bool vis[N]; const int dx1[6] = {1,-1,-1,1,0,0}; const int dy1[6] = {1,-1,0,0,-1,1};
const int dx2[6] = {1,-1,-1,1,0,0}; const int dy2[6] = {0,0,1,-1,-1,1};
const int dx3[6] = {1,-1,-1,1,0,0}; const int dy3[6] = {0,-1,0,-1,-1,1}; pii sta[20];
bool valid(int x,int y) { if (x < 1 || y < 1 || x > 5 || y > 5) return 0; int r = 2; if (x <= 3) r += x; else if (x == 4) r += 2; else r++; if (y <= r) return 1; else return 0; }
pii getnxt(int x,int y,int typ) { if (x < 3) { int nx = dx1[typ] + x,ny = y + dy1[typ]; return mp(nx,ny); } else if (x > 3){ int nx = dx2[typ] + x,ny = y + dy2[typ]; return mp(nx,ny); } else {
int nx = dx3[typ] + x,ny = y + dy3[typ]; return mp(nx,ny); } }
int dfs(int now) { if (vis[now]) return f[now]; int ans = 0; int ma[10][10]; vector <pii> st; for (int i = 0;i < 20;++i) { pii ps = sta[i]; if (now & (1 << i)) { ma[ps.first][ps.second] = 1; st.push_back(ps); } else { ma[ps.first][ps.second] = 0; } } if (st.size() == 1) { f[now] = 0; vis[now] = 1; return 0; }
for (auto p : st) { int x = p.first,y = p.second; for (int i = 0;i < 6;++i) { pii np = getnxt(x,y,i); int nx = np.first,ny = np.second; pii npp = getnxt(np.first,np.second,i); int nxx = npp.first,nyy = npp.second; if (valid(nx,ny) && valid(nxx,nyy) && ma[nx][ny] > 0 && (!(nxx == x && nyy == y))){ int jpst = now; jpst -= (1 << qwq[x][y]); jpst -= (1 << qwq[nx][ny]); jpst |= (1 << qwq[nxx][nyy]); if (f[jpst]) ans = max(ans,f[jpst] + a[nx][ny]); else ans = max(ans,dfs(jpst) + a[nx][ny]); } } } vis[now] = 1; return f[now] = ans; }
signed main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int cnt = 0; for (int i = 1;i <= 5;++i) { int r = 2; if (i <= 3) r += i; else if (i == 4) r += 2; else r += 1; for (int j = 1;j <= r;++j) { cin >> a[i][j]; qwq[i][j] = cnt; sta[cnt++] = mp(i,j); } } int q;cin >> q;
while (q--) { int cnt = 0; int stat = 0; for (int i = 1;i <= 5;++i) { int r = 2; if (i <= 3) r += i; else if (i == 4) r += 2; else r += 1; for (int j = 1;j <= r;++j) { char c;cin >> c; if (c == '#') stat += (1 << cnt); ++cnt; } } if (vis[stat]) cout << f[stat] << "\n"; else cout << dfs(stat) << "\n"; } return 0; }
|