• Depth-First Search (II)


    1. Generating Permutations

      Here is a class that can generate all possible k-permutations from 0 through n-1:

     1 import java.util.*; 
     2 
     3 public class Permutation {
     4     public static int [] combArr;
     5     public static int [] permArr;
     6 
     7     public static void genPerm(int pos,int k,int visit) {
     8         if (pos==k) {
     9             // Print the solution:
    10             for (int i=0;i<k-1;i++) {
    11                 System.out.print(permArr[i]+" ");
    12             }
    13             System.out.print(permArr[k-1]+"
    ");
    14             return;
    15         }
    16         int test = 1;
    17         for (int b=0;b<k;b++){
    18             if ((test&visit)==0){
    19                 permArr[pos] = combArr[b];
    20                 genPerm(pos+1,k,visit|test);
    21             }
    22             test <<= 1;
    23         }
    24     }
    25     public static void genComb(int pos,int n,int k) {
    26         if (pos==k) {
    27             // Generate the permutations:
    28             genPerm(0,k,0);
    29             return;
    30         }
    31         for (int v=combArr[pos-1]+1;v<n;v++){
    32             combArr[pos] = v;
    33             genComb(pos+1,n,k);
    34         }
    35     }
    36     public static void main(String[] args) {
    37         Scanner in = new Scanner(System.in);
    38         int n = in.nextInt();
    39         int k = in.nextInt();
    40         in.close();
    41         combArr = new int [k];
    42         permArr = new int [k];
    43         for (int v=0;v<n;v++){
    44             combArr[0] = v;
    45             genComb(1,n,k);
    46         }
    47         System.out.println();
    48     }
    49 }

      The program above was written when I took up Java in the summer of 2014.

    2. N-Queen Problem

      This is my solution to the USACO training problem "checker":

     1 import java.io.*;
     2 import java.util.*;
     3 
     4 public class checker {
     5     public static BufferedReader input;
     6     public static PrintWriter output;
     7     public static int val, size;
     8     public static int [] state;
     9     
    10     public static void dfs(int depth,int inval,int left,int right) {
    11         if (depth==size) {
    12             if (++val<4) {
    13                 // print one of the first three solutions:
    14                 for (int i=0;i<size-1;i++) {
    15                     output.print(state[i]+" ");
    16                 }
    17                 output.println(state[size-1]);
    18             }
    19             return;
    20         }
    21         int pos = (inval|left|right);
    22         for (int i=0;i<size;i++) {
    23             if ((pos&(1<<i))==0) {
    24                 int lt = ((left|(1<<i))<<1);
    25                 int rt = ((right|(1<<i))>>1);
    26                 state[depth] = i+1;
    27                 dfs(depth+1,inval|(1<<i),lt,rt);
    28             }
    29         }
    30     }
    31     public static void solve() {
    32         for (int i=0;i<size;i++) {
    33             int inval = (1<<i);
    34             state[0] = i+1;
    35             dfs(1,inval, (inval<<1),(inval>>1));
    36         }
    37     }
    38     public static void main(String[] args) throws IOException {
    39         input = new BufferedReader(new FileReader("checker.in"));
    40         size = Integer.parseInt(input.readLine());
    41         input.close();
    42         state = new int [size];
    43         solve();
    44         output = new PrintWriter(new FileWriter("checker.out"));
    45         output.println(val);
    46         output.close();
    47     }
    48 }

    3. Party Lamps

      This is my solution to the USACO training problem "lamps":

     1 import java.io.*;
     2 import java.util.*;
     3 
     4 public class lamps {
     5     public static BufferedReader input;
     6     public static PrintWriter output;
     7     public static boolean [] list;
     8     public static int num,cnt,open,closed;
     9     
    10     public static int turn(int state,int mode) {
    11         switch (mode) {
    12         case 0: state ^= 63; break;
    13         case 1: state ^= 21; break;
    14         case 2: state ^= 42; break;
    15         case 3: state ^= 9;
    16         } 
    17         return state;
    18     }
    19     public static boolean test(int state)  {
    20         for (int i=0;i<6;i++) {
    21             if ((open&(1<<i))>0 && (state&(1<<i))==0) {
    22                 return false;
    23             } else if ((closed&(1<<i))>0 && (state&(1<<i))>0) {
    24                 return false;
    25             }
    26         }
    27         return true;
    28     }
    29     public static int inv(int state)  {
    30         int val = 0;
    31         for (int i=0;i<6;i++) {
    32             val <<= 1;
    33             val += (state&1);
    34             state >>= 1;
    35         }
    36         return val;
    37     }
    38     public static void dfs(int depth,int state,int op) {
    39         if (depth==4) {
    40             if (test(state) && op%2==cnt%2 && op<=cnt) {
    41                 list[inv(state)] = true;
    42             }
    43             return;
    44         } 
    45         dfs(depth+1,turn(state,depth),op+1);
    46         dfs(depth+1,state,op);
    47     }
    48     public static void show(int state)  {
    49         state = inv(state);
    50         for (int i=0;i<num;i++) {
    51             if ((state&(1<<(i%6)))>0) {
    52                 output.print(1);
    53             } else {
    54                 output.print(0);
    55             }
    56         } 
    57         output.println();
    58     }
    59     public static void main(String[] args) throws IOException{
    60         // Obtain the Input:
    61         input = new BufferedReader(new FileReader("lamps.in"));
    62         String line = input.readLine(); // first line of input
    63         num = Integer.parseInt(line);
    64         line = input.readLine(); // second line of input
    65         cnt = Integer.parseInt(line);
    66         line = input.readLine(); // third line of input
    67         StringTokenizer str = new StringTokenizer(line);
    68         int idx = Integer.parseInt(str.nextToken())-1;
    69         while (idx>=0) {
    70             open |= (1<<(idx%6));
    71             idx = Integer.parseInt(str.nextToken())-1;
    72         }
    73         line = input.readLine(); // fourth line of input
    74         str = new StringTokenizer(line);
    75         idx = Integer.parseInt(str.nextToken())-1;
    76         while (idx>=0) {
    77             closed |= (1<<(idx%6));
    78             idx = Integer.parseInt(str.nextToken())-1;
    79         }
    80         input.close();
    81         // Solve the Problem:
    82         list = new boolean[64];
    83         dfs(0,63,0);
    84         boolean flag = false;
    85         for (int i=0;i<64;i++) {
    86             if (list[i]) {
    87                 flag = true;
    88                 show(i);
    89             }
    90         }
    91         // Phrase the Output:
    92         output = new PrintWriter(new FileWriter("lamps.out"));
    93         if (!flag) {
    94             output.println("IMPOSSIBLE");
    95         }
    96         output.close();
    97     }
    98 }
  • 相关阅读:
    12个JavaScript MVC框架评估 简单
    chrome developer tool 调试技巧 简单
    转CSS3线性渐变 简单
    base64:URL背景图片与web页面性能优化 简单
    转linux下apache安装gzip压缩 简单
    转思考什么时候使用Canvas 和SVG 简单
    转周报的逻辑 简单
    浏览器三种刷新方式采取的不同缓存机制 简单
    poj 1308 Is It A Tree? (并查集)
    poj 2912 Rochambeau (并查集+枚举)
  • 原文地址:https://www.cnblogs.com/DevinZ/p/4411440.html
Copyright © 2020-2023  润新知