fork download
  1. def f(B,S,o=0):
  2. if[]==S:print'\n'.join(B);exit()
  3. s=S[0]
  4. for i in r:
  5. for j in R(Z-s+1):
  6. if(B[i][j]in' '+'>v'[o])*(B[i][j+s-1]in' '+'<^'[o])*({' ','-|'[o]}>=set(B[i][j+1:j+s-1]))*all(B[x][y]in'# 'for x,y in [(x,y)for y in R(j-1,j+s+1)for x in i-1,i+1]+[(i,j-1),(i,j+s)]if 0<=x<Z>y>=0):q=B[:];q[i]=q[i][:j]+'*'*s+q[i][j+s:];q=(q,t(q))[o];any((t(q)+q)[k].count('*')>m[k]for k in R(Z+Z))or f(q,S[1:])
  7. o or f(t(B),S,1)
  8. y=raw_input;S=[];s=str.split
  9. for i in s(y()):u,v=map(int,s(i,'x'));S+=[u]*v
  10. m=map(int,s(y())+s(y()));Z=input();R=range;r=R(Z);B=[y()for _ in r];J=''.join;t=lambda x:map(J,zip(*x))
  11. f(B,S[:len(S)-J(B).count('*')])
Success #stdin #stdout 0.02s 7740KB
stdin
5x1 4x1 2x1 1x2
1 2 3 3 2 2
0 5 0 4 0 4
6
#     
  -   
      
      
 #    
   <  
stdout
#     
 *****
      
 **** 
 #    
* ** *