Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions week4/KCM/[W4]17387.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
using namespace std;

struct point {
long long x;
long long y;
};

int ccw(point a, point b, point c) {
long long s = a.x * b.y + b.x * c.y + c.x * a.y;
s -= b.x * a.y + c.x * b.y + a.x * c.y;

if (s > 0)
return 1;
else if (s == 0)
return 0;
else
return -1;
}

bool isCrossed(point p1, point p2, point p3, point p4) {
int p1p2, p3p4;

p1p2 = ccw(p1, p2, p3) * ccw(p1, p2, p4);
p3p4 = ccw(p3, p4, p1) * ccw(p3, p4, p2);

if (p1p2 == 0 && p3p4 == 0) {
if (p3.x == p2.x && p1.x == p4.x)
return p3.y <= p2.y && p1.y <= p4.y;

return p3.x <= p2.x && p1.x <= p4.x;
}

return p1p2 <= 0 && p3p4 <= 0;
}

int main() {
point p1, p2, p3, p4;
point tmp;
int result;

cin >> p1.x >> p1.y >> p2.x >> p2.y;
cin >> p3.x >> p3.y >> p4.x >> p4.y;

if (p1.x > p2.x || (p1.x == p2.x && p1.y > p2.y)) {
tmp = p1;
p1 = p2;
p2 = tmp;
}

if (p3.x > p4.x || (p3.x == p4.x && p3.y > p4.y)) {
tmp = p3;
p3 = p4;
p4 = tmp;
}

result = isCrossed(p1, p2, p3, p4);

cout << result;
}
70 changes: 70 additions & 0 deletions week4/KCM/[W4]_11048.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <vector>
#include <queue>
#include <math.h>
using namespace std;

vector<vector<int>> candyNum;
vector<vector<bool>> isVisited;

int candyMax(int h, int w) {
int x, y, len;
queue<pair<int, int>> q;

q.push({ 0, 0 });
isVisited[0][0] = true;

while (!q.empty()) {
len = q.size();

for (int i = 0; i < len; i++) {
x = q.front().second;
y = q.front().first;
q.pop();

if (x + 1 < w && !isVisited[y][x + 1]) {
if (y == 0) {
candyNum[y][x + 1] += candyNum[y][x];
}
else {
candyNum[y][x + 1] += max(candyNum[y][x], candyNum[y - 1][x + 1]);
}

q.push({ y, x + 1 });
isVisited[y][x + 1] = true;
}

if (y + 1 < h && !isVisited[y + 1][x]) {
if (x == 0) {
candyNum[y + 1][x] += candyNum[y][x];
}
else {
candyNum[y + 1][x] += max(candyNum[y][x], candyNum[y + 1][x - 1]);
}

q.push({ y + 1, x });
isVisited[y + 1][x] = true;
}
}
}

return candyNum[h - 1][w - 1];
}

int main() {
int n, m;

cin >> n >> m;

vector<int> v(m, 0);
candyNum.assign(n, v);

vector<bool> v2(m, false);
isVisited.assign(n, v2);

for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> candyNum[i][j];

cout << candyMax(n, m);
}
88 changes: 88 additions & 0 deletions week4/KCM/[W4]_1765.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <iostream>
#include <vector>
using namespace std;

class teamMaking {
vector<int> team;
vector<bool> isVisited;
vector<vector<pair<int, char>>> graph;
int numOfTeam;
public:
teamMaking(int n = 0) {
vector<pair<int, char>> v;
graph.assign(n + 1, v);

numOfTeam = n;

for (int i = 0; i <= n; i++) {
team.push_back(i);
}

isVisited.assign(n + 1, false);
}
void inputGraph(int);
int makeTeam();
void dfs(int, int, char);
};

void teamMaking::inputGraph(int n) {
char cn;
int p, q;

for (int i = 0; i < n; i++) {
cin >> cn >> p >> q;

graph[p].push_back({ q, cn });
graph[q].push_back({ p, cn });
}
}

int teamMaking::makeTeam() {
int teams = numOfTeam;

for (int i = 1; i <= teams; i++) {
if (i == team[i]) {
isVisited.assign(teams + 1, false);
isVisited[i] = true;
dfs(i, i, 'F');
}
}

return numOfTeam;
}

void teamMaking::dfs(int cur, int root, char con) {
for (int i = 0; i < graph[cur].size(); i++) {
if (root != team[graph[cur][i].first] && graph[cur][i].second == con) {
if (team[graph[cur][i].first] != graph[cur][i].first) {
team[cur] = team[graph[cur][i].first];
numOfTeam--;
}
else {
team[graph[cur][i].first] = root;
isVisited[graph[cur][i].first] = true;
numOfTeam--;

dfs(graph[cur][i].first, root, 'F');
}
}
else if (con == 'F' && graph[cur][i].second == 'E') {
dfs(graph[cur][i].first, root, 'E');
}
}
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);

int n, m;

cin >> n;
teamMaking tmk(n);

cin >> m;
tmk.inputGraph(m);

cout << tmk.makeTeam();
}
130 changes: 130 additions & 0 deletions week4/KCM/[W4]_2174.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <iostream>
#include <vector>
using namespace std;

class RobotSimulation {
struct robot {
int x;
int y;
int dir;
};
int w, h;
vector<vector<int>> land;
vector<robot> info;
public:
void setLand(int, int);
void inputRobot(int);
void inputCommand(int);
};

void RobotSimulation::setLand(int x, int y) {
w = x;
h = y;

vector<int> v(x + 2, -1);
land.push_back(v);

v.assign(x + 2, 0);
v[0] = -1;
v[x + 1] = -1;
for (int i = 0; i < y; i++)
land.push_back(v);

v.assign(x + 2, -1);
land.push_back(v);
}

void RobotSimulation::inputRobot(int n) {
int x, y;
char dir;
robot r;

info.assign(n + 1, r);

for (int i = 1; i <= n; i++) {
cin >> x >> y >> dir;

land[y][x] = i;

info[i].x = x;
info[i].y = y;

if (dir == 'E')
info[i].dir = 0;
else if (dir == 'N')
info[i].dir = 1;
else if (dir == 'W')
info[i].dir = 2;
else
info[i].dir = 3;
}
}

void RobotSimulation::inputCommand(int n) {
int robotNum, rep;
char cmd;
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
int tx, ty;
int curX, curY;
int landInfo;

for (int i = 0; i < n; i++) {
cin >> robotNum >> cmd >> rep;

if (cmd == 'L') {
info[robotNum].dir = (info[robotNum].dir + rep) % 4;
}
else if (cmd == 'R') {
info[robotNum].dir = (info[robotNum].dir - rep + 100) % 4;
}
else {
curX = info[robotNum].x;
curY = info[robotNum].y;

tx = curX;
ty = curY;

for (int j = 0; j < rep; j++) {
tx += dx[info[robotNum].dir];
ty += dy[info[robotNum].dir];

landInfo = land[ty][tx];

if (landInfo == -1) {
cout << "Robot " << robotNum << " crashes into the wall";
return;
}
else if (landInfo != 0) {
cout << "Robot " << robotNum << " crashes into robot " << landInfo;
return;
}
}

land[ty][tx] = robotNum;
land[curY][curX] = 0;

info[robotNum].x = tx;
info[robotNum].y = ty;
}
}

cout << "OK";
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);

int A, B, N, M;
RobotSimulation rs;

cin >> A >> B;

rs.setLand(A, B);

cin >> N >> M;

rs.inputRobot(N);
rs.inputCommand(M);
}
30 changes: 30 additions & 0 deletions week5/KCM/[W5]_13458.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <vector>
using namespace std;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);

int N;
int B, C;
long long sum = 0;

cin >> N;
vector<int> examinee(N);

for (int i = 0; i < N; i++)
cin >> examinee[i];

cin >> B >> C;

sum += N;
for (int i = 0; i < N; i++) {
examinee[i] -= B;

if (examinee[i] > 0)
sum += (examinee[i] - 1) / C + 1;
}

cout << sum;
}