标签:des style blog http color os io strong for
Description
Input
Output
Sample Input
Sample Output
1 #include<cstdio> 2 #include<string.h> 3 using namespace std; 4 int s,n,m,pre[1000005][3],ans[1000005]; 5 int vis[120][120]; 6 int ans1; 7 void bfs() 8 { 9 int first=0,end=0,a,b,c; 10 pre[end][0]=s; 11 pre[end][1]=0; 12 pre[end++][2]=0; 13 vis[s][0]=1; 14 while(first<end) 15 { 16 a=pre[first][0]; 17 b=pre[first][1]; 18 c=pre[first][2]; 19 if((a==b&&c==0)||(a==c&&b==0)||(b==c&&a==0)) 20 { 21 ans1 =ans[first]; 22 return; 23 } 24 int a1,b1,c1; 25 if(a+b>=n) 26 { 27 a1=a+b-n; 28 b1=n; 29 c1=c; 30 } 31 else{ 32 b1=a+b; 33 a1=0; 34 c1=c; 35 } 36 if(vis[a1][b1]==0) 37 { 38 vis[a1][b1]=1; 39 pre[end][0]=a1; 40 pre[end][1]=b1; 41 pre[end][2]=c1; 42 ans[end++]=ans[first]+1; 43 } 44 if(a+c>=m) 45 { 46 a1=a+c-m; 47 c1=m; 48 b1=b; 49 } 50 else{ 51 c1=a+c; 52 a1=0; 53 b1=b; 54 } 55 if(vis[a1][b1]==0) 56 { 57 vis[a1][b1]=1; 58 pre[end][0]=a1; 59 pre[end][1]=b1; 60 pre[end][2]=c1; 61 ans[end++]=ans[first]+1; 62 } 63 if(b+a>=s) 64 { 65 a1=s; 66 b1=0; 67 c1=c; 68 } 69 else{ 70 a1=a+b; 71 b1=0; 72 c1=c; 73 } 74 if(vis[a1][b1]==0) 75 { 76 vis[a1][b1]=1; 77 pre[end][0]=a1; 78 pre[end][1]=b1; 79 pre[end][2]=c1; 80 ans[end++]=ans[first]+1; 81 } 82 if(b+c>=m) 83 { 84 b1=b+c-m; 85 c1=m; 86 a1=a; 87 } 88 else{ 89 c1=b+c; 90 b1=0; 91 a1=a; 92 } 93 if(vis[a1][b1]==0) 94 { 95 vis[a1][b1]=1; 96 pre[end][0]=a1; 97 pre[end][1]=b1; 98 pre[end][2]=c1; 99 ans[end++]=ans[first]+1; 100 } 101 if(c+a>=s) 102 { 103 a1=s; 104 c1=0; 105 b1=b; 106 } 107 else{ 108 a1=a+c; 109 c1=0; 110 b1=b; 111 } 112 if(vis[a1][b1]==0) 113 { 114 vis[a1][b1]=1; 115 pre[end][0]=a1; 116 pre[end][1]=b1; 117 pre[end][2]=c1; 118 ans[end++]=ans[first]+1; 119 } 120 if(c+b>=n) 121 { 122 c1=c+b-n; 123 b1=n; 124 a1=a; 125 } 126 else{ 127 b1=b+c; 128 c1=0; 129 a1=a; 130 } 131 if(vis[a1][b1]==0) 132 { 133 vis[a1][b1]=1; 134 pre[end][0]=a1; 135 pre[end][1]=b1; 136 pre[end][2]=c1; 137 ans[end++]=ans[first]+1; 138 } 139 first++; 140 } 141 142 } 143 int main() 144 { 145 while(scanf("%d %d %d",&s,&n,&m)&&(s+n+m)) 146 { 147 if(s%2) 148 { 149 printf("NO\n"); 150 continue; 151 } 152 ans1=0; 153 memset(vis,0,sizeof(vis)); 154 bfs(); 155 if(ans1) printf("%d\n",ans1); 156 else printf("NO\n"); 157 } 158 return 0; 159 }
标签:des style blog http color os io strong for
原文地址:http://www.cnblogs.com/angledamon/p/3932579.html