{"id":25600,"date":"2017-10-15T20:24:33","date_gmt":"2017-10-15T14:54:33","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25600"},"modified":"2017-10-15T20:24:33","modified_gmt":"2017-10-15T14:54:33","slug":"25600-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/25600-2\/","title":{"rendered":"C++ Programming-Backtracking Set 3 (N Queen Problem)"},"content":{"rendered":"<p>We have discussed Knight\u2019s tour and Rat in a Maze problems in Set 1 and Set 2 respectively. Let us discuss N Queen as another example problem that can be solved using Backtracking.<\/p>\n<p>The N Queen is the problem of placing N chess queens on an N\u00d7N chessboard so that no two queens attack each other. For example, following is a solution for 4 Queen problem.<\/p>\n<p>The expected output is a binary matrix which has 1s for the blocks where queens are placed. For example following is the output matrix for above 4 queen solution.<\/p>\n<pre>              { 0,  1,  0,  0}\r\n              { 0,  0,  0,  1}\r\n              { 1,  0,  0,  0}\r\n              { 0,  0,  1,  0}<\/pre>\n<p><strong>Naive Algorithm<\/strong><br \/>\nGenerate all possible configurations of queens on board and print a configuration that satisfies the given constraints.<\/p>\n<pre>while there are untried conflagrations\r\n{\r\n   generate the next configuration\r\n   if queens don't attack in this configuration then\r\n   {\r\n      print this configuration;\r\n   }\r\n}<\/pre>\n[ad type=\u201dbanner\u201d]\n<p><strong>Backtracking Algorithm<\/strong><br \/>\nThe idea is to place queens one by one in different columns, starting from the leftmost column. When we place a queen in a column, we check for clashes with already placed queens. In the current column, if we find a row for which there is no clash, we mark this row and column as part of the solution. If we do not find such a row due to clashes then we backtrack and return false.<\/p>\n<pre>1) Start in the leftmost column\r\n2) If all queens are placed\r\n    return true\r\n3) Try all rows in the current column.  Do following for every tried row.\r\n    a) If the queen can be placed safely in this row then mark this [row, \r\n        column] as part of the solution and recursively check if placing  \r\n        queen here leads to a solution.\r\n    b) If placing queen in [row, column] leads to a solution then return \r\n        true.\r\n    c) If placing queen doesn't lead to a solution then umark this [row, \r\n        column] (Backtrack) and go to step (a) to try other rows.\r\n3) If all rows have been tried and nothing worked, return false to trigger \r\n    backtracking.<\/pre>\n<p><strong>Implementation of Backtracking solution<\/strong><\/p>\n<p><strong>C++ Programming<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F*%20C%2FC%2B%2B%20program%20to%20solve%20N%20Queen%20Problem%20using%0A%20%20%20backtracking%20*%2F%0A%23define%20N%204%0A%23include%3Cstdio.h%3E%0A%20%0A%2F*%20A%20utility%20function%20to%20print%20solution%20*%2F%0Avoid%20printSolution(int%20board%5BN%5D%5BN%5D)%0A%7B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20N%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%200%3B%20j%20%3C%20N%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%20%25d%20%22%2C%20board%5Bi%5D%5Bj%5D)%3B%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20A%20utility%20function%20to%20check%20if%20a%20queen%20can%0A%20%20%20be%20placed%20on%20board%5Brow%5D%5Bcol%5D.%20Note%20that%20this%0A%20%20%20function%20is%20called%20when%20%22col%22%20queens%20are%0A%20%20%20already%20placed%20in%20columns%20from%200%20to%20col%20-1.%0A%20%20%20So%20we%20need%20to%20check%20only%20left%20side%20for%0A%20%20%20attacking%20queens%20*%2F%0Abool%20isSafe(int%20board%5BN%5D%5BN%5D%2C%20int%20row%2C%20int%20col)%0A%7B%0A%20%20%20%20int%20i%2C%20j%3B%0A%20%0A%20%20%20%20%2F*%20Check%20this%20row%20on%20left%20side%20*%2F%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20col%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(board%5Brow%5D%5Bi%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%0A%20%20%20%20%2F*%20Check%20upper%20diagonal%20on%20left%20side%20*%2F%0A%20%20%20%20for%20(i%3Drow%2C%20j%3Dcol%3B%20i%3E%3D0%20%26%26%20j%3E%3D0%3B%20i\u2013%2C%20j\u2013)%0A%20%20%20%20%20%20%20%20if%20(board%5Bi%5D%5Bj%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%0A%20%20%20%20%2F*%20Check%20lower%20diagonal%20on%20left%20side%20*%2F%0A%20%20%20%20for%20(i%3Drow%2C%20j%3Dcol%3B%20j%3E%3D0%20%26%26%20i%3CN%3B%20i%2B%2B%2C%20j\u2013)%0A%20%20%20%20%20%20%20%20if%20(board%5Bi%5D%5Bj%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%0A%20%20%20%20return%20true%3B%0A%7D%0A%20%0A%2F*%20A%20recursive%20utility%20function%20to%20solve%20N%0A%20%20%20Queen%20problem%20*%2F%0Abool%20solveNQUtil(int%20board%5BN%5D%5BN%5D%2C%20int%20col)%0A%7B%0A%20%20%20%20%2F*%20base%20case%3A%20If%20all%20queens%20are%20placed%0A%20%20%20%20%20%20then%20return%20true%20*%2F%0A%20%20%20%20if%20(col%20%3E%3D%20N)%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%20%20%2F*%20Consider%20this%20column%20and%20try%20placing%0A%20%20%20%20%20%20%20this%20queen%20in%20all%20rows%20one%20by%20one%20*%2F%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20N%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20Check%20if%20queen%20can%20be%20placed%20on%0A%20%20%20%20%20%20%20%20%20%20board%5Bi%5D%5Bcol%5D%20*%2F%0A%20%20%20%20%20%20%20%20if%20(%20isSafe(board%2C%20i%2C%20col)%20)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Place%20this%20queen%20in%20board%5Bi%5D%5Bcol%5D%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20board%5Bi%5D%5Bcol%5D%20%3D%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20recur%20to%20place%20rest%20of%20the%20queens%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(%20solveNQUtil(board%2C%20col%20%2B%201)%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20If%20placing%20queen%20in%20board%5Bi%5D%5Bcol%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20doesn\u2019t%20lead%20to%20a%20solution%2C%20then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20remove%20queen%20from%20board%5Bi%5D%5Bcol%5D%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20board%5Bi%5D%5Bcol%5D%20%3D%200%3B%20%2F%2F%20BACKTRACK%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%2F*%20If%20queen%20can%20not%20be%20place%20in%20any%20row%20in%0A%20%20%20%20%20%20%20%20this%20colum%20col%20%20then%20return%20false%20*%2F%0A%20%20%20%20return%20false%3B%0A%7D%0A%20%0A%2F*%20This%20function%20solves%20the%20N%20Queen%20problem%20using%0A%20%20%20Backtracking.%20It%20mainly%20uses%20solveNQUtil()%20to%0A%20%20%20solve%20the%20problem.%20It%20returns%20false%20if%20queens%0A%20%20%20cannot%20be%20placed%2C%20otherwise%20return%20true%20and%0A%20%20%20prints%20placement%20of%20queens%20in%20the%20form%20of%201s.%0A%20%20%20Please%20note%20that%20there%20may%20be%20more%20than%20one%0A%20%20%20solutions%2C%20this%20function%20prints%20one%20%20of%20the%0A%20%20%20feasible%20solutions.*%2F%0Abool%20solveNQ()%0A%7B%0A%20%20%20%20int%20board%5BN%5D%5BN%5D%20%3D%20%7B%20%7B0%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%2C%0A%20%20%20%20%20%20%20%20%7B0%2C%200%2C%200%2C%200%7D%0A%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20if%20(%20solveNQUtil(board%2C%200)%20%3D%3D%20false%20)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20printf(%22Solution%20does%20not%20exist%22)%3B%0A%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20printSolution(board)%3B%0A%20%20%20%20return%20true%3B%0A%7D%0A%20%0A%2F%2F%20driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20solveNQ()%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output: The 1 values indicate placements of queens<\/strong><\/p>\n<pre> 0  0  1  0 \r\n 1  0  0  0 \r\n 0  0  0  1 \r\n 0  1  0  0<\/pre>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming-Backtracking Set 3 (N Queen Problem) &#8211; Backtracking &#8211; We have discussed Knight\u2019s tour and Rat in a Maze problems in Set 1 and Set 2 respectively.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73907,83515,1],"tags":[74160,74486,74164,74492,74493,74514,74162,74519,74483,74513,74488,74481,74491,74482,74489,74502,74496,73910],"class_list":["post-25600","post","type-post","status-publish","format-standard","hentry","category-backtracking-algorithm","category-c-programming-3","category-coding","tag-backtracking-algorithm-example","tag-backtracking-algorithm-for-8-queen-problem","tag-backtracking-algorithm-pdf","tag-backtracking-definition","tag-backtracking-in-daa","tag-backtracking-method","tag-backtracking-problems","tag-backtracking-python","tag-chess-problems","tag-chess-two-queens","tag-constraint-satisfaction-problem","tag-cryptarithmetic-problem","tag-cryptarithmetic-problem-solved-examples","tag-queen-games","tag-queen-in-chess","tag-queens-problem","tag-sum-of-subset-problem-using-backtracking","tag-what-is-backtracking"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25600"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25600\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}