Measurement of the branching fractions for the exclusive decays of B0 and B+ to D(*)D(*)K