There is no harm in contacting the company where you declined the offer. There is nothing unacceptable about it. How they respond may depend on the availability of the position, how did they take your rejection and who are the concerned people now (Sometimes people change fast!)
Send them a note and politely ask if the position is still available you would like yourself to be reconsidered and see if and how they respond.
If they are still interested and get back to you, most likely you will have to explain why did you not chose them in the first place. You will have to be very careful on how to answer this. It should not sound like this company is your back-up option. You may want to do some research on the company and say why you want to join them now. Also, be prepared to accept the mistake you made previously and tell them that.
In any case, be absolutely sure you want to leave A and join B before you do any of this. If you approach B second time, keep it clear that you accept it if they offer you again. If you do not, then you permanently damage your relations with B and there will not be a third chance.