In this paper a new shape optimization algorithm is presented. As a model application we consider state problems related to fluid mechanics, namely the Navier-Stokes equations for viscous incompressible fluids. The general approach to the problem is described. Next, transformations to classical optimal control problems are presented. Then, the dynamic programming approach is used and sufficient conditions for the shape optimization problem are given. A new numerical method to find the approximate value function is developed.