#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
 
int N;
int map[25][25];
int mapCopy[25][25];
int candidate[10];
int Max;
 
void reset()
{
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            map[i][j] = mapCopy[i][j];
        }
    }
}
 
int find()
{
    int val = 0;
    
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if(val < map[i][j])
            {
                val = map[i][j];
            }
        }
    }
    
    return val;
}
 
void move()
{
    for(int k = 0; k < 5; k++)
    {        
        // 오른쪽 
        if(candidate[k] == 0)
        {
            for(int i = 1; i <= N; i++)
            {
                deque<int> q;        // 유효한 숫자만 저장하는 덱 -> 0은 저장X 
                bool merge = false// 이미 합쳐졌던 블록인지 확인 
                
                for(int j = N; j >= 1; j--)
                {
                    // 마지막 숫자와 현재 숫자가 같으면 합침 + 전에 합쳐진 블록이 아니면
                    if(q.size() >= 1 && q[q.size()-1== map[i][j] && merge == false)
                    {
                        q[q.size()-1+= map[i][j];
                        merge = true;
                    }
                    // 0인 경우는 저장X 
                    else if(map[i][j] != 0)
                    {
                        q.push_back(map[i][j]);        
                        merge = false;
                    }
                }        
                
                for(int j = N; j >= 1; j--)
                {
                    // 유효한 숫자가 있는 구간에서는 차례대로 넣어줌 
                    if(q.size() >= 1)
                    {
                        map[i][j] = q.front();
                        q.pop_front();
                    }
                    // 유효한 숫자가 아닌 구간에서는 0을 넣어줌 
                    else
                    {
                        map[i][j] = 0;
                    }
                }
            } 
        }
        // 왼쪽
        else if(candidate[k] == 1)
        {        
            for(int i = 1; i <= N; i++)
            {
                deque<int> q;        
                bool merge = false
            
                for(int j = 1; j <= N; j++)
                {
                    if(q.size() >= 1 && q[q.size()-1== map[i][j] && merge == false)
                    {
                        q[q.size()-1+= map[i][j];
                        merge = true;
                    }
                    else if(map[i][j] != 0)
                    {
                        q.push_back(map[i][j]);        
                        merge = false;
                    }
                }        
                
                for(int j = 1; j <= N; j++)
                {
                    if(q.size() >= 1)
                    {
                        map[i][j] = q.front();
                        q.pop_front();
                    }
                    else
                    {
                        map[i][j] = 0;
                    }
                }
            } 
        } 
        // 위쪽 
        else if(candidate[k] == 2)
        {                
            for(int j = 1; j <= N; j++)
            {
                deque<int> q;
                bool merge = false;
            
                for(int i = 1; i <= N; i++)
                {
                    if(q.size() >= 1 && q[q.size()-1== map[i][j] && merge == false)
                    {
                        q[q.size()-1+= map[i][j];
                        merge = true;
                    }
                    else if(map[i][j] != 0)
                    {
                        q.push_back(map[i][j]);        
                        merge = false;
                    }
                }        
                
                for(int i = 1; i <= N; i++)
                {
                    if(q.size() >= 1)
                    {
                        map[i][j] = q.front();
                        q.pop_front();
                    }
                    else
                    {
                        map[i][j] = 0;
                    }
                }
            } 
        }
        // 아래쪽 
        else if(candidate[k] == 3)
        {
            for(int j = 1; j <= N; j++)
            {
                deque<int> q;
                bool merge = false;
            
                for(int i = N; i >= 1; i--)
                {
                    if(q.size() >= 1 && q[q.size()-1== map[i][j] && merge == false)
                    {
                        q[q.size()-1+= map[i][j];
                        merge = true;
                    }
                    else if(map[i][j] != 0)
                    {
                        q.push_back(map[i][j]);        
                        merge = false;
                    }
                }        
                
                for(int i = N; i >= 1; i--)
                {
                    if(q.size() >= 1)
                    {
                        map[i][j] = q.front();
                        q.pop_front();
                    }
                    else
                    {
                        map[i][j] = 0;
                    }
                }
            } 
        }
    }
}
 
void permutation(int cnt)
{
    if(cnt == 5)
    {
        // 이동 
        move();
        
        // 최댓값 확인 
        Max = max(Max, find());
        
        // 리셋 
        reset();
        
        return;
    }
    
    for(int i = 0; i < 4; i++)
    {
        candidate[cnt] = i;
        permutation(cnt+1);
    }
}
 
 
int main(void)
{
//    freopen("B12100_input.txt", "r", stdin);
    
    cin >> N;
    
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            cin >> map[i][j];
            mapCopy[i][j] = map[i][j];
        }
    }
    
    permutation(0);
    
    cout << Max << endl;
 
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
 
int N;
int test[1000010];
int super, sub;
long long ans;
 
int main(void)
{
//    freopen("B13458_input.txt", "r", stdin);
    
    cin >> N;
    
    for(int i = 1; i <= N; i++)
    {
        cin >> test[i];    
    }
    
    cin >> super >> sub;
    
    for(int i = 1; i <= N; i++)
    {
        test[i] -= super;
        ans++;
    }
    
    for(int i = 1; i <= N; i++)
    {
        if(test[i] <= 0)
        {
            continue;
        }
        else
        {
            if(test[i] % sub == 0)
            {
                ans += test[i] / sub;
            }
            else
            {
                ans += test[i] / sub + 1;
            }
        }
    }
    
    cout << ans;
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
 
int N;
int RotateNum;
int result;
deque<int> gear[5];
 
void Rotate(int visited[5], int num, int dir)
{
    visited[num] = 1;
    
    // 좌측 비교 
    if(num-1 >= 1 && gear[num][6!= gear[num-1][2&& visited[num-1== 0)
    {
        if(dir == 1)
        {
            Rotate(visited, num-1-1);    
        }
        else
        {
            Rotate(visited, num-11);
        }
    } 
    
    // 우측 비교 
    if(num+1 <= 4 && gear[num][2!= gear[num+1][6&& visited[num+1== 0)
    {
        if(dir == 1)
        {
            Rotate(visited, num+1-1);    
        }
        else
        {
            Rotate(visited, num+11);
        }
    } 
    
    // 시계방향 
    if(dir == 1)
    {
        int temp = gear[num].back();
        gear[num].pop_back();
        gear[num].push_front(temp); 
    }
    // 반시계방향 
    else
    {
        int temp = gear[num].front();
        gear[num].pop_front();
        gear[num].push_back(temp);
    }
}
 
int main(void)
{
//    freopen("B14891_input.txt", "r", stdin);
    
    for(int i = 1; i <= 4; i++)
    {
        string input;
        cin >> input;    
        
        for(int j = 0; j < input.size(); j++)
        {
            gear[i].push_back(input[j]-'0');
        }
    }
    
    cin >> RotateNum;
    
    while(RotateNum--)
    {
        int visited[5= {0};
        int num, dir;
        cin >> num >> dir;
        
        Rotate(visited, num, dir);
    }
    
    for(int i = 1; i <= 4; i++)
    {
        if(gear[i][0== 1)
        {
            if(i == 1)
            {
                result += 1;
            }
            else if(i == 2)
            {
                result += 2;
            }
            else if(i == 3)
            {
                result += 4;
            }
            else if(i == 4)
            {
                result += 8;
            }
        }
    }
    
    cout << result;
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
int N, treeNum, year;
vector<int> tree[15][15];
vector<int> die[15][15];
int    food[15][15];
int add_food[15][15];
int ans;
 
int dx[4= {-1100};
int dy[4= {00-11};
 
int safe(int x, int y)
{
    if(x >= 1 && y >= 1 && x <= N && y <= N)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
 
void spring()
{
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if(tree[i][j].size() != 0)
            {
                // 죽는 나무 인덱스 저장 
                int eraseIdx = -1;
                
                // 하나의 칸에 여러 개의 나무가 있다면, 나이가 어린 나무부터 양분을 먹는다.
                sort(tree[i][j].begin(), tree[i][j].end());
                
                for(int k = 0; k < tree[i][j].size(); k++)
                {
                    // 나이만큼 양분이 있어야 가능 
                    if(food[i][j] - tree[i][j][k] >= 0)
                    {
                        // 양분 먹음 
                        food[i][j] -= tree[i][j][k];
                        
                        // 나무가 자신의 나이만큼 양분을 먹고, 나이가 1 증가한다.
                        tree[i][j][k] += 1;     
                    }
                    else
                    {
                        eraseIdx = k;
                        break;
                    }
                }
                
                if(eraseIdx != -1)
                {
                    // 죽은 나무의 나이 저장 
                    for(int k = eraseIdx; k < tree[i][j].size(); k++)
                    {
                        die[i][j].push_back(tree[i][j][k]);
                    }
                    
                    tree[i][j].erase(tree[i][j].begin()+eraseIdx, tree[i][j].end());
                }
            }
        }
    }
}
 
void summer()
{
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if(die[i][j].size() != 0)
            {
                // 각각의 죽은 나무마다 나이를 2로 나눈 값이 나무가 있던 칸에 양분으로 추가된다. 
                for(int k = 0; k < die[i][j].size(); k++)
                {
                    food[i][j] += die[i][j][k] / 2;
                }
                
                die[i][j].clear();
            }
        }
    }
}
 
void fall()
{
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if(tree[i][j].size() != 0)
            {
                // 번식하는 나무는 나이가 5의 배수이어야 하며, 인접한 8개의 칸에 나이가 1인 나무가 생긴다. 
                for(int k = 0; k < tree[i][j].size(); k++)
                {
                    if(tree[i][j][k] % 5 == 0)
                    {
                        for(int m = i-1; m <= i+1; m++)
                        {
                            for(int n = j-1; n <= j+1; n++)
                            {
                                if(m == i && n == j)
                                {
                                    continue;
                                }
                                
                                if(safe(m, n) == 1)
                                {
                                    tree[m][n].push_back(1);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
 
void winter()
{
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            food[i][j] += add_food[i][j];
        }
    }
}
 
int main(void)
{
//    freopen("B15685_input.txt", "r", stdin);
    
    ios_base :: sync_with_stdio(false); 
    cin.tie(NULL); 
    cout.tie(NULL);
 
    cin >> N >> treeNum >> year;
    
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            food[i][j] = 5;
            cin >> add_food[i][j];    
        }
    }
    
    for(int i = 1; i <= treeNum; i++)
    {
        int x, y, age;
        cin >> x >> y >> age;
    
        tree[x][y].push_back(age);
    }
    
    while(year--)
    {
        spring();
        summer();
        fall();
        winter();
    }
    
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            ans += tree[i][j].size();
        }
    }
    
    cout << ans;
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
int N;
int map[300][300];
int square;
 
int dx[4= {10-10};
int dy[4= {0-101};
 
int main(void)
{
//    freopen("B15685_input.txt", "r", stdin);
    
    cin >> N;
    
    for(int k = 1; k <= N; k++)
    {
        int x, y, d, gen;
        
        cin >> x >> y >> d >> gen;
        
        x += 150;
        y += 150;
        map[x][y] = k;
        
        vector<int> dir;
        dir.push_back(d);
        
        for(int i = 1; i <= gen; i++)
        {
            vector<int> tempDir;
            
            for(int j = dir.size()-1; j >= 0; j--)
            {
                tempDir.push_back((dir[j]+1) % 4);
            }
            
            for(int j = 0; j < tempDir.size(); j++)
            {
                dir.push_back(tempDir[j]);
            }
        }
        
        for(int i = 0; i < dir.size(); i++)
        {
            x += dx[dir[i]];
            y += dy[dir[i]];
            
            map[x][y] = k;
        }
    }
    
    for(int i = 0; i < 300; i++)
    {
        for(int j = 0; j < 300; j++)
        {
            int cnt = 0;
            
            for(int m = i; m < i+2; m++)
            {
                for(int n = j; n < j+2; n++)
                {
                    if(map[m][n] != 0)
                    {
                        cnt++;            
                    }
                }
            }
            
            if(cnt == 4)
            {
                square++;
            }
        }
    }
    
    cout << square;
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
int N, L;
int map[110][110];
int ans;
 
void Rotate()
{
    int temp[110][110];
    
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            temp[i][j] = map[i][j];
        }
    }
    
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            map[i][j] = temp[N-1-j][i];
        }
    }
}
 
int main(void)
{
//    freopen("B14890_input.txt", "r", stdin);
    
    cin >> N >> L;
    
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            cin >> map[i][j];  
        }
    }
    
    for(int i = 0; i < N; i++)
    {
        int up = 0;
        
        for(int j = 0; j < N-1; j++)
        {                
            // 내리막 
            if(map[i][j]-map[i][j+1== 1)
            {
                int std = map[i][j];
                int cnt = 0;
                
                for(int k = j+1; k <= j+&& k < N; k++)
                {
                    if(std-1 == map[i][k])
                    {
                        cnt++;
                    }
                    else
                    {
                        break;
                    }
                }
                
                if(cnt != L)
                {
                    break;
                }
                else
                {
                    j += (L-1);
                    up = j+2;
                }
            }
            // 오르막 
            else if(map[i][j]-map[i][j+1== -1)
            {
                int std = map[i][j+1]; 
                int cnt = 0;
 
                for(int k = j; k > j-&& k >= up; k--)
                {
                    if(std-1 == map[i][k])
                    {
                        cnt++;
                    }
                    else
                    {
                        break;
                    }
                }
                
                if(cnt != L)
                {
                    break;
                }
            }
            // 평지 
            else if(map[i][j]-map[i][j+1== 0)
            {
                // pass
            }
            else
            {
                break;
            }    
    
            if(j >= N-2)
            {
                ans++;
                break;
            }
        }
    }
    
    // 왼쪽 90도 회전 
    Rotate();
    
    for(int i = 0; i < N; i++)
    {
        int up = 0;
        
        for(int j = 0; j < N-1; j++)
        {                
            // 내리막 
            if(map[i][j]-map[i][j+1== 1)
            {
                int std = map[i][j];
                int cnt = 0;
                
                for(int k = j+1; k <= j+&& k < N; k++)
                {
                    if(std-1 == map[i][k])
                    {
                        cnt++;
                    }
                    else
                    {
                        break;
                    }
                }
                
                if(cnt != L)
                {
                    break;
                }
                else
                {
                    j += (L-1);
                    up = j+2;
                }
            }
            // 오르막 
            else if(map[i][j]-map[i][j+1== -1)
            {
                int std = map[i][j+1]; 
                int cnt = 0;
 
                for(int k = j; k > j-&& k >= up; k--)
                {
                    if(std-1 == map[i][k])
                    {
                        cnt++;
                    }
                    else
                    {
                        break;
                    }
                }
                
                if(cnt != L)
                {
                    break;
                }
            }
            // 평지 
            else if(map[i][j]-map[i][j+1== 0)
            {
                // pass
            }
            else
            {
                break;
            }    
    
            if(j >= N-2)
            {
                ans++;
                break;
            }
        }
    }
    
    cout << ans;
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
 
int row, col;
int map[55][55];
int visited[55][55];
int x, y, dir;
int cleanCnt;
 
int dx[4= {-1010}; // 북 동 남 서 
int dy[4= {010-1}; // 북 동 남 서
 
int main(void)
{
//    freopen("B14503_input.txt", "r", stdin);
    
    cin >> row >> col;
    
    cin >> x >> y >> dir;
    
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            cin >> map[i][j];
        }
    }
    
    bool clean = true;
    int all_cleanCnt = 0;
    while(1)
    {    
        // c. 네 방향 모두 청소가 이미 되어있거나 벽인 경우에는
        if(all_cleanCnt == 4)
        {
            all_cleanCnt = 0;
            
            int nDir = (dir+2) % 4;
            int nx = x+dx[nDir];
            int ny = y+dy[nDir];
            
            // d. 뒤쪽 방향이 벽이라 후진도 할 수 없는 경우에는 작동을 멈춘다.
            if(map[nx][ny] == 1)
            {
                break;
            }
            // c. 바라보는 방향을 유지한 채로 한 칸 후진을 하고 2번으로 돌아간다.
            else
            {
                x = nx;
                y = ny;
                
                continue;
            }    
        }
        
        // 1. 현재 위치를 청소한다.    
        if(clean)
        {
            all_cleanCnt = 0;
            
            visited[x][y] = 1;
            cleanCnt++;
            clean = false;
        }
        
        // 2. 현재 위치에서 현재 방향을 기준으로 왼쪽방향부터
        int tempDir = (dir+3) % 4;
        int xpos = x+dx[tempDir];
        int ypos = y+dy[tempDir];
        
        // a. 왼쪽 방향에 아직 청소하지 않은 공간이 존재한다면, 그 방향으로 회전한 다음 한 칸을 전진하고 1번부터 진행한다.
        if(visited[xpos][ypos] == 0 && map[xpos][ypos] == 0)
        {
            dir = tempDir;
            x = xpos;
            y = ypos;
            clean = true;
            
            continue;
        }
        
        // b. 왼쪽 방향에 청소할 공간이 없다면, 그 방향으로 회전하고 2번으로 돌아간다.
        if(visited[xpos][ypos] == 1 || map[xpos][ypos] == 1)
        {            
            all_cleanCnt++;
            
            dir = tempDir;
            continue;
        }    
    }
    
    cout << cleanCnt;
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
 
int row, col;
int map[25][25];
int dice[7];
int diceX, diceY;
int commandNum;
 
int dx[5= {000-11}; // 동 서 북 남
int dy[5= {01-100}; // 동 서 북 남  
 
int safe(int x, int y)
{
    if(x >= 0 && y >= 0 && x < row && y < col)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
 
void changeDice(int command)
{
    int temp[7];
    for(int i = 1; i <= 6; i++)
    {
        temp[i] = dice[i];
    }
    
    if(command == 1)
    {
        dice[1= temp[4];
        dice[3= temp[1];
        dice[4= temp[6];
        dice[6= temp[3];
    }
    else if(command == 2)
    {
        dice[1= temp[3];
        dice[3= temp[6];
        dice[4= temp[1];
        dice[6= temp[4];
    }
    else if(command == 3)
    {
        dice[1= temp[5];
        dice[2= temp[1];
        dice[5= temp[6];
        dice[6= temp[2];
    }
    else if(command == 4)
    {
        dice[1= temp[2];
        dice[2= temp[6];
        dice[5= temp[1];
        dice[6= temp[5];
    }    
}
 
int main(void)
{
//    freopen("B14499_input.txt", "r", stdin);
    
    cin >> row >> col >> diceX >> diceY >> commandNum;
    
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            cin >> map[i][j];
        }
    }
    
    for(int i = 1; i <= commandNum; i++)
    {
        int command;
        cin >> command;
        
        diceX += dx[command];
        diceY += dy[command];
        
        if(safe(diceX, diceY) == 0)
        {
            diceX -= dx[command];
            diceY -= dy[command];
            continue;
        }
        
        changeDice(command);
        
        // 주사위를 굴렸을 때, 이동한 칸에 쓰여 있는 수가 0이면, 주사위의 바닥면에 쓰여 있는 수가 칸에 복사된다.
        if(map[diceX][diceY] == 0)
        {
            map[diceX][diceY] = dice[6];
        }
        // 0이 아닌 경우에는 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사되며, 칸에 쓰여 있는 수는 0이 된다.
        else
        {
            dice[6= map[diceX][diceY];
            map[diceX][diceY] = 0;    
        }
        
        cout << dice[1<< "\n";
    }
    
    return 0;
}
cs

+ Recent posts