本文共 2365 字,大约阅读时间需要 7 分钟。
有一个农夫带一只羊、一筐菜和一只狼过河.
题目没有任何输入。
题目可能有种解决方法,求出步骤最少的解决方法,
题目可能有多组解决方法,每种方法输出后要再空一行。
1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 12 using namespace std; 13 14 typedef long long ll; 15 16 queue > q; 17 int pre[20],ans[20]; 18 bool vis[20]; 19 20 bool check(int x){ 21 if((((x>>0)&1)^((x>>2)&1))==0 && (((x>>3)&1)^((x>>2)&1))==1) 22 return true; 23 if((((x>>1)&1)^((x>>2)&1))==0 && (((x>>3)&1)^((x>>2)&1))==1) 24 return true; 25 return false; 26 } 27 28 void bfs(){ 29 while(!q.empty()) 30 q.pop(); 31 int st = 0; 32 for(int i=0;i<4;i++) 33 st |= (1< cur = q.front(); 40 q.pop(); 41 // cout<<"test:"< <<' '< < 0;j--){ //j大是前面的状态 53 int a = ans[j]^ans[j-1]; 54 if(a&(1<<3)){ 55 if(a&(1<<2)){ 56 if((ans[j-1]&(1<<3)) && (ans[j-1]&(1<<2))) 57 printf("sheep_come\n"); 58 else{ 59 printf("sheep_go\n"); 60 } 61 } 62 else if(a&(1<<1)){ 63 if((ans[j-1]&(1<<3)) && (ans[j-1]&(1<<1))) 64 printf("vegetable_come\n"); 65 else{ 66 printf("vegetable_go\n"); 67 } 68 } 69 else if(a&(1<<0)){ 70 if((ans[j-1]&(1<<3)) && (ans[j-1]&(1<<0))) 71 printf("wolf_come\n"); 72 else{ 73 printf("wolf_go\n"); 74 } 75 } 76 else{ 77 if(ans[j-1]&(1<<3)){ 78 printf("nothing_come\n"); 79 } 80 else{ 81 printf("nothing_go\n"); 82 } 83 } 84 } 85 } 86 printf("succeed\n"); 87 return; 88 } 89 int x; 90 x = (cur.first^(1<<3)); 91 92 if(!check(x) && !vis[x]){ 93 q.push(make_pair(x,cur.second+1)); 94 vis[x] = true; 95 pre[x] = cur.first; 96 } 97 98 for(int i=0;i<3;i++){ 99 if(~((cur.first&(1< <<3))))100 x = ((cur.first^(1< <<3));101 if(check(x)) continue;102 if(vis[x]) continue;103 q.push(make_pair(x,cur.second+1));104 vis[x] = true;105 pre[x] = cur.first;106 }107 }108 }109 110 int main()111 {112 bfs();113 return 0;114 }
转载于:https://www.cnblogs.com/hadis-yuki/p/4835613.html