zoukankan      html  css  js  c++  java
  • USACO 4.4 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 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 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
    

     ——————————————————题解

    设白棋是1黑棋是2空格是0

    若此时1101222 如果白棋左移那么会回到之前的一个状态1110222

    若此时1011222 如果白棋左移那么会回到之前的一个状态1101222

    重复状态不优所以只要搜索白棋右移黑棋左移即可

      1 /*
      2 ID: ivorysi
      3 LANG: C++
      4 TASK: shuttle
      5 */
      6 #include <iostream>
      7 #include <cstdio>
      8 #include <cstring>
      9 #include <algorithm>
     10 #include <queue>
     11 #include <set>
     12 #include <vector>
     13 #include <string.h>
     14 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
     15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
     16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
     17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
     18 #define inf 0x7fffffff
     19 #define ivorysi
     20 #define mo 97797977
     21 #define hash 974711
     22 #define base 47
     23 #define pss pair<string,string>
     24 #define MAXN 30005
     25 #define fi first
     26 #define se second
     27 #define pii pair<int,int>
     28 using namespace std;
     29 struct node {
     30     int line[30],bl;
     31     vector<int> v;
     32 }f;
     33 queue<node > q;
     34 int n;
     35 bool check(node tem) {
     36     if(tem.bl==n+1) {
     37         bool flag=1;
     38         siji(i,1,n) {
     39             if(tem.line[i]!=2) flag=0;
     40         }
     41         siji(i,n+2,2*n+1) {
     42             if(tem.line[i]!=1) flag=0;
     43         }
     44         if(flag) return true;
     45     }
     46     return false;
     47 }
     48 void bfs() {
     49     scanf("%d",&n);
     50     siji(i,1,n) {
     51         f.line[i]=1;
     52     }
     53     f.bl=n+1;
     54     siji(i,n+2,2*n+1) {
     55         f.line[i]=2;
     56     }
     57     q.push(f);
     58     while(!q.empty()) {
     59         node now=q.front();q.pop();
     60         if(check(now)) {f=now;break;}
     61         node t=now;
     62         if(t.bl>2 && t.line[t.bl-1]!=t.line[t.bl-2] && t.line[t.bl-2]==1) {
     63             t.v.push_back(t.bl-2);
     64             t.line[t.bl]=t.line[t.bl-2];
     65             t.line[t.bl-2]=0;
     66             t.bl=t.bl-2;
     67             q.push(t);
     68             t=now;
     69         }
     70         
     71         if(t.bl>1 && t.line[t.bl-1]==1) {
     72             t.v.push_back(t.bl-1);
     73             t.line[t.bl]=t.line[t.bl-1];
     74             t.line[t.bl-1]=0;
     75             t.bl=t.bl-1;
     76             q.push(t);
     77             t=now;
     78         }
     79         
     80         if(t.bl<2*n+1 && t.line[t.bl+1]==2) {
     81             t.v.push_back(t.bl+1);
     82             t.line[t.bl]=t.line[t.bl+1];
     83             t.line[t.bl+1]=0;
     84             t.bl=t.bl+1;
     85             q.push(t);
     86             t=now;
     87         }
     88         
     89         if(t.bl<2*n && t.line[t.bl+1]!=t.line[t.bl+2] && t.line[t.bl+2]==2) {
     90             t.v.push_back(t.bl+2);
     91             t.line[t.bl]=t.line[t.bl+2];
     92             t.line[t.bl+2]=0;
     93             t.bl=t.bl+2;
     94             q.push(t);
     95         }
     96     }
     97     xiaosiji(i,0,f.v.size()) {
     98         printf("%d%c",f.v[i]," 
    "[i==f.v.size()-1 || (i+1)%20==0]);
     99     }
    100 }
    101 int main(int argc, char const *argv[])
    102 {
    103 #ifdef ivorysi
    104     freopen("shuttle.in","r",stdin);
    105     freopen("shuttle.out","w",stdout);
    106 #else
    107     //freopen("f1.in","r",stdin);
    108 #endif
    109     bfs();
    110     return 0;
    111 }
  • 相关阅读:
    Ubuntu 16.04 OneDrive自动同步
    在conda环境中pip使用清华源秒速安装skimage、opencv、tensorflow、pytorch1.2.0等p
    写论文的最佳实践
    训练误差、测试误差、泛化误差的区别
    输入法 ctrl+句号 切换 中英文符号
    理解Graham扫描算法 查找凸包
    PDF阅读器 SumatraPDF 设置:电子书字体字号的更换及行距设置
    友情链接
    CRC全套~~~ 转载
    mysql插入中文出错,提示1366
  • 原文地址:https://www.cnblogs.com/ivorysi/p/6361162.html
Copyright © 2011-2022 走看看