백준 2508번 사탕 박사 고창영 JAVA

YB·2025년 2월 5일

링크텍스트

설명

조건에 맞을 때 count 해주면 된다.
시간복잡도: O(n*m),공간복잡도: O(n*m)

코드

import java.util.*;
import java.io.*;

class Main {
    static char[][] arr;
    static int r, c;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());

        while(t-->0){
            int count = 0;
			br.readLine();
            StringTokenizer st = new StringTokenizer(br.readLine());

            r = Integer.parseInt(st.nextToken());
            c = Integer.parseInt(st.nextToken());

            arr = new char[r][c];
            for(int i=0;i<r;i++){
                arr[i] = br.readLine().toCharArray();
            }

            for(int i=0;i<r;i++){
                for(int j=0;j<c-2;j++){
                    if(arr[i][j] == '>' && arr[i][j + 1] == 'o' && arr[i][j + 2] == '<'){
                        count++;
                    }
                }
            }

            for(int j=0; j<c;j++){
                for(int i=0;i<r-2;i++){
                    if(arr[i][j] == 'v' && arr[i + 1][j] == 'o' && arr[i + 2][j] == '^'){
                        count++;
                    }
                }
            }
            System.out.println(count);
        }
    }
}

profile
안녕하세요

0개의 댓글