Imersive N-queens(var.hell) ๋ฏธ์™„์„ฑ

chltndid724ยท2019๋…„ 8์›” 1์ผ
0

immersive

๋ชฉ๋ก ๋ณด๊ธฐ
5/22
post-thumbnail

๐Ÿงš N-queens ์•Œ๊ณ ๋ฆฌ์ฆ˜

์†Œ๊ฐ
ํ ..... ๋„ˆ๋ฌด ์–ด๋ ค์› ๊ณ  ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ํž˜๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํ€ธ์ฆˆ๊นŒ์ง€๋Š” ๋ชป ํ•˜์˜€์ง€๋งŒ Rook๊ณผ ๋Œ€๊ฐ์„ ์—
๊ด€ํ•œ ๊ฒƒ์€ ์™„๋ฃŒํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. solves๋„ ๋ชป ํ•˜์˜€๋‹ค๋Š” ๋น„๋ฐ€.....

  • ์ฒซ๋ฒˆ์งธ ํ–‰
    hasRowConflictAt: function (rowIndex) { // ํ–‰ ์ž์ฒด์— 
         // console.log("hasRow: ",this.attributes)
         let chass = this.attributes[rowIndex];
         // console.log(this.attributes) ์ฐ์–ด๋ณด๋ฉด ์œ ์‚ฌ๋ฐฐ์—ด๋กœ ์•„๋ž˜๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค.
         // ์—ฌ๊ธฐ์„œ, ์ฐจ๊ทผ์ฐจ๊ทผ ๊ฐ€์•ผํ•ฉ๋‹ˆ๋‹ค.
         // {0: Array(4), 1: Array(4), 2: Array(4), 3: Array(4), n: 4}
         let count = 0;
           // console.log('first',chass)
    		for(let i = 0 ; i < chass.length; i++){ 
             // ๊ฐ ํ–‰์„ ๋Œ๋ฉด์„œ 1์„ ์ฐพ๊ฒŒ๋˜๋ฉด count๋ฅผ ๋Š˜๋ ค์„œ 2๊ฐ€ ๋˜๋ฉด true๋ฅผ ํ„ด์ ธ์„œ ๋นจ๊ฐ›๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
             if(chass[i] === 1){  // 0 1 0 0
               count++;           // 0 0 0 1
             }                    // 1 0 0 0
         }                        // 0 0 1 0
         return count > 1; // fixme
       }

* ์ „์ฒด ํ–‰์„ ๊ด€๋ฆฌ

```javascript
  hasAnyRowConflicts: function () { // ํŒ ์ž์ฒด์—์„œ ์ฒดํฌ
      let isConflict = false;       // _.every ์ฒ˜๋Ÿผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.
      let chassLength = this.attributes.n; // ๊ธธ์ด๋งŒ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
        for (let i = 0; i < chassLength; i++) {  // ์œ„์—์„œ ์‚ฌ์šฉํ•œ๊ฒƒ์„ ํ† ๋Œ€๋กœ ๊ฒฐ๊ณผ ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
          this.hasRowConflictAt(i) && (isConflict = true);
        }
      return isConflict; // fixme
    },
  • ์„ธ๋กœ ํ–‰

    ์„ธ๋กœ ํ–‰์€ ์ฒ˜์Œ์— ์ดํ•ดํ•˜๋Š”๋ฐ ์กฐ๊ธˆ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค. key ๊ฐ’์„ ์›€์ง์—ฌ ์„ธ๋กœ๋กœ ๊ฐ„๋‹ค๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 hasColConflictAt: function (colIndex) {
      let chass = this.attributes;
      // console.log(chass)
      let count = 0;
      for(let key in chass){ // key 0, 1, 2, 3
          if(chass[key][colIndex] === 1){
            count++
          }
      }
      return count > 1; // fixme
    },
  • ์ „์ฒด ์„ธ๋กœ ์—ด
  hasAnyColConflicts: function () { // ์œ„์— ํ–ˆ๋˜๊ฑฐ์™€ ๋˜‘๊ฐ™์ด ํ•˜๋ฉด ๋˜์—ˆ๋‹ค.
      let isConflict = false;
      let chassLength = this.attributes.n;
       for (let i = 0; i < chassLength; i++) {
         this.hasColConflictAt(i) && (isConflict = true);
       }
     return isConflict; // fixme
    },
	
  • ๋Œ€๊ฐ์„  (์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ)


i(ํ–‰)์—์„œ j(์—ด)๋นผ์„œ

hasMajorDiagonalConflictAt: function (majorDiagonalColumnIndexAtFirstRow) {
      let chass = Object.values(this.attributes)
      let chassLength = chass[4]
      let count = 0;
      // console.log('top', majorDiagonalColumnIndexAtFirstRow)
      for (let i = 0; i < chassLength; i++ ){
        for (let j = 0; j < chassLength; j++) {
          if(i - j === majorDiagonalColumnIndexAtFirstRow && chass[j][i]){
            count++
          }
        }
      }
      // [0, 1, 2, 3]   [0,0,0,1]
      // [-1,0, 1, 2]   [0,0,0,1]
      // [-2,-1,0, 1]   [1,0,0,0]
      // [-3,-2,-1,0]   [0,1,0,0]

      return count > 1  // fixme
    },
	

๋ฐฑํŠธ๋ ‰ํ‚น ์ด๋ž€?

ํŠน์ • ๋…ธ๋“œ์—์„œ ์œ ๋ง์„ฑ์„ ์ ๊ฒ€ํ•˜๊ณ , ์œ ๋งํ•˜์ง€ ์•Š์œผ๋ฉด ๋‹ค์‹œ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๋กœ ๋Œ์•„๊ฐ€์„œ ๋‹ค์Œ ๋…ธ๋“œ์— ๋Œ€ํ•œ
๊ฒ€์ƒ‰์„ ๊ณ„์† ํ•˜๊ฒŒ๋˜๋Š” ์ ˆ์ฐจ ์ž…๋‹ˆ๋‹ค.

profile
ํž˜๋“ค๋• ๋ธ”๋กœ๊ทธ ํ•˜๋‚˜๋” ์ ์ž!!![ Suyang ]

0๊ฐœ์˜ ๋Œ“๊ธ€