Comment detail

情報オリンピック2006年度国内予選問題6 (Nested Flatten)

単純にルートを数え上げで解いて見ました。

 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
79
80
81
82
83
84
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Sample142 {
    public final Point goal_;
    private final Set<Point> blocedList_ = new HashSet<Point>();

    public Sample142(int x, int y) {
        goal_ = new Point(x, y);
    }
    public void addBlocked(int x, int y) {
        blocedList_.add(new Point(x, y));
    }

    public int calc() {
        return calc(goal_.x, goal_.y, new HashMap<Point, Integer>());
    }
    private int calc(int x, int y, Map<Point, Integer> map) {
        Set<Point> prev = previousPoint(x, y);
        if (prev.isEmpty()) return 1;
        int count = 0;
        for (Point p: prev) {
            if (blocedList_.contains(p)) continue;
            Integer route = map.get(p);
            if (route == null) {
                route = calc(p.x, p.y, map);
                map.put(p, route);
            }
            count += route.intValue();
        }
        return count;
    }
    private Set<Point> previousPoint(int x, int y) {
        Set<Point> result = new HashSet<Point>();
        if (x > 1) result.add(new Point(x - 1, y));
        if (y > 1) result.add(new Point(x, y - 1));
        return result;
    }


    public static class Point {
        public final int x;
        public final int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) return true;
            if (!(obj instanceof Point)) return false;
            Point other = (Point) obj;
            return this.x == other.x && this.y == other.y;
        }
        @Override
        public int hashCode() {
            return x * 17 + y;
        }
    }

    public static void main(String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        try {
            String[] goal = reader.readLine().split("\\s");
            Sample142 sample = new Sample142(Integer.parseInt(goal[0]), Integer.parseInt(goal[1]));
            int n = Integer.parseInt(reader.readLine());
            for (int index = 0; index < n; index++) {
                String[] closed = reader.readLine().split("\\s");
                sample.addBlocked(Integer.parseInt(closed[0]), Integer.parseInt(closed[1]));
            }
            int result = sample.calc();
            System.out.println(result);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

無駄な処理を消し忘れていたので、修正版を投稿。

 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Sample142 {
    public final Point goal_;
    private final Set<Point> blocedList_ = new HashSet<Point>();

    public Sample142(int x, int y) {
        goal_ = new Point(x, y);
    }
    public void addBlocked(int x, int y) {
        blocedList_.add(new Point(x, y));
    }

    public int calc() {
        return calc(goal_.x, goal_.y);
    }
    private int calc(int x, int y) {
        Set<Point> prev = previousPoint(x, y);
        if (prev.isEmpty()) return 1;
        int count = 0;
        for (Point p: prev) {
            if (blocedList_.contains(p)) continue;
            count += calc(p.x, p.y);
        }
        return count;
    }
    private Set<Point> previousPoint(int x, int y) {
        Set<Point> result = new HashSet<Point>();
        if (x > 1) result.add(new Point(x - 1, y));
        if (y > 1) result.add(new Point(x, y - 1));
        return result;
    }


    public static class Point {
        public final int x;
        public final int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) return true;
            if (!(obj instanceof Point)) return false;
            Point other = (Point) obj;
            return this.x == other.x && this.y == other.y;
        }
        @Override
        public int hashCode() {
            return x * 17 + y;
        }
    }

    public static void main(String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        try {
            String[] goal = reader.readLine().split("\\s");
            Sample142 sample = new Sample142(Integer.parseInt(goal[0]), Integer.parseInt(goal[1]));
            int n = Integer.parseInt(reader.readLine());
            for (int index = 0; index < n; index++) {
                String[] closed = reader.readLine().split("\\s");
                sample.addBlocked(Integer.parseInt(closed[0]), Integer.parseInt(closed[1]));
            }
            int result = sample.calc();
            System.out.println(result);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Index

Feed

Other

Link

Pathtraq

loading...