锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
1.絎竴閲岯FS鏍規嵁綆卞瓙鍙Щ鍔ㄧ殑浣嶇疆榪涜BFS.
2.絎簩閲嶅皢綆卞瓙鐩墠鎵鍦ㄧ殑浣嶇疆璁句負涓嶅彲杈?鏍規嵁綆卞瓙鎵鍦ㄧ殑浣嶇疆寰楀嚭浜烘墍搴旇鍦ㄧ殑浣嶇疆,鏍規嵁姝や綅緗浜鴻繘琛屼簡BFS.
鍗沖彲.
]]>Code
1#include <iostream>
2#include <iomanip>
3using namespace std;
4
5const int N = 9;
6
7int factorial(int n)
8{
9 if(n == 0)
10 return 1;
11 return n * factorial(n - 1);
12}
13
14int _tmain(int argc, _TCHAR* argv[])
15{
16 cout << "n e" << endl;
17 cout << "- -----------" << endl;
18
19 double result = 0.0f, temp;
20
21 for(int i = 0; i <= N; ++i)
22
{
23 result = 0.0f;
24
25 for(int j = i; j > -1; --j)
26
{
27 result += 1.0 / factorial(j);
28 }
29
30 if(i > 2)
31
{
32 printf("%d %.9f\n", i, result);
33 }
34 else
35
{
36 cout << i << " " << result << endl;
37 }
38 }
39 return 0;
40}
]]>
]]>
2 #include <iostream>
3 #include <queue>
4 using namespace std;
5
6 const int MAX_STATE = 65536;
7 const int ALL_WHILE_STATE = 0;
8 const int ALL_BLACK_STATE = 65535;
9 const int WIDTH_OF_BOARD = 4;
10 const int SIZE_OF_BOARD = WIDTH_OF_BOARD * WIDTH_OF_BOARD; // 4 * 4
11
12 int ConvertPieceColorToInt(char color)
13 {
14 switch(color)
15 {
16 case 'b':
17 return 1;
18 case 'w':
19 return 0;
20 }
21 }
22
23 int FlipPiece(int state_id, int position)
24 {
25 state_id ^= (1 << position);
26
27 // up
28 if(position - 4 >= 0)
29 state_id ^= (1 << (position - 4));
30 // down
31 if(position + 4 < SIZE_OF_BOARD)
32 state_id ^= (1 << (position + 4));
33 // left
34 if(position % 4 != 0)
35 state_id ^= (1 << (position - 1));
36 // right
37 if(position % 4 != 3)
38 state_id ^= (1 << (position + 1));
39
40 return state_id;
41 }
42
43 int _tmain(int argc, _TCHAR* argv[])
44 {
45 int current_state_id = 0;
46 int state[MAX_STATE];
47 queue<int> search_queue;
48
49 memset(state, -1, sizeof(state));
50
51 char color;
52
53 for(int i = 0; i < SIZE_OF_BOARD; ++i)
54 {
55 cin >> color;
56 current_state_id += ConvertPieceColorToInt(color) << i;
57 }
58
59 if(current_state_id == ALL_WHILE_STATE
60 || current_state_id == ALL_BLACK_STATE)
61 {
62 cout << "0" << endl;
63 return 0;
64 }
65
66 state[current_state_id] = 0;
67 search_queue.push(current_state_id);
68
69 int next_state_id;
70
71 while(!search_queue.empty())
72 {
73 current_state_id = search_queue.front();
74 search_queue.pop();
75
76 for(int i = 0; i < SIZE_OF_BOARD; ++i)
77 {
78 next_state_id = FlipPiece(current_state_id, i);
79 if(next_state_id == ALL_WHILE_STATE
80 || next_state_id == ALL_BLACK_STATE)
81 {
82 cout << state[current_state_id] + 1 << endl;
83 return 0;
84 }
85 assert(next_state_id < MAX_STATE);
86 if(state[next_state_id] == -1 /* not visited */)
87 {
88 state[next_state_id] = state[current_state_id] + 1;
89 search_queue.push(next_state_id);
90 }
91 }
92 }
93
94 cout << "Impossible" << endl;
95 return 0;
96 }
97
98
]]>