I solved the issue with this code, and I hope it costs less CPU than the method using maxOps.
    if (creep.moveTo(pos) == ERR_NO_PATH) {
      if (this.pos.x <= 1) {
        if (this.move(RIGHT) == OK) {
          return
        }
      }
      if (this.pos.x >= 48) {
        if (this.move(LEFT) == OK) {
          return
        }
      }
      if (this.pos.y <= 1) {
        if (this.move(BOTTOM) == OK) {
          return
        }
      }
      if (this.pos.y >= 48) {
        if (this.move(TOP) == OK) {
          return
        }
      }
    }
It seems like when a creep is not on the room border, the creep can find a path.