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
79 changes: 79 additions & 0 deletions [최단경로]1108/1063.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// Created by LG on 2021-11-07.
//
#include <iostream>

using namespace std;

void move(string dir, int &r, int &c){

if(dir == "R"){
r += 0;
c += 1;
}
else if(dir == "L"){
r += 0;
c += -1;
}
else if(dir == "B"){
r += -1;
c += 0;
}
else if(dir == "T"){
r += 1;
c += 0;
}
else if(dir == "RT"){
r += 1;
c += 1;
}
else if(dir == "LT"){
r += 1;
c += -1;
}
else if(dir == "RB"){
r += -1;
c += 1;
}
else{
r += -1;
c += -1;
}
}

int main(){
int r_king, c_king, r_rock, c_rock, n;
string king, rock;
cin >> king >> rock >> n;

r_king = king[1]-'0';
c_king = king[0] - 'A';
r_rock = rock[1]-'0';
c_rock = rock[0] - 'A';

while(n--){
string dir;
cin >> dir;
int r_temp = r_king;
int c_temp = c_king;
move(dir, r_temp, c_temp);
if(r_temp < 1 || r_temp > 8 || c_temp < 0 || c_temp > 7)
continue;
if(r_temp == r_rock && c_temp == c_rock){
int r_rocktemp = r_rock;
int c_rocktemp = c_rock;
move(dir, r_rocktemp, c_rocktemp);
if(r_rocktemp < 1 || r_rocktemp > 8 || c_rocktemp < 0 || c_rocktemp > 7)
continue;
r_rock = r_rocktemp;
c_rock = c_rocktemp;
}
r_king = r_temp;
c_king = c_temp;
}

cout << char(c_king + 'A') << r_king << '\n';
cout << char(c_rock + 'A') << r_rock;

return 0;
}
60 changes: 60 additions & 0 deletions [최단경로]1108/1238.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Created by LG on 2021-11-07.
//
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int, int> ci;
const int INF = 1e5*2;

vector<int> dijkstra(int n, int start, vector<vector<ci>> &graph){
vector<int> dist(n+1, INF);
priority_queue<ci, vector<ci>, greater<>> pq;

dist[start] = 0;
pq.push({0, start});

while(!pq.empty()){
int weight = pq.top().first;
int node = pq.top().second;
pq.pop();

if(weight > dist[node])
continue;
for(int i=0; i<graph[node].size(); i++){
int next_node = graph[node][i].first;
int next_weight = weight + graph[node][i].second;
if(dist[next_node] > next_weight){
dist[next_node] = next_weight;
pq.push({next_weight, next_node});
}
}
}
return dist;
}

int main(){
int n, m, x, u, v, w;
cin >> n >> m >> x;

vector<vector<ci>> graph(n+1, vector<ci>(0));
while(m--){
cin >> u >> v >> w;
graph[u].emplace_back(v, w); //u에서 v로 가는 가중치 w인 간선
}

vector<int> back = dijkstra(n, x, graph);
vector<vector<int>> go(n+1, vector<int>());
for(int i=1; i<=n; i++){
go[i] = dijkstra(n, i, graph);
}

int ans = 0;
for(int i=1; i<=n; i++){
ans = max(go[i][x] + back[i], ans);
}
cout << ans;
}
48 changes: 48 additions & 0 deletions [최단경로]1108/15685.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Created by LG on 2021-11-07.
//
#include <iostream>
#include <vector>

using namespace std;

int main(){
int n;
cin >> n;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
bool check [101][101] = {false, };

while(n--){
int x, y, d, g;
cin >> x >> y >> d >> g;
vector<int> dragon;
check[x][y] = true;
x += dx[d];
y += dy[d];
check[x][y] = true;
dragon.push_back(d);
for(int i=0; i<g; i++){
for(int j=(1<<i)-1; j>=0; j--){
int t = (dragon[j]+1)%4;
x += dx[t];
y += dy[t];
check[x][y] = true;
dragon.push_back(t);
}
}
}

int cnt = 0;
for(int i=0; i<100; i++){
for(int j=0; j<100; j++){
if(check[i][j] && check[i + 1][j] && check[i][j + 1] && check[i + 1][j + 1])
cnt++;
}
}

cout << cnt;

return 0;
}
47 changes: 47 additions & 0 deletions [최단경로]1108/1613.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream> //표준 입력스트림 사용
#include <vector> //vector

using namespace std; //표준 네임스페이스 사용

void floydWarshall(int n, vector<vector<bool>> &graph) { //사건 수 n, 알고 있는 전후관계 그래프
for (int k = 1; k <= n; k++) { //모든 k에 대해서
for (int i = 1; i <= n; i++) { //어떤 i와
for (int j = 1; j <= n; j++) { //어떤 j에 대해서
if (graph[i][k] && graph[k][j]) //i->k, k->j 전후관계 있는 경우
graph[i][j] = true; //i와 j는 전후관계에 있음
}
}
}
}

int main() {
ios::sync_with_stdio(false);//입출력 속도 개선
cin.tie(NULL); //입출력 속도 개선

int n, k, s, a, b; //사건 수, 전후 관계 수, 알고 싶은 사건 수, 사건 a와 b

//입력
cin >> n >> k; //사건 수, 전후 관계 수
vector<vector<bool>> graph(n + 1, vector<bool>(n + 1, false)); // false이면 전후관계 아님, true이면 전후관계
while (k--) { // 전후 관계 수만큼
cin >> a >> b; //a 와 b 입력받음
graph[a][b] = true; //전후관계 -> 단방향 그래프
}

//연산
floydWarshall(n, graph); //전후관계 그래프 연산

//입력 & 출력
cin >> s; //사건 수 입력
while (s--) { //사건 수만큼
cin >> a >> b; //확인할 a와 b입력받음
if (graph[a][b]) //graph[a][b]가 true라면
cout << "-1"; //a 후에 b가 발생함
else if (graph[b][a]) //graph[b][a]가 true라면
cout << "1"; //b 후에 a가 발생함
else
cout << "0"; // 둘 다 false라면 전후관계 알 수 없음
cout << '\n'; // 출력 형식
}
return 0; //종료
}
86 changes: 86 additions & 0 deletions [최단경로]1108/4485.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// Created by LG on 2021-11-07.
//
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int, int> ci;
const int INF = 1e5;

vector<int> dijkstra(int n, int start, vector<vector<ci>> &graph){
vector<int> dist(n+1, INF);
priority_queue<ci, vector<ci>, greater<>> pq;

dist[1] = start;
pq.push({start, 1});

while(!pq.empty()){
int weight = pq.top().first;
int node = pq.top().second;
pq.pop();

if(weight > dist[node])
continue;
for(int i=0; i<graph[node].size(); i++){
int next_node = graph[node][i].first;
int next_weight = weight + graph[node][i].second;
if(dist[next_node] > next_weight){
dist[next_node] = next_weight;
pq.push({next_weight, next_node});
}
}
}
return dist;

}

int main(){

int n;
cin >> n;
int t = 1;
while(n != 0){
vector<int> matrix(n*n+1);
for(int i=1; i<=n*n; i++){
cin >> matrix[i];
}

vector<vector<ci>> graph(n*n+1, vector<ci>(0));
for(int i=1; i<n; i++){
graph[i].emplace_back(i+1, matrix[i+1]);
graph[i].emplace_back(i+n, matrix[i+n]);
}
for(int i=n+1; i<=n*n-n; i++){
if(i%n == 1){
graph[i].emplace_back(i+1, matrix[i+1]);
graph[i].emplace_back(i+n, matrix[i+n]);
graph[i].emplace_back(i-n, matrix[i-n]);
}
else if(i%n == 0){
graph[i].emplace_back(i-1, matrix[i-1]);
graph[i].emplace_back(i+n, matrix[i+n]);
graph[i].emplace_back(i-n, matrix[i-n]);
}
else{
graph[i].emplace_back(i-1, matrix[i-1]);
graph[i].emplace_back(i+1, matrix[i+1]);
graph[i].emplace_back(i+n, matrix[i+n]);
graph[i].emplace_back(i-n, matrix[i-n]);
}
}
for(int i=n*n-n; i<n*n; i++){
graph[i].emplace_back(i-n, matrix[i-n]);
graph[i].emplace_back(i+1, matrix[i+1]);
}
graph[n].emplace_back(2*n, matrix[2*n]);

vector<int> ans = dijkstra(n*n, matrix[1], graph);
cout << "Problem " << t << ": " << ans[n*n] << '\n';
t++;
cin >> n;
}

}