// 用一个二进制串表示状态0,表示东西或者人在河的这边
// 1表示东西或者人在河的另一边
// 比如0000表示都在起始的位置,1111表示都到了对岸
// 通过状态的转移,来找到路径
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100;
const int inf = 0x7f7f7f7f;
int a[maxn];
int n,m;
int v[4] = {8,4,2,1};
int vis[16];
int get(int x,int i){
return (x & v[i])==0;
}
bool ok(int x){
if (get(x,1)==get(x,2) && get(x,0)!=get(x,1)){
//cout << " 6 " << endl;
return false;
}
if (get(x,2)==get(x,3)&& get(x,0)!=get(x,2)){
return false;
}
return true;
}
bool judge(int l,int n){
if ((l & v[0])==(n & v[0]))
return false;
int cnt = 0;
for (int i=1;i<=3;i++){
if (get(l,i)!=get(n,i)){
cnt++;
}
}
if (cnt>1)
return false;
return true;
}
int flag = 0;
void print(int m){
for (int i=0;i<m;i++){
for (int j=0;j<4;j++){
if (a[i] & (1<<j)){
cout << 1 << " ";
}else {
cout << 0 << " ";
}
}
cout << endl;
}
}
void dfs(int x,int cnt){
// cout << "x = " << x << " cnt = " << cnt << endl;
if (flag)
return ;
if (x==15){
print(cnt);
flag = 1;
return;
}
vis[x] = 1;
for (int i=15;i>=1;i--){
if (!vis[i] && ok(i)&& judge(x,i)){
a[cnt] = i;
dfs(i,cnt+1);
a[cnt] = 0;
}
}
}
void init(){
memset(vis,0,sizeof(vis));
dfs(0,1);
}
int main(){
init();
}
农夫过河问题 简单的搜索
最新推荐文章于 2021-11-28 17:53:13 发布