We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
# Enter your code here. Read input from STDIN. Print output to STDOUTread_ints=lambda:list(map(int,input().split()))defsolve(rect,mat_len):l=len(rect)ret=[0foriinrange(l)]pos=0foriinrange(l):whileTrue:find=Falseforjinrange(i,l):ifrect[j][pos]==1:rect[i],rect[j]=rect[j],rect[i]find=Truebreakiffind:breakelse:pos+=1ifpos==l:breakifpos==l:breakforjinrange(i+1,l):ifrect[j][pos]:forkinrange(pos,l+1):rect[j][k]^=rect[i][k]pos+=1ifpos==l:breakrect.reverse()foriinrange(l):pos=-1forjinrange(l+1):ifrect[i][j]:pos=jbreakifpos==l:print('Impossible')returnifpos==-1:continueret[pos]=rect[i][l]forjinrange(i+1,l):ifrect[j][pos]:forkinrange(pos,l+1):rect[j][k]^=rect[i][k]print('Possible')print(sum(ret))k=0foriinrange(mat_len):forjinrange(mat_len):if(ret[k]):print(i,j)k+=1if__name__=="__main__":[r,d]=read_ints()mat=[]foriinrange(r):mat.append(read_ints())rect=[]k=0foriinrange(r):forjinrange(r):rect.append([])foriter_iinrange(r):foriter_jinrange(r):ifabs(i-iter_i)+abs(j-iter_j)<=d:rect[k].append(1)else:rect[k].append(0)ifmat[i][j]:rect[k].append(1)else:rect[k].append(0)k+=1solve(rect,r)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Find The Operations
You are viewing a single comment's thread. Return to all comments →
Python 3 solution