Created
October 10, 2012 10:06
-
-
Save shisashi/3864520 to your computer and use it in GitHub Desktop.
テトロミノ認識
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
def judge(s): | |
# 重複チェック | |
bs = set((xy / 10, xy % 10) for xy in map(int, s.split(','))) | |
if len(bs) != 4: | |
# 全部違う座標ではないときはテトロミノを成さない | |
return '-' | |
# x,y の最小値と最大値、幅、高さを求める | |
minx = min(x for (x, y) in bs) | |
miny = min(y for (x, y) in bs) | |
maxx = max(x for (x, y) in bs) | |
maxy = max(y for (x, y) in bs) | |
w = maxx - minx + 1 | |
h = maxy - miny + 1 | |
if w == h == 2: | |
# 2x2 は O | |
return 'O' | |
elif w == 1 and h == 4 or w == 4 and h == 1: | |
# 4x1 は I | |
return 'I' | |
elif not (w == 2 and h == 3 or w == 3 and h == 2): | |
# 上記以外で 2x3でない場合は、テトロミノを成さない | |
return '-' | |
# 以下、2x3 か 3x2 のとき | |
# 左下に寄せる | |
bs = set((x - minx, y - miny) for (x, y) in bs) | |
if h == 2: | |
# 3x2 は 2x3に正規化 | |
bs = set((y, x) for (x, y) in bs) | |
# 空点の座標に変換 | |
es = tuple(set((x, y) for x in range(2) for y in range(3)) - bs) | |
if es[0][0] == es[1][0]: | |
# 空欄のX座標が同じ | |
if es[0][1] == 1 or es[1][1] == 1: | |
# 真ん中が空欄(つまり、上または下と真ん中が空欄 | |
return 'L' | |
else: | |
# 両端が空欄 | |
return 'T' | |
else: | |
# 空欄のX座標が違う | |
if es[0][1] != es[1][1] and es[0][1] + es[1][1] == 2: | |
# 空欄のYが、0,2 または 2,0 | |
return 'S' | |
else: | |
return '-' | |
def test(query, expected): | |
ans = judge(query) | |
assert(ans == expected) | |
def main(): | |
test("55,55,55,55", "-") | |
test("07,17,06,05", "L") | |
test("21,41,31,40", "L") | |
test("62,74,73,72", "L") | |
test("84,94,74,75", "L") | |
test("48,49,57,47", "L") | |
test("69,89,79,68", "L") | |
test("90,82,91,92", "L") | |
test("13,23,03,24", "L") | |
test("24,22,25,23", "I") | |
test("51,41,21,31", "I") | |
test("64,63,62,65", "I") | |
test("49,69,59,79", "I") | |
test("12,10,21,11", "T") | |
test("89,99,79,88", "T") | |
test("32,41,43,42", "T") | |
test("27,16,36,26", "T") | |
test("68,57,58,67", "O") | |
test("72,62,61,71", "O") | |
test("25,24,15,14", "O") | |
test("43,54,53,42", "S") | |
test("95,86,76,85", "S") | |
test("72,73,84,83", "S") | |
test("42,33,32,23", "S") | |
test("66,57,67,58", "S") | |
test("63,73,52,62", "S") | |
test("76,68,77,67", "S") | |
test("12,11,22,01", "S") | |
test("05,26,06,25", "-") | |
test("03,11,13,01", "-") | |
test("11,20,00,21", "-") | |
test("84,95,94,86", "-") | |
test("36,56,45,35", "-") | |
test("41,33,32,43", "-") | |
test("75,94,84,95", "-") | |
test("27,39,28,37", "-") | |
test("45,34,54,35", "-") | |
test("24,36,35,26", "-") | |
test("27,27,27,27", "-") | |
test("55,44,44,45", "-") | |
test("70,73,71,71", "-") | |
test("67,37,47,47", "-") | |
test("43,45,41,42", "-") | |
test("87,57,97,67", "-") | |
test("49,45,46,48", "-") | |
test("63,63,52,72", "-") | |
test("84,86,84,95", "-") | |
test("61,60,62,73", "-") | |
test("59,79,69,48", "-") | |
test("55,57,77,75", "-") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment