Breadth-First Search

1. Maze

  This is my solution to a problem in  SJTU ACM Online Judge:

 1 import java.util.*;
 2 
 3 public class Main {
 4     public static final int INF = 65535;
 5     public static int wid, len;
 6     public static char [] map;
 7     
 8     public static int bfs(int x1,int y1,int x2,int y2)  {
 9         Queue<Integer> q = new LinkedList<Integer>();
10         boolean [] vis = new boolean[wid*len];
11         int [] dist = new int[wid*len];
12         for (int i=0;i<wid;i++) {
13             for (int j=0;j<len;j++) {
14                 dist[i*len+j] = INF;
15             }
16         }
17         q.add(new Integer(x1*len+y1));
18         vis[x1*len+y1] = true;
19         dist[x1*len+y1] = 0;
20         while (dist[x2*len+y2]==INF) {
21             if (q.isEmpty()) {
22                 // no such path exists
23                 return -1;
24             }
25             int pos = q.poll().intValue();
26             if (map[pos]=='.') {
27                 if (pos%len>0 && !vis[pos-1] && map[pos-1]!='|') { //West
28                     q.add(new Integer(pos-1));
29                     vis[pos-1] = true;
30                     dist[pos-1] = dist[pos]+1;
31                 }
32                 if (pos/len>0 && !vis[pos-len] && map[pos-len]!='-') { //North
33                     q.add(new Integer(pos-len));
34                     vis[pos-len] = true;
35                     dist[pos-len] = dist[pos]+1;
36                 }
37                 if (pos%len<len-1 && !vis[pos+1] && map[pos+1]!='|') { //East
38                     q.add(new Integer(pos+1));
39                     vis[pos+1] = true;
40                     dist[pos+1] = dist[pos]+1;
41                 }
42                 if (pos/len<wid-1 && !vis[pos+len] && map[pos+len]!='-') { //South
43                     q.add(new Integer(pos+len));
44                     vis[pos+len] = true;
45                     dist[pos+len] = dist[pos]+1;
46                 }
47             }  else if (map[pos]=='|') {
48                 if (pos/len>0 && !vis[pos-len] && map[pos-len]!='-') { //North
49                     q.add(new Integer(pos-len));
50                     vis[pos-len] = true;
51                     dist[pos-len] = dist[pos]+1;
52                 }
53                 if (pos/len<wid-1 && !vis[pos+len] && map[pos+len]!='-') { //South
54                     q.add(new Integer(pos+len));
55                     vis[pos+len] = true;
56                     dist[pos+len] = dist[pos]+1;
57                 }
58             } else if (map[pos]=='-') {
59                 if (pos%len>0 && !vis[pos-1] && map[pos-1]!='|') { //West
60                     q.add(new Integer(pos-1));
61                     vis[pos-1] = true;
62                     dist[pos-1] = dist[pos]+1;
63                 }
64                 if (pos%len<len-1 && !vis[pos+1] && map[pos+1]!='|') { //East
65                     q.add(new Integer(pos+1));
66                     vis[pos+1] = true;
67                     dist[pos+1] = dist[pos]+1;
68                 }
69            } 
70         }
71         return dist[x2*len+y2];
72     }
73     public static void main(String[] args)  {
74         Scanner in = new Scanner(System.in);
75         wid = in.nextInt();
76         len = in.nextInt();
77         map = new char[wid*len];
78         int x1=in.nextInt()-1;
79         int y1=in.nextInt()-1;
80         int x2=in.nextInt()-1;
81         int y2=in.nextInt()-1;
82         for (int i=0;i<wid;i++) {
83             String line = in.next();
84             for (int j=0;j<len;j++) {
85                 map[i*len+j]=line.charAt(j);
86             }
87         }
88         in.close();
89         System.out.println(bfs(x1,y1,x2,y2));
90     }
91 }


2. The Clocks

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

  1 import java.io.*;
  2 import java.util.*;
  3 
  4 public class clocks {
  5     public static BufferedReader input;
  6     public static PrintWriter output;
  7     
  8     private static int turn(int state,int pos) {
  9         int tmp = state;
 10         pos = 8 - pos;
 11         tmp &= (3<<pos<<pos);
 12         tmp >>= (pos<<1);
 13         tmp = ((tmp+1)&3);
 14         tmp <<= (pos<<1);
 15         state &= (~(3<<pos<<pos));
 16         state |= tmp;
 17         return state;
 18     }
 19     private static int operate(int state,int mode) {
 20         switch (mode) {
 21         case 1:
 22             state = turn(state,0);
 23             state = turn(state,1);
 24             state = turn(state,3);
 25             state = turn(state,4);
 26             break;
 27         case 2:
 28             state = turn(state,0);
 29             state = turn(state,1);
 30             state = turn(state,2);
 31             break;
 32         case 3:
 33             state = turn(state,1);
 34             state = turn(state,2);
 35             state = turn(state,4);
 36             state = turn(state,5);
 37             break;
 38         case 4:
 39             state = turn(state,0);
 40             state = turn(state,3);
 41             state = turn(state,6);
 42             break;
 43         case 5:
 44             state = turn(state,1);
 45             state = turn(state,3);
 46             state = turn(state,4);
 47             state = turn(state,5);
 48             state = turn(state,7);
 49             break;
 50         case 6:
 51             state = turn(state,2);
 52             state = turn(state,5);
 53             state = turn(state,8);
 54             break;
 55         case 7:
 56             state = turn(state,3);
 57             state = turn(state,4);
 58             state = turn(state,6);
 59             state = turn(state,7);;
 60             break;
 61         case 8:
 62             state = turn(state,6);
 63             state = turn(state,7);
 64             state = turn(state,8);
 65             break;
 66         case 9:
 67             state = turn(state,4);
 68             state = turn(state,5);
 69             state = turn(state,7);
 70             state = turn(state,8);
 71         }
 72         return state;
 73     }
 74     private static boolean threeTimes(int path,int mode)   {
 75         path &= (3<<mode<<mode);
 76         path >>= (mode<<1);
 77         return (path==3);
 78     }
 79     private static void printPath(int path) {
 80         // Print the move sequence except the last one
 81         for (int i=1;i<=9;i++) {
 82             int cnt = (3<<i<<i);
 83             cnt &= path;
 84             cnt >>= (i<<1);
 85             for (int j=0;j<cnt;j++) {
 86                 output.print(i+" ");
 87             }
 88         }
 89     }
 90     private static void bfs(int clk) {
 91         // Breadth-First Search in a State Space:
 92         //        state and path are both coded into integers
 93         Queue<Integer> pathQ = new LinkedList<Integer>();
 94         Queue<Integer> stateQ = new LinkedList<Integer>();
 95         boolean [] vis = new boolean[1<<18];
 96         pathQ.add(new Integer(0));
 97         stateQ.add(new Integer(clk));
 98         vis[clk] = true;
 99         while (!pathQ.isEmpty()) {
100             int path = pathQ.poll().intValue();
101             int state = stateQ.poll().intValue();
102             for (int i=1;i<=9;i++) {
103                 if (threeTimes(path,i)) {
104                     // each move can be operated no more than 3 times
105                     continue;
106                 }
107                 int newState = operate(state,i);
108                 if (newState==0) {
109                     // print the path when reaching the destination
110                     printPath(path);
111                     output.println(i);
112                     return;
113                 } else if (!vis[newState]) {
114                     vis[newState] = true;
115                     stateQ.add(new Integer(newState));
116                     pathQ.add(new Integer(path+(1<<i<<i)));
117                 }
118             }
119         }
120     }
121     public static void main(String [] args) throws IOException {
122         input = new BufferedReader(new FileReader("clocks.in"));
123         int clk = 0;
124         for (int i=0;i<3;i++) {
125             StringTokenizer str = new StringTokenizer(input.readLine());
126             for (int j=0;j<3;j++) {
127                 clk = (clk<<2)+((12-Integer.parseInt(str.nextToken()))&3);
128             }
129         }
130         input.close();
131         output = new PrintWriter(new FileWriter("clocks.out"));
132         if (clk==0) {
133             output.println();
134         } else {
135             bfs(clk);
136         }
137         output.close();
138     }
139 }
原文地址:https://www.cnblogs.com/DevinZ/p/4411438.html