@Eeap
velog
@Eeap
전체 방문자
오늘
어제
  • 전체 (168)
    • osam (1)
    • Cloud (21)
      • Docker (2)
      • AWS (13)
    • AI & Data (7)
    • Algorithm (76)
      • Baekjoon (75)
      • Codeforces (1)
    • Language (18)
      • Java (18)
    • Back-end (17)
      • Spring (3)
      • JSP & Servlet (12)
      • Go (2)
    • 일상 (4)
    • 기타 (8)
    • git (1)
    • Infra (9)
      • Apache Kafka (5)
      • Kubernetes (4)
      • 기타 (0)

블로그 메뉴

  • 홈
  • 태그

공지사항

인기 글

태그

  • 오블완
  • 인터페이스
  • invokemodel api
  • java
  • AWS CodeArtifact
  • Python
  • SageMaker
  • bedrock agent
  • 심폴릭링크
  • knowledge bases
  • flink
  • 티스토리챌린지
  • bedrock api
  • CLASS
  • converse api
  • sagemaker unified studio
  • bedrock
  • AWS CodeStar
  • Agent
  • AWS CodeCatalyst

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
@Eeap

velog

Algorithm/Baekjoon

3085번 파이썬

2022. 3. 13. 18:34
반응형
두번이나 문제를 잘못보고 풀어서 여러 번 풀어서 나온 답ㅎ!,,,
문제의 의도는
1. 인접한 두칸을 고름
2. 두칸의 사탕을 바꿈
3. 사탕 배열(행, 열) 중 연속으로 긴 사탕이 있는 사탕을 모두 먹음
ex( pyyyp인 max인 경우 y세개를 먹고 최대 사탕 개수는 3개가 됨) 
copy를 안쓰고 다시 원래 자리로 원위치 시키는 방법도 있지만 copy하는 방법으로 푸니까 코드가 길어짐,,
copy는 copy 모듈을 이용해서 shallow copy가 아닌 deepcopy로 구현(다른 copy인 경우 idx 값을 바꾸면 복사한 ary 값도 바뀌기 때문에)
 
import sys
input = sys.stdin.readline
import copy

def counting(mat,n):
    res=0
    for i in range(n):
        cnt = 1
        for j in range(n-1):
            if mat[i][j] == mat[i][j+1]:
                cnt+=1
            else:
                if cnt > res:
                    res=cnt
                cnt=1
        if cnt > res:
            res=cnt

        cnt=1
        for j in range(n-1):
            if mat[j][i] == mat[j+1][i]:
                cnt+=1
            else:
                if cnt > res:
                    res=cnt
                cnt=1
        if cnt > res:
            res=cnt
    return res

n = int(input())
ary=[list(input()) for i in range(n)]
res=[]
ismax=False
for row in range(n-1):
    if ismax:
        break
    for col in range(n-1):
        temp_ary=copy.deepcopy(ary)
        #값 바꾸기
        temp = temp_ary[row+1][col]
        temp_ary[row+1][col]=temp_ary[row][col]
        temp_ary[row][col] = temp
        m_candy = counting(temp_ary,n)
        res.append(m_candy)
        if m_candy == n:
            ismax = True
            break

        temp_ary=copy.deepcopy(ary)
        temp = temp_ary[row][col+1]
        temp_ary[row][col+1]=temp_ary[row][col]
        temp_ary[row][col] = temp
        m_candy = counting(temp_ary,n)
        res.append(m_candy)
        if m_candy == n:
            ismax = True
            break

    #col가 n일때
    temp_ary=copy.deepcopy(ary)
    #값 바꾸기
    temp = temp_ary[row+1][col]
    temp_ary[row+1][col]=temp_ary[row][col]
    temp_ary[row][col] = temp
    m_candy = counting(temp_ary,n)
    res.append(m_candy)
    if m_candy == n:
        ismax = True
        break


#row가 n일때
if not ismax:
    for idx in range(n-1):
        temp_ary = copy.deepcopy(ary)
        temp = temp_ary[n-1][idx]
        temp_ary[n-1][idx] = temp_ary[n-1][idx+1]
        temp_ary[n-1][idx+1] = temp
        m_candy = counting(temp_ary, n)
        res.append(m_candy)
        if m_candy == n:
            break
print(max(res))

 

반응형
저작자표시 (새창열림)

'Algorithm > Baekjoon' 카테고리의 다른 글

1107번 파이썬  (0) 2022.03.14
1476번 파이썬  (0) 2022.03.13
2309번 파이썬  (0) 2022.03.12
6588번 파이썬  (0) 2022.03.12
13458번 파이썬  (0) 2022.03.12
    'Algorithm/Baekjoon' 카테고리의 다른 글
    • 1107번 파이썬
    • 1476번 파이썬
    • 2309번 파이썬
    • 6588번 파이썬
    @Eeap
    @Eeap

    티스토리툴바