-
Notifications
You must be signed in to change notification settings - Fork 0
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
[ DFS, BFS ] 10월 9일 #7
base: main
Are you sure you want to change the base?
Changes from all commits
7d13621
985ec4a
68967a7
2cfbe8c
6f43e6a
ac979a7
ca72479
0fb8f0e
0ff6f97
bb924ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"files.associations": { | ||
"queue": "cpp", | ||
"xiosbase": "cpp", | ||
"vector": "cpp", | ||
"ostream": "cpp", | ||
"iostream": "cpp", | ||
"ios": "cpp" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
void dfs(int c, vector<bool>& visited, vector<vector<int>>& com, int& cnt){ | ||
visited[c]=true; | ||
|
||
for(int i=0;i<com[c].size();i++){ | ||
int temp=com[c][i]; | ||
if(!visited[temp]){ | ||
cnt++; | ||
dfs(temp, visited, com, cnt); | ||
} | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 문제는 BFS로는 쓸 수 없을까요? 가능하다면 한번 작성해보세요! DFS 함수도 스택을 이용해서 구현해보셔도 도움이 많이 될 것 같습니다! |
||
int main() | ||
{ | ||
int n, m, cnt=1, hacking=-1; | ||
vector<pair<int, int>> trust(100001); | ||
vector<bool> visited(100001); | ||
vector<vector<int>> com(100001); | ||
vector<int> answer(100001); | ||
|
||
cin >> n >> m; | ||
|
||
for(int i=0;i<m;i++){ | ||
int c1, c2; | ||
|
||
cin >> c1 >> c2; | ||
com[c2].push_back(c1); | ||
} | ||
|
||
for(int i=1;i<=n;i++){ | ||
for(int j=0;j<=n;j++){ | ||
visited[j]=false; | ||
} | ||
dfs(i, visited, com, cnt); | ||
trust.push_back({i, cnt}); | ||
cnt=1; | ||
} | ||
|
||
for(int i=0;i<trust.size();i++){ | ||
if(trust[i].second>hacking){ | ||
hacking=trust[i].second; | ||
} | ||
} | ||
|
||
for(int i=0;i<trust.size();i++){ | ||
if(trust[i].second==hacking){ | ||
cout << trust[i].first << " "; | ||
} | ||
} | ||
|
||
return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다음번에는 주석 작성 부탁드립니다! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
typedef vector<vector<int>> matrix; //vector<vector<int>>를 matrix라 하겠다 선언 | ||
|
||
const int SIZE = 20, EMPTY = 0; //상수 선언 | ||
|
||
const vector<int> dx = {-1, 0, 1, 1}; //x축 방향 정의 | ||
const vector<int> dy = {1, 1, 1, 0}; //y축 방향 정의 | ||
|
||
bool isValid(matrix &board, int x, int y, int color) { //유효한지 확인 | ||
return (x > 0 && x < SIZE && y > 0 && y < SIZE && board[x][y] == color); //돌이 보드의 위에 있는지, 종류가 맞는지 | ||
} | ||
|
||
bool checkWin(matrix &board, int x, int y) { //승리(5알이 연속으로 놓임)했는지 확인 | ||
int color = board[x][y]; //기준이 되는 색 | ||
|
||
for (int idx = 0; idx < 4; idx++) { //4번 반복 | ||
int cnt = 1; //같은 방향으로 몇 개의 돌이 놓였는지 셀 변수 | ||
int prev_x = x - dx[idx], prev_y = y - dy[idx]; //현재 돌의 이전 돌 | ||
int next_x = x + dx[idx], next_y = y + dy[idx]; //현재 돌의 다음 돌 | ||
|
||
if (isValid(board, prev_x, prev_y, color)) { //유효하다면, 이전 돌도 같은 색이었다면 | ||
continue; //건너 뜀(6목 방지 위함) | ||
} | ||
|
||
while (isValid(board, next_x, next_y, color) && cnt < 6) { //같은 방향으로 놓인 5알이 같은 색인지 확인 | ||
next_x += dx[idx]; //다음 돌 추가 | ||
next_y += dy[idx]; //다음 돌 추가 | ||
cnt++; //돌 세기 | ||
} | ||
if (cnt == 5) { //5알만 연속으로 놓였을 경우 | ||
return true; //true 리턴 | ||
} | ||
} | ||
return false; //아닐 경우 false 리턴 | ||
} | ||
|
||
int main() { //메인 | ||
matrix board(SIZE, vector<int>(SIZE, 0)); //바둑 보드 | ||
for (int i = 1; i < SIZE; i++) //가로 길이동안 | ||
for (int j = 1; j < SIZE; j++) //세로 길이동안 | ||
cin >> board[i][j]; //보드 위의 돌 입력받기 | ||
|
||
for (int y = 1; y < SIZE; y++) { //세로 길이동안 | ||
for (int x = 1; x < SIZE; x++) { //가로 길이동안 | ||
if (board[x][y] == EMPTY) { //돌이 없는 칸일 시 | ||
continue; //건너뜀 | ||
} | ||
if (checkWin(board, x, y)) { //현재 돌로부터 연속으로 5알만 놓였다면 | ||
cout << board[x][y] << '\n' //이 돌의 색상 출력 | ||
<< x << ' ' << y; //이 돌의 좌표 출력 | ||
return 0; //종료 | ||
} | ||
} | ||
} | ||
cout << 0; //모두 아닐 경우 0 출력 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
bool is_valid(int x, int y){ | ||
return (x>=0 && x<19 && y>=0 && y<19); | ||
} | ||
|
||
bool check(int num, int x, int y, vector<vector<int>>& dir, vector<vector<int>>& board, int& dir_5){ | ||
for(int d=0; d<4; d++){ | ||
int count=1; | ||
int dx=dir[d][0]; | ||
int dy=dir[d][1]; | ||
|
||
for(int i=1; i<=4; i++){ | ||
int nx=x+i*dx; | ||
int ny=y+i*dy; | ||
|
||
if(is_valid(nx, ny) && board[nx][ny]==num){ | ||
count++; | ||
} | ||
else{ | ||
break; | ||
} | ||
} | ||
|
||
if(count==5){ | ||
int dx_6=dir[dir_5][0], dy_6=dir[dir_5][1]; | ||
if(is_valid(x-dx_6, y-dy_6) && board[x-dx_6][y-dy_6]==num){ | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
bool six_check(int num, int x, int y, vector<vector<int>>& dir, vector<vector<int>>& board, int& dir_5){ | ||
for(int d=0; d<4; d++){ | ||
int count=1; | ||
int dx=dir[d][0]; | ||
int dy=dir[d][1]; | ||
|
||
for(int i=1; i<=5; i++){ | ||
int nx=x+i*dx; | ||
int ny=y+i*dy; | ||
|
||
if(is_valid(nx, ny) && board[nx][ny]==num){ | ||
count++; | ||
} | ||
else{ | ||
break; | ||
} | ||
} | ||
|
||
if(count>5){ | ||
dir_5=d; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(NULL); cout.tie(NULL); | ||
|
||
int winner, x_result, y_result, dir_5=2; | ||
vector<vector<int>> board(19,vector <int>(19)); | ||
vector<vector<int>> dir={{1, 0}, {0, 1}, {1, 1}, {1, -1}}; | ||
|
||
for(int i=0; i<19; i++){ | ||
for(int j=0; j<19; j++){ | ||
cin >> board[i][j]; | ||
} | ||
} | ||
|
||
for(int i=0; i<19; i++){ | ||
for(int j=0; j<19; j++){ | ||
if(board[i][j]!=0){ | ||
if(!six_check(board[i][j], i, j, dir, board, dir_5)){ | ||
if(check(board[i][j], i, j, dir, board, dir_5)){ | ||
winner=board[i][j]; | ||
x_result=i+1; | ||
y_result=j+1; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
if(winner!=0){ | ||
break; | ||
} | ||
} | ||
|
||
cout << winner << '\n'; | ||
if(winner!=0){ | ||
cout << x_result << " " << y_result; | ||
} | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <iostream> | ||
#include <cstring> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
vector<int> dir_x={-1, 1, -1, 1, 0, 0, -1, 1}; | ||
vector<int> dir_y={-1, 1, 1, -1, 1, -1, 0, 0}; | ||
int board[50][50]; | ||
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋습니다! |
||
bool visited[50][50]={false,}; | ||
|
||
bool is_ok(int x, int y, int w, int h){ | ||
return (x>=0 && x<h && y>=0 && y<w); | ||
} | ||
|
||
void dfs(int x, int y, int w, int h){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 문제도 BFS로는 쓸 수 없을까요? 가능하다면 한번 작성해보세요! |
||
|
||
for(int i=0; i<8; i++){ | ||
int nx=x+dir_x[i]; | ||
int ny=y+dir_y[i]; | ||
|
||
if(is_ok(nx, ny, w, h)){ | ||
if(!visited[nx][ny]&&board[nx][ny]){ | ||
visited[nx][ny]=true; | ||
dfs(nx, ny, w, h); | ||
} | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 깔끔하고 좋습니다! |
||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); | ||
|
||
int w, h, count=0; | ||
|
||
while(1){ | ||
|
||
cin >> w >> h; | ||
|
||
if(w==0 && h==0){ | ||
break; | ||
} | ||
for(int i=0; i<h; i++){ | ||
for(int j=0;j<w;j++){ | ||
cin >> board[i][j]; | ||
} | ||
} | ||
for(int i=0; i<h;i++){ | ||
for(int j=0;j<w;j++){ | ||
if(!visited[i][j] && board[i][j]){ | ||
count++; | ||
dfs(i, j, w, h); | ||
} | ||
} | ||
} | ||
cout << count << '\n'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. endl을 사용하시는 편이 시간복잡도에 좋습니다! |
||
|
||
memset(visited,false,sizeof(visited)); | ||
count=0; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
참조로 넘겨 주신 점, 깔끔한 기본 DFS 함수 좋습니다!