Skip to content

Commit ec9b3f7

Browse files
authored
Merge pull request #73 from heon0945/songheon
2 parents c56c945 + 4a34287 commit ec9b3f7

File tree

56 files changed

+5428
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+5428
-0
lines changed

songheon/week5/study/BOJ_21608.cc

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
5+
using namespace std;
6+
7+
8+
struct student {
9+
int num = -1;
10+
int friends[4] = { -1, -1, -1, -1 };
11+
};
12+
13+
struct cell {
14+
int x, y;
15+
bool take = false;
16+
int fav = 0;
17+
int emp = 0;
18+
};
19+
20+
struct compare {
21+
bool operator()(cell a, cell b) {
22+
if (a.fav != b.fav) {
23+
return a.fav < b.fav;
24+
}
25+
else {
26+
if (a.emp != b.emp)
27+
return a.emp < b.emp;
28+
else {
29+
if (a.x != b.x) {
30+
return a.x > b.x;
31+
}
32+
else
33+
return a.y > b.y;
34+
}
35+
}
36+
}
37+
};
38+
39+
int n;
40+
int map[20][20]; //finally seat number
41+
student students[401];
42+
queue<student> q;
43+
int dx[4] = { -1, 0, 1, 0 };
44+
int dy[4] = { 0, 1, 0, -1 };
45+
priority_queue<cell, vector<cell>, compare> info;
46+
47+
48+
bool OOB(int x, int y) {
49+
if (x >= 0 && x < n && y >= 0 && y < n)
50+
return false;
51+
return true;
52+
}
53+
54+
int main() {
55+
ios_base::sync_with_stdio(false);
56+
cin.tie(NULL);
57+
cout.tie(NULL);
58+
59+
cin >> n;
60+
61+
for (int i = 1; i <= n * n; i++) {
62+
int num, f0, f1, f2, f3;
63+
cin >> num >> f0 >> f1 >> f2 >> f3;
64+
students[num] = student();
65+
students[num].num = num;
66+
students[num].friends[0] = f0;
67+
students[num].friends[1] = f1;
68+
students[num].friends[2] = f2;
69+
students[num].friends[3] = f3;
70+
71+
q.push(students[num]);
72+
73+
}
74+
75+
76+
77+
while (!q.empty()) {
78+
student cur = q.front();
79+
q.pop();
80+
81+
//initial map info
82+
while (!info.empty()) {
83+
info.pop();
84+
}
85+
86+
//update map info
87+
for (int i = 0; i < n; i++) {
88+
for (int j = 0; j < n; j++) {
89+
cell tmp;
90+
tmp.x = i;
91+
tmp.y = j;
92+
if (map[i][j] == 0)
93+
tmp.take = false;
94+
else
95+
tmp.take = true;
96+
97+
if (tmp.take == false) {
98+
//4search
99+
for (int k = 0; k < 4; k++) {
100+
int tx = tmp.x + dx[k];
101+
int ty = tmp.y + dy[k];
102+
if (!OOB(tx, ty)) {
103+
if (map[tx][ty] == 0)
104+
tmp.emp++;
105+
else {
106+
for (int f = 0; f < 4; f++) {
107+
if (cur.friends[f] == map[tx][ty]) {
108+
tmp.fav++;
109+
break;
110+
}
111+
}
112+
}
113+
}
114+
}
115+
info.push(tmp);
116+
}
117+
118+
}
119+
}
120+
//find best place
121+
map[info.top().x][info.top().y] = cur.num;
122+
123+
////print
124+
//for (int i = 0; i < n; i++) {
125+
// for (int j = 0; j < n; j++) {
126+
// cout << map[i][j] << " ";
127+
// }
128+
// cout << '\n';
129+
//}
130+
131+
//cout << '\n';
132+
}
133+
134+
/*cout << " test " << '\n';*/
135+
136+
//만족도 검사
137+
int answer = 0;
138+
for (int i = 0; i < n; i++) {
139+
for (int j = 0; j < n; j++) {
140+
int cnt = 0;
141+
for (int k = 0; k < 4; k++) {
142+
int tx = i + dx[k];
143+
int ty = j + dy[k];
144+
if (!OOB(tx, ty)) {
145+
for (int f = 0; f < 4; f++) {
146+
if (students[map[i][j]].friends[f] == map[tx][ty]) {
147+
cnt++;
148+
break;
149+
}
150+
}
151+
}
152+
}
153+
if (cnt == 1)
154+
answer += 1;
155+
else if(cnt == 2) {
156+
answer += 10;
157+
}
158+
else if (cnt == 3) {
159+
answer += 100;
160+
}
161+
else if (cnt == 4) {
162+
answer += 1000;
163+
}
164+
}
165+
}
166+
167+
168+
169+
170+
171+
cout << answer;
172+
173+
174+
175+
return 0;
176+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
static int n, m, r;
7+
static int map[][], buffer[][]; //기존 배열, 정답 저장 버퍼
8+
9+
public static void main(String[] args) {
10+
11+
Scanner sc = new Scanner(System.in);
12+
StringBuilder sb = new StringBuilder();
13+
14+
//입력
15+
n = sc.nextInt();
16+
m = sc.nextInt();
17+
r = sc.nextInt();
18+
map = new int[100][100];
19+
buffer = new int[100][100];
20+
for (int i = 0; i < n; i++) {
21+
for (int j = 0; j < m; j++) {
22+
map[i][j] = sc.nextInt();
23+
}
24+
}
25+
26+
//회전하기
27+
for (int i = 0; i < r; i++) {
28+
rotate(sc.nextInt());
29+
}
30+
31+
//정답 출력
32+
for (int i = 0; i < n; i++) {
33+
for (int j = 0; j < m; j++) {
34+
sb.append(map[i][j]).append(" ");
35+
}
36+
sb.append('\n');
37+
}
38+
System.out.println(sb);
39+
40+
}
41+
42+
static void rotate(int type) {
43+
44+
//초기화
45+
for (int i = 0; i < 100; i++) {
46+
for (int j = 0; j < 100; j++) {
47+
buffer[i][j] = 0;
48+
}
49+
}
50+
51+
52+
if (type == 1) {
53+
//상하반전
54+
for(int i = 0; i < n; i++) {
55+
for(int j = 0; j < m; j++) {
56+
buffer[n-1-i][j] = map[i][j];
57+
}
58+
}
59+
60+
} else if (type == 2) {
61+
//좌우 반전
62+
for(int i = 0; i < n; i++) {
63+
for(int j = 0; j < m; j++) {
64+
buffer[i][m-1-j] = map[i][j];
65+
}
66+
}
67+
} else if (type == 3) {
68+
//오른쪽 회전
69+
int tn = m;
70+
int tm = n;
71+
for(int i = 0; i < n; i++) {
72+
for(int j = 0; j < m; j++) {
73+
buffer[j][tm-1-i] = map[i][j];
74+
}
75+
}
76+
n = tn;
77+
m = tm;
78+
79+
80+
} else if (type == 4) {
81+
//왼쪽 회전
82+
int tn = m;
83+
int tm = n;
84+
for(int i = 0; i < n; i++) {
85+
for(int j = 0; j < m; j++) {
86+
buffer[tn-1-j][i] = map[i][j];
87+
}
88+
}
89+
n = tn;
90+
m = tm;
91+
} else if (type == 5) {
92+
//시계 방향
93+
//1구역
94+
for(int i = 0; i < n/2; i++) {
95+
for(int j = 0; j < m/2; j++) {
96+
buffer[i][j + m/2] = map[i][j];
97+
}
98+
}
99+
//2구역
100+
for(int i = 0; i < n/2; i++) {
101+
for(int j = m/2; j < m; j++) {
102+
buffer[i + n/2][j] = map[i][j];
103+
}
104+
}
105+
//3구역
106+
for(int i = n/2; i < n; i++) {
107+
for(int j = m/2; j < m; j++) {
108+
buffer[i][j - m/2] = map[i][j];
109+
}
110+
}
111+
//4구역
112+
for(int i = n/2; i < n; i++) {
113+
for(int j = 0; j < m/2; j++) {
114+
buffer[i - n/2][j] = map[i][j];
115+
}
116+
}
117+
} else if (type == 6) {
118+
//반시계 방향
119+
//1구역
120+
for(int i = 0; i < n/2; i++) {
121+
for(int j = 0; j < m/2; j++) {
122+
buffer[i + n/2][j] = map[i][j];
123+
}
124+
}
125+
//2구역
126+
for(int i = 0; i < n/2; i++) {
127+
for(int j = m/2; j < m; j++) {
128+
buffer[i][j - m/2] = map[i][j];
129+
}
130+
}
131+
//3구역
132+
for(int i = n/2; i < n; i++) {
133+
for(int j = m/2; j < m; j++) {
134+
buffer[i - n/2][j] = map[i][j];
135+
}
136+
}
137+
//4구역
138+
for(int i = n/2; i < n; i++) {
139+
for(int j = 0; j < m/2; j++) {
140+
buffer[i][j + m/2] = map[i][j];
141+
}
142+
}
143+
}
144+
//옮기기
145+
for (int i = 0; i < 100; i++) {
146+
for (int j = 0; j < 100; j++) {
147+
map[i][j] = buffer[i][j];
148+
}
149+
}
150+
}
151+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Main {
5+
static int n;
6+
static int[][] map;
7+
static String answer = "";
8+
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
12+
13+
n = Integer.parseInt(br.readLine());
14+
map = new int[n][n];
15+
for(int i = 0; i < n; i++) {
16+
String str = br.readLine();
17+
for(int j = 0; j < n; j++) {
18+
map[i][j] = str.charAt(j) - '0';
19+
}
20+
}
21+
22+
search(0, 0, n);
23+
System.out.print(answer);
24+
}
25+
26+
static void search(int r, int c, int size) {
27+
int sum = 0;
28+
for(int x = r; x < r + size; x++) {
29+
for(int y = c; y < c + size; y++) {
30+
sum += map[x][y];
31+
}
32+
}
33+
34+
if(sum == 0) {
35+
answer += "0";
36+
}
37+
else if(sum == size * size) {
38+
answer += "1";
39+
}
40+
else {
41+
int half = size / 2;
42+
answer += "(";
43+
search(r, c, half);
44+
search(r, c + half, half);
45+
search(r + half, c, half);
46+
search(r + half, c + half, half);
47+
answer += ")";
48+
}
49+
50+
}
51+
}

0 commit comments

Comments
 (0)