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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
| #include "solver.h" #include <iostream> #include <cstring> #include <cstdio> using namespace std; int n, uu; bool block[15][5][5]; bool board[15][15], tmpb[15][15];
char fileName[1005]; double zxxd[15]={0, 0.5, 1.0, 0.5, 1.5, 0.5, 1.0, 1.0, 1.0, 0.0}; int height[15]={0, 1, 2, 1, 3, 1, 2, 2, 2, 1};
int canSet(bool tmp[15][15], int col, int bt) { int ret = 0; for (int row = 10-height[bt]; row >= 1 ; row--) { bool flag = true; for (int i = 1; i <= 3; i++) for (int j = 1; j <= 4; j++) if (block[bt][i][j]) { int ri = col + i - 1; int rj = row + j - 2; if (!(ri >= 1 && ri <= 9 && rj >= 1 && rj <= 9 && !tmp[ri][rj])) { flag = false; } } if (flag) ret = row; else break; } return ret; }
void setNotClear(bool tmp[15][15], int col, int row, int bt) { for (int i = 1; i <= 3; i++) for (int j = 1; j <= 4; j++) if (block[bt][i][j]) tmp[col+i-1][row+j-2] = true; }
bool canClear(bool tmp[15][15], int row) { for (int i = 1; i <= 9; i++) if (!tmp[i][row]) return false; return true; }
void clearRow(bool tmp[15][15], int row) { for (int i = row; i <= 9; i++) for (int j = 1; j <= 9; j++) tmp[j][i] = tmp[j][i+1]; }
int colDiffInRow(int row) { int ret = 0; for (int i = 2; i <= 9; i++) if (tmpb[i-1][row] != tmpb[i][row]) ret++; return ret; }
int rowDiffInCol(int col) { int ret = 0; for (int i = 2; i <= 9; i++) if (tmpb[col][i-1] != tmpb[col][i]) ret++; return ret; }
int getJiao(int bt, int row) { int ret = 0; if (row <= 4 && row >= 1) for (int i = 1; i <= 3; i++) ret += block[bt][i][row]; return ret; }
int getHoles() { int ret = 0; for (int i = 1; i <= 9; i++) { bool flag = false; for (int j = 9; j >= 1; j--) { flag |= tmpb[i][j]; if (!tmpb[i][j] && flag) ret++; } } return ret; }
int getWells() { int ret = 0; for (int i = 1; i <= 9; i++) { int jing = 0; for (int j = 1; j <= 9; j++) if (tmpb[i][j]) jing = 0; else { if ((i==1 && tmpb[2][j]) || (i==9 && tmpb[8][j]) || (i >= 2 && i <= 8 && tmpb[i-1][j] && tmpb[i+1][j])) { jing++; ret += jing; } else { jing = 0; } } } return ret; }
double chang[6]={ -108.1552516822, 93.4515835788, -33.8268913751, -36.4696344915, -165.6161517320, -34.6613258266 };
double getScore(int col, int row, int bt) { double landingHeight = row + zxxd[bt]; double rowsEliminated = 0.0; double rowTransitions = 0.0; double colTransitions = 0.0; setNotClear(tmpb, col, row, bt); int jiao = 0; for (int i = 1, trueI=1; i <= 9; i++, trueI++) if (canClear(tmpb, i)) { rowsEliminated += 1.0; clearRow(tmpb, i); i--; jiao += getJiao(bt, trueI-row+2); } if (jiao == 0 && rowsEliminated > 0.1) { printf("impossible!\n"); } rowsEliminated *= jiao; for (int i = 1; i <= 9; i++) { rowTransitions += colDiffInRow(i); colTransitions += rowDiffInCol(i); } double holeNums = getHoles(); double wells = getWells(); return chang[0] * landingHeight + chang[1] * rowsEliminated + chang[2] * rowTransitions + chang[3] * colTransitions + chang[4] * holeNums + chang[5] * wells; }
int setter(int bt) { int idx = -1, row; double sco = 0.0, score = 0.0;
for (int i = 1; i <= 9; i++) { memcpy(tmpb, board, sizeof(board)); row = canSet(tmpb, i, bt); if (row) { score = getScore(i, row, bt); if (idx == -1 || score > sco) { sco = score; idx = i; } } } return idx; }
void setBlock(int col, int bt) { int row = canSet(board, col, bt); setNotClear(board, col, row, bt); for (int i = 1, trueI=1; i <= 9; i++, trueI++) if (canClear(board, i)) { clearRow(board, i); i--; } }
double solve(int number) { sprintf(fileName, "tiny%d.in", number); FILE *inFp = fopen(fileName, "r"); sprintf(fileName, "tiny.o%d", number); FILE *outFp = fopen(fileName, "w"); memset(board, false, sizeof(board));
int solved = 0, ret; fscanf(inFp, "%d", &n);
for (int i = 1; i <= n; i++) { fscanf(inFp, "%d", &uu); ret = setter(uu);
if (ret == -1) { break; } else { setBlock(ret, uu); solved++; fprintf(outFp, "%d\n", ret); } }
fclose(inFp); fclose(outFp); return 1.0*solved/n; } double mnthjk(double cchang[]) { for (int i = 0; i < 6; i++) chang[i] = cchang[i]; FILE *blockFp = fopen("blocks.txt", "r"); for (int i = 1; i <= 9; i++) for (int row = 4; row >= 1; row--) for (int col = 1; col <= 3; col++) { fscanf(blockFp, "%d", &uu); block[i][col][row] = uu > 0; } double ret = 0.0; for (int i = 1; i <= 5; i++) ret +=solve(i); fclose(blockFp); return ret; }
int main() { FILE *blockFp = fopen("blocks.txt", "r"); for (int i = 1; i <= 9; i++) for (int row = 4; row >= 1; row--) for (int col = 1; col <= 3; col++) { fscanf(blockFp, "%d", &uu); block[i][col][row] = uu > 0; } for (int i = 1; i <= 5; i++) solve(i); fclose(blockFp); return 0; }
|