-
Notifications
You must be signed in to change notification settings - Fork 0
[ADD] week_74 #221
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
[ADD] week_74 #221
Changes from all commits
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,56 @@ | ||
package com.example.algorithm; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.Arrays; | ||
import java.util.StringTokenizer; | ||
|
||
public class B1189_컴백홈 { | ||
static int r, c, k, answer = 0; | ||
static char[][] arr; | ||
static boolean[][] visited; | ||
static int[] dx = {0, 0, -1, 1}, dy = {-1, 1, 0, 0}; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
r = Integer.parseInt(st.nextToken()); | ||
c = Integer.parseInt(st.nextToken()); | ||
k = Integer.parseInt(st.nextToken()); | ||
|
||
arr = new char[r][c]; | ||
visited = new boolean[r][c]; | ||
|
||
for (int i = 0; i < r; i++) { | ||
arr[i] = br.readLine().toCharArray(); | ||
} | ||
|
||
visited[r - 1][0] = true; | ||
dfs(r - 1, 0, 1); | ||
|
||
System.out.println(answer); | ||
} | ||
|
||
static void dfs(int x, int y, int cnt) { | ||
if (k < cnt) return; | ||
|
||
if (x == 0 && y == c - 1 && cnt == k) { | ||
answer++; | ||
} | ||
|
||
for (int i = 0; i < 4; i++) { | ||
int nx = x + dx[i]; | ||
int ny = y + dy[i]; | ||
|
||
if (nx < 0 || nx >= r || ny < 0 || ny >= c) continue; | ||
if (visited[nx][ny] || arr[nx][ny] == 'T') continue; | ||
|
||
visited[nx][ny] = true; | ||
dfs(nx, ny, cnt + 1); | ||
visited[nx][ny] = false; | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.example.algorithm; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.StringTokenizer; | ||
|
||
// 오른쪽, 아래, 오른쪽대각선, 왼쪽대각선 | ||
public class B2615_오목 { | ||
static int[][] arr = new int[19][19]; | ||
static final int[] dx = {0, 1, 1, 1}; | ||
static final int[] dy = {1, 0, 1, -1}; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
for (int i = 0; i < 19; i++) { | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
for (int j = 0; j < 19; j++) { | ||
arr[i][j] = Integer.parseInt(st.nextToken()); | ||
} | ||
} | ||
|
||
for (int i = 0; i < 19; i++) { | ||
for (int j = 0; j < 19; j++) { | ||
if (arr[i][j] != 0) { | ||
int color = arr[i][j]; | ||
|
||
for (int d = 0; d < 4; d++) { | ||
if (check(i, j, dx[d], dy[d], color)) { | ||
int finalX = (d == 3) ? i + 4 : i; | ||
int finalY = (d == 3) ? j - 4 : j; | ||
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. 3항연산자의 달인이시네요 |
||
|
||
System.out.println(color); | ||
System.out.println((finalX + 1) + " " + (finalY + 1)); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
System.out.println(0); | ||
} | ||
|
||
static boolean check(int x, int y, int dx, int dy, int color) { | ||
int cnt = 1; | ||
|
||
for (int i = 1; i <= 5; i++) { | ||
int nx = x + dx * i; | ||
int ny = y + dy * i; | ||
|
||
if (isValid(nx, ny) && arr[nx][ny] == color) cnt++; | ||
else break; | ||
} | ||
|
||
if (cnt == 5) { | ||
int prevX = x - dx; | ||
int prevY = y - dy; | ||
int nextX = x + dx * 5; | ||
int nextY = y + dy * 5; | ||
|
||
if ((!isValid(prevX, prevY) || arr[prevX][prevY] != color) && | ||
(!isValid(nextX, nextY) || arr[nextX][nextY] != color)) { | ||
return true; | ||
} | ||
} | ||
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. 방식이 거의 같군 |
||
|
||
return false; | ||
} | ||
|
||
static boolean isValid(int x, int y) { | ||
return x >= 0 && x < 19 && y >= 0 && y < 19; | ||
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. 나도 항상 이걸 따로 뺄까 고민하는데 |
||
} | ||
} |
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.
아래에 cnt ==k가 있는데 이게 있어야 하는 이유가 뭘까?
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.
k 넘어가면 고려 안한다는 뜻으로 적고 아래에는 return 안시켯는데 그냥 해도 됐으려나