Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

TitanYang's Programming

[JAVA 순환 알고리즘] 백준 알고리즘 1074번 / Z탐색알고리즘 본문

JAVA

[JAVA 순환 알고리즘] 백준 알고리즘 1074번 / Z탐색알고리즘

타이탄양 2018. 2. 27. 17:18


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
 * @Summary   : 
 * @Package : recusion
 * @FileName : zSeek.java
 * @Author : Yang TaeIl
 * @date : 2018. 2. 12.  
 * 
 */
package recursion;
 
import java.util.Scanner;
 
/**
 * z형태로 탐색할 배열을 만들고, 탐색하는 메소드가 들어있는 클래스 
 * 
 * @Package : recursion
 * @FileName : zSeek.java
 * @Author : Yang TaeIl
 * @date : 2018. 2. 12. 
 *
 */
class zArray{
    int n;
    int w;
    int findX,findY;
    int cnt=-1;
    boolean flag=false;
    zArray(int n,int y, int x){
        this.n=n;
        this.w=(int)Math.pow(2, n);
        findY=y;
        findX=x;
    }
    
    /**
     * 
     * 탐색을하면서 찾는 알고리즘
     *
     * @Method Name : zFind
     * @param x 
     * @param y 
     * @param width 넓이가 1일때 출력하기위함.
     */
    void zFind(int x,int y,int width) { 
        if(width==1) {
            cnt++;
            if(x==findX&&findY==y) {
                System.out.println(cnt);
                flag=true;
            }
            return;
        }
        if(flag)return;
        zFind(x,y,width/2);
        zFind(x+width/2,y,width/2);
        zFind(x,y+width/2,width/2);
        zFind(x+width/2,y+width/2,width/2);
        return;
    }
}
 
/**
 * 
 * @Package : recursion
 * @FileName : zSeek.java
 * @Author : Yang TaeIl
 * @date : 2018. 2. 12.
 * 
 */
public class zSeek {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        zArray z= new zArray(sc.nextInt(),sc.nextInt(),sc.nextInt());
        z.zFind(00,z.w);
    }
 
}
 
cs


2차원의 배열을 Z모양의 순서로 탐색하는 알고리즘입니다. 항상 4개의 공간으로 배열을 나누어 순차척으로 탐색하는 문제였습니다. 주어진 규칙을 알고리즘으로 잘 풀어낼 수만 있다면 어렵지 않게 풀 수 있는 문제였습니다.