알고리즘/SWEA

D2 1979. 어디에 단어가 들어갈 수 있을까

  • -
반응형

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PuPq6AaQDFAUq

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com

 

import java.util.Scanner;

public class Solution {
	static int T, N, W;
	static int [][] map;
	public static void main(String[] args) {
		Scanner scanner = new Scanner(src);
		T = scanner.nextInt();
		for(int t=1; t<=T; t++) {
			int answer = 0;
			N = scanner.nextInt();
			map = new int[N][N];
			W = scanner.nextInt();
			for(int row=0; row<N; row++) {
				for(int col=0; col<N; col++) {
					map[row][col]=scanner.nextInt();
				}
			}
			// 입력 완료-------------------------------------------

			for(int row=0; row<N; row++) {
				for(int col=0; col<N; col++) {
					if(isStartCol(row, col)) {
						if(getColLen(row, col)==W) {
							answer++;
						}
					}
					if(isStartRow(row, col)) {
						if(getRowLen(row, col)==W) {
							answer++;
						}
					}
				}
			}
			// 처리 완료-------------------------------------------
			System.out.printf("#%d %d%n",t, answer);
		}
	}
	static boolean isStartRow(int row, int col) {
		return row==0 || map[row-1][col]==0;
	}
	static boolean isStartCol(int row, int col) {
		return col==0 || map[row][col-1]==0;
	}
	static int getRowLen(int row, int col) {
		int len = 0;
		for(int r=row; r<N; r++) {
			if(map[r][col]==1) {
				len++;
			}else {
				break;
			}
		}
		return len;
	}

	static int getColLen(int row, int col) {
		int len = 0;
		for(int c=col; c<N; c++) {
			if(map[row][c]==1) {
				len++;
			}else {
				break;
			}
		}
		return len;
	}

	static String src = "4\r\n" +
			"2 2 2\r\n" +
			"3 4\r\n" +
			"2 2 2\r\n" +
			"1 2\r\n" +
			"2 2 1\r\n" +
			"4 2\r\n" +
			"2 2 1\r\n" +
			"3 2";
}
반응형

'알고리즘 > SWEA' 카테고리의 다른 글

D3 1873. 상호의 배틀필드  (0) 2019.07.26
D3 1860. 진기의 최고급 붕어빵  (0) 2019.07.26
D3 1860. 진기의 최고급 붕어빵  (0) 2019.07.26
D3 7675. 통역사 성경이  (0) 2019.07.24
D3 3431. 준환이의 운동관리  (0) 2019.07.23
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.