[SWEA]2819_격자판의 숫자 이어붙이기

Cornflower blue·2022년 3월 25일
0

SWEA 풀이

목록 보기
1/1

정답은 하단에

  • 처음 접근 방법(오답)
    처음에는 BFS로 접근했다. 아직 DFS와 BFS의 개념과 사용쓰임에 대해 잘 이해하지 못했기 때문이다.
from collections import deque

def BFS(si, sj):
    global str_                                         # 전역변수
    global lst                                          # 전역변수
    q = deque([])                                       # 덱 사용
    q.append([si, sj])                                  # 초깃값 입력(원점에서 시작한다.), (0,0)

    while q:                                            # 덱 안에 좌표가 없을 때 까지 반복한다.
        ci, cj= q.popleft()                             # 덱에 들어있는 가장 왼쪽 좌표값을 꺼낸다.
        str_ = arr[ci][cj]                              # 7자리의 숫자의 초깃값으로 현재 위치를 설정한다.
        if (ci==3) and (cj==3):                         # 마지막 좌표까지 도달할 경우 종료한다.
            return lst                                  # 7자리의 숫자가 담긴 리스트를 반환한다.
        for di, dj in ([-1,0], [1,0], [0,-1], [0, 1]):  # 동서남북 네 방향으로 이동한다. ★ 여섯번 이동이 고려되지 않았다.
            ni, nj = ci + di, cj + dj                   # 이동한 좌표 값
            if 0<=ni<4 and 0<=nj<4:                     # 좌표가 범위 내에 있는지 확인한다.
                str_ += arr[ni][nj]                     # 범위 내에 있을 경우 해당 좌표값을 문자열에 더해준다.
                q.append([ni,nj])                       # 나중에 해당 좌표로 이동할 수 있도록 이동한 좌표 값을 덱에 더해준다.
        lst += [str_]                                   # 여섯번의 이동을 끝낸 후 7자리의 숫자를 리스트에 담아준다.


T = int(input())

for test_case in range(1, T+1):
    arr = [list(input().split()) for _ in range(4)]
    print(arr)
    str_ = ''                                           # 7자리의 숫자
    lst = []                                            # 7자리의 숫자가 담긴 리스트
    ans = len(lst)                                      # 7자리의 숫자가 담긴 리스트에서 set함수를 이용하여 중복을 제거한 값을 담으려고 한다.
    BFS(0,0)
    # print(lst)
    # print(f'#{test_case} {ans}')

개념부터 다시

BFS

  • 한번에 한 단계씩 발전하여,
  • 목적지까지 가는데 걸리는 시간, 특정 목적지까지 갈 수 있느냐의 여부, 어디가 제일 좋은 위치인가에 대한 판단이 가능하다.
  • 초기에 queue나 visited를 생성한다.

DFS/백트래킹

  • 가능한 모든 경우를 처리해서 답을 찾는 문제
  • 시간 초과를 고려해야 한다.
  • 종료 조건을 잘 체크해야 한다.
  • 가능한 모든 경우를 표현하는 효율적인 방법은 Tree이다.

'격자판의 숫자 이어붙이기'또한 가능한 모든 경우를 처리해서 답을 찾는 문제이므로 DFS를 이용하여 풀어야 한다.


정답

DFS로 다시 짜서 통과할 수 있었다.

def DFS(ci, cj, str_):                              # 초기 좌표값과 일곱자리의 숫자를 담을 문자열을 전달받는다.
    global set_                                     # 7자리의 숫자들을 담을 변수. set을 통해 중복을 제거한다.

    # 종료조건
    if len(str_) == 7:                              # 일곱자리의 숫자가 완성되면 종료한다.
        set_.add(str_)                              # 이 떄, set_ 에 해당 문자열을 더해준다.
        return
    # 함수호출 (4방향 호출)
    for di, dj in ([-1,0], [1,0], [0,-1], [0, 1]):  # 상, 하, 좌, 우 방향
        ni = ci + di                                # 새로운 좌표값(행)
        nj = cj + dj                                # 새로운 좌표값(열)
        if 0<=ni<4 and 0<=nj<4:
            # str_ += arr[ni][nj]                   # ★ 1, 11 식으로 2번 누적되서 문제였던 코드
            DFS(ni, nj, str_ + arr[ni][nj])


T = int(input())

for test_case in range(1, T+1):
    arr = [list(input().split()) for _ in range(4)] # 격자판의 정보를 입력받는다.
    str_ = ''                                       # 7자리의 숫자를 담을 변수(문자열로 취급)
    set_ = set()                                    # 7자리의 숫자들을 담을 변수. set을 통해 중복을 제거한다
    cnt = 0
    for i in range(4):
        for j in range(4):
            DFS(i, j, arr[i][j])                    # DFS 함수에 원점의 좌표값과 일곱자리의 숫자를 담을 문자열을 전달한다.
    print(f'#{test_case} {len(set_)}')
profile
무언가를 만들어낸다는 것은 무척이나 즐거운 일입니다.

1개의 댓글

comment-user-thumbnail
2024년 8월 23일

太阳城 | https://1433123.com/
太阳城赌场 | https://1433123.com/home.html
太阳城娱乐官网 | http://www.1433123.com/
太阳城APP | http://www.1433123.com/home.html
太阳城娱乐城 | https://1433115.com/
太阳城娱乐 | https://1433115.com/%E7%9C%9F%E4%BA%BA%E8%A6%96%E8%A8%8A.html
太阳城集团 | https://1433115.com/sport.html
太阳城官网 | https://1433115.com/%E5%BD%A9%E7%A5%A8.html
太阳城app下载 | https://1433115.com/%E6%A3%8B%E7%89%8C.html
太阳城集团官网 | https://1433115.com/%E9%9B%BB%E5%AD%90%E9%81%8A%E6%88%B2.html
太阳城娱乐开户 | https://1433115.com/promotion.html
太阳城娱乐代理 | https://1433115.com/%E9%97%9C%E6%96%BC%E5%A8%9B%E6%A8%82%E5%9F%8E.html
线上太阳城 | https://1433115.com/contact-us.html
线上太阳城开户 | https://1433115.com/privacy-policy.html
线上太阳城代理 | https://1433115.com/terms-of-use.html
线上太阳城官网 | https://1433116.com/539crack.html
线上太阳城娱乐 | https://1433116.com/96m.html
太阳城澳门 | https://1433116.com/bng-slot-houyi.html
澳门太阳城 | https://1433116.com/bng-slot-jungle.html
澳门太阳城集团 | https://1433116.com/casino-168.html
澳门太阳城集团APP | https://1433116.com/casino-experience.html
澳门太阳城洗钱案 | https://1433116.com/chess-game.html
澳门太阳城周焯华 | https://1433116.com/fm-evaluate.html
澳门太阳城娱乐 | https://1433116.com/fm-guaranteed-withdrawal.html
澳门太阳城赌场 | https://1433116.com/greyhound-racing.html
澳门太阳城官网 | https://1433116.com/horse-racing.html
澳门太阳城网址 | https://1433116.com/
澳门太阳城博彩 | https://1433116.com/landing.html
澳门太阳城网络赌博平台 | https://1433116.com/live-casino.html
澳门太阳城娱乐城 | https://1433116.com/login-tutorial.html
澳门太阳城APP | https://1433116.com/lottery-online-vs-store.html
澳门太阳城集团官网 | https://1433116.com/mahjong.html
澳门太阳城开户 | https://1433116.com/new-post.html
澳门太阳城代理 | https://1433116.com/ob-game.html
澳门太阳城娱乐开户 | https://1433116.com/sale.html
澳门太阳城娱乐代理 | https://1433116.com/slot-machine-online-vs-store-2.html
澳门太阳城网络博彩 | https://1433116.com/slotmachine-2.html
澳门太阳城在线开户 | https://1433116.com/sport.html
澳门太阳城在线代理 | https://1433119.com/2.html
澳门太阳城在线娱乐 | https://1433119.com/2023wbc.html
澳门太阳城在线官网 | https://1433119.com/3.html
澳门太阳城赌博 | https://1433119.com/4.html
澳门太阳城在线赌博 | https://1433119.com/casino-168.html
澳门太阳城赌博开户 | https://1433119.com/casino-addicted.html
澳门太阳城赌博代理 | https://1433119.com/casino-fraud-certified.html
澳门太阳城博彩公司 | https://1433119.com/casino-terminology.html
澳门太阳城AG | https://1433119.com/core-value.html
澳门太阳城网址大全 | https://1433119.com/electronic-games.html
澳门太阳城如何开户 | https://1433119.com/gamble-law.html
澳门太阳城在线网址 | https://1433119.com/house-edge.html
澳门太阳城娱乐网址 | https://1433119.com/
澳门太阳城博彩网址 | https://1433119.com/jiutian-xuannv.html
澳门太阳城博彩开户 | https://1433119.com/latest-offers.html
澳门太阳城如何代理 | https://1433119.com/live-baccarat.html
澳门太阳城哪里开户 | https://1433119.com/live-betting.html
澳门太阳城官方网址 | https://1433119.com/new-post.html
菲律宾太阳城 | https://1433119.com/pmmahjong.html
菲律宾太阳城集团 | https://1433119.com/terms.html
菲律宾太阳城集团APP | https://1433119.com/tu-comment.html
菲律宾太阳城洗钱案 | https://1433119.com/wbc-os.html
菲律宾太阳城周焯华 | https://1433119.com/wbc-rule.html
菲律宾太阳城娱乐 | https://1433119.com/wbc-tw.html
菲律宾太阳城赌场 | https://1433122.com/2.html
菲律宾太阳城官网 | https://1433122.com/21.html
菲律宾太阳城网址 | https://1433122.com/22.html
菲律宾太阳城博彩 | https://1433122.com/23.html
菲律宾太阳城网络赌博平台 | https://1433122.com/24.html
菲律宾太阳城娱乐城 | https://1433122.com/3.html
菲律宾太阳城APP | https://1433122.com/31.html
菲律宾太阳城集团官网 | https://1433122.com/32.html
菲律宾太阳城开户 | https://1433122.com/33.html
菲律宾太阳城代理 | https://1433122.com/34.html
菲律宾太阳城娱乐开户 | https://1433122.com/4.html
菲律宾太阳城娱乐代理 | https://1433122.com/5.html
菲律宾太阳城网络博彩 | https://1433122.com/6.html
菲律宾太阳城在线开户 | https://1433122.com/61.html
菲律宾太阳城在线代理 | https://1433122.com/62.html
菲律宾太阳城在线娱乐 | https://1433122.com/63.html
菲律宾太阳城在线官网 | https://1433122.com/64.html
菲律宾太阳城赌博 | https://1433122.com/chess-casino.html
菲律宾太阳城在线赌博 | https://1433122.com/electronic-games.html
菲律宾太阳城赌博开户 | https://1433122.com/googleddc850bb99118592.html
菲律宾太阳城赌博代理 | https://1433122.com/
菲律宾太阳城博彩公司 | https://1433122.com/latest-offers.html
菲律宾太阳城AG | https://1433122.com/new-post.html
菲律宾太阳城网址大全 | https://1433122.com/sporting-events.html
菲律宾太阳城如何开户 | https://1433126.com/about-at99.html
菲律宾太阳城在线网址 | https://1433126.com/app-download.html
菲律宾太阳城娱乐网址 | https://1433126.com/beijing-pk10.html
菲律宾太阳城博彩网址 | https://1433126.com/bng-slot-jungle.html
菲律宾太阳城博彩开户 | https://1433126.com/bng-slot-lion.html
菲律宾太阳城如何代理 | https://1433126.com/bwin-slot-houyi.html
菲律宾太阳城哪里开户 | https://1433126.com/bwin-slot-mazu.html
菲律宾太阳城官方网址 | https://1433126.com/casino-term.html
太阳城集团APP | https://1433126.com/chess-casino.html
太阳城洗钱案 | https://1433126.com/choose-an-online-casino.html
太阳城周焯华 | https://1433126.com/contact-us-2.html
太阳城网址 | https://1433126.com/e-sports.html
太阳城博彩 | https://1433126.com/electronic-games.html
太阳城网络赌博平台 | https://1433126.com/entertainment-city-guide.html
太阳城开户 | https://1433126.com/fishing.html
太阳城代理 | https://1433126.com/forget-password.html
太阳城网络博彩 | https://1433126.com/googleddc850bb99118592.html
太阳城在线开户 | https://1433126.com/
太阳城在线代理 | https://1433126.com/live-casino-2.html
太阳城在线娱乐 | https://1433126.com/live-score.html
太阳城在线官网 | https://1433126.com/login-tutorial.html
太阳城赌博 | https://1433126.com/lottery-comparison.html
太阳城在线赌博 | https://1433126.com/lottery.html
太阳城赌博开户 | https://1433126.com/rsg-slot-farm.html
太阳城赌博代理 | https://1433126.com/rsg-slot-lubu.html
太阳城博彩公司 | https://1433126.com/rsg-slot-mahjong.html
太阳城AG | https://1433126.com/rsg-slot-plutus.html
太阳城网址大全 | https://1433126.com/rsg-slot-rooster.html
太阳城如何开户 | https://1433126.com/rsg-slot-thor.html
太阳城在线网址 | https://1433126.com/sale.html
太阳城娱乐网址 | https://1433126.com/slot-machine-comparison.html
太阳城博彩网址 | https://1433126.com/sporting-events-2.html
太阳城博彩开户 | https://1433126.com/sports-lottery-comparison.html
太阳城如何代理 | https://1433128.com/2022-fifa-final.html
太阳城哪里开户 | https://1433128.com/2022-fifia.html
太阳城官方网址 | https://1433128.com/2022fifa-betting.html
太陽城賭場 | https://1433128.com/2022soccer-rules.html
太陽城網絡賭博平台 | https://1433128.com/2023wbc.html
太阳城app | https://1433128.com/2024fraud-2.html
太阳城集团老板 | https://1433128.com/2024new-year.html
太阳城集团酒店 | https://1433128.com/539-tricks.html
太阳城博弈 | https://1433128.com/539crack.html
太阳城迪士尼 | https://1433128.com/96m.html
太阳城 斗六 | https://1433128.com/9gamble.html
太阳城股票 | https://1433128.com/about-b9li.html
太阳城集团股东 | https://1433128.com/ai-analyze.html
太阳城集团ptt | https://1433128.com/analysis-of-online-casinos.html
太阳城集团主席 | https://1433128.com/app-download.html
太阳城集团 周焯华 | https://1433128.com/avia-gaming.html
太阳城老板被抓 | https://1433128.com/b9li-casino-fraud.html
太阳城logo | https://1433128.com/b9li-comment.html
太阳城品牌 | https://1433128.com/b9li-evaluate-fraud.html
太阳城 ptt | https://1433128.com/b9li-evaluate.html
太阳城sunshine city | https://1433128.com/b9li-guaranteed-withdrawal.html
太阳城推荐 | https://1433128.com/baccarat-strategy-2.html
太阳城体育 | https://1433128.com/baccarat-strategy-3.html
太阳城网站 | https://1433128.com/baccarat-strategy.html
太阳城 洗米华 | https://1433128.com/baccarat.html
太阳城娱乐城ptt | https://1433128.com/beijing-pk10.html
太阳城娱乐集团 | https://1433128.com/bng-slot-2.html
太阳城 周焯华 | https://1433128.com/bng-slot-diamond.html
太阳城集团 赌场 | https://1433128.com/bng-slot-houyi.html
太阳城集团app | https://1433128.com/bng-slot-jungle.html
太阳城集团网址 | https://1433128.com/bng-slot.html
太阳城游戏 | https://1433128.com/boya-game.html
太阳城游戏官网 | https://1433128.com/bwin-slot-mazu.html
太阳城游戏代理 | https://1433128.com/bwin-slot.html
太阳城游戏开户 | https://1433128.com/casino-168.html
太阳城游戏网址 | https://1433128.com/casino-addicted.html
太阳城游戏赚钱 | https://1433128.com/casino-experience.html
太阳城游戏官方 | https://1433128.com/casino-fraud-certified.html
太阳城游戏登入 | https://1433128.com/casino-free-cash.html
太阳城真人荷官 | https://1433128.com/casino-profits-scamming.html
太阳城试玩 | https://1433128.com/casino-recommend.html
太阳城免费开户 | https://1433128.com/casino-scam.html
太阳城免费代理 | https://1433128.com/casino-term-2.html
太阳城试玩账号 | https://1433128.com/casino-term.html
太阳城真人 | https://1433128.com/casino-terminology.html
太阳城真人网站 | https://1433128.com/champion.html
太阳城真人在线游戏 | https://1433128.com/chess-casino.html
太阳城真人在线 | https://1433128.com/contact-us-2.html
太阳城真人游戏 | https://1433128.com/contact-us.html
太阳城真人官网 | https://1433128.com/core-value.html
太阳城真人开户 | https://1433128.com/dg-baccarat-win.html
太阳城真人代理 | https://1433128.com/disclaimer.html
太阳城真人百家乐 | https://1433128.com/discount.html
太阳城真人百家乐开户 | https://1433128.com/dragon-legend-intro.html
太阳城真人百家乐代理 | https://1433128.com/e-sports.html
太阳城真人百家乐游戏 | https://1433128.com/electronic-games.html
太阳城真人百家乐官网 | https://1433128.com/entertainment-city-guide.html
太阳城娱乐城官方网站 | https://1433128.com/evaluate.html
太阳城百家乐 | https://1433128.com/faq-100.html
太阳城百家乐开户 | https://1433128.com/farmslot.html
太阳城百家乐代理 | https://1433128.com/first-savings-offer.html
太阳城百家乐官网 | https://1433128.com/fishing-2.html
太阳城百家乐游戏 | https://1433128.com/fishing-machine-8tips.html
太阳城百家乐在线 | https://1433128.com/fishing.html
太阳城百家乐在线开户 | https://1433128.com/forget-password.html
太阳城百家乐在线代理 | https://1433128.com/fraud-casino.html
太阳城集團 | https://1433128.com/gamble-law.html
太阳城娛樂場 | https://1433128.com/gambling.html
澳门申博太阳城 | https://1433128.com/game-tips.html
澳门申博太阳城真人官方 | https://1433128.com/gaming-knowledge.html
澳门申博太阳城游戏 | https://1433128.com/googleddc850bb99118592.html
澳门申博太阳城游戏规则 | https://1433128.com/gr-fishing.html
澳门申博太阳城技巧 | https://1433128.com/gr-slot.html
澳门申博太阳城试玩 | https://1433128.com/how-to-choose-an-online-casino.html
澳门申博太阳城补牌规则 | https://1433128.com/how-to-deposit.html
澳门申博太阳城技巧打法 | https://1433128.com/how-to-withdraw.html
澳门申博太阳城公式打法 | https://1433128.com/id-personal-security.html
澳门申博太阳城开户 | https://1433128.com/
澳门申博太阳城代理 | https://1433128.com/jiutian-xuannv.html
澳门申博太阳城在线 | https://1433128.com/latest-entertainment-city-news.html
澳门申博太阳城在线娱乐 | https://1433128.com/live-betting.html
澳门申博太阳城真人 | https://1433128.com/live-casino.html
澳门申博太阳城真人荷官 | https://1433128.com/live-score.html
澳门申博太阳城真人游戏 | https://1433128.com/login-tutorial.html
澳门申博太阳城官方网 | https://1433128.com/lottery-compare.html
澳门申博太阳城AG | https://1433128.com/lottery-online-vs-store-2.html
澳门申博太阳城真人免费玩 | https://1433128.com/lottery-online-vs-store.html
澳门申博太阳城真人免费开户 | https://1433128.com/lottery-ticket.html
申博太阳城游戏 | https://1433128.com/lottery.html
申博太阳城游戏官网 | https://1433128.com/machine.html
申博太阳城游戏代理 | https://1433128.com/mahjong.html
申博太阳城游戏开户 | https://1433128.com/marketing-chaos.html
申博太阳城游戏网址 | https://1433128.com/nba-livebet.html
申博太阳城游戏赚钱 | https://1433128.com/nba-odds.html
申博太阳城游戏官方 | https://1433128.com/nba-playoffs.html
申博太阳城游戏登入 | https://1433128.com/ob-fishing-machine.html
申博太阳城真人荷官 | https://1433128.com/ob-game.html
申博太阳城试玩 | https://1433128.com/ob-sport.html
申博太阳城免费开户 | https://1433128.com/offside.html
申博太阳城免费代理 | https://1433128.com/online-vs-store-lottery-2.html
申博太阳城试玩账号 | https://1433128.com/online-vs-store-lottery.html
申博太阳城真人 | https://1433128.com/pm21bj.html
申博太阳城真人网站 | https://1433128.com/pm66poker-2.html

답글 달기