Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Solve(args) {
- var n = parseInt(args[0]);
- var allWords = [""];
- for (var i = 1; i < args.length; i++) {
- allWords.push(args[i]);
- }
- var endResult = [];
- for (var i = 1; i < n*2+1; i++) {
- for (var j = 1; j < n*2+1; j++) {
- for (var k = 1; k < n*2+1; k++) {
- for (var l = 1; l < n*2+1; l++) {
- for (var m = 0; m < n*2+1; m++) {
- for (var o = 0; o < n*2+1; o++) {
- var needsToBeFour = allWords[i] + allWords[j] + allWords[k] + allWords[l] +
- allWords[m] + allWords[o];
- var words = [];
- if(needsToBeFour.length == n*n) {
- if(i != 0) {
- words.push(allWords[i]);
- }
- if(j != 0) {
- words.push(allWords[j]);
- }
- if(k != 0) {
- words.push(allWords[k]);
- }
- if(l != 0) {
- words.push(allWords[l]);
- }
- if(m != 0) {
- words.push(allWords[m]);
- }
- if(o != 0) {
- words.push(allWords[o]);
- }
- var matrix1 = [];
- for (var p = 0; p < n; p++) {
- matrix1[p] = [];
- var currWord = words[p];
- for (var q = 0; q < n; q++) {
- matrix1[p][q] = currWord.charAt(q);
- }
- }
- var isThere = true;
- for (var p = 0; p < matrix1.length; p++) {
- var wordzy = "";
- for (var q = 0; q < matrix1.length; q++) {
- wordzy += matrix1[q][p];
- }
- if(allWords.indexOf(wordzy) == -1) {
- isThere = false;
- }
- }
- if(isThere) {
- var currResult = [];
- for (var p = 0; p < matrix1.length; p++) {
- currResult.push(matrix1[p].join(''));
- }
- if(endResult.length == 0) {
- endResult = currResult;
- }
- else {
- var endRes = endResult.join('');
- var currRes = currResult.join('');
- if(endRes > currRes) {
- endResult = currResult;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- if(endResult == "") {
- console.log('NO SOLUTION!');
- }
- else {
- console.log(endResult.join('\n'));
- }
- }
- Solve(["4",
- "FIRE",
- "ACID",
- "CENG",
- "EDGE",
- "FACE",
- "ICED",
- "RING",
- "CERN"
- ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement