字节跳动 - 2048(AC 90%)

# l = [[0, 0, 0, 2], [0, 0, 0, 2], [0, 0, 4, 8], [0 ,0, 4, 8]]
com = int(input())
l=[]
for _ in range(4):
    l.append(list(map(int,input().split())))

if com == 1:
    for i in range(3, 0, -1):
        for j in range(4):
            if l[i-1][j] == l[i][j]:
                l[i][j] = 0
                l[i-1][j] *= 2

            for k in range(3, 0, -1):
                if l[k-1][j] == 0 and l[k][j] != 0:
                    l[k-1][j] = l[k][j]
                    l[k][j] = 0
                if l[k-1][j] == l[k][j]:
                    l[k-1][j] *= 2
                    l[k][j] = 0

elif com == 2:
    for i in range(0, 3, 1):
        print(i)
        for j in range(4):
            if l[i + 1][j] == l[i][j]:
                l[i][j] = 0
                l[i + 1][j] *= 2

            for k in range(0, 3, 1):
                if l[k + 1][j] == 0 and l[k][j] != 0:
                    l[k + 1][j] = l[k][j]
                    l[k][j] = 0
                if l[k + 1][j] == l[k][j]:
                    l[k + 1][j] *= 2
                    l[k][j] = 0
elif com == 3:
    for i in range(4):
        print(i)
        for j in range(3, 0, -1):
            if l[i][j - 1] == l[i][j]:
                l[i][j] = 0
                l[i][j - 1] *= 2

            for k in range(3, 0, -1):
                if l[i][k - 1] == 0 and l[i][k] != 0:
                    l[i][k - 1] = l[i][k]
                    l[i][k] = 0
                if l[i][k - 1] == l[i][k]:
                    l[i][k - 1] *= 2
                    l[i][k] = 0

elif com == 4:
    for i in range(4):
        print(i)
        for j in range(0, 3, 1):
            if l[i][j + 1] == l[i][j]:
                l[i][j] = 0
                l[i][j + 1] *= 2

            for k in range(0, 3, 1):
                if l[i][k + 1] == 0 and l[i][k] != 0:
                    l[i][k + 1] = l[i][k]
                    l[i][k] = 0
                if l[i][k + 1] == l[i][k]:
                    l[i][k + 1] *= 2
                    l[i][k] = 0

for i in range(4):
    print(" ".join(map(str, l[i])))
剩下的10%找不到样例了,有看出来的请告知呀!
#字节跳动##笔试题目#
全部评论
这道题边界条件好重要。。。卡50卡了半天
点赞 回复 分享
发布于 2019-08-25 21:47
80%我也是哭了,写了好久,最后一题没时间了
点赞 回复 分享
发布于 2019-08-25 21:23
刚开始也是90%,后来发现有个边界出了问题,改完就ac了 def up(a): # 向上合并, 1 for i in range(3): for j in range(4): if a[i][j] == a[i + 1][j]: a[i][j] = 2*a[i][j] a[i + 1][j] = 0 for j in range(4): idxs = [] for i in range(4): if a[i][j] == 0: idxs.append(i) elif a[i][j] != 0 and len(idxs) >0: idx = idxs.pop(0) a[i][j],a[idx][j] = a[idx][j],a[i][j] idxs.append(i) return a def low(a): # 向下合并, 2 for i in range(3,0,-1): for j in range(4): if a[i][j] == a[i - 1][j]: a[i][j] = 2*a[i][j] a[i - 1][j] = 0 for j in range(4): idxs = [] for i in range(3,-1,-1): if a[i][j] == 0: idxs.append(i) elif a[i][j] != 0 and len(idxs) >0: idx = idxs.pop(0) a[i][j],a[idx][j] = a[idx][j],a[i][j] idxs.append(i) return a def left(a): #向左合并, 3 for i in range(4): for j in range(3): if a[i][j] == a[i][j+1]: a[i][j] = 2*a[i][j] a[i][j+1] =0 # 去除0 for i in range(4): idxs = [] for j in range(4): if a[i][j] == 0: idxs.append(j) elif a[i][j] != 0 and len(idxs) >0: idx = idxs.pop(0) a[i][j],a[i][idx] = a[i][idx],a[i][j] idxs.append(j) return a def right(a): for i in range(4): for j in range(3,0,-1): if a[i][j] == a[i][j-1]: a[i][j] = 2*a[i][j] a[i][j-1] =0 #去零 for i in range(4): idxs = [] for j in range(3,-1,-1): if a[i][j] == 0: idxs.append(j) elif a[i][j] != 0 and len(idxs) >0: idx = idxs.pop(0) a[i][j],a[i][idx] = a[i][idx],a[i][j] idxs.append(j) return a dx = int(input()) a = [] for _ in range(4): a.append(list(map(int, input().split()))) if dx == 1: res = up(a) elif dx ==2: res = low(a) elif dx ==3: res = left(a) elif dx == 4: res = right(a) for item in res: print(' '.join('%s' %i for i in item))
点赞 回复 分享
发布于 2019-08-25 21:31

相关推荐

评论
点赞
2
分享
牛客网
牛客企业服务