锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
銆愰瑙c戯細綺劇‘瑕嗙洊錛孌LX鏈鍩虹鐨勯鐩紝鍗佸瓧閾捐〃紜疄瓚沖寮哄ぇ銆傜暀涓婦LX妯℃澘~
銆愪唬鐮併戯細
2 #include "cstdio"
3 #include "cstring"
4 #include "algorithm"
5 #include "vector"
6 #include "queue"
7 #include "cmath"
8 #include "string"
9 #include "cctype"
10 #include "map"
11 #include "iomanip"
12 #include "set"
13 #include "utility"
14 using namespace std;
15 typedef pair<int, int> pii;
16 #define pb push_back
17 #define mp make_pair
18 #define fi first
19 #define se second
20 #define sof(x) sizeof(x)
21 #define lc(x) (x << 1)
22 #define rc(x) (x << 1 | 1)
23 #define lowbit(x) (x & (-x))
24 #define ll long long
25
26 #define CC 1011
27 #define MM 102011
28
29 int L[MM], R[MM], U[MM], D[MM], C[MM], X[MM];
30 int H[CC], S[CC], Q[CC];
31 int size, n, m;
32
33 void remove(int c) {
34 R[L[c]] = R[c], L[R[c]] = L[c];
35 for(int i = D[c]; i != c; i = D[i])
36 for(int j = R[i]; j != i; j = R[j])
37 U[D[j]] = U[j], D[U[j]] = D[j], --S[C[j]];
38 }
39
40 void resume(int c) {
41 R[L[c]] = L[R[c]] = c;
42 for(int i = U[c]; i != c; i = U[i])
43 for(int j = L[i]; j != i ; j = L[j])
44 ++S[C[U[D[j]] = D[U[j]] = j]];
45 }
46
47 bool Dance(int k) {
48 int c = -1;
49 if(!R[0]) {
50 printf("%d", k);
51 for(int i = 0; i < k; i++) printf(" %d", X[Q[i]]);
52 puts("");
53 return true;
54 }
55 for(int tmp = CC, i = R[0]; i; i = R[i])
56 if(S[i] < tmp) tmp = S[c=i];
57 remove(c);
58 for(int i = D[c]; i != c; i = D[i]) {
59 Q[k] = i;
60 for(int j = R[i]; j != i; j = R[j]) remove(C[j]);
61 if(Dance(k + 1)) return true;
62 for(int j = L[i]; j != i; j = L[j]) resume(C[j]);
63 }
64 resume(c);
65 return false;
66 }
67
68 void Link(int r, int c) {
69 ++S[C[size] = c];
70 D[size] = D[c];
71 U[D[c]] = size;
72 U[size] = c;
73 D[c] = size;
74 if(H[r] < 0) H[r] = L[size] = R[size] = size;
75 else {
76 R[size] = R[H[r]];
77 L[R[H[r]]] = size;
78 L[size] = H[r];
79 R[H[r]] = size;
80 }
81 X[size++] = r;
82 }
83
84 int main() {
85 int num;
86 while(~scanf("%d%d", &n, &m)) {
87 for(int i = 0; i <= m; i++) {
88 S[i] = 0;
89 D[i] = U[i] = i;
90 L[i+1] = i;
91 R[i] = i + 1;
92 }
93 R[m] = 0;
94 L[0] = m;
95 size = m + 1;
96 for(int i = 1, j; i <= n; i++) {
97 H[i] = -1;//璁板緱瑕佸厛緗澶存寚閽堜負-1
98 scanf("%d", &num);
99 while(num--) {
100 scanf("%d", &j);
101 Link(i, j);
102 }
103 }
104 if(!Dance(0)) puts("NO");
105 }
106 return 0;
107 }
108
銆愰瑙c戯細璧よ8瑁哥殑浼樺厛闃熷垪+bfs銆?br /> 鎼滃埌絎竴涓竟鐣岀偣鍗充負絳旀銆?br />
銆愪唬鐮併戯細
2 #include "cstdio"
3 #include "cstring"
4 #include "algorithm"
5 #include "vector"
6 #include "queue"
7 #include "cmath"
8 #include "string"
9 #include "cctype"
10 #include "map"
11 #include "iomanip"
12 using namespace std;
13 #define pb push_back
14 #define mp make_pair
15 #define fi first
16 #define se second
17 #define lc(x) (x << 1)
18 #define rc(x) (x << 1 | 1)
19 #define lowbit(x) (x & (-x))
20 #define ll long long
21 int h, w, d;
22 int sx, sy;
23 char maz[510][510];
24 int dx[] = {0, 0, 1, -1};
25 int dy[]= {-1, 1, 0, 0};
26 bool visit[510][510];
27 struct Node {
28 int x, y, ti;
29 Node(){}
30 Node(int _x, int _y, int _ti) {
31 x = _x, y = _y, ti = _ti;
32 }
33 bool operator<(const Node &x) const {
34 return ti > x.ti;
35 }
36 };
37
38 bool check(int x, int y) {
39 if(x >= 1 && x <= h && y >= 1 && y <= w) return true;
40 return false;
41 }
42
43 void bfs() {
44 int ans;
45 memset(visit, false, sizeof(visit));
46 priority_queue<Node> que;
47 que.push(Node(sx, sy, 1));
48 while(!que.empty()) {
49 Node now = que.top();
50 que.pop();
51 if(visit[now.x][now.y]) continue;
52 else visit[now.x][now.y] = true;
53 if((now.x == 1 || now.x == h) || (now.y == 1 || now.y == w)) {
54 ans = now.ti;
55 break;
56 }
57 for(int i = 0; i < 4; i++) {
58 int nx = now.x + dx[i], ny = now.y + dy[i];
59 if(!check(nx, ny) || maz[nx][ny] == '#' || visit[nx][ny]) continue;
60 if(maz[nx][ny] == '@') que.push(Node(nx, ny, now.ti + d + 1));
61 else que.push(Node(nx, ny, now.ti + 1));
62 }
63 }
64 cout << ans << endl;
65 }
66
67 int main() {
68 int T;
69 scanf("%d", &T);
70 while(T--) {
71 scanf("%d%d%d", &h, &w, &d);
72 for(int i = 1; i <= h; i++) {
73 scanf("%s", &maz[i][1]);
74 }
75 for(int i = 1; i <= h; i++) {
76 for(int j = 1; j <= w; j++) {
77 if(maz[i][j] == 'S') sx = i, sy = j;
78 }
79 }
80 bfs();
81 }
82 return 0;
83 }
84
銆愰瑙c戯細騫抽潰鍥炬煋鑹詫紝鍥涜壊瀹氱悊銆?br /> 鐩存帴dfs鍗沖彲銆?br />
銆愪唬鐮併戯細
2 #include "cstdio"
3 #include "cstring"
4 #include "algorithm"
5 #include "vector"
6 #include "queue"
7 #include "cmath"
8 #include "string"
9 #include "cctype"
10 #include "map"
11 #include "iomanip"
12 using namespace std;
13 #define pb push_back
14 #define mp make_pair
15 #define fi first
16 #define se second
17 #define lc(x) (x << 1)
18 #define rc(x) (x << 1 | 1)
19 #define lowbit(x) (x & (-x))
20 #define ll long long
21 #define maxn 30
22 bool maz[maxn][maxn], visit[maxn];
23 int color[maxn];
24 int n;
25 char a, ch;
26
27 int dfs(int u, int cnt) {
28 memset(visit, false, sizeof(visit));
29 if(u == n) return true;
30 for(int i = 0; i < n; i++) {
31 if(color[i] != -1 && maz[u][i]) {
32 visit[color[i]] = true;
33 }
34 }
35 for(int i = 0; i < cnt; i++) {
36 if(!visit[i]) {
37 color[u] = i;
38 if(dfs(u+1, cnt)) return true;
39 }
40 }
41 return false;
42 }
43
44 void solve() {
45 int ans = 5;
46 for(int i = 1; i < 5; i++) {
47 memset(color, -1, sizeof(color));
48 if(dfs(0, i)) {
49 ans = i;
50 break;
51 }
52 }
53 if(ans == 1) printf("1 channel needed.\n");
54 else printf("%d channels needed.\n", ans);
55 }
56
57 int main() {
58 while(~scanf("%d", &n), n) {
59 getchar();
60 memset(maz, false, sizeof(maz));
61 for(int i = 0; i < n; i++) {
62 scanf("%c:", &a);
63 while((ch = getchar()) != 10) {
64 maz[a-'A'][ch-'A'] = maz[ch-'A'][a-'A'] = true;
65 }
66 }
67 solve();
68 }
69 return 0;
70 }
71
宸茬煡媯嬬洏鏈澶氭湁15涓瀛愶紝鏈変笖鍙湁涓涓櫧鑹茬殑鐨囷紝 涓旈粦媯嬪湪鍚冧笉鍒扮殗鐨勬儏鍐典笅鏄笉浼氱Щ鍔ㄧ殑銆?br /> 闂紝鍦ㄤ笉琚悆鐨勬儏鍐典笅錛岀殗鏈灝戠敤澶氬皯姝ュ彲浠ュ悆鍏夋墍鏈夌殑榛戞銆?br />
銆愰瑙c戯細鑰冭檻鍒版渶澶氬彧鏈?5涓瀛愶紝鍙互榪涜鐘舵佸帇緙╃殑bfs銆傝繖閲屾湁涓猼rick灝辨槸錛岃薄鍜岃濺鍙兘浼氳鍏朵粬媯嬪瓙鎸$潃鑰屽悆涓嶅埌鐨囥?br />
銆愪唬鐮併戯細
2 #include "cstdio"
3 #include "cstring"
4 #include "queue"
5 using namespace std;
6 int r, c, sx, sy;
7 int cnt;
8 char maz[20][20];
9 int maze[20][20];
10 bool visit[20][20][40000];
11 int dir[][2] = {0, 1, 1, 0, -1, 0, 0, -1, 1, -1, -1, 1, 1, 1, -1, -1};
12 int kdir[][2] = {-2, -1, -2, 1, 2, 1, 2, -1, 1, 2, 1, -2, -1, 2, -1, -2};
13 int bdir[][2] = {1, -1, 1, 1, -1, -1, -1, 1};
14 int rdir[][2] = {0, 1, 1, 0, -1, 0, 0, -1};
15 struct Node {
16 int x, y, ti, mask;
17 Node(){}
18 Node(int _x, int _y, int _ti, int _mask) {
19 x = _x, y = _y, ti = _ti, mask = _mask;
20 }
21 };
22
23 struct Piece {
24 int x, y, type;
25 Piece(){}
26 Piece(int _x, int _y, int _type) {
27 x = _x, y = _y, type = _type;
28 }
29 }p[20];
30
31 bool check(int x, int y) {
32 if(x >= 1 && x <= r && y >= 1 && y <= c) return true;
33 return false;
34 }
35
36 bool IsEat(int x, int y, int mask) {
37 //Knight
38 for(int i = 0; i < 8; i++) {
39 int xx = x + kdir[i][0], yy = y + kdir[i][1];
40 int tmp = maze[xx][yy];
41 if(!check(xx, yy)) continue;
42 if(tmp >= 0 && !(mask & (1 << tmp)) && p[tmp].type == 0) return true;
43 }
44 //Bishop
45 for(int i = 0; i < 4; i++) {
46 int xx = x + bdir[i][0], yy = y + bdir[i][1];
47 int tmp = maze[xx][yy];
48 while(check(xx, yy)) {
49 if(tmp >= 0 && !(mask & (1 << tmp))) {
50 if(p[tmp].type == 1) return true;
51 else break;
52 } else xx += bdir[i][0], yy += bdir[i][1], tmp = maze[xx][yy];
53 }
54 }
55 //Rook
56 for(int i = 0; i < 4; i++) {
57 int xx = x + rdir[i][0], yy = y + rdir[i][1];
58 int tmp = maze[xx][yy];
59 while(check(xx, yy)) {
60 if(tmp >= 0 && !(mask & (1 << tmp))) {
61 if(p[tmp].type == 2) return true;
62 else break;
63 } else xx += rdir[i][0], yy += rdir[i][1], tmp = maze[xx][yy];
64 }
65 }
66 return false;
67 }
68
69 int bfs() {
70 queue<Node> que;
71 memset(visit, false, sizeof(visit));
72 visit[sx][sy][0] = true;
73 que.push(Node(sx, sy, 0, 0));
74 while(!que.empty()) {
75 Node now = que.front();
76 que.pop();
77 if(now.mask == (1 << cnt) - 1) return now.ti;
78 for(int i = 0; i < 8; i++) {
79 int nx = now.x + dir[i][0], ny = now.y + dir[i][1], mask = now.mask;
80 if(!check(nx, ny)) continue;
81 int tmp = maze[nx][ny];
82 if(tmp >= 0) mask |= (1 << tmp);
83 if(visit[nx][ny][mask]) continue;
84 if(IsEat(nx, ny, mask)) continue;
85 que.push(Node(nx, ny, now.ti + 1, mask));
86 visit[nx][ny][mask] = true;
87 }
88 }
89 return -1;
90 }
91
92 int main() {
93 while(~scanf("%d%d", &r, &c)) {
94 cnt = 0;
95 memset(maze, -1, sizeof(maze));
96 for(int i = 1; i <= r; i++) scanf("%s", &maz[i][1]);
97 for(int i = 1; i <= r; i++) {
98 for(int j = 1; j <= c; j++) {
99 if(maz[i][j] == '.') continue;
100 if(maz[i][j] == '*') sx = i, sy = j;
101 else if(maz[i][j] == 'K') {
102 maze[i][j] = cnt, p[cnt++] = Piece(i, j, 0);
103 } else if(maz[i][j] == 'B') {
104 maze[i][j] = cnt, p[cnt++] = Piece(i, j, 1);
105 } else maze[i][j] = cnt, p[cnt++] = Piece(i, j, 2);
106 }
107 }
108 int ans = bfs();
109 cout << ans << endl;
110 }
111 return 0;
112 }
銆愰瑙c戯細闈炲父濂界殑鎼滅儲棰橈紒鍒氬紑濮嬩互涓烘槸涓崥寮堢殑涓滆タ錛屽悗鏉ヤ竴鐩存帹涓嶅埌鏈浼樺喅絳栵紝鎰忚瘑鍒版暟鎹湁榪欎箞灝戯紝搴旇鍙互鎼滅儲榪囧幓銆?br /> 鍥犱負n灝忎簬絳変簬21錛屽彲浠ラ噰鐢ㄧ姸鎬佸帇緙┿傚浜庣尨瀛愬彲鑳藉湪鐨勬煇浜涙5鏍戞爣璁頒負1錛屽惁鍒欐爣璁頒負0錛屾渶緇堢姸鎬佷負鍏?錛屽嵆0.
鐚村瓙浠庡綋鍓嶇殑鏍戣煩鍒扮浉閭葷殑鏍戣繖涓鍏堥澶勭悊錛屼互鍚庢瘡嬈″彲浠ョ敤O(n)鐨勬椂闂村鏉傚害杞Щ鐘舵併?br /> 涓嶅彲鑳藉瓨鍦ㄧ尨瀛愮殑鏍戞槸涓嶇敤綆$殑錛岃繖閲屽彲浠ュ壀鏋濓紱濡傛灉m > n - 1鍗沖叧緋諱腑瀛樺湪鐜垯涓瀹氭棤瑙c?br />
銆愪唬鐮併戯細
2 #include "cstdio"
3 #include "cstring"
4 #include "queue"
5 #include "string"
6 using namespace std;
7 int n, m;
8 bool visit[1<<22];
9 int edge[22];
10 string ans;
11 struct Node {
12 int mask;
13 string path;
14 Node(){}
15 Node(int _mask, string _path) {
16 mask = _mask, path = _path;
17 }
18 };
19
20 bool bfs() {
21 queue<Node> que;
22 memset(visit, false, sizeof(visit));
23 que.push(Node((1 << n) - 1, ""));
24 visit[(1<<n)-1] = true;
25 while(!que.empty()) {
26 Node now = que.front();
27 que.pop();
28 for(int i = 0; i < n; i++) {
29 if(now.mask & (1 << i)) {
30 int mask = now.mask ^ (1 << i);
31 int newmask = 0;
32 for(int j = 0; j < n; j++)
33 if(mask & (1 << j)) newmask |= edge[j];
34 if(visit[newmask]) continue;
35 visit[newmask] = true;
36 char ch = i + '0';
37 string tmp = now.path + ch;
38 if(newmask == 0) {
39 ans = tmp;
40 return true;
41 }
42 que.push(Node(newmask, tmp));
43 }
44 }
45 }
46 return false;
47 }
48
49 void solve() {
50 if((m > n - 1) || !bfs()) printf("Impossible\n");
51 else {
52 printf("%d:", ans.length());
53 for(int i = 0; i < ans.length(); i++) printf(" %d", ans[i] - '0');
54 printf("\n");
55 }
56 return;
57 }
58
59 int main() {
60 while(~scanf("%d%d", &n, &m)) {
61 if(!n && !m) break;
62 memset(edge, 0, sizeof(edge));
63 for(int i = 0; i < m; i++) {
64 int u, v;
65 scanf("%d%d", &u, &v);
66 edge[u] |= (1 << v), edge[v] |= (1 << u);
67 }
68 solve();
69 }
70 return 0;
71 }
銆愰瑙c戯細IDA*錛岀敤姣忎釜鏁板瓧鍒扮洰鏍囦綅緗殑鍝堟浖欏胯礬闀挎潵鍋氫及浠峰嚱鏁幫紝濂囧伓鍒ゆ柇鏈夋病鏈夊彲琛岃В銆?br />
銆愪唬鐮併戯細
2 #include "cstdio"
3 #include "cstring"
4 #include "cstdlib"
5 #include "queue"
6 using namespace std;
7 #define SIZE 3
8 const int dx[] = {-1, 0, 0, 1};
9 const int dy[] = {0, -1, 1, 0};
10 char op[] = {'u', 'l', 'r', 'd'};
11 int goal_state[10][2] = {{0,0}, {1,1}, {1,2}, {1,3}, {2,1}, {2,2}, {2,3}, {3,1}, {3,2}, {3,3}};
12 int bound;
13 char a[10];
14 bool flag;
15 char solution[1000];
16
17 struct Eight {
18 char board[4][4];
19 int x, y;
20 };
21
22 int h(char board[][4]) {
23 int cost = 0;
24 for(int i = 1; i <= SIZE; i++)
25 for(int j = 1; j <= SIZE; j++)
26 if(board[i][j]) cost += abs(i - goal_state[board[i][j]][0]) + abs(j - goal_state[board[i][j]][1]);
27 return cost;
28 }
29
30 bool solvable() {
31 int cnt = 0;
32 for(int i = 1; i <= 9; i++)
33 for(int j = 1; j < i; j++)
34 if(a[j] < a[i] && a[j]) cnt++;
35 if(cnt & 1) return false;
36 return true;
37 }
38
39 bool check(int x, int y) {
40 if(x >= 1 && x <= SIZE && y >= 1 && y <= SIZE) return true;
41 return false;
42 }
43
44 bool dfs(Eight now, int dv, char pre) {
45 if(h(now.board) == 0) {
46 solution[dv] = 0;
47 return true;
48 }
49 for(int i = 0; i < 4; i++) {
50 if(i + pre == 3) continue;//涓庝笂涓姝ョ浉鍙嶇殑縐誨姩
51 Eight next = now;
52 next.x += dx[i], next.y += dy[i];
53 if(check(next.x, next.y)) {
54 solution[dv] = op[i];
55 swap(next.board[next.x][next.y], next.board[now.x][now.y]);
56 if(h(next.board) + dv >= bound) continue;
57 if(dfs(next, dv + 1, i)) return true;
58 }
59 }
60 return false;
61 }
62
63 void IDA_star(Eight st) {
64 bound = h(st.board);
65 for(bound = h(st.board); !dfs(st, 0, -10); bound++);
66 puts(solution);
67 }
68
69 int main() {
70 int sx, sy;
71 char c[2];
72 while(1) {
73 Eight st;
74 for(int i = 1; i <= SIZE; i++) {
75 for(int j = 1; j <= SIZE; j++) {
76 if(scanf("%s", c) == EOF) return 0;
77 if(c[0] == 'x') {
78 st.board[i][j] = 0;
79 st.x = i, st.y = j;
80 } else st.board[i][j] = c[0] - '0';
81 a[(i - 1) * SIZE + j] = st.board[i][j];
82 }
83 }
84 if(solvable()) IDA_star(st);
85 else printf("unsolvable\n");
86 }
87 return 0;
88 }
89
銆愰殢絎斻戯細榪欓亾棰樺緢鏃╀箣鍓嶅氨榪囦簡錛岃繖鍑犲ぉ鐪嬪洖涔嬪墠鐨勪唬鐮侊紝瑙夊緱浠ュ墠鐨勪唬鐮佸啓寰楀お鐑備簡錛岃屼笖閭d釜浠g爜椋庢牸涔熶笉濂界湅錛屼簬鏄喅瀹氬啀閲嶅啓涓杈癸紝欏轟究鎶妘va鐨勪篃榪囦簡銆傝繖嬈¢噸鍐欐瘮涔嬪墠鐨勪唬鐮佸姞澶氫簡涓涓噸瑕佺殑鍓灊錛屾渶鍚庡湪uva涓婁互0.2s榪囦簡銆?br />
銆愰瑙c戯細涓嶅緱涓嶈錛岃繖閬撻鍑哄緱闈炲父濂斤紝鐗瑰埆鏄痷va閭i噷鐨勫ぇ鏁版嵁錛坧oj鐨勬暟鎹お姘翠簡錛夛紝瀵逛簬鍓灊鑳藉姏瑕佹眰寰堥珮銆備笅闈㈣涓嬪嚑涓噸瑕佺殑鍓灊錛?br />1.鎶婃墍鏈夋湪媯嶇殑闀垮害浠庡ぇ鍒板皬鎺掑垪錛岀粍鍚堟湪媯掓椂浼樺厛浣跨敤闀跨殑鏈ㄦ錛岃繖鏍峰彲浠ュ姞蹇粍鍚堥熷害錛屽茍涓斿鍚庨潰鐨勫壀鏋濇湁甯姪銆?br />2.鏈ㄦ鐨勯暱搴︿竴瀹氭槸澶т簬絳変簬鏈闀挎湪媯嶇殑闀垮害騫朵笖灝忎簬絳変簬鎵鏈夋湪媯嶉暱搴︾殑鍜岋紝榪欎釜寰堝鏄撹瘉鏄庛?br />3.鏈ㄦ鐨勯暱搴︿竴瀹氭槸鎵鏈夋湪媯嶉暱搴︾殑鍜岀殑綰︽暟錛岃繖涓篃寰堝鏄撹瘉鏄庛?br />4.鍦ㄦ煇涓涓湪媯掔殑緇勫悎榪囩▼涓紝瀵逛簬褰撳墠鐨勬湪媯峴tick[i]錛屽鏋渟tick[i-1]娌℃湁琚粍鍚堝茍涓攕tick[i] == stick[i-1]錛岄偅涔堜笉鐢ㄨ冭檻stick[i]錛屾樉鐒秙tick[i]鏈緇堜篃涓嶄細琚粍鍚堛?br />5.濡傛灉姝ゆ鏄湪灝濊瘯絎琲涓湪媯掔殑絎竴孌碉紝鍋囪stick[j]涓哄綋鍓嶅彲浠ヨ浣跨敤鐨勬渶闀跨殑鏈ㄦ錛屽鏋滄嬈$粍鍚堝け璐ワ紝鐩存帴閫鍑烘悳绱紝鍗抽鍥炲埌瀵圭i-1涓湪媯掔殑鎼滅儲銆傝瘯鎯籌細澶辮觸璇存槑鐜板湪浣跨敤stick[j]鏄笉鍙鐨勶紝閭d箞浠ュ悗鏃犺浠涔堟椂鍊欎嬌鐢╯tick[j]閮芥槸涓嶅彲琛岀殑錛屽洜涓轟互鍚庡啀澶勭悊stick[j]鏃跺彲浣跨敤鐨勬湪媯嶄竴瀹氭槸褰撳墠鍙嬌鐢ㄧ殑鏈ㄦ鐨勫瓙闆嗭紝鍦ㄦ洿澶氭湪媯嶉夋嫨鐨勬儏鍐典笅閮戒笉鑳界粍鍚堟垚鍔燂紝閭d箞錛屽湪鏇村皯鏈ㄦ閫夋嫨鐨勬儏鍐典笅涓瀹氫笉鑳界粍鍚堟垚鍔熴?br />
銆愪唬鐮併戯細
2 #include "cstdio"
3 #include "cstring"
4 #include "algorithm"
5 #include "functional"
6 using namespace std;
7 #define maxn 65
8 int n, sum, goal;
9 int stick[maxn];
10 bool visit[maxn];
11
12 bool cmp(const int &a, const int &b) {
13 return a > b;
14 }
15
16 bool dfs(int now, int index, int cnt) {
17 if(goal * cnt == sum) return true;
18 for(int i = index; i < n; i++) {
19 if(visit[i] || (i && !visit[i-1] && stick[i] == stick[i-1])) continue;
20 if(now + stick[i] == goal) {
21 visit[i] = true;
22 if(dfs(0, 0, cnt + 1)) return true;
23 visit[i] = false;
24 return false;
25 } else if(now + stick[i] < goal) {
26 visit[i] = true;
27 if(dfs(now + stick[i], i + 1, cnt)) return true;
28 visit[i] = false;
29 if(now == 0) return false;
30 }
31 }
32 return false;
33 }
34
35 int solve() {
36 sort(stick, stick + n, cmp);
37 for(goal = stick[0]; goal < sum; goal++) {
38 if(sum % goal != 0) continue;
39 memset(visit, false, sizeof(visit));
40 if(dfs(0, 0, 0)) break;
41 }
42 return goal;
43 }
44
45 int main() {
46 while(~scanf("%d", &n)) {
47 if(!n) break;
48 sum = 0;
49 for(int i = 0; i < n; i++) {
50 scanf("%d", &stick[i]);
51 sum += stick[i];
52 }
53 printf("%d\n", solve());
54 }
55 return 0;
56 }
銆愰瑙c戯細閲囩敤浜嗕紭鍏堥槦鍒?鐘舵佸帇緙゜fs錛岃繖閲岃棰勫鐞嗗ソ鎼哄甫浜嗙姸鎬乻鐨勪唬浠穋ost[s]錛屼箣鍚庣殑bfs璇ユ庝箞鍋氬氨鎬庝箞鍋氥傝繖閲岀敤浼樺厛闃熷垪鏈変釜濂藉錛岃繖鏍峰氨淇濊瘉姣忔鍑洪槦鐨勯兘鏄渶浼樼殑鐐癸紝鐩告瘮鐢ㄤ竴涓暟緇勬潵璁板綍錛屾洿鐪佺┖闂達紝騫朵笖浠g爜閲忎篃灝戜簡銆?br />
銆愪唬鐮併戯細
2 #include "cstdio"
3 #include "cstring"
4 #include "queue"
5 using namespace std;
6 char maz[25][25];
7 int k, p, r, c;
8 int a[15], b[15], cost[1<<11];
9 const int dx[] = {0, 1, 0, -1};
10 const int dy[] = {-1, 0, 1, 0};
11 int sx, sy;
12 bool visit[25][25][1<<11];
13
14 struct pos {
15 int x, y, cost, collect;
16 pos() {}//鏋勯犲嚱鏁?/span>
17 pos(int a, int b, int c, int d) {
18 x = a, y = b, cost = c, collect = d;
19 }
20 bool operator< (const pos &a) const {
21 return cost > a.cost;
22 }
23 };
24
25 bool check(int x, int y) {
26 return x >= 0 && x < r && y >= 0 && y < c;
27 }
28
29 int bfs() {
30 priority_queue<pos> que;
31 memset(visit, false, sizeof(visit));
32 que.push(pos(sx, sy, 0, 0));
33 visit[sx][sy][0] = true;
34 while(!que.empty()) {
35 pos now = que.top();
36 que.pop();
37 int x = now.x, y = now.y, collect = now.collect, ncost = now.cost + cost[collect] + 1;
38 if(ncost > p) continue;
39 for(int i = 0; i < 4; i++) {
40 int nx = x + dx[i], ny = y + dy[i];
41 if(!check(nx, ny) || maz[nx][ny] == '#' || visit[nx][ny][collect]) continue;
42 if(nx == sx && ny == sy) {
43 if(collect == (1 << k) - 1) return ncost;
44 continue;
45 }
46 if(maz[nx][ny] <= 'Z' && maz[nx][ny] >= 'A') {
47 int t = maz[nx][ny] - 'A';
48 if((collect & (1 << t)) == 0) {
49 que.push(pos(nx, ny, ncost + a[t], collect | (1<<t)));
50 visit[nx][ny][collect|(1<<t)] = true;
51 }
52 }
53 que.push(pos(nx, ny, ncost, collect));
54 visit[nx][ny][collect] = true;
55 }
56 }
57 return -1;
58 }
59
60 int main() {
61 int T;
62 scanf("%d", &T);
63 while(T--) {
64 scanf("%d%d%d%d\n", &r, &c, &k, &p);
65 for(int i = 0; i < r; i++) {
66 for(int j = 0; j <= c; j++) {
67 scanf("%c", &maz[i][j]);
68 if(maz[i][j] == '*') sx = i, sy = j;
69 }
70 }
71 for(int i = 0; i < k; i++) scanf("%d%d", &a[i], &b[i]);
72 memset(cost, 0, sizeof(cost));
73 for(int t = 0; t < (1 << k); t++)
74 for(int j = 0; j < k; j++)
75 if(t & (1 << j)) cost[t] += b[j];
76 int ans = bfs();
77 if(ans == -1) printf("Impossible\n");
78 else printf("%d\n", ans);
79 }
80 return 0;
81 }
1.鏉姝諱竴涓晫浜哄茍浣縎P鍥炲1鐐?nbsp;
2.鎭㈠floor(0.2*LH錛夌殑琛閲?nbsp;
3.濡傛灉褰撳墠SP澶т簬0錛屽彲浠ュ彂鍔ㄦ妧鑳芥潃姝籇[SP]涓晫浜猴紝騫朵笖SP娓呴浂
4.寰呮満錛屽叾瀹炲氨鏄粈涔堥兘涓嶅仛
姣忔鍦―oraemon琛屽姩瀹屽悗錛屾瘡涓晫浜哄彲浠ユ秷鑰桪oraemon涓婊磋銆傚湪姣忎釜鍥炲悎鐨勬渶鍚庯紝Doraemon鑳藉鎭㈠[褰撳墠鏁屼漢鏁?3]鐨凷P銆備竴寮濮婦oraemon澶勪簬婊¤錛岄浂SP鐨勭姸鎬侊紝闂瓺oraemon鏉姝繪墍鏈夋晫浜虹殑鏈灝忓洖鍚堟暟鏄灝戯紵濡傛灉Doraemon涓嶅彲鑳芥垬鑳滐紝璇瘋緭鍑?#8220;HELP!”銆?br />
銆愰瑙c戯細鍦ㄦ瘮璧涗腑鐪嬪埌榪欓亾棰樿寰楁槸鍗氬紙錛岀劧鍚嶻Y浜嗕竴杞箣鍚庢病鎵懼埌浠諱綍瑙勫緥銆傚悗鏉ュ彂鐜拌繖鏄竴閬撴悳绱㈤錛屼竴涓崟綰殑bfs鍙В錛岃屼笖bfs鑳芥弧瓚蟲渶灝忕殑鍥炲悎鏁般傝繖閬撻緇嗚妭鐨勫湴鏂規瘮杈冨錛?br /> 1.褰撳墠鐨凥P鍜孲P鍦ㄤ換浣曟椂鍊欓兘涓嶈兘瓚呰繃涓婇檺LH銆丩S
2.姣忓洖鍚堢粨鏉熷墠閮借鎭㈠[褰撳墠鏁屼漢鏁?3]鐨凷P
3.姣忓洖鍚堟繪槸Doraemon鍏堣鍔紝鐒跺悗杞埌鏁屼漢琛屽姩
4.鍦ㄤ嬌鐢ㄥ畬鎶鑳藉悗SP瑕佸彉鎴?錛屽茍涓?[褰撳墠鏁屼漢鏁?- D[SP]] 鍙兘鍑虹幇璐熸暟
5.[灝忎紭鍖朷絎洓縐嶆搷浣滃緟鏈烘樉鐒舵槸娌$敤鐨勶紝濡傛灉閫夋嫨寰呮満鐨勮瘽榪樹笉濡傞夋嫨鍔犺錛岃繖閲屽彲浠ュ壀鎺夈?br />
銆愪唬鐮併戯細
2 #include "cstdio"
3 #include "cstring"
4 #include "queue"
5 #include "cmath"
6 using namespace std;
7 #define MAX 105
8 int lh, ls, n;
9 int d[MAX];
10 int visit[255][105][45];
11 int add;
12
13 struct st {
14 int hp, sp, cnt;
15 void init(int a, int b, int c) {
16 if(a > lh) a = lh;
17 if(b > ls) b = ls;
18 hp = a, sp = b, cnt = c;
19 }
20 } start;
21
22 int bfs() {
23 queue<st> que;
24 que.push(start);
25 visit[start.hp][start.sp][start.cnt] = 1;
26 while (!que.empty()) {
27 st now = que.front(), next;
28 que.pop();
29 //絎竴縐嶆搷浣?/span>
30 next.init(now.hp - (now.cnt - 1), now.sp + 1 + (now.cnt - 1) % 3, (now.cnt - 1));
31 if (next.cnt <= 0) return visit[now.hp][now.sp][now.cnt];
32 if (next.hp > 0 && !visit[next.hp][next.sp][next.cnt]) {
33 visit[next.hp][next.sp][next.cnt] = visit[now.hp][now.sp][now.cnt] + 1;
34 que.push(next);
35 }
36 //絎簩縐嶆搷浣?/span>
37 next.init(now.hp + add - now.cnt, now.sp + now.cnt % 3, now.cnt);
38 if (next.hp > 0 && !visit[next.hp][next.sp][next.cnt]) {
39 visit[next.hp][next.sp][next.cnt] = visit[now.hp][now.sp][now.cnt] + 1;
40 que.push(next);
41 }
42 //絎笁縐嶆搷浣?/span>
43 if (now.sp > 0) {
44 if(now.cnt - d[now.sp] <= 0) return visit[now.hp][now.sp][now.cnt];
45 next.init(now.hp - (now.cnt - d[now.sp]), (now.cnt - d[now.sp]) % 3, (now.cnt - d[now.sp]));
46 if (next.hp > 0 && !visit[next.hp][next.sp][next.cnt]) {
47 visit[next.hp][next.sp][next.cnt] = visit[now.hp][now.sp][now.cnt] + 1;
48 que.push(next);
49 }
50 }
51 }
52 return -1;
53 }
54
55 void init() {
56 for (int i = 0; i < 255; i++)
57 for (int j = 0; j < 105; j++)
58 for (int k = 0; k < 45; k++)
59 visit[i][j][k] = 0;
60 add = (int) floor(0.2 * lh);
61 }
62
63 int main() {
64 while (~scanf("%d%d%d", &lh, &ls, &n)) {
65 for (int i = 1; i <= ls; i++) scanf("%d", &d[i]);
66 start.init(lh, 0, n);
67 init();
68 int ans = bfs();
69 if (ans == -1) printf("HELP!\n");
70 else printf("%d\n", ans);
71 }
72 return 0;
73 }
銆愰瑙c戯細鐪嬪埌鏈鐭椂闂達紝鎴戜滑寰堝鏄撳氨鎯沖埌bfs銆傝繖棰樹笌騫蟲椂鐨勮糠瀹棶棰樻湁涓涓笉鍚岀殑鍦版柟灝辨槸錛屽鉤鏃墮鐩墍緇欑殑榪峰鏄刊瑙嗗浘錛岃岃繖閲屾槸涓昏鍥撅紝鑰冭檻鍒拌繖涓鐐癸紝鎴戜滑灝辮鎶婇噸鍔涘姞鍏ユ垜浠殑鎬濊冭寖鍥淬傜畝鍗曠偣鏉ヨ灝辨槸錛屽鏋滃綋鍓嶈繖涓漢鍚戝乏璧頒簡涓姝ワ紝鑰岄偅涓綅緗槸娌℃湁绔欏潡鐨勶紝閭d箞浜哄氨浼氬悜涓嬭穼錛岀洿鍒版湁涓滆タ欏朵綇涓烘銆傚疄闄呮搷浣滅殑鏃跺欏彲浠ュ浐瀹氳糠瀹笉鍔紝鑰屾槸閫氳繃浜烘潵鏃嬭漿錛岃繖鏄劇劧鏄彲浠ョ殑銆備竴寮濮嬫垜浠彲浠ラ澶勭悊涓涓嬪浜庢瘡涓牸杞姩涔嬪悗浜轟細璺屽湪鍝噷錛岃繖鏍鋒垜浠互鍚庡氨鍙互鐢∣錛?錛夌殑鏃墮棿鏉ユ壘鍒頒笅涓涓姸鎬併傚叾浣欑殑鎿嶄綔璺熸櫘閫歜fs涓鏍楓?br />
銆愪唬鐮併戯細
2 #include "queue"
3 #include "cstdio"
4 using namespace std;
5 #define MAX 505
6 struct node {
7 int x, y, dir;
8 void init(int a, int b, int c) {
9 x = a, y = b, dir = c;
10 }
11 }rot[MAX][MAX][4], s;
12 int n, m;
13 int ti[MAX][MAX][4];
14 char maz[MAX][MAX];
15 const int dx[4] = {1, 0, -1, 0};
16 const int dy[4] = {0, 1, 0, -1};
17
18 bool check(int i, int j) {
19 if(i >= 1 && i <= n && j >= 1 && j <= m) return true;
20 return false;
21 }
22
23 int bfs() {
24 queue<node> que;
25 que.push(s);
26 ti[s.x][s.y][s.dir] = 1;
27 while(!que.empty()) {
28 node now = que.front(), next;
29 que.pop();
30 if(maz[now.x][now.y] == 'E') return ti[now.x][now.y][now.dir];
31 for(int i = 0; i < 4; i++) {
32 if((now.dir % 2 == 0 && i % 2 != 0) || (now.dir % 2 != 0 && i % 2 == 0)) {
33 if(maz[now.x+dx[i]][now.y+dy[i]] != '#') {
34 next = rot[now.x+dx[i]][now.y+dy[i]][now.dir];
35 if(!ti[next.x][next.y][next.dir]) {
36 ti[next.x][next.y][next.dir] = ti[now.x][now.y][now.dir] + 1;
37 que.push(next);
38 }
39 }
40 }
41 if(i % 2) {
42 next = rot[now.x][now.y][(now.dir + i) % 4];
43 if(!ti[next.x][next.y][next.dir]) {
44 ti[next.x][next.y][next.dir] = ti[now.x][now.y][now.dir] + 1;
45 que.push(next);
46 }
47 }
48 }
49 }
50 return 0;
51 }
52
53 void init() {
54 for(int i = n; i; i--)
55 for(int j = m; j; j--)
56 if(maz[i][j] != '#') {
57 if(check(i + 1, j) && maz[i+1][j] != '#') rot[i][j][0] = rot[i+1][j][0];
58 else rot[i][j][0].init(i, j, 0);
59 if(check(i, j + 1) && maz[i][j+1] != '#') rot[i][j][3] = rot[i][j+1][3];
60 else rot[i][j][3].init(i, j, 3);
61 }
62 for(int i = 1; i <= n; i++)
63 for(int j = 1; j <= m; j++)
64 if(maz[i][j] != '#') {
65 if(check(i - 1, j) && maz[i-1][j] != '#') rot[i][j][2] = rot[i-1][j][2];
66 else rot[i][j][2].init(i, j, 2);
67 if(check(i, j - 1) && maz[i][j-1] != '#') rot[i][j][1] = rot[i][j-1][1];
68 else rot[i][j][1].init(i, j, 1);
69 }
70 }
71
72 int main() {
73 while(~scanf("%d%d", &n, &m)) {
74 getchar();
75 for(int i = 1; i <= n; i++)
76 for(int j = 1; j <= m + 1; j++) {
77 for(int k = 0; k < 4; k++) ti[i][j][k] = 0;
78 scanf("%c", &maz[i][j]);
79 if(maz[i][j] == '|') s.init(i, j, 0);
80 }
81 init();
82 int ans = bfs();
83 if(!ans) printf("Can not escape!\n");
84 else printf("%d\n", ans - 1);
85 }
86 return 0;
87 }
銆愰瑙c戯細棰樼洰鏄庢樉灝辨槸璁╀綘鎼滅儲鐨勶紝瀵逛簬姣忎釜鏍煎瓙i,j鍔犲涓緇磋褰曟柟鍚戝氨濂戒簡銆傜敱浜庢暟鎹渶澶ф槸500*500錛屾墍浠ョ敤緋葷粺鏍堜細鐖嗘帀錛屾敼鎴愰潪閫掑綊鍗沖彲銆傚鏋滀綘娣變竴灞傜悊瑙i鐩殑璇濓紝浼氬彂鐜版槸涓嶉渶瑕佺敤鏍堢殑錛屽彧闇瑕佷竴涓驚鐜氨鍙互浜嗭紝鐩存帴涓婁唬鐮佷簡錛屽ぇ瀹朵笉鎳傚啀闂惂銆?br />
銆愪唬鐮併戯細
2 #include <cstdio>
3 using namespace std;
4 #define MAX 505
5
6 struct node {
7 int x, y, dir;
8 void init(int a, int b, int c) {
9 x = a, y = b, dir = c;
10 }
11 } s, t, goal;
12 int r, c, n, m;
13 int maz[MAX][MAX][4];
14 bool visit[MAX][MAX];
15 const int dx[] = {0, 1, 0, -1};
16 const int dy[] = {1, 0, -1, 0};
17
18 void dfs(node now, node goal) {
19 node next;
20 while (1) {
21 visit[now.x][now.y] = true;
22 if (now.x == goal.x && now.y == goal.y && now.dir == goal.dir) break;
23 for (int i = now.dir; i < 4 + now.dir; i++) {
24 int index = (now.dir - (i - now.dir) + 4) % 4;
25 if (maz[now.x][now.y][(now.dir - (i - now.dir) + 5) % 4] == 0) {
26 next.init(now.x + dx[index], now.y + dy[index], (now.dir - (i - now.dir) + 5) % 4);
27 now = next;
28 break;
29 }
30 }
31 }
32 }
33
34 void init() {
35 for (int i = 1; i <= r; i++)
36 for (int j = 1; j <= c; j++) {
37 visit[i][j] = false;
38 for (int k = 0; k < 4; k++)
39 maz[i][j][k] = 1;
40 }
41 maz[t.x][t.y][s.dir] = maz[s.x][s.y][t.dir] = 0;
42 }
43
44 int main() {
45 int T;
46 scanf("%d", &T);
47 while (T--) {
48 scanf("%d%d", &r, &c);
49 scanf("%d%d", &s.y, &t.y);
50 s.init(1, s.y + 1, 2), t.init(r, t.y + 1, 0);
51 init();
52 for (int i = 1; i < 2 * r; i++) {
53 if (i % 2 != 0) {
54 for (int j = 1; j < c; j++) {
55 scanf("%d", &maz[(i + 1) / 2][j][1]);
56 maz[(i + 1) / 2][j + 1][3] = maz[(i + 1) / 2][j][1];
57 }
58 } else {
59 for (int j = 1; j <= c; j++) {
60 scanf("%d", &maz[i / 2][j][2]);
61 maz[i / 2 + 1][j][0] = maz[i / 2][j][2];
62 }
63 }
64 }
65 goal.init(t.x + 1, t.y, s.dir);
66 dfs(s, goal);
67 goal.init(s.x - 1, s.y, t.dir);
68 dfs(t, goal);
69 bool flag = false;
70 for (int i = 1; i <= r; i++)
71 for (int j = 1; j <= c; j++)
72 if (!visit[i][j])
73 flag = true;
74 if (!flag) printf("YES\n");
75 else printf("NO\n");
76 }
77 return 0;
78 }