ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 2013 코드잼 - Problem B.
    Developer/Java 2013. 4. 16. 11:57

    코드잼 예선 B.Proble. Lawnmower



    2번째 내용의 핵심은 다음과 같다



    잔디 깍기 문제이며, 규칙은 다음과 같다. 

    1. 시작은 외곽에서부터 동작.

    2. 직선으로만 움직인다. 

    3. 이동 도중에는 높이보다 높은 풀들은 잘라버린다.

    4. 끝에 가면 나가버린다.

    5. 박에서만 높이를 다시 설정할 수 있다.

    6. 도중에 잔디를 깍는도중 멈출수 없다.


    예시는 다음과 같습니다. 






    처음에 6번 문제를 관과하여. 소스를 작성했으나. 지금은 수정하여 정답을 일치하였다. 


    즉 주요 핵심 사항을 정리하자면 

    잔디 높이. 깍는 방향이 되겠습니다. 


    저는 int 2차원 배열을 이용했습니다. 




    package com.google.codezem.chap2;


    import java.io.IOException;


    import com.google.codezem.common.ExFileReader;


    public class Main {

    public ExFileReader reader;

    public static void main(String[] args) 

    {

    Main main = new Main();

    main.start();

    }

    public Main() {

    reader =new ExFileReader("file/B-small-practice.in");

    }

    public void start() {

    long stime = System.currentTimeMillis();

    String line;

    int gameCount = 0;

    try {

    gameCount  = Integer.parseInt(reader.getBr().readLine());

    Grass g = new Grass(gameCount);

    while((line = reader.getBr().readLine()) != null)

    {

    String[] sLine = line.split(" ");

    if(g.isNext()) 

    g.setArray(sLine[0], sLine[1]);

    else 

    g.setLine(sLine);

    }

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    System.out.println((System.currentTimeMillis() - stime ) + " ms");

    }


    }





    Grass클래스

    package com.google.codezem.chap2;

    public class Grass {
    private int[][] array;
    private int x;
    private int y;
    private boolean next = true;
    private boolean available = true;
    private int rownum = 0;
    private int gameCount = 0;
    private int gameNum = 1;
    public Grass(int gameCount) {
    this.gameCount = gameCount;
    }
    public boolean isNext() {
    return next;
    }
    public void setArray(String x, String y) {
    this.x = getInt(x);
    this.y = getInt(y);
    array = new int[this.x][this.y];
    next = false;
    }
    public void setLine(String[] sLine) {
    int size = sLine.length;
    for(int i=0; i < size; i++) {
    array[rownum][i] = getInt(sLine[i]);
    }
    rownum++;
    if(rownum==x) {
    isPossable();
    printF();
    init();
    }
    }
    private void printF() {
    /*for(int i=0; i<x;i++)
    {
    for(int j=0; j<y;j++)
    {
    System.out.print(array[i][j]);
    }
    System.out.println();
    }
    System.out.println("------------");*/
    if(available) 
    System.out.println("Case #"+gameNum+": YES" );
    else
    System.out.println("Case #"+gameNum+": NO" );
    }
    public void isPossable() {
    for(int i = 0; i < x; i++) {
    for(int j = 0; j < y; j++) {
    //초기화
    boolean bRow = true, bCol = true;
    for (int k = 0; k < x; k++) {
    if (check(array[k][j] , array[i][j])) {
    bCol = false;
    break;
    }
    }
    for (int k = 0; k < y; k++) {
    if(check(array[i][k], array[i][j]) ) {
    bRow = false;
    break;
    }
    }

    if (!bRow && !bCol){
    available = false;
    return;
    }
    }
    }
    }
    private boolean check(int a, int b) {
    return a > b? true : false;
    }
    private int getInt(String a) {
    return Integer.parseInt(a);
    }
    private void init() {
    rownum = 0;
    next = true;
    array = null;
    available = true;
    gameNum++;
    }
    }





    'Developer > Java' 카테고리의 다른 글

    자바 기초 - 누군가를 위한..ㅋ  (0) 2013.05.07
    2013 코드잼 - Problem C.  (0) 2013.04.19
    2013 코드잼 - Problem A. Tic-Tac-Toe-Tomek  (0) 2013.04.16
    [Tomcat] 성능 향상  (0) 2013.03.14
    [Java] 리플렉션 (Reflection)  (0) 2013.03.13
© 2018 T-Story. All right reserved.