We study a mobile wireless sensor network (MWSN) consisting of multiple mobile sensors or robots. Three key factors in MWSNs, sensing quality, energy consumption, and connectivity, have attracted plenty of attention, but the interaction of these factors is not well studied. To take all the three factors into consideration, we model the sensor deployment problem as a constrained source coding problem. Our goal is to find an optimal sensor deployment (or relocation) to optimize the sensing quality with a limited communication range and a specific network lifetime constraint. We derive necessary conditions for the optimal sensor deployment in both homogeneous and heterogeneous MWSNs. According to our derivation, some sensors are idle in the optimal deployment of heterogeneous MWSNs. Using these necessary conditions, we design both centralized and distributed algorithms to provide a flexible and explicit trade-off between sensing uncertainty and network lifetime. The proposed algorithms are successfully extended to more applications, such as area coverage and target coverage, via properly selected density functions. Simulation results show that our algorithms outperform the existing relocation algorithms.