google code jam exercise——Rotate

之前没搞定的几道题先跳过了,来做做简单的吧。

Round 1A 2010,第一道题,Rotate,join-K的游戏,不太了解,略过。

按照题目意思,将数据先顺时针转90度,然后由于重力作用,有空白的地方会被上面的棋子落下来填上。然后如果出现一定数目的同一种颜色的棋子在一条线上,那么该颜色的一方获胜。与五子棋类似,只是它这里添加了旋转和重力的问题。

其实旋转和重力不是大问题,顺时针旋转90度,然后重力填补空白,将相当于水平方向上棋子向右落来填补空白。是否旋转了90度与最后谁获胜没有关系。那么如何填补右方的空白呢?

strip(".")可以去掉字符串中的".",但只能去掉两端的,中间的没有办法去掉,如"..BR...R."经过strip之后是"BR...R",程序中使用的是replace(".",""),就把一行中所有的"."去掉了。

接下来是检测哪一放获胜。就分四个方向分别检测好了。之前总想着遍历一遍,把四个方向的都计算出来,不过那样好麻烦。既然测试的例子都不会太大,多遍历几遍也没关系。

最后代码如下,

#!/usr/bin/python
#encoding:UTF-8
#Filename:Rotate.py

import sys
import string

def RotateAndGravity(data,n):
    for i,line in enumerate(data):
    #    print "before:",line
        line = line.replace(".","")
    #    print "after: ",line
        pad = ""
        padNum = n - len(line)
        for j in range(padNum):
            pad += "."
        data[i] = pad + line
#    print "after Rotate & Gravity:"
#    for i in xrange(n):
#        for j in xrange(n):
#            print data[i][j],
#        print "\n"

    return data


def judgeWinH(data,n,w):
    red = 0
    blue = 0
    dataLineH = [[0 for j in xrange(n)] for i in xrange(n)]
    j = 0
    for i in xrange(n):
        if data[i][j]!=".":
            dataLineH[i][j] = 1
    for i in xrange(n):
        for j in xrange(1,n):
            if data[i][j]!=".":
                if data[i][j] == data[i][j-1]:
                    dataLineH[i][j] = 1 + dataLineH[i][j-1]
                else:
                    dataLineH[i][j] = 1
                if dataLineH[i][j]>=w:
                    if data[i][j]=="R":
                        red = 1
                    elif data[i][j]=="B":
                        blue = 1

#    print "H:",red,blue
    return (red,blue)

def judgeWinV(data,n,w):
    red = 0
    blue = 0
    dataLineV = [[0 for j in xrange(n)] for i in xrange(n)]
    i = 0
    for j in xrange(n):
        if data[i][j]!=".":
            dataLineV[i][j] = 1
    for i in xrange(1,n):
        for j in xrange(n):
            if data[i][j]!=".":
                if data[i][j]==data[i-1][j]:
                    dataLineV[i][j] = 1 + dataLineV[i-1][j]
                else:
                    dataLineV[i][j] = 1
                if dataLineV[i][j]>=w:
                    if data[i][j]=="R":
                        red = 1
                    elif data[i][j]=="B":
                        blue = 1
#    print "V:",red,blue
    return (red,blue)


def judgeWinDL(data,n,w):
    red = 0
    blue = 0
    dataLineDL = [[0 for j in xrange(n)] for i in xrange(n)]
    i = 0 
    for j in xrange(n):
        if data[i][j]!=".":
            dataLineDL[i][j] = 1
    j = n-1
    for i in xrange(n):
        if data[i][j]!=".":
            dataLineDL[i][j] = 1
    for i in xrange(1,n):
        for j in xrange(n-1):
            if data[i][j]!=".":
                if data[i][j]==data[i-1][j+1]:
                    dataLineDL[i][j] = 1 + dataLineDL[i-1][j+1]
                else:
                    dataLineDL[i][j] = 1
                if dataLineDL[i][j]>=w:
                    if data[i][j]=="R":
                        red = 1
                    elif data[i][j]=="B":
                        blue = 1
#    print "DL:",red,blue
    return (red,blue)

def judgeWinDR(data,n,w):
    red = 0
    blue = 0
    dataLineDR = [[0 for j in xrange(n)] for i in xrange(n)]
    i = 0 
    for j in xrange(n):
        if data[i][j]!=".":
            dataLineDR[i][j] = 1
    j = 0 
    for i in xrange(n):
        if data[i][j]!=".":
            dataLineDR[i][j] = 1
    for i in xrange(1,n):
        for j in xrange(1,n):
            if data[i][j]!=".":
                if data[i][j]==data[i-1][j-1]:
                    dataLineDR[i][j] = 1 + dataLineDR[i-1][j-1]
                else:
                    dataLineDR[i][j] = 1

                if dataLineDR[i][j]>=w:
                    if data[i][j]=="R":
                        red = 1
                    elif data[i][j]=="B":
                        blue = 1
#    print "DR:",red,blue
    return (red,blue)



def judgeWin(data,n,w):
    (red0,blue0) = judgeWinH(data,n,w)
    (red1,blue1) = judgeWinV(data,n,w)
    (red2,blue2) = judgeWinDL(data,n,w)
    (red3,blue3) = judgeWinDR(data,n,w)
    red = 0
    blue = 0
    if red0 or red1 or red2 or red3:
        red = 1
    if blue0 or blue1 or blue2 or blue3:
        blue = 1
    an = ""
    if red and blue:
        an = "Both"
    elif red:
        an = "Red"
    elif blue:
        an = "Blue"
    else:
        an = "Neither"
    return an




inname = "input.txt"
outname = "output.txt"
if len(sys.argv)>1:
    inname = sys.argv[1]
    outname = inname.rstrip(".in")
    outname = outname + ".out"
fin = open(inname,"r")
fout = open(outname,"w")

testCaseNum = int(fin.readline().rstrip("\n"))
caseNum = 0

for caseNum in xrange(1,testCaseNum+1):
    (caseSize,winSize) = list(int(val) for val in fin.readline().rstrip("\n").split())
    data = []
    for lineNum in xrange(caseSize):
        data.append(fin.readline().rstrip("\n"))
    data = RotateAndGravity(data,caseSize)
    an = judgeWin(data,caseSize,winSize)
    answer = "Case #%d: " %(caseNum)
    answer = answer + an + "\n"
    fout.write(answer)

fin.close()
fout.close()

最后small和large的case都测试通过。

 

原文地址:https://www.cnblogs.com/Frandy/p/google_code_rotate_python.html