Skip to content

Commit 52cacaa

Browse files
committed
Add swea
1 parent 29ee668 commit 52cacaa

File tree

4 files changed

+400
-0
lines changed

4 files changed

+400
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
Personal Solution of Algorithm Probs
33
개인적인 알고리즘 문제 풀이
44

5+
6+
## [SW Expert Academy](https://swexpertacademy.com/main/main.do)
7+
8+
### 모의 SW 역량테스트
9+
- [2382](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV597vbqAH0DFAVl) : 미생물 격리 = [풀이](https://github.com/devOTTO/Algorithm-Study/blob/master/swea/2382.cpp)
10+
- [2117](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5V61LqAf8DFAWu) : 홈 방범 서비스 = [풀이](https://github.com/devOTTO/Algorithm-Study/blob/master/swea/2117.cpp)
11+
- [1953](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PpLlKAQ4DFAUq) : 탈주범 검거 = [풀이](https://github.com/devOTTO/Algorithm-Study/blob/master/swea/1953.cpp)
12+
13+
514
## [백준 온라인 저지](https://www.acmicpc.net/)
615

716
### 테마별로 풀어보기

swea/1953.cpp

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
//
2+
// Created by 융미 on 2019-10-19.
3+
//
4+
5+
//1953 탈주범 검거 swea
6+
#include <iostream>
7+
#include <queue>
8+
#include <utility>
9+
using namespace std;
10+
11+
#define MAX 51
12+
13+
int dx[4] = {0,0,1,-1};
14+
int dy[4] = {1,-1,0,0};
15+
int arr[MAX][MAX] = {0,};
16+
int dis[MAX][MAX] = {0,};
17+
int t, n, m, r,c, l;
18+
int answer;
19+
20+
void initArr(){
21+
for(int i = 0; i<MAX; ++i)
22+
{
23+
for(int j = 0; j<MAX; ++j)
24+
{
25+
arr[i][j] = 0;
26+
dis[i][j] = 0;
27+
}
28+
}
29+
}
30+
void print(){
31+
cout << endl;
32+
for(int i = 0; i<n; ++i)
33+
{
34+
for(int j = 0; j<m; ++j){
35+
cout << dis[i][j] << ' ';
36+
}
37+
cout << endl;
38+
}
39+
40+
}
41+
bool checkGo(int x, int y, int nx, int ny, int num){
42+
bool check = false;
43+
switch (num){
44+
case 1:
45+
check = true;
46+
break;
47+
case 2: //상 하
48+
if((x + 1 == nx && y == ny) || (x-1 == nx && y == ny) ) check = true;
49+
break;
50+
case 3: //좌 우
51+
if((x == nx && y + 1 == ny) || (x == nx && y -1 == ny)) check = true;
52+
break;
53+
case 4: //하 좌
54+
if((x + 1 == nx && y == ny) || (x == nx && y - 1 == ny)) check = true;
55+
break;
56+
case 5: //상 좌
57+
if((x - 1 == nx && y == ny) || (x == nx && y - 1 == ny)) check = true;
58+
break;
59+
case 6: //상 우
60+
if((x - 1 == nx && y == ny) || (x == nx && y + 1 == ny)) check = true;
61+
break;
62+
case 7: //하 우
63+
if((x + 1 == nx && y == ny) || (x == nx && y + 1 == ny)) check = true;
64+
break;
65+
}
66+
return check;
67+
}
68+
69+
bool checkSelf(int x, int y, int num, int nx, int ny, int nnum){
70+
71+
bool check= false;
72+
73+
if(x - 1 == nx && y == ny) //
74+
{
75+
if(num == 1 || num == 2 || num == 4 || num == 7)
76+
check = checkGo(x,y,nx,ny,nnum);
77+
78+
}else if(x + 1 == nx && y == ny){ //
79+
if(num == 1 || num == 2 || num == 5 || num == 6)
80+
check = checkGo(x,y,nx,ny,nnum);
81+
82+
}else if(x == nx && y -1 == ny) { //
83+
if(num == 1 || num == 3 || num == 6 || num == 7)
84+
check = checkGo(x,y,nx,ny,nnum);
85+
86+
}else { //
87+
if(num == 1 || num == 3 || num == 4 || num == 5)
88+
check = checkGo(x,y,nx,ny,nnum);
89+
}
90+
return check;
91+
}
92+
93+
void bfs(){
94+
answer = 0;
95+
queue<pair<int,int> > q;
96+
q.push(make_pair(r,c));
97+
dis[r][c] = 1;
98+
int time= 1;
99+
while(!q.empty()){
100+
101+
pair<int,int> cur = q.front();
102+
int x = cur.first;
103+
int y = cur.second;
104+
q.pop();
105+
106+
for (int i = 0; i < 4; ++i) {
107+
int nx = x + dx[i];
108+
int ny = y + dy[i];
109+
110+
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
111+
if (arr[nx][ny] == 0 || dis[nx][ny] != 0) continue;
112+
if (!checkSelf(x, y, arr[x][y], nx, ny, arr[nx][ny])) continue;
113+
114+
dis[nx][ny] = dis[x][y] + 1;
115+
q.push(make_pair(nx, ny));
116+
117+
}
118+
119+
}
120+
121+
for(int i = 0; i<n; ++i){
122+
for(int j = 0; j<m; ++j)
123+
if(dis[i][j] >=1 && dis[i][j] <= l) ++answer;
124+
}
125+
//print();
126+
127+
}
128+
129+
int main(){
130+
cin.tie(0);
131+
ios::sync_with_stdio(0);
132+
133+
cin >> t;
134+
for(int tc = 1; tc<= t; ++tc){
135+
136+
initArr();
137+
138+
cin >> n >> m >> r >> c >> l;
139+
for(int i = 0; i<n; ++i){
140+
for(int j = 0; j<m; ++j){
141+
cin >> arr[i][j];
142+
}
143+
}
144+
bfs();
145+
cout << '#' << tc << ' ' << answer << '\n';
146+
}
147+
148+
return 0;
149+
}

swea/2117.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//
2+
// Created by 융미 on 2019-10-19.
3+
//
4+
//swea 2117 홈 방범 서비스
5+
#include <iostream>
6+
#include <vector>
7+
#include <utility>
8+
#include <queue>
9+
#include <algorithm>
10+
using namespace std;
11+
#define MAX 21
12+
13+
int tc, n, m;
14+
int answer;
15+
int arr[MAX][MAX] = {0,};
16+
int dis[MAX][MAX] = {0,};
17+
int costArr[2*MAX] = {0,};
18+
19+
int dx[4] = {0,0,1,-1};
20+
int dy[4] = {1,-1,0,0};
21+
queue<pair<int,int>> q;
22+
23+
void make_cost(){
24+
for(int i = 1; i<2*MAX; ++i){
25+
costArr[i] = i*i + (i - 1)*(i - 1);
26+
}
27+
28+
}
29+
30+
void init(){
31+
for(int i = 0; i<n; ++i)
32+
for(int j = 0; j<n; ++j)
33+
dis[i][j]=0;
34+
}
35+
36+
void print(){
37+
for(int i = 0; i<n; i++){
38+
for(int j = 0; j<n; j++)
39+
{
40+
cout << arr[i][j] << ' ';
41+
}
42+
cout << endl;
43+
}
44+
cout << endl;
45+
for(int i = 0; i<n; i++){
46+
for(int j = 0; j<n; j++)
47+
{
48+
cout << dis[i][j] << ' ';
49+
}
50+
cout << endl;
51+
}
52+
cout << endl;
53+
}
54+
55+
void bfs(int a, int b){
56+
int cnt = 0;
57+
int benefit = 0;
58+
59+
q.push(make_pair(a,b));
60+
dis[a][b]=1;
61+
62+
if(arr[a][b] == 1) cnt++;
63+
64+
65+
for(int k=1;;k++){
66+
if( q.empty() )
67+
break;
68+
69+
int size = (int)q.size();
70+
71+
72+
benefit = (cnt * m) - costArr[k];
73+
74+
if(benefit >= 0)
75+
{
76+
if(cnt > answer) answer = cnt;
77+
}
78+
79+
for(int t=0; t<size; t++){
80+
int x = q.front().first;
81+
int y = q.front().second;
82+
q.pop();
83+
84+
for(int i=0; i<4; i++){
85+
int nx = x + dx[i];
86+
int ny = y + dy[i];
87+
88+
89+
if(nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
90+
if(dis[nx][ny] != 0) continue;
91+
92+
dis[nx][ny] = dis[x][y] + 1;
93+
q.push(make_pair(nx,ny));
94+
95+
if(arr[nx][ny] == 1) ++cnt;
96+
}
97+
}
98+
}
99+
init();
100+
101+
102+
103+
}
104+
105+
int main(){
106+
cin.tie(0);
107+
ios::sync_with_stdio(0);
108+
make_cost();
109+
110+
cin >> tc;
111+
for(int t = 1; t<= tc; ++t){
112+
cin >> n >> m;
113+
for(int i = 0; i<n; ++i){
114+
for(int j = 0; j<n; ++j) {
115+
cin >> arr[i][j];
116+
}
117+
}
118+
119+
for(int i = 0; i<n;++i){
120+
for(int j = 0; j<n; ++j){
121+
bfs(i,j);
122+
}
123+
}
124+
125+
cout << "#" << t << ' ' << answer << '\n';
126+
answer = -1;
127+
}
128+
129+
return 0;
130+
}

0 commit comments

Comments
 (0)