算导4(8皇后-回溯问题)

本文介绍了解决8皇后问题的两种方法:迭代回溯法和递归回溯法,并详细阐述了其解题思路及复杂度分析。通过具体的C++代码实现展示了如何找到所有可行解。
Solve the 8-Queenproblem using back-trackingalgorithm.
解题思路:
   这道题目可以使用回溯算法。这种方法的思想是:为了求得问题的解,先选择某一种可能情况向前探索,如果发现是错误的,则退回一步重新选择,继续向前探索,如此反复进行,直至得到解或者证明无解。要解决N皇后问题,其实就是要解决好怎么放置这n个皇后,每一个皇后与前面的所有皇后不能在同一行、同一列、同一对角线,在这里我们可以以行优先,就是说皇后的行号按顺序递增,只考虑第i个皇后放置在第i行的哪一列,所以在放置第i个皇后的时候,可以从第1列判断起,如果可以放置在第1个位置,则跳到下一行放置下一个皇后。如果不能,则跳到下一列...直到最后一列,如果最后一列也不能放置,则说明此时放置方法出错,则回到上一个皇后向之前放置的下一列重新放置。此即是回溯法的精髓所在。当第n个皇后放置成功后,即得到一个可行解,此时再回到上一个皇后重新放置寻找下一个可行解...如此后,即可找出一个n皇后问题的所有可行解。 
复杂度分析:
    
复杂度小于n^3。棋盘是n行n列的,但是在每列选择可否放置的比较上又做了一次循环。但不是循环到n,而是根据皇后i的取值而变化的。所以复杂度应该是1/3n^3左右。

这是迭代的方法:
#include <cstdlib>
#include <iostream>
#include <math.h>
#define N 8
int x[N+1];//放置的列数
//int final[N+1][N+1];
using namespace std;
void print(int x[])
{
     int i= 1;
    for(int u = 1; u <= N; u++)
    {
           for(int v = 1; v <= N; v++)
           {
                  if(u == i&& v == x[i])
                      cout << "*"<< " ";
                  else 
                      cout << 0<< " ";
           }
           i++;
           cout << endl;
    }
     cout<< endl;
    //memset(final, 0,sizeof(final));//这个绝对不能加,每次memset要浪费很多时间 
     
}
bool Place(int k)
{
     int i= 1;
    while(i < k)
    {
           if(x[i] == x[k] || abs(x[i] - x[k]) == abs(i -k))
//如果实在同一对角线上,则他们横坐标的差值和纵坐标的差值绝对值相同。
               returnfalse;
           else i = i + 1;
    }
     returntrue;
int NQuees(int num)
{
     x[1] =0;
     int k= 1; 
    while(k > 0)
    {
           x[k] = x[k] + 1;
           while(x[k] <= N&& !Place(k))
           {
                     x[k] =x[k] + 1;
//不断搜寻第k个皇后应该放的位置,直到找到合适的或者此行搜索完毕也没有发现合适的位置。
           }
           if(x[k] <= N)//搜索到合适的位置
           {
                  if(k ==N)//如果对于第n个皇后已经搜索到,则可以打印x[k]
                  {
                      num++;
                      cout << "第"<< num<<"种:"<< endl;
                      print(x);
                      
                  }
                 else//如果没有到达最后一行,则行数k加1。并且从第一列开始尝试。 
                  {
                      k = k + 1;
                      x[k] = 0;
                  }    
           }
           else k = k -1;//不能搜索到合适的位置则说明上面的步骤有问题,则回溯到上一行重新比对。 
    }
     returnnum;
}
int main(int argc, char *argv[])
{
    int num = 0;
    cout<< "num:"<< NQuees(num)<< endl;
    system("PAUSE");
    returnEXIT_SUCCESS;
}
这是递归的方法:
#include <cstdlib>
#include <iostream>
#define N 8
using namespace std;
int final[N+1][N+1];
int x[N+1];
int sum = 0;
void print()
{
    for(int u = 1; u <= N; u++)
    {
           for(int v = 1; v <= N; v++)
           {
                  if(v == x[u])
                      cout << "*"<< " ";
                  else 
                      cout << "0"<< " ";
           }
           cout << endl;
    }
           
}
bool place(int k)
{
    for(int i = 1; i< k; i++)
    {
           if(x[k] ==x[i] || abs(x[k] - x[i]) == abs(k - i))
                 return false;
    }
    return true;
}
    
int quees(int k)
{
     if(k> N)
    {
         sum++;
         cout<< "第"<< sum<< "种:"<<endl; 
         print();
         cout<< endl;
         
    }
    else
    {
        for(int i = 1; i<= N; i++)
        {
               x[k] =i;
              if(place(k))
               {
                     quees(k+1);
              
        }
    }
     returnsum;
}
           
int main(int argc, char *argv[])
{
   quees(1); 
    cout<< sum<< endl;
    system("PAUSE");
    returnEXIT_SUCCESS;
}
      迭代回溯和递归回溯差不多。递归回溯存在一个潜在的问题,即当n增大时,递归的复杂度也将是几何级的增长,可能出现重复的情况。回溯法的解就是展开一个树。比如说这个N皇后问题,好像当n>60的时候,回溯法就不能完全地解决问题了,这时可以考虑用概率算法来解决,它可以解决很大的基数,只不过结果不是很精确而已。
Practice 1 Date: Monday, March 18th, 2013 We highly encourage being environment friendly and trying all problems on your own. Implement exercise 2.3-7. Implement priority queue. Implement Quicksort and answer the following questions. (1) How many comparisons will Quicksort do on a list of n elements that all have the same value? (2) What are the maximum and minimum number of comparisons will Quicksort do on a list of n elements, give an instance for maximum and minimum case respectively. Give a divide and conquer algorithm for the following problem: you are given two sorted lists of size m and n, and are allowed unit time access to the ith element of each list. Give an O(lg m + lgn) time algorithm for computing the kth largest element in the union of the two lists. (For simplicity, you can assume that the elements of the two lists are distinct). Practice 2 Date: Monday, April 1st, 2013 We highly encourage being environment friendly and trying all problems on your own. Matrix-chain product. The following are some instances. Longest Common Subsequence (LCS). The following are some instances. X: xzyzzyx Y: zxyyzxz X:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCALLAAQANKESSSESFISRLLAIVAD Y:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCTLLAAQANKENSNESFISRLLAIVAG Longest Common Substring. The following are some instances. X: xzyzzyx Y: zxyyzxz X:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCALLAAQANKESSSESFISRLLAIVAD Y:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCTLLAAQANKENSNESFISRLLAIVAG Max Sum. The following is an instance. (-2,11,-4,13,-5,-2) Shortest path in multistage graphs. Find the shortest path from 0 to 15 for the following graph.   A multistage graph is a graph (1) G=(V,E) with V partitioned into K >= 2 disjoint subsets such that if (a,b) is in E, then a is in Vi , and b is in Vi+1 for some subsets in the partition; and (2) | V1 | = | VK | = 1.     Practice 3 Date: Monday, April 15th, 2013 We highly encourage being environment friendly and trying all problems on your own. Knapsack Problem. There are 5 items that have a value and weight list below, the knapsack can contain at most 100 Lbs. Solve the problem both as fractional knapsack and 0/1 knapsack. A simple scheduling problem. We are given jobs j1, j2… jn, all with known running times t1, t2… tn, respectively. We have a single processor. What is the best way to schedule these jobs in order to minimize the average completion time. Assume that it is a nonpreemptive scheduling: once a job is started, it must run to completion. The following is an instance. (j1, j2, j3, j4) : (15,8,3,10) Single-source shortest paths. The following is the adjacency matrix, vertex A is the source.  A B C D E A -1 3 B 3 2 2 C D 1 5 E -3 All-pairs shortest paths. The adjacency matrix is as same as that of problem 3.(Use Floyd or Johnson’s algorithm)     Practice 4 Date: Monday, May 8th, 2013 We highly encourage being environment friendly and trying all problems on your own. 0/1 Knapsack Problem. There are 5 items that have a value and weight list below, the knapsack can contain at most 100 Lbs. Solve the problem using back-tracking algorithm and try to draw the tree generated. Solve the 8-Queen problem using back-tracking algorithm.    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值