Skip to content

Instantly share code, notes, and snippets.

@bhind
Last active May 25, 2017 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhind/417391b855c3aedd8fb4a8263cf88845 to your computer and use it in GitHub Desktop.
Save bhind/417391b855c3aedd8fb4a8263cf88845 to your computer and use it in GitHub Desktop.
パイザの
process.stdin.resume();
process.stdin.setEncoding('utf8');
// 自分の得意な言語で
// Let's チャレンジ!!
function createPermutation(inputArray) {
if(inputArray.length===1) return [[inputArray[0]]];
var result = [];
for(var index=0; index<inputArray.length; index++) {
let cloneArray = inputArray.concat([]);
let head = cloneArray.splice(index,1);
let remainArray = createPermutation(cloneArray);
for(var _index=0;_index < remainArray.length; _index++) {
remainArray[_index].unshift(head[0]);
result.push(remainArray[_index]);
}
}
return result;
}
function applyParameters(inputArray, parameters) {
let ret = new Array(inputArray.length);
var parametersIndex = 0;
for(var index=0; index < inputArray.length; index++) {
let row = inputArray[index];
ret[index] = new Array(row.length);
for(var _index=0; _index < row.length; _index++) {
if(row[_index] === 0) {
ret[index][_index] = parameters[parametersIndex];
parametersIndex++;
} else {
ret[index][_index] = row[_index];
}
}
}
return ret;
}
function checkArray(trialArray) {
var base = 0;
for(var index=0; index<trialArray.length; index++) {
var tmp = 0;
var row = trialArray[index];
for(var _index=0; _index<row.length; _index++) {
tmp += row[_index];
}
if(base === 0) base = tmp;
else if(tmp !== base) return false;
}
for(index=0; index<trialArray[0].length; index++) {
tmp = 0;
for(_index = 0; _index<trialArray.length; _index++) {
tmp += trialArray[_index][index];
}
if(tmp !== base) return false;
}
if(trialArray%2 === 0) return true;
tmp = 0;
for(index=0; index<trialArray.length; index++) {
tmp += trialArray[index][index];
}
if( tmp !== base ) return false;
tmp = 0;
for(index=0; index<trialArray.length; index++) {
tmp += trialArray[trialArray.length-1-index][index];
}
if( tmp !== base ) return false;
return true;
}
process.stdin.on('data', function (chunk) {
var arr = chunk.toString().split('\n');
if(arr.length < 2) throw new Error('too short');
let count = parseInt(arr[0]);
if(!Number.isInteger(count)) throw new Error('wrong number');
let ret = new Array(count);
let unexists = Array.apply(null,new Array(Math.pow(count,2))).map(function(){return true;});
var zeroCount = 0;
let mapfunc = function(v,i){
ret[index][i]=parseInt(v);
if( ret[index][i] > 0 ) {
unexists[ret[index][i]-1] = false;
} else {
zeroCount++;
}
};
for(var index=0; index<count; index++) {
ret[index] = new Array(count);
let tmp = arr[index+1].replace(/\s| /g,' ');
tmp.split(' ').map(mapfunc);
}
let resultsArray = new Array(zeroCount);
index = 0;
unexists.map(function(v,i){if(v){resultsArray[index]=++i;index++;}})
let allPermutation = createPermutation(resultsArray);
for(index=0; index<allPermutation.length; index++) {
let trialArray = applyParameters(ret, allPermutation[index]);
if(checkArray(trialArray)) {
trialArray.map(function(v){console.log(v.join(' '));});
return;
}
}
throw new Error('couldnt solve');
});
@bhind
Copy link
Author

bhind commented May 25, 2017

    let tmp = arr[index+1].replace(/\s| /g,' ');

の全角スペース(or|のあと)が表示上半角になってそうで気持ち悪いです

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment