Shuttle Puzzle[USACO]

news/2025/1/12 3:00:06/

开始试图找挪动的规律,找了半天,成功的挪动规律虽然没找到,但发现了失败的规律。如果出现两个连续的W或B,而它的连续没能连续到边界,则铁定失败。如:...W..BB..W.. 或者 ..B..WW..B..

于是,可以递归挪动了~,速度很快,基本为0

 

/*
ID: zhangyc1
LANG: C++
TASK: shuttle
*/
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <stack>
using namespace std;stack<int> s;
int N, nBLeft = 0, nWRight = 0;
ofstream fileout("shuttle.out");
char strShuttle[26];
int nLineCount = 0;void prepairData()
{ifstream filein("shuttle.in");filein >> N;filein.close();memset(strShuttle, 'W', N);strShuttle[N] = ' ';memset(strShuttle + N + 1, 'B', N);
}inline void record(int i)
{s.push(i+1);
}bool move(int nPosSpace, int nStep)
{if (nBLeft == N && nWRight == N){return true;}// 判断上次移动是否造成了 ...W..BB..W.. 或者 ..B..WW..B..出现,如有,则本次移动失败bool bDuplicate = false;int nCheckPos = nPosSpace - nStep;if (nCheckPos > 0 && strShuttle[nCheckPos] == strShuttle[nCheckPos - 1]){// 左侧发生的重复,必为Bif (nBLeft != nCheckPos + 1){bDuplicate = true;}}else if (nCheckPos < 2*N && strShuttle[nCheckPos] == strShuttle[nCheckPos + 1]){// 右侧发生的重复,必为Wif (nWRight != 2 * N + 1 - nCheckPos){bDuplicate = true;}}if (bDuplicate)return false;//依次尝试左2,左1,右1,右2的移动if (nPosSpace >= 2 && strShuttle[nPosSpace - 2] == 'W' && strShuttle[nPosSpace - 1] == 'B'){strShuttle[nPosSpace] = 'W', strShuttle[nPosSpace - 2] = ' ';nBLeft--, nWRight++;if (move(nPosSpace - 2, -2)){record(nPosSpace - 2);return true;}strShuttle[nPosSpace] = ' ', strShuttle[nPosSpace - 2] = 'W';nBLeft++, nWRight--;}if (nPosSpace >= 1 && strShuttle[nPosSpace - 1] == 'W'){strShuttle[nPosSpace] = 'W', strShuttle[nPosSpace - 1] = ' ';nWRight++;if (move(nPosSpace - 1, -1)){record(nPosSpace - 1);return true;}strShuttle[nPosSpace] = ' ', strShuttle[nPosSpace - 1] = 'W';nWRight--;}if (nPosSpace <= 2*N-1 && strShuttle[nPosSpace + 1] == 'B'){strShuttle[nPosSpace] = 'B', strShuttle[nPosSpace + 1] = ' ';nBLeft++;if (move(nPosSpace + 1, 1)){record(nPosSpace + 1);return true;}strShuttle[nPosSpace] = ' ', strShuttle[nPosSpace + 1] = 'B';nBLeft--;}if (nPosSpace <= 2*N-2 && strShuttle[nPosSpace + 1] == 'W' && strShuttle[nPosSpace + 2] == 'B'){strShuttle[nPosSpace] = 'B', strShuttle[nPosSpace + 2] = ' ';nBLeft++, nWRight--;if (move(nPosSpace + 2, 2)){record(nPosSpace + 2);return true;}strShuttle[nPosSpace] = ' ', strShuttle[nPosSpace + 2] = 'B';nBLeft--, nWRight++;}return false;
}void process()
{nWRight = 1;strShuttle[N] = 'W';strShuttle[N-1] = ' ';move(N - 1, -1);fileout << N;nLineCount = 1;while (!s.empty()){int nTemp = s.top();s.pop();if (nLineCount % 20 == 0)fileout << endl;elsefileout << " ";fileout << nTemp;nLineCount++;}fileout << endl;
}int main(){prepairData();process();fileout.close();return 0;
}

  

转载于:https://www.cnblogs.com/doublemystery/archive/2013/04/11/3015079.html


http://www.ppmy.cn/news/869721.html

相关文章

初步了解Shuttle ESB

ESB&#xff1a;EnterpriseService Bus&#xff0c;即企业服务总线。它是传统中间件技术与XML、Web服务等技术结合的产物&#xff1b;从面向服务体系架构发展而来。 ESB採用了“总线”这种模式来管理和简化应用之间的集成拓扑结构&#xff0c;以广为接受的开放标准为基础来支持…

Shuttle ESB介绍

背景介绍 背景一 项目中使用到消息中间件。之前是采用另一位同事的思路实现&#xff1a;主要通过OPC通道&#xff0c;检测前端的消息。一旦发现有新消息&#xff0c;马上发送到各个终端&#xff0c;终端再根据自己的业务需要进行各自的显示以及处理。不过这样实现&#xff0…

A Singing Shuttle

A Singing Shuttle project presentation the video shows how it works how it work laser cut part we use the software “Lasermaker” 1. practice first we can print some templates provided. then we can try to create our own pattern another practice: …

上天入地Shuttle多穿再升级!

导语 大家好&#xff0c;我是智能仓储物流技术研习社的社长&#xff0c;你的老朋友&#xff0c;老K。 新书上市《智能物流系统构成与技术实践》 知名企业 更多推荐 如何将AGV&#xff0c;堆垛机&#xff0c;RGV连上5G&#xff1f; 最强盘点&#xff01;16种不同堆垛机让你一次看…

Shuttle安装以及配置简介

本文说明&#xff1a; 本文介绍Shuttle的安装以及配置&#xff0c;主要是根据Github上的官方文档进行翻译说明&#xff0c;还有自己的一些补充&#xff0c;如果习惯直接看文档的朋友&#xff0c;可以直接关掉这篇文章了~ 本文说明 Shuttle是什么Shuttle怎么用 安装ShuttleShutt…

Shuttle 学习

见 http://blog.csdn.net/liu765023051/article/details/38521039 转载于:https://www.cnblogs.com/lhxsoft/p/8535055.html

shuttle介绍

2019独角兽企业重金招聘Python工程师标准>>> http://shuttle.github.io/shuttle-esb/architecture/ 该网址的翻译 概念 本页提供的阳历代码并不代表着一个样例或者解决方案&#xff0c;但是&#xff0c;确实演示了一些概念在 Shuttle ESB中的应用。为了帮助整合你的…

Mac下shuttle的配置

1.配置 点击快捷菜单的shuttle小图标&#xff0c;设置-编辑。打开配置文件。 {"_comments": ["Valid terminals include: Terminal.app or iTerm","In the editor value change default to nano, vi, or another terminal based editor.",&quo…