标签:bsp name closed mem amp close eve 相同 分享图片
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<memory.h> 5 #include<string> 6 #define INF 0x3f3f3f3f 7 using namespace std; 8 9 const int indexs[8][7]= 10 { 11 {0,2,6,11,15,20,22}, 12 {1,3,8,12,17,21,23}, 13 {10,9,8,7,6,5,4}, 14 {19,18,17,16,15,14,13}, 15 {23,21,17,12,8,3,1}, 16 {22,20,15,11,6,2,0}, 17 {13,14,15,16,17,18,19}, 18 {4,5,6,7,8,9,10}, 19 }; 20 const int ni[]={5,4,7,6,1,0,3,2,-1}; 21 const int center[]={6,7,8,11,12,15,16,17}; 22 int min(int a,int b) {return a>b ? b : a;} 23 int max(int a,int b) {return a<b ? b : a;} 24 char map[24]; 25 char route[101]; 26 bool hasSolution; 27 28 void pull(int op) 29 { 30 int temp=map[indexs[op][0]]; 31 for (int i=0;i<7-1;i++) 32 { 33 map[indexs[op][i]]=map[indexs[op][i+1]]; 34 } 35 map[indexs[op][6]]=temp; 36 } 37 38 int h() 39 { 40 int cnt[3]={0,0,0}; 41 int maxCnt= -1; 42 for(int i=0;i<8;i++) 43 { 44 cnt[map[center[i]]-1]++; 45 maxCnt=max(maxCnt,cnt[map[center[i]]-1]);//作用和if.m>cnt.m=cnt[]一样; 46 } 47 return 8-maxCnt; 48 } 49 50 void dfs(int depth,int lastop,int maxdepth ) 51 { 52 if(hasSolution) return ; 53 if(h()==0) 54 { 55 hasSolution=true; 56 route[depth]=‘\0‘; 57 printf("%s\n%d\n",route, map[center[0]]); 58 return; 59 } 60 if(depth>maxdepth||depth+h()>maxdepth) return; 61 for(int nextop=0;nextop<8;nextop++) 62 { 63 if(nextop != ni[lastop]) 64 { 65 pull(nextop); 66 route[depth]=nextop+‘A‘; 67 dfs(depth+1,nextop,maxdepth); 68 pull(ni[nextop]); 69 } 70 } 71 } 72 73 int main() 74 { 75 int x; 76 while(cin>>x&&x) 77 { 78 map[0]=x; 79 for(int i=1;i<24;i++) 80 { 81 cin>>map[i]; 82 if(map[i]==0) return 0; 83 } 84 hasSolution = false; 85 if(h()==0) 86 { 87 printf("No moves needed\n%d\n", map[center[0]]); 88 continue; 89 } 90 for(int depth =1;!hasSolution;depth++) 91 { 92 dfs(0,8,depth); 93 } 94 } 95 return 0; 96 }
AC的代码。。求哪里不同啊。。
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <string> 5 #include <memory.h> 6 using namespace std; 7 #define INF 0x3f3f3f3f 8 const int indexs[8][7] = { // 每种操作变动的下标 9 { 0,2,6,11,15,20,22 }, //AF 10 { 1,3,8,12,17,21,23 }, //BE 11 { 10,9,8,7,6,5,4 }, //CH 12 { 19,18,17,16,15,14,13 }, //DG 13 { 23,21,17,12,8,3,1 }, //EB 14 { 22,20,15,11,6,2,0 }, //FA 15 { 13,14,15,16,17,18,19 }, //GD 16 { 4,5,6,7,8,9,10 }, //HC 17 }; 18 const int reverseOp[] = { 5,4,7,6,1,0,3,2,-1 }; // A-H的逆操作 19 const int center[] = { 6,7,8,11,12,15,16,17 }; // 中心八点下标 20 int min(int a, int b) { return a>b ? b : a; } 21 int max(int a, int b) { return a<b ? b : a; } 22 char map[24]; 23 char route[101]; // 操作序列 24 bool hasSolution; 25 26 void pull(int op) { 27 int tmp = map[indexs[op][0]]; 28 for (int i = 0; i < 7 - 1; i++) { 29 map[indexs[op][i]] = map[indexs[op][i + 1]]; 30 } 31 map[indexs[op][6]] = tmp; 32 } 33 34 int h() { 35 int cnt[3] = { 0,0,0 }; // 计数 1, 2, 3 36 int maxCnt = -1; 37 for (int i = 0; i < 8; i++) { 38 cnt[map[center[i]] - 1]++; 39 maxCnt = max(maxCnt, cnt[map[center[i]] - 1]); 40 } 41 return 8 - maxCnt; 42 } 43 44 void d(int depth, int lastOp, int maxDepth) {// 当前深度、到达当前深度所做的操作 45 if (hasSolution)return; 46 if (h() == 0) { // 中心都相同了 47 hasSolution = true; 48 route[depth] = ‘\0‘; 49 printf("%s\n%d\n",route, map[center[0]]); 50 return; 51 } 52 if (depth > maxDepth || depth + h() > maxDepth) return; // 可行性剪枝 53 for (int nextOp = 0; nextOp < 8; nextOp++) { 54 if (nextOp != reverseOp[lastOp]) { //操作不互逆 55 pull(nextOp); 56 route[depth] = nextOp + ‘A‘; 57 d(depth + 1, nextOp, maxDepth); 58 pull(reverseOp[nextOp]); //还原 59 } 60 } 61 } 62 int main() { 63 while (1) { 64 for (int i = 0; i < 24; i++) { 65 scanf("%d", &map[i]); 66 if (map[i] == 0) return 0; 67 } 68 hasSolution = false; 69 if (h() == 0) { 70 printf("No moves needed\n%d\n", map[center[0]]); 71 continue; 72 } 73 for (int depth = 1; !hasSolution; depth++) { // 迭代加深 74 d(0, 8, depth); 75 } 76 } 77 return 0; 78 }
标签:bsp name closed mem amp close eve 相同 分享图片
原文地址:https://www.cnblogs.com/jzzb/p/9372833.html