Skip to content

Instantly share code, notes, and snippets.

@niyarin
Last active August 29, 2015 14:03
Show Gist options
  • Save niyarin/816e2e235f217c4e61ea to your computer and use it in GitHub Desktop.
Save niyarin/816e2e235f217c4e61ea to your computer and use it in GitHub Desktop.
aoj_1174.py
def Search_friend(fr,pos,cl):
if pos[0] > W - 1 or pos[1] > H - 1 or pos[0] < 0 or pos[1] < 0:
return []
if note[pos[0]][pos[1]] == id:
return []
temp = []
if pos in fr :
pass
elif cl == area_data[pos[0]][pos[1]]:
temp += [pos]
else:
return []
note[pos[0]][pos[1]] = id
for m in ( (1,0),(-1,0),(0,1),(0,-1) ):
temp += Search_friend(fr,[pos[0] + m[0],pos[1] + m[1] ],cl)
return temp
def Check(pos,col,friend):
global ans
global id
id += 1
sr= Search_friend(friend,[0,0],col)
if pos >= 0:
if sr or len(friend) == H * W or pos == 4:
friend += sr
else:
return
else:
friend += sr
if pos == 4:
if ans < len(friend):
ans = len(friend)
return
if pos == 3:
Check(pos + 1,C,list(friend))
else:
for i in range(6):
Check(pos + 1,i,list(friend))
while True:
HS,WS,CS = raw_input().split(" ")
H,W,C = int(HS),int(WS),int(CS)
if (not H) and (not W) and (not C):
break
C -= 1
area_data = [[0 for h in range(H)] for w in range(W)]
note = [[0 for h in range(H)] for w in range(W)]
ans = 0
id = 1
for h in range(H):
w_line = raw_input().split(" ")
for w in range(W):
area_data[w][h] = int(w_line[w]) - 1
Check(-1,area_data[0][0],[[0,0]])
print ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment