4.1 递归和排列
4.1.1 用 STL 输出全排列
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int data[] = {5, 2, 1, 4};
sort(data, data + 4);
do{
for(int i = 0;i < 4;i ++){
cout << data[i] << " ";
}
cout << endl;
}while(next_permutation(data, data + 4));
return 0;
}
4.1.2 用递归打印全排列
#include <bits/stdc++.h>
using namespace std;
#define swap1(a, b){int temp = a; a = b; b = temp;}
int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int num = 0;
int perm(int begin, int end){
if(begin == 3){
for(int i = 0;i <= 3;i ++){
cout << data[i] << " ";
}
cout << endl;
num ++;
}
else{
for(int i = begin;i <= end;i ++){
swap1(data[begin], data[i]);
perm(begin + 1, end);
swap1(data[begin], data[i]);
}
}
}
int main(){
perm(0, 9);
cout << num;
}
4.2 子集生成和组合问题
4.2.1 打印一个集合的非空子集
#include <bits/stdc++.h>
using namespace std;
char str[] = {'a', 'b', 'c', 'd'};
void subset(int n){
for(int i = 0;i < (1 << n);i ++){
for(int j = 0;j < n;j ++){
if(i & (1 << j)){
cout << str[j];
}
}
cout << endl;
}
}
int main(){
int n;
cin >> n;
subset(n);
return 0;
}
4.2.2 元素总数量为 n,个数为 k 的子集
#include <bits/stdc++.h>
using namespace std;
void subset(int n, int k){
for(int i = 0;i < (1 << n);i ++){
int num = 0;
int kk = i;
while(kk){
kk = kk & (kk - 1);
num ++;
}
if(num == k){
for(int j = 0;j < n;j ++)
if(i & (1 << j))
cout << j << " ";
cout << endl;
}
}
}
int main(){
int n, k;
cin >> n >> k;
subset(n, k);
return 0;
}
4.3 BFS
4.3.1 BFS 和队列
hdu 1312 Red and Black
#include <bits/stdc++.h>
using namespace std;
char room[23][23];
int dir[4][2] = {
{1, 0},
{0, 1},
{-1, 0},
{0, -1}
};
int wx,hy, num;
#define CHECK(x, y)(x < wx && x >= 0 && y < hy && y >= 0)
struct node{
int x;
int y;
};
void BFS(int dx,int dy){
num = 1;
queue<node> q;
node start, next;
start.x = dx;
start.y = dy;
q.push(start);
while(!q.empty()){
start = q.front();
q.pop();
for(int i = 0;i < 4;i ++){
next.x = dir[i][0];
next.y = dir[i][1];
if(CHECK(next.x, next.y) && room[next.x][next.y] == '.'){
room[next.x][next.y] = '#';
num ++;
q.push(next);
}
}
}
}
int main()
{
int x, y, dx, dy;
while(cin >> wx >> hy){
if(wx || hy) break;
for(y = 0;y < hy;y ++)
for(x = 0;x < wx;x ++){
cin >> room[x][y];
if(room[x][y] == '@'){
dx = x;
dy = y;
}
}
num = 0;
BFS(wx, hy);
cout << num << endl;
}
return 0;
}
4.3.2八数码问题和状态图搜索
poj 1077 八数码管问题
#include <bits/stdc++.h>
using namespace std;
const int LEN = 362880;
struct node{
int state[9];
int dis;
};
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int visited[LEN];
int start[9];
int goal[9];
long int factory[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
bool Cantor(int str[], int n){
long result = 0;
for(int i = 0;i < n;i ++){
int counted = 0;
for(int j = i + 1;j < n;j ++){
if(str[i] > str[j]) counted ++;
}
result += counted * factory[n - i - 1];
}
if(!visited[result]){
visited[result] = 1;
return 1;
}else return 0;
}
int BFS(){
node head;
memcpy(head.state, start, sizeof(head.state));
head.dis = 0;
queue<node> q;
Cantor(head.state, 9);
q.push(head);
while(!q.empty()){
head = q.front();
if(memcmp(head.state, goal, sizeof(start)) == 0) return head.dis;
q.pop();
int z;
for(z = 0;z < 9;z ++){
if(!head.state[z]) break;
}
int x = z % 3;
int y = z / 3;
for(int i = 0;i < 4;i ++){
int newx = x + dir[i][0];
int newy = y + dir[i][1];
int nz = x + 3 * newy;
if(newx >= 0 && newx < 3 && newy >= 0 && newy < 3){
node newnode;
memcmp(&newnode, &head, sizeof(struct node));
swap(newnode.state[nz], newnode.state[z]);
newnode.dis ++;
if(Cantor(newnode.state, 9)) q.push(newnode);
}
}
}
return -1;
}
int main(){
for(int i = 0;i < 9;i ++) cin >> start[i];
for(int i = 0;i < 9;i ++) cin >> goal[i];
int num = BFS();
if(num != -1) cout << num << endl;
else cout << "Impossible" << endl;
}