murry2018 / BelarusianCutlet

[벨라루스 전통 돈까스집] 알고리즘 스터디용 리포지토리입니다.
0 stars 0 forks source link

5월 2주차 #1

Open murry2018 opened 3 years ago

murry2018 commented 3 years ago

SWEA 1209. Sum

/* Naive solve */
#include <cstdio>
#include <algorithm>

using namespace std;

int const INF=1e9;

int solve(int t[100][100]) {
    // Identity elements for 'max', '+', respectively.
    int maxsum = -INF, sum = 0;

    // horizontal maximum
    for (int i = 0; i < 100; i++) {
        sum = 0;
        for (int j = 0; j < 100; j++)
            sum += t[i][j];
        maxsum = max(maxsum, sum);
    }

    // vertical maximum
    for (int j = 0; j < 100; j++) {
        sum = 0;
        for (int i = 0; i < 100; i++)
            sum += t[i][j];
        maxsum = max(maxsum, sum);
    }

    // '\' diagonal maximum
    sum = 0;
    for (int i = 0, j = 0; i < 100; i++, j++)
        sum += t[i][j];
    maxsum = max(maxsum, sum);

    // '/' diagonal maximum
    sum = 0;
    for (int i = 0, j = 99; i < 100; i++, j--)
        sum += t[i][j];
    maxsum = max(maxsum, sum);

    return maxsum;

}

int main() {
    int t[100][100];
    for (int i = 0; i < 10; i++) {
        scanf("%*d");
        for (int i = 0; i < 100; i++)
            for (int j = 0; j < 100; j++)
                scanf("%d", &t[i][j]);
        int ans = solve(t);
        printf("#%d %d\n", i+1, ans);
    }
}

SWEA 1210. Ladder1

#include <cstdio>
using namespace std;

// I know, this function is some of ugly.
// Comments will help you understand.
int solve(int t[100][100], int r, int c) {
    enum { UP, LEFT, RIGHT } dir = UP;
    while (0 <= r) {
        // Heading left or upper, and left open
        if (dir != RIGHT && 0 < c && t[r][c-1]) {
            dir = LEFT;
            c--;
            continue;
        }
        // Heading right and right open,
        // or heading upper and right open
        if (dir != LEFT && c < 99 && t[r][c+1]) {
            dir = RIGHT;
            c++;
            continue;
        }
        // Heading left but left not open,
        // or heading right but right not open,
        // or heading upper and neither left or right open,
        // and up open.
        if (0 < r && t[r-1][c]) {
            dir = UP;
            r--;
            continue;
        }
        // No further route. Maybe it's impossible case.
        break;
    }
    return c;
}

int main() {
    int t[100][100], sr, sc;
    for (int i = 0; i < 10; i++) {
        scanf("%*d");
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                scanf("%d", &t[i][j]);
                if (t[i][j] == 2) {
                    sr = i, sc = j;
                }
            }
        }
        int ans = solve(t, sr, sc);
        printf("#%d %d\n", i+1, ans);
    }
}

SWEA 1211. Ladder2

/* LADDER 2
 * 문제 디스크립션이 혼란스러워서 굵은 글씨만 보고 풀었습니다. */
#include <cstdio>
using namespace std;
int const INF=1e9;

int len_following(int t[100][100], int r, int c) {
    enum { DOWN, LEFT, RIGHT } dir = DOWN;
    int len = 0;
    while (r < 100) {
        len++;
        // heading left or down, and left open.
        if (dir != RIGHT && 0 < c && t[r][c-1]) {
            dir = LEFT;
            c--;
            continue;
        }
        // heading right or down, and right open.
        if (dir != LEFT && c < 99 && t[r][c+1]) {
            dir = RIGHT;
            c++;
            continue;
        }
        // heading left or right but heading direction close,
        // or heading up and neither left or right isn't open.
        dir = DOWN;
        r++;
        // I presume that queer case ( every route closed when 
        // gone down ) cannot happen.
    }
    return len;
}

int solve(int t[100][100]) {
    // identity element for 'min' operation.
    int minlen = INF;
    int minlen_col;
    for (int i = 0; i < 100; i++) {
        if (t[0][i]) {
            int len = len_following(t, 0, i);
            if (len <= minlen) {
                minlen = len;
                minlen_col = i;
            }
        }
    }
    return minlen_col;
}

int main() {
    int t[100][100];
    for (int i = 0; i < 10; i++) {
        scanf("%*d");
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                scanf("%d", &t[i][j]);
            }
        }
        int ans = solve(t);
        printf("#%d %d\n", i+1, ans);
    }
}
moodmine commented 3 years ago

SWEA 1209. Sum

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>

using namespace std;

int main(int argc, char** argv)
{
    int test_case;
    int T;

    //freopen("input.txt", "r", stdin);
    //cin >> T;
    /*
       여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
    */
    T = 10;
    for (test_case = 1; test_case <= T; ++test_case)
    {
        int sum = 0;
        int tmp = 0;
        int arr[100][100];
        int i = 0;
        int j = 0;
        int num;
        scanf("%d", &num);
        for (int i = 0; i < 100; i++)
        {
            for (int j = 0; j < 100; j++)
            {
                scanf("%d", &arr[i][j]);
            }
        }

        for (i = 0; i < 100; i++)
        {
            tmp = 0;
            for (j = 0; j < 100; j++)
            {
                tmp += arr[i][j];
            }
            if (tmp > sum)
            {
                sum = tmp;
            }
        }

        for (i = 0; i < 100; i++)
        {
            tmp = 0;
            for (j = 0; j < 100; j++)
            {
                tmp += arr[j][i];
            }
            if (tmp > sum)
            {
                sum = tmp;
            }
        }

        tmp = 0;

        for (i = 0; i < 100; i++)
        {
            tmp += arr[i][j];
        }

        if (tmp > sum)
        {
            sum = tmp;
        }

        tmp = 0;

        for (i = 0; i < 100; i++)
        {
            tmp += arr[i][99-j];
        }

        if (tmp > sum)
        {
            sum = tmp;
        }

        printf("#%d %d\n", test_case, sum);
    }

    return 0;//정상종료시 반드시 0을 리턴해야합니다.
}
moodmine commented 3 years ago

SWEA 1210. Ladder1

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>

using namespace std;

int main(int argc, char** argv)
{
    int test_case;
    int T;

    //freopen("input.txt", "r", stdin);
    //cin >> T;
    /*
       여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
    */
    T = 10;
    for (test_case = 1; test_case <= T; ++test_case)
    {
        int from = 0;
        int to = 0;
        int tmp = 0;
        int arr[100][102] = { 0, };
        int i = 0;
        int j = 0;
        int turn = 0;
        int num;
        scanf("%d", &num);
        for (i = 0; i < 100; i++)
        {
            for (j = 1; j <= 100; j++)
            {
                scanf("%d", &arr[i][j]);
                if (arr[i][j] == 2)
                    to = j;
            }
        }
        i = 99;
        j = to;
        while (i != 0)
        {
            if (arr[i][j - 1] == 1)
                turn = -1;
            else if (arr[i][j + 1] == 1)
                turn = 1;
            else
                turn = 0;

            switch (turn)
            {
            case -1:
                while (arr[i][j - 1] == 1)
                    j--;
                i--;
                break;
            case 1:
                while (arr[i][j + 1] == 1)
                    j++;
                i--;
                break;
            case 0:
                i--;
                break;
            }

        }
        from = j - 1;
        printf("#%d %d\n", num, from);
    }

    return 0;//정상종료시 반드시 0을 리턴해야합니다.
}
moodmine commented 3 years ago

SWEA 1211. Ladder2

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>

using namespace std;

int main(int argc, char** argv)
{
    int test_case;
    int T;

    //freopen("input.txt", "r", stdin);
    //cin >> T;
    /*
       여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
    */
    T = 10;
    for (test_case = 1; test_case <= T; ++test_case)
    {
        int from = 0;
        int count = 0;
        int tmp;
        int result;
        int arr[100][102] = { 0, };
        int i = 0;
        int j = 0;
        int k = 0;
        int turn = 0;
        int num;
        scanf("%d", &num);
        for (i = 0; i < 100; i++)
        {
            for (j = 1; j <= 100; j++)
            {
                scanf("%d", &arr[i][j]);
            }
        }
        for (k = 1; k <= 100; k++)
        {
            if (arr[0][k] == 1)
            {
                i = 0;
                j = k;
                tmp = 0;
                while (i != 99)
                {
                    if (arr[i][j - 1] == 1)
                        turn = -1;
                    else if (arr[i][j + 1] == 1)
                        turn = 1;
                    else
                        turn = 0;

                    switch (turn)
                    {
                    case -1:
                        while (arr[i][j - 1] == 1)
                        {
                            j--;
                            tmp++;
                        }
                        i++;
                        tmp++;
                        break;
                    case 1:
                        while (arr[i][j + 1] == 1)
                        {
                            j++;
                            tmp++;
                        }
                        i++;
                        tmp++;
                        break;
                    case 0:
                        i++;
                        tmp++;
                        break;
                    }
                }
                if (count == 0)
                {
                    count = tmp;
                    result = k;
                }
                else if (tmp <= count)
                {
                    count = tmp;
                    result = k;
                }
            }
        }
        printf("#%d %d\n", num, result-1);
    }

    return 0;//정상종료시 반드시 0을 리턴해야합니다.
}