https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15QRX6APsCFAYD&categoryId=AV15QRX6APsCFAYD&categoryType=CODE&problemTitle=1249&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <string>

using namespace std;

struct node {
    int y;
    int x;
};

int main() {

    int test_case;
    int T;
    cin >> T;

    for (test_case = 1; test_case <= T; ++test_case) {
        int k;
        cin >> k;

        int map[100][100];
        int used[100][100]={0};
        int path[100][100];

        string st;
        for (int y = 0; y < k; y++) {
            cin >> st;
            for (int x = 0; x < k; x++) {
                map[y][x] = st[x] - '0';
            }
        }

        queue<node>q;

        int direct[4][2] = {
            -1,0,
            1,0,
            0,-1,
            0,1
        };

        path[0][0] = 0;
        q.push({ 0,0 });
        used[0][0] = 1;
        while (!q.empty()) {
            node now = q.front();
            q.pop();

            for (int i = 0; i < 4; i++) {
                int dy = now.y + direct[i][0];
                int dx = now.x + direct[i][1];
                if (dy < 0 || dx < 0 || dy >= k || dx >= k) continue;
                if (used[dy][dx]==0 || path[dy][dx] > path[now.y][now.x] + map[dy][dx]) {
                    q.push({ dy, dx });
                    used[dy][dx] = 1;
                    path[dy][dx] = path[now.y][now.x] + map[dy][dx];
                }
            }
        }

        cout << "#" << test_case << " " << path[k-1][k-1] << endl;
    }

    return 0;
}

'APS > SWEA' 카테고리의 다른 글

1231. [S/W 문제해결 기본] 9일차 - 중위순회 C++  (0) 2021.11.20
8659.GCD c++  (0) 2021.11.19
1216. [S/W 문제해결 기본] 3일차 - 회문2  (0) 2021.11.19
1860. 진기의 최고급 붕어빵  (0) 2021.11.09
12052. 부서진 타일 c++  (0) 2021.11.09

+ Recent posts