-
Notifications
You must be signed in to change notification settings - Fork 1
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
9-mong3125 #34
Merged
Merged
9-mong3125 #34
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
import java.util.StringTokenizer; | ||
|
||
public class BOJ2178_๋ฏธ๋กํ์ํ๊ธฐ { | ||
|
||
static int n; | ||
static int m; | ||
|
||
static boolean[][] field; | ||
static boolean[][] visited; | ||
|
||
static int[] dx = {1, -1, 0, 0}; | ||
static int[] dy = {0, 0, 1, -1}; | ||
public static void main(String[] args) throws IOException { | ||
// ์ ๋ ฅ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
n = Integer.parseInt(st.nextToken()); // ์ธ๋ก ํฌ๊ธฐ | ||
m = Integer.parseInt(st.nextToken()); // ๊ฐ๋ก ํฌ๊ธฐ | ||
|
||
// ๋ฏธ๋ก ์ ์ธ | ||
field = new boolean[n][m]; | ||
visited = new boolean[n][m]; | ||
|
||
// ๋ฏธ๋ก ์ ๋ ฅ | ||
for (int i = 0; i < n; i++) { | ||
String line = br.readLine(); | ||
for (int j = 0; j < m; j++) { | ||
field[i][j] = line.charAt(j) == '1'; | ||
} | ||
} | ||
|
||
// ๋ฌธ์ ํด๊ฒฐ | ||
int answer = bfs(0, 0); | ||
|
||
// ์ถ๋ ฅ | ||
System.out.println(answer); | ||
} | ||
|
||
public static int bfs(int i, int j) { | ||
Queue<int[]> queue = new LinkedList<>(); | ||
queue.add(new int[]{i, j, 1}); | ||
visited[i][j] = true; | ||
|
||
while (!queue.isEmpty()) { | ||
int[] now = queue.poll(); | ||
|
||
if (now[0] == n - 1 && now[1] == m - 1) { | ||
return now[2]; | ||
} | ||
|
||
for (int k = 0; k < 4; k++) { | ||
int x = now[0] + dx[k]; | ||
int y = now[1] + dy[k]; | ||
|
||
if (isInRange(x, y)) { | ||
if (!visited[x][y] && field[x][y]) { | ||
queue.add(new int[]{x, y, now[2] + 1}); | ||
visited[x][y] = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
public static boolean isInRange(int i, int j) { | ||
return 0 <= i && i < n && 0 <= j && j < m; | ||
} | ||
} |
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.
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.
now[0]
๊ณผnow[1]
์ ์์น๊ฐ(N,M)
์ผ ๋ ์ด๋ ํ์now[2]
๋ฅผ ๋ฐํํ๋ค๊ณ ํด์๋๋ค์.