Skip to content

220731/이현동 #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2022
Merged
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
39 changes: 39 additions & 0 deletions [Week15 - BruteForce]/이현동/14500.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys
input = sys.stdin.readline


def dfs(x, y, idx, total):
global ans
if ans >= total + max_val*(3-idx):
return
if idx == 3:
ans = max(total, ans)
return
for i in range(4):
rx = x + dx[i]
ry = y + dy[i]
if 0 <= rx < n and 0 <= ry < m and visited[rx][ry] == 0:
if idx == 1:
visited[rx][ry] = 1
dfs(x, y, idx+1, total+graph[rx][ry])
visited[rx][ry] = 0
visited[rx][ry] = 1
dfs(rx, ry, idx+1, total+graph[rx][ry])
visited[rx][ry] = 0


n, m = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(n)]
ans = 0
max_val = max(map(max, graph))
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
visited = [[0]*m for _ in range(n)]

for i in range(n):
for j in range(m):
visited[i][j] = 1
dfs(i, j, 0, graph[i][j])
visited[i][j] = 0

print(ans)
Binary file added [Week15 - BruteForce]/이현동/2961
Binary file not shown.
54 changes: 54 additions & 0 deletions [Week15 - BruteForce]/이현동/2961.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

using pii = std ::pair<int, int>;

int N;
vector<pii> ingred;
vector<pii> res(11);
bool visited[11];
int ans = 2e9;

void dfs(int idx, int depth)
{
if(depth > 0){
int S, B;
S = res[0].first;
B = res[0].second;
for (int i = 1; i < depth;i++){
S *= res[i].first;
B += res[i].second;
}
ans = min(ans, abs(B - S));
}

if(depth == N)
return;

for (int i = idx; i < N;i++){
res[depth] = {ingred[i].first, ingred[i].second};
dfs(i + 1, depth + 1);

}
}


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

cin >> N;

int a, b;
for (int i = 0;i < N;i++){
cin >> a >> b;
ingred.push_back({a, b});
}

dfs(0, 0);
cout << ans;

return 0;
}
Empty file.