murry2018 / BelarusianCutlet

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

21W21-Queue #8

Open murry2018 opened 3 years ago

murry2018 commented 3 years ago

SWEA 1225. 암호생성기

#include <cstdio>

#define DEBUG false

using namespace std;

int solve(int arr[8]) {
  int i = 0;
  int round = 1;
  while (arr[i]) {
    arr[i] -= round;
    #if DEBUG
    printf("after round %d: ", round);
    for (int j = 0; j < 8; j++) {
      printf(" %d", arr[(i+1+j)%8]);
    }
    printf("\n");
    #endif
    if (arr[i] <= 0) {
      arr[i] = 0;
      return (i+1)%8;
    }
    round = round%5+1;
    i = (i+1)%8;
  }
}

int main() {
  int arr[8];
  for (int i = 0; i < 10; i++) {
    scanf("%*d");
    for (int i = 0; i < 8; i++)
      scanf("%d", &arr[i]);
    int s = solve(arr);
    printf("#%d", i+1);
    for (int i = 0; i < 8; i++)
      printf(" %d", arr[(s+i)%8]);
    printf("\n");
  }
}
murry2018 commented 3 years ago

SWEA 1226. 미로1

SWEA 1227. 미로2

같은 코드에 상수값만 바꾸어 풀었습니다.

#include <cstdio>
#include <queue>

#define SZ 16 /* [1227. 미로2]의 경우 100 */

using namespace std;

struct pii {
  int l, r;
  pii() {}
  pii(int l, int r): l(l), r(r) {}
  bool operator==(const pii& rhs) const {
    return l == rhs.l && r == rhs.r;
  }
  pii operator+(const pii& rhs) const {
    pii n = {l, r};
    n.l += rhs.l;
    n.r += rhs.r;
    return n;
  }
};

bool exists_path(int maze[SZ][SZ], 
                  pii s, pii e)
{
  static pii const dirs[4] = {
    {1, 0},
    {0, 1},
    {-1, 0},
    {0, -1}
  };
  bool visit[SZ][SZ] = {0};
  queue<pii> q;
  q.push(s);
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    visit[p.l][p.r] = true;
    for (int i = 0; i < 4; i++) {
      pii t = p+dirs[i];
      if (t == e) {
        return true;
      }
      if (maze[t.l][t.r] == 0
          && !visit[t.l][t.r]) {
        q.push(t);
      }
    }
  }
  return false;
}

int main() {
  pii s, e;
  int maze[SZ][SZ];
  for (int i = 0; i < 10; i++) {
    scanf("%*d");
    for (int i = 0; i < SZ; i++) {
      for (int j = 0; j < SZ; j++) {
        scanf("%1d", &maze[i][j]);
        if (maze[i][j] > 1) {
          if (maze[i][j] == 2)
            s.l = i, s.r = j;
          else
            e.l = i, e.r = j;
        }
      }
    }
    printf("#%d %d\n", i+1,
          exists_path(maze, s, e)? 1: 0);
  }
}
moodmine commented 3 years ago

SWEA 1225. 암호생성기

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <stdio.h>

using namespace std;

#define MAX_N 10

int front;
int rear;
int queue[MAX_N];

void queueInit(void)
{
    front = 0;
    rear = 0;
}

int queueIsEmpty(void)
{
    return (front == rear);
}

int queueIsFull(void)
{
    if ((rear + 1) % MAX_N == front)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int queueEnqueue(int value)
{
    if (queueIsFull())
    {
        printf("queue is full!");
        return 0;
    }
    rear++;
    if (rear == MAX_N - 1)
    {
        rear = 0;
    }
    queue[rear] = value;
    return 1;
}

int queueDequeue(int* value)
{
    if (queueIsEmpty())
    {
        printf("queue is empty!");
        return 0;
    }
    front++;
    if (front == MAX_N - 1)
    {
        front = 0;
    }
    *value = queue[front];

    return 1;
}

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 num;
        int data;
        int i;
        queueInit();
        scanf("%d", &num);

        for (i = 0; i < 8; i++)
        {
            scanf("%d", &data);
            queueEnqueue(data);
        }
        i = 1;
        while (queue[rear])
        {
            queueDequeue(&data);
            if (i == 6)
                i = 1;
            data -= i;
            if (data < 0)
                data = 0;
            queueEnqueue(data);
            i++;
        }

        printf("#%d ", num);
        queueDequeue(&data);
        while (data != 0)
        {
            printf("%d ", data);
            queueDequeue(&data);
        }
        printf("%d ", data);
        printf("\n");
    }
    return 0;
}
moodmine commented 3 years ago

SWEA 1226. 미로1

SWEA 1226. 미로2

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <stdio.h>

using namespace std;

#define MAX_N 100

int front;
int rear;
int queue[MAX_N][2];

void queueInit(void)
{
    front = 0;
    rear = 0;
}

int queueIsEmpty(void)
{
    return (front == rear);
}

int queueIsFull(void)
{
    if ((rear + 1) % MAX_N == front)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int queueEnqueue(int y, int x)
{
    if (queueIsFull())
    {
        printf("queue is full!");
        return 0;
    }
    rear++;
    if (rear == MAX_N - 1)
    {
        rear = 0;
    }
    queue[rear][0] = y;
    queue[rear][1] = x;
    return 1;
}

int queueDequeue(int* y, int* x)
{
    if (queueIsEmpty())
    {
        printf("queue is empty!");
        return 0;
    }
    front++;
    if (front == MAX_N - 1)
    {
        front = 0;
    }
    *y = queue[front][0];
    *x = queue[front][1];
    return 1;
}

int next_front(int i)
{
    i++;
    if (i == MAX_N - 1)
    {
        i = 0;
    }
    return i;
}

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 num;
        char s[2][100][101] = { 0, };
        int i;
        int j;
        int y = 0;
        int x = 0;
        queueInit();
        scanf("%d", &num);
        for (i = 0; i < 100; i++)
        {
            scanf("%s", s[0][i]);
        }
        for (i = 0; i < 100; i++)
        {
            for (j = 0; j < 100; j++)
            {
                if (s[0][i][j] == '2')
                {
                    s[1][i][j] = 1;
                    y = i;
                    x = j;
                    i = 100;
                    break;
                }
            }
        }
        int result = -1;
        int flag = 0;
        int cross = 4;
        int allcheck = 1;

        while (result == -1) // 3을 못찾고 돌아올 경우 불가능한 걸로 판명하고 종료
        {
            while (1)
            {
                if (s[0][y][x] == '3')
                {
                    result = 1;
                    break;
                }

                cross = 0;
                allcheck = 1;
                if (s[0][y + 1][x] != '1' && s[1][y + 1][x] == 0) // 몇 갈래길인지 확인
                {
                    cross++;
                }
                if (s[0][y][x + 1] != '1' && s[1][y][x + 1] == 0)
                {
                    cross++;
                }
                if (s[0][y - 1][x] != '1' && s[1][y - 1][x] == 0)
                {
                    cross++;
                }
                if (s[0][y][x - 1] != '1' && s[1][y][x - 1] == 0)
                {
                    cross++;
                }

                if (cross == 0) // 사방이 막혔을 때
                {
                    if (queueIsEmpty())
                    {
                        result = 0;
                        break;
                    }
                    queueDequeue(&y, &x);
                    break;
                }

                if (cross >= 2) //교차로 일 때
                {
                    if (s[0][y + 1][x] != '1' && s[1][y + 1][x] == 0)
                    {
                        queueEnqueue(y + 1, x);
                        s[1][y + 1][x] = 1;
                    }
                    if (s[0][y][x + 1] != '1' && s[1][y][x + 1] == 0)
                    {
                        queueEnqueue(y, x + 1);
                        s[1][y][x + 1] = 1;
                    }
                    if (s[0][y - 1][x] != '1' && s[1][y - 1][x] == 0)
                    {
                        queueEnqueue(y - 1, x);
                        s[1][y - 1][x] = 1;
                    }
                    if (s[0][y][x - 1] != '1' && s[1][y][x - 1] == 0)
                    {
                        queueEnqueue(y, x - 1);
                        s[1][y][x - 1] = 1;
                    }
                    queueDequeue(&y, &x);
                    break;
                }

                if (cross == 1) // 일직선일 때 
                {
                    if (s[0][y + 1][x] != '1' && s[1][y + 1][x] == 0) // 진행
                    {
                        y++;
                        s[1][y][x] = 1;
                        break;
                    }
                    if (s[0][y][x + 1] != '1' && s[1][y][x + 1] == 0)
                    {
                        x++;
                        s[1][y][x] = 1;
                        break;
                    }
                    if (s[0][y - 1][x] != '1' && s[1][y - 1][x] == 0)
                    {
                        y--;
                        s[1][y][x] = 1;
                        break;
                    }
                    if (s[0][y][x - 1] != '1' && s[1][y][x - 1] == 0)
                    {
                        x--;
                        s[1][y][x] = 1;
                        break;
                    }
                }
            }
        }
        printf("#%d %d\n", num, result);
    }
    return 0;
}