usaco 4.4 Shuttle Puzzle 2010.8.8

来源:互联网 发布:java跳出foreach循环 编辑:程序博客网 时间:2024/05/08 16:41

Shuttle Puzzle

Traditional

The Shuttle Puzzle of size 3 consists of 3white marbles, 3 black marbles, and a strip of wood with 7 holes. The marblesof 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 twotypes of moves. Move 1 marble 1 space (into the empty hole) or jump 1 marbleover 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 Nwhite marbles and N black marbles and 2N+1 holes.

 

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

 

WWW BBB

WW WBBB

WWBW BB

WWBWB B

WWB BWB

W BWBWB

 WBWBWB

BW WBWB

BWBW WB

BWBWBW

BWBWB W

BWB BWW

B BWBWW

BB WBWW

BBBW WW

BBB WWW

 

Write a program that will solve the SHUTTLEPUZZLE for any size N (1 <= N <= 12) in the minimum number of moves anddisplay 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 asspace-separated integers, 20 per line (except possibly the last line). Numberthe marbles/holes from the left, starting with one.

 

Output the the solution that would appearfirst among the set of minimal solutions sorted numerically (first by the firstnumber, 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

 

 

 

WA:各种PE。。不解释。。以后一定要把题目的输出格式看清楚

分析:看了nocow的分析,好强大啊~~~有些题目的话,找规律也不错哈~~O(∩_∩)O~

数据分析

Usaco在这题上并没有指明不可以用分析法,而且dfs肯定TLE,所以我们取巧。

先观察样例数据,如果把还没移动的那一步也算上,那么空格的位置为

4 3 5 6 4 2 1 3 5 7 6 4 2 3 5 4 (n=3,样例)

5 4 6 7 5 3 2 4 6 8 9 7 5 3 1 2 4 6 8 7 5 34 6 5 (n=4)

我们凭借极其敏锐的眼光发现这组序列为

435 642 1357 642 35 4 (n=3,样例)

5 46 753 2468 97531 2468 753 46 5 (n=4)

即长度为1,2,3,4,...,n,n+1,n,...,4,3,2,1这样的2n+1组等差序列

我们讨论第1~n+1组序列,这些序列满足

  *公差的绝对值为2

  *奇数组为降序列,偶数组为升序列

  *对于第i组(1<=i<=n+1),若为奇数组则首项为n+i,偶数组则首项为n-i+2

对于第n+2~2n+1组,可以由对称性求出。

输出时从第二组开始即可。

把规律总结成这样后,代码应该很好写了吧

 

/*NAME: PROG: shuttleLANG: C++*/#include <cstdio>#include <string>#include <iostream>using namespace std;int n;string ans1,temp,ans2,mid;string key[10]={"0","1","2","3","4","5","6","7","8","9"};int main(){int i,j,k;freopen("shuttle.in","r",stdin);freopen("shuttle.out","w",stdout);scanf("%d",&n);ans1.clear();ans2.clear();for(int i=1;i<=n+1;i++){   mid.clear();if (i%2==1){for(j=n+i,k=1;k<=i;k++,j=j-2){int p=j;temp.clear();while(p){temp=key[p%10]+temp;p/=10;}mid=mid+" "+temp;}}else{for(j=n-i+2,k=1;k<=i;k++,j=j+2){int p=j;temp.clear();while(p){temp=key[p%10]+temp;p/=10;}mid=mid+" "+temp;}}ans1=ans1+mid;if (i!=n+1)ans2=mid+ans2;}ans1=ans1+ans2;int cou=0,flag=0,s;for(s=0;;s++)if (ans1[s]==' '){flag++;if (flag==2)break;}for(int i=s;i<ans1.size();i++){if (ans1[i]==' '){cou++;if ((cou%20==1)&&(cou>1))cout<<endl;if (cou%20!=1)cout<<ans1[i];}elsecout<<ans1[i];}cout<<endl;//cout<<"---"<<ans1<<"---"<<endl;return 0;}


Compiling...

Compile: OK

 

Executing...

  Test 1: TEST OK [0.011 secs, 3024 KB]

  Test 2: TEST OK [0.000 secs, 3024 KB]

  Test 3: TEST OK [0.000 secs, 3024 KB]

  Test 4: TEST OK [0.000 secs, 3024 KB]

  Test 5: TEST OK [0.011 secs, 3024 KB]

  Test 6: TEST OK [0.011 secs, 3024 KB]

  Test 7: TEST OK [0.000 secs, 3024 KB]

  Test 8: TEST OK [0.022 secs, 3024 KB]

  Test 9: TEST OK [0.011 secs, 3024 KB]

  Test 10: TEST OK [0.011 secs, 3024 KB]

 

All tests OK.

Your program ('shuttle') produced allcorrect answers!  This is your

submission #8 for this problem.  Congratulations!

 

Here are the test data inputs:

 

------- test 1 -------

1

------- test 2 -------

3

------- test 3 -------

4

------- test 4 -------

5

------- test 5 -------

7

------- test 6 -------

8

------- test 7 -------

9

------- test 8 -------

10

------- test 9 -------

11

------- test 10 -------

12

 

Keep up the good work!

 

 

Thanks for your submission!


0 0