标签:style blog http color strong os
2 6 3 1 4 1 1 9 3 2 7 1 1
3 -1
解题:BFS。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <ctype.h> 8 #include <cmath> 9 #include <algorithm> 10 #define LL long long 11 using namespace std; 12 const int maxn = 100000; 13 struct CUP { 14 int d[3],step; 15 }; 16 CUP cup[maxn],target,initial; 17 int head,tail; 18 bool vis[100][100][100]; 19 int bfs() { 20 tail = head = 0; 21 tail++; 22 CUP temp,temp2; 23 int t,i,j; 24 while(head < tail) { 25 temp = cup[head++]; 26 if(temp.d[0] == target.d[0] && temp.d[1] == target.d[1] && temp.d[2] == target.d[2]) { 27 return temp.step; 28 } 29 for(i = 0; i < 3; i++) { 30 for(j = 0; j < 3; j++) { 31 if((i^j) && temp.d[i] && temp.d[j] < initial.d[j]) { 32 t = min(initial.d[j] - temp.d[j],temp.d[i]); 33 temp2 = temp; 34 temp2.step++; 35 temp2.d[i] -= t; 36 temp2.d[j] += t; 37 if(!vis[temp2.d[0]][temp2.d[1]][temp2.d[2]]) { 38 vis[temp2.d[0]][temp2.d[1]][temp2.d[2]] = true; 39 cup[tail++] = temp2; 40 } 41 } 42 } 43 } 44 } 45 return -1; 46 } 47 int main() { 48 int kase,temp,i,mx,index; 49 scanf("%d",&kase); 50 while(kase--) { 51 mx = -1; 52 for(i = 0; i < 3; i++) { 53 scanf("%d",&initial.d[i]); 54 if(initial.d[i] > mx) mx = initial.d[index = i]; 55 } 56 for(i = 0; i < 3; i++) 57 scanf("%d",&target.d[i]); 58 memset(cup,0,sizeof(cup)); 59 memset(vis,false,sizeof(vis)); 60 cup[0].d[index] = mx; 61 cup[0].step = 0; 62 vis[cup[0].d[0]][cup[0].d[1]][cup[0].d[2]] = true; 63 if(target.d[0] + target.d[1] + target.d[2] != mx){ 64 puts("-1");continue; 65 }else printf("%d\n",bfs()); 66 } 67 return 0; 68 }
标签:style blog http color strong os
原文地址:http://www.cnblogs.com/crackpotisback/p/3854598.html