题目链接
题意就不解释了。
总之这很暴力,就6种倒法
(1)s != 0 , s->n;
(2)s != 0 , s->m;
(3)n != 0 , n->s;
(4)n != 0 , n->m;
(5)m != 0 , m->n;
(6)m != 0 , m->s;
bfs一遍就好了。
#include#include #include #include #include #define inf 0X3f3f3f3fusing namespace std;int s , n , m , ans , vis[110][110][110];struct TnT { int x , y , z , step;};int bfs(int xx , int yy , int zz , int step) { memset(vis , 0 , sizeof(vis)); TnT g; g.x = xx , g.y = yy , g.z = zz , g.step = step; queue q; q.push(g); vis[xx][yy][zz] = 1; while(!q.empty()) { TnT gg = q.front(); int x = gg.x , y = gg.y , z = gg.z; if((x == y && x == s / 2) || (x == z && x == s / 2) || (z == y && z == s / 2)) { return gg.step; } if(x != 0) { if(y != n && !vis[max(0 , x - (n - y))][min(n , y + x)][z]) { TnT gl = gg; gl.x = max(0 , x - (n - y)) , gl.y = min(n , y + x) , gl.z = z , gl.step++;; vis[max(0 , x - (n - y))][min(n , y + x)][z] = 1; q.push(gl); } if(z != m && !vis[max(0 , x - (m - z))][y][min(m , z + x)]) { TnT gl = gg; gl.x = max(0 , x - (m - z)) , gl.y = y , gl.z = min(m , z + x) , gl.step++; vis[max(0 , x - (m - z))][y][min(m , z + x)] = 1; q.push(gl); } } if(y != 0) { if(x != s && !vis[min(s , x + y)][max(0 , y - (s - x))][z]) { TnT gl = gg; gl.x = min(s , x + y) , gl.y = max(0 , y - (s - x)) , gl.z = z , gl.step++;; vis[min(s , x + y)][max(0 , y - (s - x))][z] = 1; q.push(gl); } if(z != m && !vis[x][max(0 , y - (m - z))][min(m , z + y)]) { TnT gl = gg; gl.x = x , gl.y = max(0 , y - (m - z)) , gl.z = min(m , z + y) , gl.step++; vis[x][max(0 , y - (m - z))][min(m , z + y)] = 1; q.push(gl); } } if(z != 0) { if(x != s && !vis[min(s , x + z)][y][max(0 , z - (s - x))]) { TnT gl = gg; gl.x = min(s , x + z) , gl.y = y , gl.z = max(0 , z - (s - x)) , gl.step++; vis[min(s , x + z)][y][max(0 , z - (s - x))] = 1; q.push(gl); } if(y != n && !vis[x][min(n , y + z)][max(0 , z - (n - y))]) { TnT gl = gg; gl.x = x , gl.y = min(n , y + z) , gl.z = max(0 , z - (n - y)) , gl.step++; vis[x][min(n , y + z)][max(0 , z - (n - y))] = 1; q.push(gl); } } q.pop(); } return -1;}int main(){ while(scanf("%d%d%d" , &s , &n , &m) != EOF) { if(s == 0) { break; } if(s % 2 != 0) { printf("NO\n"); continue; } else { int a[3]; memset(vis , 0 , sizeof(vis)); a[0] = s , a[1] = n , a[2] = m; sort(a , a + 3); s = a[2] , n = a[1] , m = a[0]; ans = bfs(s , 0 , 0 , 0); if(ans != -1) printf("%d\n" , ans); else { printf("NO\n"); } } } return 0;}