DFS水题(HDU2616)

Kill the monster

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1635    Accepted Submission(s): 1117


Problem Description
There is a mountain near yifenfei’s hometown. On the mountain lived a big monster. As a hero in hometown, yifenfei wants to kill it.
Now we know yifenfei have n spells, and the monster have m HP, when HP <= 0 meaning monster be killed. Yifenfei’s spells have different effect if used in different time. now tell you each spells’s effects , expressed (A ,M). A show the spell can cost A HP to monster in the common time. M show that when the monster’s HP <= M, using this spell can get double effect.
 

Input
The input contains multiple test cases.
Each test case include, first two integers n, m (2 Next n line , each line express one spell. (Ai, Mi).(0
 

Output
For each test case output one integer that how many spells yifenfei should use at least. If yifenfei can not kill the monster output -1.
 

Sample Input

3 100 10 20 45 89 5 40 3 100 10 20 45 90 5 40 3 100 10 20 45 84 5 40
 

Sample Output

3 2 -1
import java.awt.RenderingHints.Key;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeSet;

public class Main {
    static int n, m, sum, max = 10000515;
    static boolean d[];
    static int shu[][];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            sum=0;
            max=10000515;
            n = in.nextInt();
            m = in.nextInt();
            shu = new int[n][2];
            d = new boolean[n];
            for (int i = 0; i < n; i++) {
                shu[i][0] = in.nextInt();
                shu[i][1] = in.nextInt();
            }
            dfs();
            if (max == 10000515) {
                System.out.println("-1");
            } else {
                System.out.println(max);
            }
        }
    }

    public static void dfs() {
        for (int i = 0; i < n; i++) {
            if (d[i] == false) {
                d[i] = true;
                int s = 0;
                if (m <= shu[i][1]) {
                    s = shu[i][0] * 2;
                } else {
                    s = shu[i][0];
                }
                m -= s;
                sum++;
                if (m <= 0) {
                    if (sum < max) {
                        max = sum;
                    }
                }
                
                dfs();
                m += s;
                d[i] = false;
                sum--;
            }
        }
    }
}

你可能感兴趣的:(ACM)