-
Notifications
You must be signed in to change notification settings - Fork 2
Kjh 3주차 #29
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
Open
hannahf97
wants to merge
6
commits into
main
Choose a base branch
from
kjh
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
Author
바이러스예전에 풀었던 문제라 쉬웠다 import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class Main{
static int n,m;
static ArrayList<Integer>[] arrayList;
static boolean[] visited;
static int count=0;
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine()); //컴퓨터 수
m = Integer.parseInt(br.readLine()); //컴퓨터 번호 쌍
arrayList = new ArrayList[n+1];
visited = new boolean[n+1];
for(int i=1; i<=n; i++){
arrayList[i] = new ArrayList<Integer>();
}
for(int i=0; i<m; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
arrayList[x].add(y);
arrayList[y].add(x);
}
bfs(1);
System.out.println(count);
}
public static void bfs(int num){
visited[num] = true;
for(int x : arrayList[num]){
if(!visited[x]){
count+=1;
bfs(x);
}
}
}
} |
DongGeun974
approved these changes
May 25, 2022
Contributor
Author
경로 찾기다른 사람꺼 참고 하긴 했는데 n^3제곱으로 풀었는데 다른 방법 없는지 고민해봐야겠다. import java.io.BufferedReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
int[][] arr = new int[n][n];
for(int i=0; i<n; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j=0; j<n; j++){
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
for(int k=0; k<n; k++){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(arr[i][k] ==1 && arr[k][j] == 1){
arr[i][j] =1;
}
}
}
}
StringBuilder sb = new StringBuilder();
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
sb.append(arr[i][j] + " ");
}
sb.append("\n");
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
} |
Contributor
Author
트리의 부모찾기parents 생각하는 부분이 어려웠다. import java.util.*;
import java.io.*;
public class Main
{
static boolean visited[];
static int N;
static ArrayList<Integer>[] arr;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
arr = new ArrayList[N+1];
visited = new boolean[N+1];
for(int i=1; i<N+1; i++){
arr[i] = new ArrayList();
}
for(int i=0; i<N-1; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
arr[a].add(b);
arr[b].add(a);
}
int[] parents = new int[N+1];
dfs(parents, 1,0);
for(int j=2; j<=N; j++) System.out.println(parents[j]);
}
public static void dfs(int[] parents, int start,int parent){
parents[start] = parent; // 1부터 시작하기 때문에 1은 먼저 루트로 만들어준다
for(int x: arr[start]){
if(x != parent){
dfs(parents, x, start); //1을 통해 찾은 다음것인기 때문에 1이 부모가된다
}
}
}
} |
Contributor
Author
나이트의 이동class 만드는것 참고해서 만들어보았습니다. import java.io.*;
import java.util.*;
public class Main{
static int l;
static int dx[] = {-2,-1,1,2,-2,-1,1,2};
static int dy[] = {1,2,2,1,-1,-2,-2,-1};
static Point now, goal;
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
ArrayList<Integer> arrlist = new ArrayList<>();
for(int i=0; i<T; i++){
l = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
now = new Point(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()), 0);
st = new StringTokenizer(br.readLine());
goal = new Point(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()), 0);
bfs(now);
}
}
static void bfs(Point p){
boolean visited[][] = new boolean[l][l];
Queue<Point> queue = new LinkedList<>();
visited[p.r][p.c] = true;
queue.add(p);
while(!queue.isEmpty()){
Point tmp = queue.poll();
int tmpX = tmp.r;
int tmpY = tmp.c;
int cnt = tmp.cnt;
if(tmpX == goal.r && tmpY == goal.c){
System.out.println(tmp.cnt);
return;
}
for(int i=0; i<8; i++){
int nx = tmpX + dx[i];
int ny = tmpY + dy[i];
if(nx >= 0 && nx < l && ny>= 0 && ny< l && !visited[nx][ny]){
queue.add(new Point(nx, ny, cnt+1));
visited[nx][ny] = true;
}
}
}
}
static class Point{
int r, c, cnt;
public Point(int r, int c, int cnt){
this.r = r;
this.c = c;
this.cnt = cnt;
}
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
양부터 다시 시작하겠습니다.