|
| 1 | +'use strict' |
| 2 | + |
| 3 | +// |
| 4 | +// solve |
| 5 | +// should only return 'POSSIBLE' or 'IMPOSSIBLE' |
| 6 | +// |
| 7 | +function solve(prob) { |
| 8 | + if (prob.numChips === 0) { |
| 9 | + return 'POSSIBLE' |
| 10 | + } |
| 11 | + |
| 12 | + const chipsPerPiece = prob.numChips / prob.numPieces |
| 13 | + if (!Number.isInteger(chipsPerPiece)) { |
| 14 | + return 'IMPOSSIBLE' |
| 15 | + } |
| 16 | + |
| 17 | + // start row processing |
| 18 | + const HChipCount = prob.numChips / (prob.H + 1) |
| 19 | + let cumuRowCutChips = 0 |
| 20 | + let rowCut = [] |
| 21 | + for (let i = 0; i < prob.R; i++) { |
| 22 | + cumuRowCutChips += prob.rowChips[i] |
| 23 | + |
| 24 | + if (cumuRowCutChips === HChipCount) { |
| 25 | + rowCut.push(i) |
| 26 | + cumuRowCutChips = 0 |
| 27 | + } |
| 28 | + |
| 29 | + if (cumuRowCutChips > HChipCount) { |
| 30 | + return 'IMPOSSIBLE' |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + if (rowCut.length !== prob.H + 1) { |
| 35 | + return 'IMPOSSIBLE' |
| 36 | + } |
| 37 | + |
| 38 | + // get pieceChips set to all 0. |
| 39 | + let pieceChips = rowCut.map(rc => 0) |
| 40 | + |
| 41 | + // start column and piece processing |
| 42 | + const VChipCount = prob.numChips / (prob.V + 1) |
| 43 | + let cumuColCutChips = 0 |
| 44 | + let colCut = [] |
| 45 | + for (let j = 0; j < prob.C; j++) { |
| 46 | + cumuColCutChips += prob.colChips[j] |
| 47 | + |
| 48 | + let rowCutIndex = 0 |
| 49 | + for (let i = 0; i < prob.R; i++) { |
| 50 | + if (prob.rows[i][j] === '@') { |
| 51 | + pieceChips[rowCutIndex] += 1 |
| 52 | + } |
| 53 | + |
| 54 | + if (i === rowCut[rowCutIndex]) { |
| 55 | + rowCutIndex += 1 |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (cumuColCutChips === VChipCount) { |
| 60 | + if (!pieceChips.every(pieceChip => pieceChip === chipsPerPiece)) { |
| 61 | + return 'IMPOSSIBLE' |
| 62 | + } |
| 63 | + |
| 64 | + colCut.push(j) |
| 65 | + cumuColCutChips = 0 |
| 66 | + pieceChips = rowCut.map(rc => 0) |
| 67 | + } |
| 68 | + |
| 69 | + if (cumuColCutChips > VChipCount) { |
| 70 | + return 'IMPOSSIBLE' |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (colCut.length !== prob.V + 1) { |
| 75 | + return 'IMPOSSIBLE' |
| 76 | + } |
| 77 | + |
| 78 | + return 'POSSIBLE' |
| 79 | +} |
| 80 | + |
| 81 | +function canSimpleDivide(numChips, numPieces) { |
| 82 | + if (numChips % numPieces !== 0) { |
| 83 | + return false |
| 84 | + } |
| 85 | + return true |
| 86 | +} |
| 87 | + |
| 88 | +// |
| 89 | +// processCases |
| 90 | +// |
| 91 | +function processCases(probs) { |
| 92 | + for (let index = 0; index < probs.length; index++) { |
| 93 | + const result = solve(probs[index]) |
| 94 | + console.log(`Case #${index + 1}: ${result}`) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// |
| 99 | +// CaseParser |
| 100 | +// |
| 101 | +class CaseParser { |
| 102 | + constructor(caseNumber) { |
| 103 | + this.caseNo = caseNumber |
| 104 | + this.R = 0 |
| 105 | + this.C = 0 |
| 106 | + this.H = 0 |
| 107 | + this.V = 0 |
| 108 | + this.rows = [] |
| 109 | + |
| 110 | + this.rowChips = [] |
| 111 | + this.colChips = [] |
| 112 | + this.numChips = 0 |
| 113 | + this.numPieces = 0 |
| 114 | + |
| 115 | + this.currentR = 0 |
| 116 | + this.state = '1' |
| 117 | + } |
| 118 | + |
| 119 | + readline(line) { |
| 120 | + switch (this.state) { |
| 121 | + case '1': { |
| 122 | + const firstLine = line.split(' ') |
| 123 | + this.R = parseInt(firstLine[0]) |
| 124 | + this.C = parseInt(firstLine[1]) |
| 125 | + this.H = parseInt(firstLine[2]) |
| 126 | + this.V = parseInt(firstLine[3]) |
| 127 | + |
| 128 | + for (let i = 0; i < this.C; i++) { |
| 129 | + this.colChips.push(0) |
| 130 | + } |
| 131 | + |
| 132 | + this.numPieces = (this.H + 1) * (this.V + 1) |
| 133 | + |
| 134 | + this.state = 'rows' |
| 135 | + break |
| 136 | + } |
| 137 | + |
| 138 | + case 'rows': { |
| 139 | + const chars = line.split('') |
| 140 | + this.rows.push(chars) |
| 141 | + |
| 142 | + const chipCount = chars.filter(char => char === '@').length |
| 143 | + this.rowChips.push(chipCount) |
| 144 | + |
| 145 | + for (let i = 0; i < chars.length; i++) { |
| 146 | + if (chars[i] === '@') { |
| 147 | + this.colChips[i] += 1 |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + this.numChips += chipCount |
| 152 | + |
| 153 | + this.currentR += 1 |
| 154 | + |
| 155 | + if (this.currentR === this.R) { |
| 156 | + this.state = 'done' |
| 157 | + } |
| 158 | + break |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + isComplete() { |
| 164 | + return (this.state === 'done') |
| 165 | + } |
| 166 | + |
| 167 | + getCase() { |
| 168 | + return { |
| 169 | + caseNo: this.caseNo, |
| 170 | + R: this.R, |
| 171 | + C: this.C, |
| 172 | + H: this.H, |
| 173 | + V: this.V, |
| 174 | + rows: this.rows, |
| 175 | + rowChips: this.rowChips, |
| 176 | + colChips: this.colChips, |
| 177 | + numChips: this.numChips, |
| 178 | + numPieces: this.numPieces |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +// |
| 184 | +// ProblemParser |
| 185 | +// |
| 186 | +class ProblemParser { |
| 187 | + constructor() { |
| 188 | + this.t = 0 |
| 189 | + this.currentT = 0 |
| 190 | + this.cases = [] |
| 191 | + this.caseParser = new CaseParser(1) |
| 192 | + this.state = 't' |
| 193 | + } |
| 194 | + |
| 195 | + readline(line) { |
| 196 | + switch (this.state) { |
| 197 | + case 't': { |
| 198 | + this.t = parseInt(line) |
| 199 | + this.state = 'case' |
| 200 | + break |
| 201 | + } |
| 202 | + |
| 203 | + case 'case': { |
| 204 | + this.caseParser.readline(line) |
| 205 | + |
| 206 | + if (this.caseParser.isComplete()) { |
| 207 | + this.cases.push(this.caseParser.getCase()) |
| 208 | + this.currentT += 1 |
| 209 | + this.caseParser = new CaseParser(this.currentT + 1) |
| 210 | + } |
| 211 | + |
| 212 | + break |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + if (this.currentT === this.t) { |
| 217 | + this.state = 'done' |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + isComplete() { |
| 222 | + return (this.state === 'done') |
| 223 | + } |
| 224 | + |
| 225 | + getCases() { |
| 226 | + return this.cases |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +// |
| 231 | +// Main |
| 232 | +// |
| 233 | +function main() { |
| 234 | + const readline = require('readline') |
| 235 | + const problemParser = new ProblemParser() |
| 236 | + |
| 237 | + const rl = readline.createInterface({ |
| 238 | + input: process.stdin, |
| 239 | + output: process.stdout |
| 240 | + }) |
| 241 | + |
| 242 | + rl.on('line', (line) => { |
| 243 | + problemParser.readline(line) |
| 244 | + |
| 245 | + if (problemParser.isComplete()) { |
| 246 | + rl.close() |
| 247 | + } |
| 248 | + }).on('close', () => { |
| 249 | + processCases(problemParser.getCases()) |
| 250 | + } |
| 251 | + ) |
| 252 | +} |
| 253 | + |
| 254 | +if (!module.parent) { |
| 255 | + main() |
| 256 | +} |
| 257 | + |
| 258 | +module.exports = { |
| 259 | + solve, |
| 260 | + CaseParser |
| 261 | +} |
0 commit comments