×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / I want to write a method with the function of recursion, can anybody tell me how can I do it by useing Java or C++?
    I want to write a method with the function of recursion, can anybody tell me how can I do it by useing Java or C++(I prefer Java, but if you can tell both of them is the best), the recursive method is used for 8x8 matrix grid which can put 8 components into the grid with the conditions as they do not attack each other (do not cross each others paths). they move vertically, horizontally and diagonally. The program must provide the components positions of the example solution, if one exists. If more than one solution exists we want to know the number of possible solutions.
    Thank you!
    • For recurcive methods, you can get examples from a lot of textbooks, to say, C++ Primer and Thinking in C++.
    • That's the recursive solution of the famous problem of eight queens.
      Basic idea:

      //
      // n -- the number of non-attacked compoments already in the grid
      // _ -- space
      //
      components(n)
      {
      __while (There is an empty cell (say C) in the grid where
      _________the n+1th compoment may (or may not) be put
      _________in a non-attacked manner) {
      ____Selects the cell C and tests if it is non-attacked.
      ____if (The cell C is non-attacked) {
      ______Puts the n+1th component into the cell C.
      ______if (n + 1 == 8 (the maximum number of components))
      ________Output the results (Gets one of the solutions).
      ______else
      ________Calls components(n + 1)
      ______Removes the n+1th compoment from the cell C
      ______(Tries the next empty cell).
      ____}
      __}
      __return
      }
      • thank you very much, i do want to write a method for the eight queens problem, i know u should be an expert, may i have u'r source code of the eightqueens problem? thanks anyway!
        • 1234
          1. The difficulty of the problem is its algorithm, not its code writing.
          2. Usually, the pseudo code is more helpful than source code because the pseudo code usually is easier to read, understand and remember, and focus to the key, and is computer language independent, and eliminate many useless coding details, and so on.
          3. For coding, my suggestion is to practice and try yourself first. If having problem, then post your problem and discuss with others on this forum. I believe that by this way you will gain much more than a piece of source code.
          4. I am only another amateur. Thank you!