日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

usaco4.4.1 Shuttle Puzzle

發布時間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 usaco4.4.1 Shuttle Puzzle 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一 原題

Shuttle Puzzle
Traditional

The Shuttle Puzzle of size 3 consists of 3 white marbles, 3 black marbles, and a strip of wood with 7 holes. The marbles of the same color are placed in the holes at the opposite ends of the strip, leaving the center hole empty.

INITIAL STATE: WWW_BBB GOAL STATE: BBB_WWW

To solve the shuttle puzzle, use only two types of moves. Move 1 marble 1 space (into the empty hole) or jump 1 marble over 1 marble of the opposite color (into the empty hole). You may not back up, and you may not jump over 2 marbles.

A Shuttle Puzzle of size N consists of N white marbles and N black marbles and 2N+1 holes.

Here's one solution for the problem of size 3 showing the initial, intermediate, and end states:

WWW BBB WW WBBB WWBW BB WWBWB B WWB BWB W BWBWBWBWBWB BW WBWB BWBW WB BWBWBW BWBWB W BWB BWW B BWBWW BB WBWW BBBW WW BBB WWW

Write a program that will solve the SHUTTLE PUZZLE for any size N (1 <= N <= 12) in the minimum number of moves and display the successive moves, 20 per line.

PROGRAM NAME: shuttle

INPUT FORMAT

A single line with the integer N.

SAMPLE INPUT (file shuttle.in)

3

OUTPUT FORMAT

The list of moves expressed as space-separated integers, 20 per line (except possibly the last line). Number the marbles/holes from the left, starting with one.

Output the the solution that would appear first among the set of minimal solutions sorted numerically (first by the first number, using the second number for ties, and so on).

SAMPLE OUTPUT (file shuttle.out)

3 5 6 4 2 1 3 5 7 6 4 2 3 5 4


二 分析

加一個小小的剪枝的BFS 即最優解里W只會向右移 B只會向左移。最后沒輸出換行WA一次


三 代碼

運行結果: USER: Qi Shen [maxkibb3] TASK: shuttle LANG: C++Compiling... Compile: OKExecuting...Test 1: TEST OK [0.000 secs, 4192 KB]Test 2: TEST OK [0.000 secs, 4192 KB]Test 3: TEST OK [0.000 secs, 4192 KB]Test 4: TEST OK [0.000 secs, 4192 KB]Test 5: TEST OK [0.011 secs, 4324 KB]Test 6: TEST OK [0.022 secs, 4456 KB]Test 7: TEST OK [0.054 secs, 4976 KB]Test 8: TEST OK [0.130 secs, 6032 KB]Test 9: TEST OK [0.302 secs, 7884 KB]Test 10: TEST OK [0.799 secs, 11684 KB]All tests OK.

Your program ('shuttle') produced all correct answers! This is your submission #2 for this problem. Congratulations!


AC代碼: /* ID:maxkibb3 LANG:C++ PROB:shuttle */#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<set> #include<algorithm> using namespace std;int n; set<string> vis;struct Node {char a[25];int space_idx;vector<int> trace;string get_str() {string s = "";for(int i = 0; i < 2 * n + 1; i++) {if(a[i] == ' ') s = s + '+';else s = s+ a[i];}//cout << s << endl;return s;}bool judge() {if(a[n] != ' ') return false;for(int i = 0; i < n; i++) {if(a[i] != 'B') return false;if(a[n + i + 1] != 'W') return false;}return true;}Node move(int type) {Node ret;for(int i = 0; i <= 2 * n; i++) ret.a[i] = a[i];ret.space_idx = space_idx;ret.trace = trace;if(type == 0) {if(space_idx == 0) {ret.space_idx = -1;return ret;}if(a[space_idx - 1] == 'B') {ret.space_idx = -1;return ret;}swap(ret.a[space_idx], ret.a[space_idx - 1]);ret.space_idx--;ret.trace.push_back(space_idx);}else if(type == 1) {if(space_idx == 2 * n) {ret.space_idx = -1;return ret;}if(a[space_idx + 1] == 'W') {ret.space_idx = -1;return ret;}swap(ret.a[space_idx], ret.a[space_idx + 1]);ret.space_idx++;ret.trace.push_back(space_idx + 2);}else if(type == 2) {if(space_idx < 2) {ret.space_idx = -1;return ret;}if(a[space_idx - 1] == a[space_idx - 2] || a[space_idx - 1] == 'W') {ret.space_idx = -1;return ret;}swap(ret.a[space_idx], ret.a[space_idx - 2]);ret.space_idx -= 2;ret.trace.push_back(space_idx - 1);}else if(type == 3) {if(space_idx > 2 * n - 2) {ret.space_idx = -1;return ret;}if(a[space_idx + 1] == a[space_idx + 2] || a[space_idx + 1] == 'B') {ret.space_idx = -1;return ret;}swap(ret.a[space_idx], ret.a[space_idx + 2]);ret.space_idx += 2;ret.trace.push_back(space_idx + 3);}return ret;} }; queue<Node> q;void init() {scanf("%d", &n);Node head;for(int i = 0; i < n; i++) {head.a[i] = 'W';head.a[i + n + 1] = 'B';}head.a[n] = ' ';head.space_idx = n;head.trace.clear();q.push(head); }void solve() {bool find_ans = false;int cnt = 0;while(!q.empty()) {Node head = q.front();q.pop();for(int i = 0; i < 4; i++) {Node new_ele = head.move(i);if(new_ele.space_idx == -1) continue;string str = new_ele.get_str();if(vis.find(str) != vis.end()) continue;vis.insert(str);if(new_ele.judge()) {for(int i = 0; i < new_ele.trace.size(); i++) {if(i % 20 == 0) printf("%d", new_ele.trace[i]);else if(i % 20 == 19) printf(" %d\n", new_ele.trace[i]);else printf(" %d", new_ele.trace[i]);}if(new_ele.trace.size() % 20 != 0) printf("\n");find_ans = true;break;}q.push(new_ele);}if(find_ans) break;} }int main() {freopen("shuttle.in", "r", stdin);freopen("shuttle.out", "w", stdout);init();solve();return 0; }

總結

以上是生活随笔為你收集整理的usaco4.4.1 Shuttle Puzzle的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。