2주차 스터디

임정우·2023년 3월 3일

코딩테스트

목록 보기
8/10

빙고

my = [list(map(int, input().split())) for _ in range(5)]
mod = [list(map(int, input().split())) for _ in range(5)]
bingo = [[0 for j in range(5)] for i in range(5)]

def find_item(tofind):
    for i in range(5):
        for j in range(5):
            if my[i][j] == tofind:
                return i,j
def is_line():
    rtn = 0
    for i in range(5):
        count=0
        for j in range(5):
            count += bingo[i][j]
        if count == 5:
            rtn += 1
            #print("가로")
            
    for j in range(5):
        count=0
        for i in range(5):
            count += bingo[i][j]
        if count == 5:
            rtn += 1
            #print("세로")
            
    count = 0
    for i in range(5):
        count += bingo[i][i]
        if count == 5:
            rtn += 1
            #print("왼쪽대각선")
    count = 0
    for i in range(5):
        j = 4 - i
        count += bingo[i][j]
        if count == 5:
            rtn += 1
            #print("오른쪽대각선")
    return rtn

ans = 0
flag = False
for i in range(5):
    for j in range(5):
        ans += 1
        x,y = find_item(mod[i][j])
        bingo[x][y] = 1
        if is_line() >= 3:
            print(ans)
            #for m in range(5):
                #print(bingo[m])
            flag = True
            break
    if flag == True:
        break

1913 달팽이

import math
n = int(input())
tofind = int(input())
bingo = [[0 for i in range(n)] for j in range(n)]
item = n*n

ans_x = 0
ans_y = 0


def go_down(x,y,item):
    global ans_x, ans_y
    bingo[x][y] = item
    while True:
        x += 1
        if x >= n or bingo[x][y] != 0:  
            return x-1, y,item
        item -= 1
        bingo[x][y] = item
        
        if item == tofind:
            ans_x, ans_y = x,y

def go_up(x,y,item):
    global ans_x, ans_y
    bingo[x][y] = item
    while True:
        x -= 1
        if x < 0 or bingo[x][y] != 0:
            return x+1,y,item
        item -= 1
        bingo[x][y] = item
        
        if item == tofind:
            ans_x, ans_y = x,y


def go_right(x,y,item):
    global ans_x, ans_y
    bingo[x][y] = item
    while True:
        y += 1
        if y >= n or bingo[x][y] != 0:
            return x,y-1,item
        item -= 1
        bingo[x][y] = item
        
        if item == tofind:
            ans_x, ans_y = x,y

def go_left(x,y,item):
    global ans_x, ans_y
    bingo[x][y] = item
    while True:
        y -= 1
        if y < 0 or bingo[x][y] != 0:
            return x,y+1,item
        item -= 1
        bingo[x][y] = item
        
        if item == tofind:
            ans_x, ans_y = x,y
    
x = 0
y = 0
loop = math.ceil(n)

for _ in range(loop):
    x,y,item = go_down(x,y,item)
    x,y,item = go_right(x,y,item)
    x,y,item = go_up(x,y,item)
    x,y,item = go_left(x,y,item)

for i in range(n):
    for j in range(n):
        print(bingo[i][j], end=" ")
    print()
print(ans_x + 1,ans_y + 1)

10703 유성

import copy

def find_highest_earth(p):
    for q in range(1,y):
        if map[q][p] == "#":
            return q
    return y

def find_lowest_star(p):
    for q in reversed(range(0,y)):
        if map[q][p] == "X":
            return q
    return 0
def is_star(p):
    for q in range(0,y):
        if map[q][p] == "X":
            return True
    return False

y,x = input().split(" ")
x = int(x)
y = int(y)
map = [list(map(str, input().split())) for _ in range(y)]
star = []

for i in range(y):
    strin = list(map[i][0])
    map[i] = strin

ran_star = []
for i in range(x):
    if is_star(i) == True:
        ran_star.append(i)
        
distance = []
for i in ran_star:
#    print(find_highest_earth(i), find_lowest_star(i))
    distance.append(find_highest_earth(i) - find_lowest_star(i) - 1)
min = min(distance)

for i in range(y):
    for j in range(x):
        if map[i][j] == "X":
            map[i][j] = "."
            star.append((i,j))

for xx,yy in star:
    map[xx+min][yy] = "X"
for i in range(y):
    for j in range(x):
        print(map[i][j], end="")
    print()

2615 오목

omok = [list(map(int,input().split())) for _ in range(19)]

def is_side(stone):
    count = 0
    for i in range(19):
        count = 0
        for j in range(19):
            if omok[i][j] == stone:
                count += 1
            else:
                count = 0
            if count == 5:
                if j+1 < 19 and omok[i][j+1] == stone:
                    pass
                else:
                    return (True,j-4,i)
    return (False,0,0)

def is_above(stone):
    count = 0
    for i in range(19):
        count = 0
        for j in range(19):
            if omok[j][i] == stone:
                count += 1
            else:
                count = 0                
            if count == 5:
                if j+1 < 19 and omok[j+1][i] == stone:
                    pass
                else:
                    return (True,j-4,i)
    return (False,0,0)

def is_diagonal(stone):
    x = 0
    y = 0
    while True:
        count = 0
        nx = x
        ny = y
        while True:
            if omok[nx][ny] == stone:
                count += 1
            else:
                count = 0
            if count == 5:
                if nx+1 < 19 and ny + 1 <19 and omok[nx+1][ny+1] == stone:
                    pass
                else:
                    return (True,nx-4,ny-4)
            
            nx += 1
            ny += 1
            if nx >= 19 or ny >= 19:
                break
        y += 1
        if y >= 19:
            break
    x = 1
    y = 0
    while True:
        count = 0
        nx = x
        ny = y
        while True:
            if omok[nx][ny] == stone:
                count += 1
            else:
                count = 0
            if count == 5:
                
                if nx + 1 < 19 and ny + 1 <19 and omok[nx+1][ny+1] == stone:
                    pass
                else:
                    return (True,nx-4,ny-4)
                        
            nx += 1
            ny += 1
            if nx >= 19 or ny >= 19:
                break
        x += 1
        if x >= 19:
            break
    return (False,0,0)

def is_diagonal_reverse(stone):
    x = 18
    y = 0
    while True:
        count = 0
        nx = x
        ny = y
        while True:
            if omok[nx][ny] == stone:
                count += 1
            else:
                count = 0            
            if count == 5:
                if nx -1 >= 0 and ny + 1 < 19 and omok[nx-1][ny+1] == stone:
                    pass
                else:
                    return (True,nx+4,ny-4)
            nx -= 1
            ny += 1
            if nx < 0 or ny > 18:
                break
        x -= 1
        if x < 0:
            break
    x = 18
    y = 1
    while True:
        count = 0
        nx = x
        ny = y
        while True:
            if omok[nx][ny] == stone:
                count += 1
            else:
                count = 0   
                         
            if count == 5:
                if nx -1 >= 0 and ny + 1 < 19 and omok[nx-1][ny+1] == stone:
                    pass
                else:
                    return (True,nx+4,ny-4)
            
            nx -= 1
            ny += 1
            if nx < 0 or ny > 18:
                break
        y += 1
        if y >= 19:
            break
    return (False,0,0)

flag = False
for i in range(1,3):
    
    flag,x,y = is_above(i)
    if flag == True:
        print(i)
        print(x+1,y+1)
        break
    flag,x,y = is_side(i)
    if flag == True:
        print(i)
        print(y+1,x+1)
        break    
    flag,x,y = is_diagonal(i)
    if flag == True:
        print(i)
        print(x+1,y+1)
        break
    flag,x,y = is_diagonal_reverse(i)
    if flag == True:
        print(i)
        print(x+1,y+1)
        break 
if flag == False:
    print(0)
profile
경희대학교 소프트웨어융합학과

0개의 댓글