회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
이게 왜 틀린지 모르겠습니다. 반례 찾아주세요 ㅜㅜ
9328번 - 열쇠
dydwo0740
2년 전
0
모든 테케 해봤는데 다 정답으로 나와요,,
import java.io.*; import java.util.*; import java.util.stream.Collectors; public class Main { static int N; static int M; static char[][] board; static Set<Integer> set; static int[] row = {0, 0, 1, -1}; static int[] col = {1, -1, 0, 0}; static class Node{ int x; int y; int hold; List<Character> keys; @Override public String toString() { return "Node{" + "x=" + x + ", y=" + y + ", hold=" + hold + ", keys=" + keys + '}'; } public Node(int x, int y, int hold, List<Character> keys) { this.x = x; this.y = y; this.hold = hold; this.keys = keys; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st = new StringTokenizer(br.readLine()); int T = Integer.parseInt(st.nextToken()); for (int test = 1; test <= T; test++) { st = new StringTokenizer(br.readLine()); N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); board = new char[N + 2][M + 2]; set = new HashSet<>(); for(int i=0;i<N+2;i++){ Arrays.fill(board[i], '.'); } for (int i = 1; i < N + 1; i++) { st = new StringTokenizer(br.readLine()); String str = st.nextToken(); for (int j = 1; j < M + 1; j++) { board[i][j] = str.charAt(j - 1); } } st = new StringTokenizer(br.readLine()); char[] keySet = st.nextToken().toCharArray(); List<Character> keys = new ArrayList<>(); for (char key : keySet) { if (key != '0') { keys.add(key); } } bfs(0, 0, keys, 0); if(test == T){ bw.write(set.size()+""); } else { bw.write(set.size() + "\n"); } } bw.flush(); bw.close(); br.close(); } public static void bfs(int x, int y, List<Character> keys, int hold) { Queue<Node> queue = new LinkedList<>(); boolean[][][] visited = new boolean[N + 2][M + 2][27]; queue.add(new Node(x, y, hold, keys)); int max = hold; if (keys.isEmpty()) { visited[x][y][0] = true; } for (char key : keys) { visited[x][y][key - 'a' + 1] = true; } while (!queue.isEmpty()) { Node cur = queue.poll(); //System.out.println("cur.toString() = " + cur.toString()); max = Math.max(max, cur.hold); for (int i = 0; i < 4; i++) { int nx = cur.x + row[i]; int ny = cur.y + col[i]; if (0 <= nx && nx < N + 2 && 0 <= ny && ny < M + 2) { if (board[nx][ny] == '*') { continue; } List<Character> temps = new ArrayList<>(); temps.addAll(cur.keys); if ('a' <= board[nx][ny] && board[nx][ny] <= 'z') { if (!temps.contains(board[nx][ny])) { temps.add(board[nx][ny]); } } else if ('A' <= board[nx][ny] && board[nx][ny] <= 'Z') { if (!temps.contains((char) (board[nx][ny] - 'A' + 'a'))) { continue; } } if (isNotVisited(visited, nx, ny, temps)) { continue; } if (temps.isEmpty()) { visited[nx][ny][0] = true; } for (char key : temps) { visited[nx][ny][key - 'a' + 1] = true; } int adder = 0; if (board[nx][ny] == '$') { set.add(nx * (M + 2) + ny); } queue.add(new Node(nx, ny, cur.hold + adder, temps)); } } } } public static boolean isNotVisited(boolean[][][] visited, int x, int y, List<Character> keys) { if (keys.isEmpty()) { if (!visited[x][y][0]) { return false; } return true; } for (char key : keys) { if (!visited[x][y][key - 'a' + 1]) { return false; } } return true; } }
댓글을 작성하려면
로그인
해야 합니다.
dydwo0740 2년 전
모든 테케 해봤는데 다 정답으로 나와요,,