반응형

https://www.acmicpc.net/problem/19236

 

19236번: 청소년 상어

첫째 줄부터 4개의 줄에 각 칸의 들어있는 물고기의 정보가 1번 행부터 순서대로 주어진다. 물고기의 정보는 두 정수 ai, bi로 이루어져 있고, ai는 물고기의 번호, bi는 방향을 의미한다. 방향 bi는

www.acmicpc.net

#include <bits/stdc++.h>

using namespace std;


int dx[9] = {0, -1, -1, 0, 1, 1, 1, 0, -1};
int dy[9] = {0, 0, -1, -1, -1, 0, 1, 1, 1};

int ans = 0;

void moveFish(int _curFishPosition[][4], int _curFishes[][3], int _skx, int _sky) {
    for (int i = 1; i <= 16; i++) {
        int x = _curFishes[i][0];
        int y = _curFishes[i][1];
        int d = _curFishes[i][2];


        if (x < 0) continue;

        int nx = x;
        int ny = y;
        int nd = d;
        for (int j = 0; j < 8; j++) {
            nx = x + dx[nd];
            ny = y + dy[nd];
            nd++;
            if (nd >= 9) nd = 1;

            if (nx < 0 || nx >= 4 || ny < 0 || ny >= 4) continue;
            if (nx == _skx && ny == _sky) continue;

            break;
        }

        nd--;
        if (nd == 0) nd = 8;


        _curFishes[i][0] = nx;
        _curFishes[i][1] = ny;
        _curFishes[i][2] = nd;


        if(_curFishPosition[nx][ny]<=0){
            cout << "Erorr!!" << '\n';
            cout <<_curFishPosition[nx][ny] << ' ' << nx << ' ' << ny << '\n';
        }

        int changeFish = _curFishPosition[nx][ny];
        //살은 경우만 x,y 동기화해줌
        if(_curFishes[changeFish][0] >= 0) {
            _curFishes[changeFish][0] = x;
            _curFishes[changeFish][1] = y;
        }

        swap(_curFishPosition[nx][ny], _curFishPosition[x][y]);

    }
}


void backTrack(int L, int _skx, int _sky, int _skd, int _fishPosition[][4], int _fishes[][3], int sum) {
    ans = max(ans, sum);
    int curFishes[17][3];
    int curFishPosition[4][4];
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            curFishPosition[i][j] = _fishPosition[i][j];
        }
    }
    for (int i = 0; i < 17; i++) {
        for (int j = 0; j < 3; j++) {
            curFishes[i][j] = _fishes[i][j];
        }
    }

    //물고기 이동
    moveFish(curFishPosition, curFishes, _skx, _sky);



    //상어 이동
    int n_skx = _skx;
    int n_sky = _sky;
    int n_skd = _skd;

    for (int i = 0; i < 4; i++) {
        n_skx += dx[_skd];
        n_sky += dy[_skd];


        if (n_skx < 0 || n_skx >= 4 || n_sky < 0 || n_sky >= 4) continue;
        int curFish = curFishPosition[n_skx][n_sky];
        int curFishX = curFishes[curFish][0];

        //물고기 없는 칸 인경우 x==-1
        if (curFishX < 0) continue;
        n_skd = curFishes[curFish][2];

        curFishes[curFish][0] = -1;
        backTrack(L + 1, n_skx, n_sky, n_skd, curFishPosition, curFishes, sum + curFish);
        curFishes[curFish][0] = curFishX;
    }
}


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int fishPosition[4][4];

//16fishes x, y, dir;
    int fishes[17][3];


    for (int x = 0; x < 4; x++) {
        for (int y = 0; y < 4; y++) {
            int a, b;
            cin >> a >> b;

            fishPosition[x][y] = a;
            fishes[a][0] = x;
            fishes[a][1] = y;
            fishes[a][2] = b;
        }
    }

    int skx = 0;
    int sky = 0;

    //잡아먹으면 0으로 표시
    ans += fishPosition[skx][sky];
    int skd = fishes[fishPosition[skx][sky]][2];
    fishes[fishPosition[skx][sky]][0] = -1;
    backTrack(0, skx, sky, skd, fishPosition, fishes, ans);

    cout << ans << '\n';
}
반응형
반응형

https://www.acmicpc.net/problem/20061

 

20061번: 모노미노도미노 2

모노미노도미노는 아래와 같이 생긴 보드에서 진행되는 게임이다. 보드는 빨간색 보드, 파란색 보드, 초록색 보드가 그림과 같이 붙어있는 형태이다. 게임에서 사용하는 좌표 (x, y)에서 x는 행,

www.acmicpc.net

1. 문제설명

구조 설계를 꼼꼼히 잘 해야 해결할 수 있는 문제이다.

먼저 파란색 블록과 초록색 블록 영역은 동일하게 둘 수 있다.

들어오는 블록이 1x2거나 2x1일 때 그 블록을 서로 바꿔서 파란 영역과 초록 영역에 넣어주면 동일한 구조를 지닌다.

 

즉 input 블록만 회전시켜주면 초록색 영역과 파란색 영역에 동일한 시행을 하면 된다.

이를 바탕으로

나는 함수를 세가지를 만들었는데

 

1. 블록을 쌓는 함수,

2. 현재 쌓인 블록에서 지워질 라인이 있는지 확인하고 지우며 점수를 증가시키는 함수

3. 특별한 영역에 블록이 있는지 확인하고 그에 따라 전체 블록을 옮기는 함수.

 

이 세 함수를 들어오는 블록마다 파란영역과 초록 영역에 시행하면

문제를 해결할 수 있다.

 

주의해야할 점은

2. 현재 쌓인 블록에서 지워질 라인이 있는지 확인하고 지운 후

다시 현재 라인부터 검사해야한다.

왜냐하면 현재 라인은 이후에 검사할 라인에서 당겨왔기 때문에

검사가 안된 상태이기 때문이다.

 

 

이러한 삼성기출문제의 구현, 시뮬레이션 문제는 

생각을 구조화하고 짜임새있게 해주어서 좋은 문제인 것 같다.

 

 

2.문제풀이코드 C++

#include <bits/stdc++.h>

using namespace std;

int N, point;
bool blueBoard[4][6], greenBoard[4][6];


void buildBlock(int t, int x, int y, bool board[][6]) {
    if(t==1){
        int ny = 0;
        while(ny+1 <= 5 && board[x][ny+1] ==0 ){
            ny++;
        }
        board[x][ny] = 1;
    }
    else if(t==2){
        int ny = 0;
        while(ny+1 <= 5 && board[x][ny+1] ==0 ){
            ny++;
        }
        board[x][ny] = 1;
        board[x][ny-1] = 1;

    }
    else if(t==3){
        int nyFirst = 0;
        while(nyFirst+1 <= 5 && board[x][nyFirst+1] ==0 ){
            nyFirst++;
        }

        int nySecond = 0;
        while(nySecond+1 <= 5 && board[x+1][nySecond+1] ==0 ){
            nySecond++;
        }

        int ny = min(nyFirst, nySecond);

        board[x][ny] = 1;
        board[x+1][ny] = 1;
    }
}

void checkNormalLine(bool blueBoard[][6]) {
    for(int i=5; i>=2; i--){
        bool flag = true;

        for(int j=0; j<4; j++){
            if(!blueBoard[j][i]){
                flag = false;
                break;
            }
        }

        if(flag){
            //Line delete;
            point++;
            for(int j=i; j>=1; j--){
                for(int k=0; k<4; k++){
                    blueBoard[k][j] = blueBoard[k][j-1];
                }
            }

            for(int k=0; k<4; k++){
                blueBoard[k][0] = 0;
            }
            //cur Line recheck!
            i++;
        }
    }
}

void checkSpecialLine(bool blueBoard[][6]) {
    bool flag0 = false;
    bool flag1 = false;
    for(int i=0; i<4; i++){
        if(blueBoard[i][0]){
            flag0 = true;
        }
        if(blueBoard[i][1]){
            flag1 = true;
        }
    }


    if(flag0&&flag1){
        //2칸
        for(int i=5; i>=2; i--){
            for(int j=0; j<4; j++){
                blueBoard[j][i] = blueBoard[j][i-2];
            }
        }
        for(int k=0; k<4; k++){
            blueBoard[k][0] = 0;
            blueBoard[k][1] = 0;
        }


    }
    else if(flag1){ //1칸
        for(int i=5; i>=1; i--){
            for(int j=0; j<4; j++){
                blueBoard[j][i] = blueBoard[j][i-1];
            }
        }
        for(int k=0; k<4; k++){
            blueBoard[k][0] = 0;
        }

    }

}


int getBlockNum() {
    int blocks = 0;

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 6; j++) {
            if (blueBoard[i][j]) blocks++;
            if (greenBoard[i][j]) blocks++;
        }
    }



    return blocks;
}


void print(){
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 6; j++) {
            cout << blueBoard[i][j] << ' ';
        }
        cout << '\n';
    }

    cout << "-----------------------\n";

    for(int i=0; i<6; i++){
        for(int j=0; j<4; j++){
            cout << greenBoard[j][i] << ' ';
        }
        cout << '\n';
    }


    cout <<'\n';
    cout << "-----------------------\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> N;

    for (int i = 0; i < N; i++) {
        int t, x, y;
        cin >> t >> x >> y;
        buildBlock(t, x, y, blueBoard);
        //90' 회전
        if (t == 2) t = 3;
        else if (t == 3) t = 2;
        buildBlock(t, y, x, greenBoard);

        checkNormalLine(blueBoard);
        checkNormalLine(greenBoard);
        checkSpecialLine(blueBoard);
        checkSpecialLine(greenBoard);
//        print();
    }


    cout << point << '\n';
    cout << getBlockNum() << '\n';

}
반응형
반응형

https://www.acmicpc.net/problem/17825

 

17825번: 주사위 윷놀이

첫째 줄에 주사위에서 나올 수 10개가 순서대로 주어진다.

www.acmicpc.net

#include <bits/stdc++.h>

using namespace std;

int dice[11];

int nextPosition[33] ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,32,22,23,29,25,29,27,28,29,30,31,20,32};
int point[33] = {0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,13,16,19,22,24,28,27,26,25,30,35,0};


int horsePosition[4];
int ans = 0;


int horseMove(int x, int cnt){
    //already arrive
    if(x==32) return -1;

    //nx : other horse

    //blue move
    if(x==5){
        x = 21;
        cnt--;
    }
    else if(x==10){
        x = 24;
        cnt--;
    }
    else if(x==15){
        x = 26;
        cnt--;
    }
    //이동
    for(int i=0; i<cnt; i++){
        x = nextPosition[x];
    }

    //other horse
    for(int i=0; i<4; i++){
        if(x!=32 && horsePosition[i] == x){
            return -1;
        }
    }

    return x;
}


void backTrack(int L, int curPoint) {
    if (L == 10) {
        ans = max(ans, curPoint);
        return;
    }

    for (int i = 0; i < 4; i++) {
        int x = horsePosition[i];
        int nx = horseMove(horsePosition[i], dice[L]);
        if (nx < 0) continue;
        horsePosition[i] = nx;
        backTrack(L + 1, point[nx] + curPoint);
        horsePosition[i] = x;
    }
}



int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    for(int i=0; i<10; i++){
        cin >> dice[i];
    }

    backTrack(0, 0);

    cout << ans << '\n';
}

 

반응형
반응형

https://www.acmicpc.net/problem/17822

 

17822번: 원판 돌리기

반지름이 1, 2, ..., N인 원판이 크기가 작아지는 순으로 바닥에 놓여있고, 원판의 중심은 모두 같다. 원판의 반지름이 i이면, 그 원판을 i번째 원판이라고 한다. 각각의 원판에는 M개의 정수가 적혀

www.acmicpc.net

1.문제설명

1. 회전하는 함수를 구현한다.

2. 각 원판을 회전 후 인접점을 검사하여 같은 값을 가진 좌표가 있을 경우 해당 지점들은 모두 없앤다.

3. 인접점이 없을 경우 평균을 구해 평균보다 작은 값은 +1 평균보다 큰 값은 모두 -1을 해준다.

 

 

 

1. 회전하는 함수는 deque를 이용하면 쉽게 구현할 수 있다.

2. 인접지점을 검사하는 기능을 구현하는 것이 이 문제의 핵심인데,

규칙을 잘 발견하면 깔끔하게 구할 수 있다.

 

인접지점 중 현재 점과 같은 값을 가진 지점이 존재하는지 검사를 해야하는데

이게 현재 지점이 어떤 점위에 있느냐에 따라 케이스가 나뉘어져

복잡한 if문을 사용해서 풀 수도 있다.

 

그런데 현재 원판이 1번, 2번, ... N번 원판 까지 있으면

가상의 0번 원판, N+1번 원판이 존재한다고 생각하고

해당 원판에 존재하는 수를 모두 0으로 두면

조건을 좀 더 간단하게 만들 수 있다.

 

이런식으로 문제를 풀 수 있는 이유는

문제 조건에서 원판 위의 숫자는 자연수로만 두기 때문에

0과 비교하면 무조건 다른 값을 가진 것으로 나타난다.

 

빨간색 0이 임의로 추가해준 지점이고

파란색 동그라미의 2와 3은 해당 지점에서 인접 지점을 검사할 때

검사할 지점들을 나타낸다

 

이런식으로 구현하면 1번 원판과 N번 원판을 굳이 따로 케이스를 분류하지 않고 풀어도 된다.

 

 


2.문제풀이코드 C++

#include <bits/stdc++.h>

using namespace std;

int N, M, T;
int arr[52][52];

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void Input();
double getMeans();
void plusMinus();


//row를 k번 돌린다
void rotateClock(int row, int k) {
    deque<int> dq;

    for (int i = 1; i <= M; i++) {
        dq.push_back(arr[row][i]);
    }

    for (int i = 0; i < k; i++) {
        dq.push_front(dq.back());
        dq.pop_back();
    }

    for (int i = 1; i <= M; i++) {
        arr[row][i] = dq.front();
        dq.pop_front();
    }
}

void rotate(int x, int d, int k) {
    for (int i = 1; i <= N; i++) {
        if (i % x == 0) {
            if (d == 0) {
                rotateClock(i, k);
            } else {
                rotateClock(i, M - k);
            }
        }
    }
}

void getAdjacentPoint() {
    vector<pair<int, int> > points;

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            if (arr[i][j] == 0) continue;
            for (int k = 0; k < 4; k++) {
                int nx = i + dx[k];
                int ny = j + dy[k];

                if (ny > M) ny = 1;
                else if (ny < 1) ny = M;

                if (arr[i][j] == arr[nx][ny]) {
                    points.push_back({i, j});
                    points.push_back({nx, ny});
                }
            }
        }
    }

    if (points.size() > 0) {
        for (auto p: points) {
            arr[p.first][p.second] = 0;
        }
    } else {
        plusMinus();
    }
}


double getMeans() {
    int cnt = 0;
    double sum = 0;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            if (arr[i][j] > 0) cnt++;
            sum += arr[i][j];
        }
    }

    if (cnt == 0) return 0;
    else return sum / cnt;
}

void plusMinus() {
    double standard = getMeans();

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            if (arr[i][j] == 0) continue;

            if (arr[i][j] > standard) {
                arr[i][j]--;
            } else if (arr[i][j] < standard) {
                arr[i][j]++;
            }
        }
    }
}


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    Input();

    int ans = 0;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            ans += arr[i][j];
        }
    }

    cout << ans << '\n';
}


void Input() {
    cin >> N >> M >> T;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            cin >> arr[i][j];
        }
    }

    for (int t = 0; t < T; t++) {
        int x, d, k;
        cin >> x >> d >> k;

        rotate(x, d, k);
        getAdjacentPoint();
    }
}
반응형
반응형

https://www.acmicpc.net/problem/17837

 

17837번: 새로운 게임 2

재현이는 주변을 살펴보던 중 체스판과 말을 이용해서 새로운 게임을 만들기로 했다. 새로운 게임은 크기가 N×N인 체스판에서 진행되고, 사용하는 말의 개수는 K개이다. 말은 원판모양이고, 하

www.acmicpc.net

#include <bits/stdc++.h>

using namespace std;

int N, K;
int chessBoard[13][13];
int horseOnChessBoard[13][13][13];
int horseInfo[11][3];  //x, y, dir
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int cnt = 1;
int ans = -1;

void Input();

vector<int> getHorseToMove(int horseX, int horseY, int i) {
    vector<int> horseToMove;
    int idx = -1;

    for (int j = 0; j < K; j++) {
        if (horseOnChessBoard[horseX][horseY][j] == i) {
            idx = j;
            break;
        }
    }

    for (int j = idx; j < K; j++) {
        if (horseOnChessBoard[horseX][horseY][j] == 0) break;
        horseToMove.push_back(horseOnChessBoard[horseX][horseY][j]);
        horseOnChessBoard[horseX][horseY][j] = 0;
    }

    return horseToMove;
}


void turnDirection(int cur, int &horseDir) {
    if (horseDir == 0) horseDir = 1;
    else if (horseDir == 1) horseDir = 0;
    else if (horseDir == 2) horseDir = 3;
    else if (horseDir == 3) horseDir = 2;
    horseInfo[cur][2] = horseDir;
}


bool checkMove(const vector<int> &horseToMove, int nx, int ny, int top) {
    for (int k = 0; k < horseToMove.size(); k++) {
        int curHorse = horseToMove[k];
        horseOnChessBoard[nx][ny][top++] = curHorse;
        horseInfo[curHorse][0] = nx;
        horseInfo[curHorse][1] = ny;
    }

    if(top>=4){
        ans = cnt;
        return true;
    }
    return false;
}


int getTop(int nx, int ny) {
    int top = 0;
    while (horseOnChessBoard[nx][ny][top] != 0) {
        top++;
    }
    return top;
}

bool moveHorse() {
    for (int i = 1; i <= K; i++) {
        int horseX = horseInfo[i][0];
        int horseY = horseInfo[i][1];
        int horseDir = horseInfo[i][2];
        int nx = horseX + dx[horseDir];
        int ny = horseY + dy[horseDir];

        //move or not move check
        if (nx <= 0 || nx > N || ny <= 0 || ny > N || chessBoard[nx][ny] == 2) {
            turnDirection(i, horseDir);
            nx = horseX + dx[horseDir];
            ny = horseY + dy[horseDir];
            if (nx <= 0 || nx > N || ny <= 0 || ny > N || chessBoard[nx][ny] == 2) continue;
        }
        
        //move
        vector<int> horseToMove = getHorseToMove(horseX, horseY, i);
        if (chessBoard[nx][ny] == 1) reverse(horseToMove.begin(), horseToMove.end());
        int top = getTop(nx, ny);

        if (checkMove(horseToMove, nx, ny, top)) {
            return false;
        }
    }

    return true;
}


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    Input();

    while (moveHorse()) {
        cnt++;

        if (cnt > 1000) {
            cout << -1 << '\n';
            return 0;
        }
    }

    cout << ans << '\n';

    return 0;
}


void Input() {
    cin >> N >> K;

    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= N; j++)
            cin >> chessBoard[i][j];

    for (int i = 1; i <= K; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        horseInfo[i][0] = a;
        horseInfo[i][1] = b;
        horseInfo[i][2] = c - 1;
        horseOnChessBoard[a][b][0] = i;
    }
}
반응형
반응형

https://www.acmicpc.net/problem/17779

 

17779번: 게리맨더링 2

재현시의 시장 구재현은 지난 몇 년간 게리맨더링을 통해서 자신의 당에게 유리하게 선거구를 획정했다. 견제할 권력이 없어진 구재현은 권력을 매우 부당하게 행사했고, 심지어는 시의 이름

www.acmicpc.net

1. 문제설명

 

 

 

 

 

2.문제풀이코드 C++

#include <bits/stdc++.h>

using namespace std;


int N, population[21][21], group[21][21]; //인구수
int ans = INT_MAX;
int x, y, d1, d2;


void Input();

int getMinDifference();

void groupMarking() {
    for (int r = 1; r <= N; r++) {
        for (int c = 1; c <= N; c++) {
            if (r < x + d1 && c <= y) group[r][c] = 1;
            else if (r <= x + d2 && c > y && c <= N) group[r][c] = 2;
            else if (r >= x + d1 && r <= N && c >= 1 && c < y - d1 + d2) group[r][c] = 3;
            else if (x + d2 < r && y - d1 + d2 <= c) group[r][c] = 4;
        }
    }


    for (int r = 1; r <= N; r++) {
        for (int c = 1; c <= N; c++) {
            if (r + c >= x + y && r + c <= x + y + 2 * d2 && r >= c + x - y && r <= c + x - y + 2 * d1) group[r][c] = 5;
        }
    }

}

//x,y, d1, d2 결정
void divideGroup() {
    for (x = 1; x <= N; x++) {
        for (y = 1; y <= N; y++) {
            for (d1 = 1; d1 <= N; d1++) {
                for (d2 = 1; d2 <= N; d2++) {
                    if (x + d1 + d2 <= N && y - d1 >= 1 && y + d2 > y && y + d2 <= N) {
                        groupMarking();
                        ans = min(ans, getMinDifference());
                    }
                }
            }
        }
    }
}

//인구 차이 최소 구함
int getMinDifference() {
    int groupPopulation[6] = {0};

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
            groupPopulation[group[i][j]] += population[i][j];
        }
    }

    int populMax = 0, populMin = INT_MAX;


    for (int i = 1; i <= 5; i++) {
        if (groupPopulation[i] == 0) return INT_MAX;
        populMax = max(populMax, groupPopulation[i]);
        populMin = min(populMin, groupPopulation[i]);
    }

    return populMax - populMin;
};


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    Input();
    divideGroup();

    if (ans == INT_MAX) cout << -1 << '\n';
    else cout << ans << '\n';


    return 0;
}


void Input() {
    cin >> N;

    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= N; j++)
            cin >> population[i][j];
}

 

반응형
반응형

https://www.acmicpc.net/problem/17471

 

17471번: 게리맨더링

선거구를 [1, 4], [2, 3, 5, 6]으로 나누면 각 선거구의 인구는 9, 8이 된다. 인구 차이는 1이고, 이 값보다 더 작은 값으로 선거구를 나눌 수는 없다.

www.acmicpc.net

1.문제설명

백준 17471번

2.문제풀이코드 C++

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;


int N, population[11]; //인구수
vector<int> adj[11];
bool group[11]; //group 표시

int ans = INT_MAX;


int calDifference() {
    int firstGroupPeople = 0, secondGroupPeople = 0;

    for (int i = 1; i <= N; i++) {
        if (group[i]) {
            firstGroupPeople += population[i];
        } else {
            secondGroupPeople += population[i];
        }
    }

    return abs(firstGroupPeople - secondGroupPeople);
}


bool executeBFS(int startNode) {
    queue<int> Q;
    bool ch[11] = {0};

    Q.push(startNode);
    ch[startNode] = 1;
    while (!Q.empty()) {
        int cur = Q.front();
        Q.pop();
        for (auto nx: adj[cur]) {
            if (!ch[nx] && group[cur]==group[nx]) {
                ch[nx] = 1;
                Q.push(nx);
            }
        }
    }


    for (int i = 1; i <= N; i++) {
        if ((group[i] == group[startNode]) && (ch[i] != 1)) {
            return false;
        }
    }

    return true;
}


bool BFS() {
    //각 그룹 시작점 설정
    int groupOneNode = -1, groupTwoNode = -1;
    for (int i = 1; i <= N; i++) {
        if (groupOneNode != -1 && groupTwoNode != -1) break;
        else if (group[i] == 0) groupOneNode = i;
        else groupTwoNode = i;
    }

    if(groupOneNode ==-1 || groupTwoNode == -1) return false;

    if (executeBFS(groupOneNode) && executeBFS(groupTwoNode)) return true;
    else return false;
}


void bruteForce(int L) {
    if (L == N + 1) {
        if (BFS()) {
            ans = min(ans, calDifference());
        }
        return;
    } else {
        group[L] = 0;
        bruteForce(L + 1);
        group[L] = 1;
        bruteForce(L + 1);
    }
}

void Input();

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    Input();
    bruteForce(1);

    if (ans == INT_MAX) cout << -1 << '\n';
    else cout << ans << '\n';

    return 0;
}


void Input() {
    cin >> N;

    for (int i = 1; i <= N; i++)
        cin >> population[i];

    for (int i = 1; i <= N; i++) {
        int adjacentNumbers;
        cin >> adjacentNumbers;

        for (int j = 0; j < adjacentNumbers; j++) {
            int num;
            cin >> num;
            adj[i].push_back(num);
        }
    }
}

 

반응형
반응형

https://www.acmicpc.net/problem/15685

 

15685번: 드래곤 커브

첫째 줄에 드래곤 커브의 개수 N(1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 드래곤 커브의 정보가 주어진다. 드래곤 커브의 정보는 네 정수 x, y, d, g로 이루어져 있다. x와 y는 드래곤 커

www.acmicpc.net

1.문제설명

드래곤 커브가 움직이는 방향을 기준을 바탕으로

드래곤 커브 규칙에 따라 지나는 지점을 표시한 후

사각형을 검사하면서 네 꼭짓점이 모두 지나간 점인지 확인해서 풀 수 있다.

 

 


 

2.문제풀이코드 C++

#include <bits/stdc++.h>
using namespace std;


int N;
bool point[103][103];

//방향벡터 모음
// 0 -> 90도회전: 1
// 0 1 -> 90도회전 : 1 2
// 0 1 2 1 -> 90도 회전 : 1 2 3 2
// 0 1 2 1 2 3 2 1
vector<int> getDirectionVector(int d, int g){
    vector<int> direction;
    direction.push_back(d);

    //90도 회전하고 역순으로 추가 g번반복
    for(int i=0; i<g; i++){
        vector<int> newDirection;

        for(int j=0; j<direction.size(); j++){
            newDirection.push_back((direction[j]+1)%4);
        }

        for(int j=direction.size()-1; j>=0; j--){
            direction.push_back(newDirection[j]);
        }
    }

    return direction;
}

void dotPoint(int x, int y, const vector<int>& direction){
    int curX = x, curY = y;

    point[curX][curY] = 1;

    for(int i=0; i<direction.size();i++){
        if(direction[i]==0){
            curY += 1;
        }
        else if(direction[i]==1){
            curX -= 1;
        }
        else if(direction[i]==2){
            curY -= 1;
        }
        else if(direction[i]==3){
            curX += 1;
        }

        point[curX][curY] = 1;
    }
}

int countRectangle(){
    int ans = 0;
    for(int i=0; i<=99; i++){
        for(int j=0; j<=99; j++){
            if(point[i][j] && point[i+1][j] && point[i][j+1] && point[i+1][j+1]){
                ans++;
            }
        }
    }
    return ans;
}



void dragonCurve(int x, int y, int d, int g){
    vector<int> direction = getDirectionVector(d,g);
    dotPoint(x,y, direction);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> N;
    for(int i=0; i<N; i++){
        int x, y, d, g;
        cin >> x >> y >> d >> g;
        dragonCurve(y,x,d,g);
    }

    cout << countRectangle() << '\n';

    return 0;
}
반응형

+ Recent posts