SOJ 1016

1016. Boundaries on A New Kind of Science

Constraints

Time Limit: 10 secs, Memory Limit: 32 MB

Description

Stephen Wolfram in a “New Kind of Science” describes a special kind of cellular automata.  The automata he describes are rather interesting.  They consist of rows of blocks, where blocks are either filled or not filled depending on the previous row.  To generate a new row of blocks, the automata looks at the preceding row and then follows a pre-set “rule” to either color or not color a square on the output row.

 

 

 

 

For example the following diagram illustrates the “output” from one of these special kind of cellular automata:

 

was generated by repeated application of “Rule 254”. The automaton was initialized with an input line that consisted of a single black square.  Repeated application of rule 254 to the ouput line from the preceding step generated the black triangle shown above.

 

For this rule, the top row in each box gives one of eight possible color combinations for a cell (the middle cell) and its two neighbors (the left and right neighbors of the cell).  The bottom row in a box specifies the color that the center cell should have on the next step for each of the 8 possible cases.

 

Given this arrangement, there are 255 different generating rules or automata numbered as follows

 

The “Bounded” Automata

 

  • Unlike the automata in “A New Kind of Science”, the automata for this problem operate in a “bounded space”.  In other words, each line output by an automaton consists of exactly n squares, and, the first square, and the last square of the output from a bounded automaton are always white no matter what the rule for the automata.  This means that while an automaton can examine the first and last square of its input line when computing the new second and second to last characters, it cannot change the first or last square when it outputs a line.  Thus, the first and the last square on a line must always remain white.
  • Bounded automata will always start life on a line with an odd number of squares, and all of the squares (except the middle square) are white.  Themiddle square for step 1 is always black.
  • The number of squares is determined by the string for which the program  is searching (the second field of an input line)

 

The Program

 

For every line in the input file, your program must determine which (if any) of the 255 possible automata could have generated that particular line when started from the standard starting state.  If none of the automata generate the sequence by the given step number, output “NONE”.  If more than one automata generated that line, then, you must output all of the automata that generate the line as described below in the output section.

Input

  • The first field on a line of input consists of a maximum step number(max_step) for the automata to run. This number can be up to 32 bits.  Values are chosen such that the problem is solvable within the given time limits given the input specifications.
  • The second field on a line of input consists of an odd-length string of characters which represent the “squares” on the line. These will not be longer than 256 characters in length, and can be shorter. The size of the second fielddetermines n -- the number of squares in a particular automata. 
  • The character ‘W’ represents a white square and a ‘B’ represents a black square
  • If an input string contains characters other than ‘W’ or ‘B’, or if the input string is an invalid string (not a properly bounded string as described previously), then obviously the string cannot be found by any of the automata, and the output will be LINE # NONE as illustrated in the sample output.
  • Each line in the input file will have a terminating newline character.

Input is terminated by a single line with the characters “END OF INPUT” as illustrated below.  The END OF INPUT line should not be processed by your search algorithm.

Output

  • The output consists of LINE # followed by pairs of numbers (rule, step) whererule is the rule 
    number of the automata (0 through 255) that generated a particular output and step is the first step in the sequence of outputs (1, 2,…, max_step) at which the automata generated the desired output sequence
  • If more than one rule generated the desired output sequence on or before themaximum step number, list a pair for each rule number that generated the desired output, in order, from lowest automata number to highest automata number with a space between each output pair. 

If none of the automata generate the particular line from the input file before or on the maximum step number, output LINE # NONE  (where # represents the number of the input line with the search string).

Sample Input

3 WBWBWBWBW1000 WBWBWBWBBBW5235 WBWWBWWBBBBWWBBWWWWWWBBWWWBBWWWWWWBBWWBBBBWWBWWBW5 WBWBDCWBWEND OF INPUT

Sample Output

LINE 1 (91,3)LINE 2 (15,8) (158,11) (159,14) (243,8)LINE 3 (129,84) (161,84)LINE 4 NONE


  题目有点难理解,简而言之就是第一行的情形已知,只有中间为黑,第二行的点(除首尾两个以外)的黑白取决于第一行与这一点最相近的三个点的情况。三个点共有八种可能,我们将黑记为1,白记为0,如黑白黑记为101,化为十进制是5,若规则号化为二进制数字串后101对应的数字是1,则rule[5]=1,否则rule[5]=0。所以要求第二行点的黑白可以先求出上一行相近三个点的十进制,记为d,然后rule[d]即为第二行这个点的数值。以此类推依次求出下一行的点的情况,直到与目标情况相同或者超过最大步数限制,然后循环,进入下一个规则号继续寻找。最后输出即可,输出的时候要留意输出格式,不要多空格之类的。

#include<iostream>
#include<map>
#include<vector>
#include<iterator>
#include<string>
#include<cmath>
using namespace std;

string maxStep;//将输入的最大步数以字符串的形式来存储,以便最后来存储"END OF INPUT" 
int maxStepNum;//将字符串形式的最大步数转换成int型存储在此变量中 
vector<int> beginning,current,final;//三个容器,分别保存初始的情况,当前情况和最后的目标情况 
map<int,int> rule,answer;// rule容器存储当前的递推法则,answer存储答案,answer.first存储规则号,second存储步数 
int step;//递推过程中的当前步数 
int haveInvalidString;//记录字符串中是否出现了除W和B以外的字符,若有则直接输出NONE 

inline int maxStepToInt(string maxstep)//将数字字符串转换成int型数字 
{
	int result=0;
	for(int i=0;i<maxstep.size();++i)
	{
		result+=(maxstep[i]-'0')*pow(10,maxstep.size()-i-1);
	}
	return result;
}
inline void makeRule(int ruleLabel)//将规则号化为二进制,再将对应关系存储在rule容器中 
{
    for(int i=0;i<8;++i)
        rule[i]=0;
    int count=0;
    while(ruleLabel!=0)
    {
    	rule[count]=ruleLabel%2;
    	ruleLabel/=2;
    	count++;
	}
}

void findAnswer()//递推寻找答案 
{
	maxStepNum=maxStepToInt(maxStep);
	final.clear();
	answer.clear();
	haveInvalidString=0;
	string s;
	cin>>s;
    for(int i=0;i<s.length();++i)//将目标情形存储在final容器中 
    {
    	if(s[i]=='W') final.push_back(0);
    	else if(s[i]=='B') final.push_back(1);
    	else {
    		haveInvalidString=1;
		}
	}

	if(!haveInvalidString)
	{	
    	for(int i=0;i<=255;++i)
    	{
    		step=1;
    		beginning.clear();
    		current.clear();
    		for(int j=0;j<final.size();++j)//将初始情形存储在beginning容器中,并确定当前情形 
    	    {
	    	    if(j==final.size()/2) 
				{
					beginning.push_back(1);
					current.push_back(1);
				}
	        	else 
				{
					beginning.push_back(0);
					current.push_back(0);
				}
        	}
    		makeRule(i);//确定递推法则 
    		while(step<=maxStepNum)//循环寻找,直到超过最大步数限制 
    	    {
    		    if(current==final)
    		    {
    			    answer[i]=step;
    			    break;
			    }
			    else
			    {
		            for(int j=1;j<beginning.size()-1;++j)//若当前情形与目标情形不符合,则递推下一行存储在current中,并同步更新beginning与current相同,方便下一次递推 
		            {
		    	        current[j]=rule[beginning[j-1]*4+beginning[j]*2+beginning[j+1]*1];
		            }
		            beginning.assign(current.begin(),current.end());
			        step++;
			    }
		    }
		}
	}	
}

int main()
{
	int numOfLine=0;
	while(cin>>maxStep)
	{
		if(maxStep=="END") break;//输入END OF INPUT,但maxStep只接收到了END,所以验证maxStep=="END" 
		findAnswer();
		cout<<"LINE "<<++numOfLine<<" ";
		if(haveInvalidString)
		{
			cout<<"NONE"<<endl;
		}
		else
		{
			for(map<int,int>::iterator iter=answer.begin();iter!=answer.end();++iter)
			{
				cout<<'('<<iter->first<<','<<iter->second<<')';
			}
			cout<<endl;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值