Wrong initialization. Instead of setting one of p, q, r to 1 depending on id, you set one of p, p, p.
In reality that initialization is unnecessary; you can simply start the following for-loop from i=1.
The previous calls to memset are also unnecessary. All you need is to set p = q = r = 0. The rest will be taken care of by your for-loop.