一、回溯算法也叫试探法,它是一种系统地搜索问题的解的方法。回溯算法的基本思想是:从一条路往前走,能进则进,不能进则退回来,换一条路再试。用回溯算法解决问题的一般步骤为:
1、定义一个解空间,它包含问题的解。
2、利用适于搜索的方法组织解空间。
3、利用深度优先法搜索解空间。
4、利用限界函数避免移动到不可能产生解的子空间。
二、回溯举例
2.1皇后问题
问题描述:在n×n 格的棋盘上放置彼此不受攻击的n 个皇后。按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。n皇后问题等价于在n×n格的棋
上放置n个皇后,任何2 个皇后不放在同一行或同一列或同一斜线上。
输入:输入数据只占一行,有1 个正整数n,n≤20。
输出:将计算出的彼此不受攻击的n个皇后的一个放置方案输出。
解答代码:
#include<iostream>
#include<cstdio>
#include<fstream>
using namespace std;
#define Row 8
#define Col 8
int sum=0;
int m[Row][Col]={0};
bool check(int row,int col)
{
if(row==0)
return true;
int i,j;
for(i=0;i<row;i++)
if(m[i][col]==1)
return false;
i=row-1;
j=col-1;
while(i>=0 && j>=0)
{
if(m[i][j]==1)
return false;
i--;
j--;
}
i=row-1;
j=col+1;
while(i>=0 && j<Col)
{
if(m[i][j]==1)
return false;
i--;
j++;
}
return true;
}
void output()
{
int i,j;
sum++;
printf("sum=%d\n",sum);
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
{
printf("%d ",m[i][j]);
}
printf("\n");
}
}
void solve(int row)
{
int j;
for(j=0;j<Col;j++)
{
m[row][j]=1;
if(check(row,j)==true)
{
if(row==Row-1)
output();
else
solve(row+1);
}
m[row][j]=0;
}
}
int main()
{
solve(0);
return 0;
}
另一种解法:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int total=0;
int C[10]={0};
int result[20][20]={0};
void search(int cur,int n)
{
int i,j;
if(cur==n)//递归的边界
{
total++;
for(int k=0;k<n;k++)
{
for(int kk=0;kk<n;kk++)
printf("%d ",result[k][kk]);
printf("\n");
}
printf("\n");
}
else
for(i=0;i<n;i++)
{
int ok=1;
C[cur]=i;//尝试把第cur行的皇后放在第i列
for(j=0;j<cur;j++)//检查是否和前面的皇后冲突
if(C[cur]==C[j]||cur-C[cur]==j-C[j]||cur+C[cur]==j+C[j])
{
ok=0;
break;
}
if(ok)
{
result[cur][i]=1;
search(cur+1,n);
result[cur][i]=0;
}
}
}
int main()
{
search(0,8);
int i,j;
printf("total=%d\n",total);
return 0;
}
2.2 老鼠走迷宫:
可以的走法定义在XH和YH数组中
#include<iostream>
#include<fstream>
#define Row 7
#define Col 7
using namespace std;
int m[Row][Col]={
{2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 0, 0, 0, 2},
{2, 0, 0, 2, 0, 2, 2},
{2, 2, 0, 2, 0, 2, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2}
};
int x=1,y=1,xx=5,yy=5;
int sum=0;
int XH[4]={1,-1,0,0};
int YH[4]={0,0,1,-1};
void find(int x,int y);
void output();
ofstream fout("estdout_2.pc2");
int main()
{
int i,j;
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
{
if(m[i][j]==2)
cout<<"█";
else
cout<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
find(1,1);
cout<<"所有走法:"<<sum<<endl;
return 0;
}
void find(int x,int y)
{
int t=m[x][y];
m[x][y]=1;
if(x==5&&y==5)
{
sum++;
output();
}
else
{
int u,v;
for(int i=0;i<4;i++)
{
u=x+XH[i];
v=y+YH[i];
if(m[u][v]==0)
find(u,v);
}
}
m[x][y]=t;
}
void output()
{
int i,j;
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
{
if(m[i][j]==2)
{
cout<<"█";
}
else if(m[i][j]==0)
{
cout<<" ";
}
else
{
cout<<"◇";
}
}
cout<<endl;
}
cout<<endl;
}
2.3 跳棋走法:可以的走法定义在XH和YH数组中
#include<iostream>
#include<cstdio>
#include<cstring>
#define SIZE 5
using namespace std;
int XH[8]={-2,-1,1,2,2,1,-1,-2};
int YH[8]={-1,-2,-2,-1,1,2,2,1};
int num=1;
int result=0;
int vist[SIZE][SIZE];
void output()
{
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
printf("%d ",vist[i][j]);
}
printf("\n");
}
printf("\n");
}
void found(int i,int x,int y)
{
int u,v;
vist[x][y]=i;
if(i==SIZE*SIZE)
{
result++;
output();
}
else
{
for(int j=0;j<8;j++)
{
u=x+XH[j];
v=y+YH[j];
if(u>=0 && u<SIZE && v>=0 && v<SIZE && vist[u][v]==0 )
{
found(i+1,u,v);
vist[u][v]=0;
}
}
}
}
int main()
{
memset(vist,0,sizeof(vist));//初始化
found(1,0,0);
printf("result=%d\n",result);
}