#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
 
int main(void)
{
//    freopen("B1484_input.txt", "r", stdin);
    
    long long G;
    vector<long long> result;
        
    cin >> G;
    
    long long left = 1;
    long long right = 1;
    
    while(left <= right && right <= G+1)
    {
        if(right*right - left*left > G)
        {
            left++;
        }
        else if(right*right - left*left < G)
        {
            right++;
        }
        else if(right*right - left*left == G)
        {
            result.push_back(right);
            
            right++;
        }
    }
    
    if(result.size() == 0)
    {
        cout << -1;
    }
    else
    {
        for(int i = 0; i < result.size(); i++)
        {
            cout << result[i] << endl;
        }
    }
 
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
 
int N, K;
int arr[1000010];
 
int main(void)
{
//    freopen("B15565_input.txt", "r", stdin);
    
    cin >> N >> K;
    
    for(int i = 1; i <= N; i++)
    {
        cin >> arr[i];
    }
    
    int left = 1;
    int right = 1;
    int lionCnt = 0;
    int Min = 99999999;
    
    while(left <= right && right <= N+1)
    {
        if(lionCnt < K)
        {
            if(arr[right] == 1)
            {
                lionCnt++;
                right++;
            }
            else
            {
                right++;
            }
        }
        else if(lionCnt > K)
        {
            if(arr[left] == 1)
            {
                lionCnt--;
                left++;
            }
            else
            {
                left++;
            }
        }
        else if(lionCnt == K)
        {
            if(Min > right-left)
            {
                Min = right-left;
            }
            
            if(arr[left] == 1)
            {
                lionCnt--;
                left++;
            }
            else
            {
                left++;
            }
        }
    }
    
    if(Min == 99999999)
    {
        cout << -1;
    }
    else
    {
        cout << Min;    
    }
    
    return 0;
}
cs
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
 
int N, M;
int A[10010];
int ans;
 
int main(void)
{
//    freopen("B2003_input.txt", "r", stdin);
    
    cin >> N >> M;
    
    int left = 1;
    int right = 1;
    int sum = 0;
    
    for(int i = 1; i <= N; i++)
    {
        cin >> A[i];
    }
    
    while(left <= right && right <= N+1)
    {
        if(sum < M)
        {
            sum += A[right++];
        }
        else if(sum == M)
        {
            ans++;
            sum += A[right++];    
        }
        else
        {
            sum -= A[left++];
        }
    }
    
    cout << ans;
    
    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