Sicily 1720. Turing-M Programming

1720. Turing-M Programming

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge

Description

A Turing machine M is a finite device, which performs operations on a paper tape. This tape is infinite in both directions, and is divided into single squares along its length. At any given time each square of the tape is either blank or contains a single symbol from a fixed finite list of symbols s1,s2,…,sn, the alphabet of M. We will let B denote a blank, and count it as the symbol s0 belonging to M’s alphabet. M has a head which at any given time may read and/or write a single square of the tape.
M is capable of three kinds of simple operation on the tape, namely:
   (a) write a symbol from the alphabet of M into the square which the head is pointing to,
   (b) move the head one square to the right of that being scanned,
   (c) move the head one square to the left of that being scanned.
At any given time M is in one of a fixed finite number of states, represented by symbols q1, q2,…,qm. During operation the state of M can change. The action that M takes at any instant depends on the current state of M and on the symbol currently being scanned. This dependence is described in M’s specification which consists of a finite set Q of quadruples, each of which takes one of the following forms:
   qi sj sk ql
   qi sj L ql
   qi sj R ql
   ( 1≤i,l≤m , 0≤j,k≤n )
A quadruple qi sj x ql in Q specifies the action to be taken by M when it is in the state qi and scanning the symbol sj , as follows:
1.Operate on the tape thus:
(a)if x = sk , erase sj and write sk in the square being scanned;
(b)if x = L , move the head one square to the left;
(c)if x = R , move the head one square to the right;
2.Change into state ql.
The specification Q is such that for every pair qi sj there’s at most one quadruple of form qi sj x ql; otherwise there could be ambiguity about what M does next.
To begin a computation, M must be provided with a tape and positioned so that a specified square is being scanned; further, M must be set in some prescribed initial state. Then, if M is in the state qi and scans the symbol sj , it acts as described above provided that there is a quadruple of form qi sj x ql in Q. This kind of action is then
repeated for the new state and symbol scanned, and so on. M continues in this way for as long as possible. The operation of M terminates only when it is in a state qi scanning a symbol sj such that there is no quadruple of the form qi sj x ql; i.e. there is no quadruple in Q that specifies what to do next.
To simplify our problem, let’s make some limits to the Turing-M and Q:
(1)n=1, s0 = B, s1 = *, i.e. the symbol is either * or blank ;
(2)m ≤ 100, i.e. there are at most 100 states;
(3)computation (determined by Q, initial state and the initial configuration of the tape) must terminate within 100000 steps.
Now we use this limited Turing-M for computing a function f(x) , x and f(x) are non-negative integers and 0≤x≤50 .That’s to say , for any given x , M terminates to f(x) . Here is how it works:
First we define the input for M , x is represented by (x+1) consecutive symbol ‘*’s on the tape , other squares on the tape contains symbol B , i.e. blank , and the head initially points to the leftmost ‘*’;
We assume that the initial state for M is q1 , now M computes according to Q until it terminates;
Finally we define the output of M is the number of ‘*’ on the tape in the end (unnecessary to be consecutive), and this number should be f(x) .
If for every x ∈dom(f), M outputs f(x) , we say M is computing f.
The problem here is to devise a program for M (Q indeed) so that it’s computing a given f . For simplicity, f here is function in the form of f(x)=ax+b  (a,b are non-negative integers and 0≤a,b≤10, dom(f)= [0,50],i.e. 0≤x≤50).

Input

There’re several test cases. For each case there’s a single line containing two numbers, a and b. Input is terminated by EOF.

Output

For each case first output the case number “case 1:”,”case 2:”, etc. then output the program, i.e. several lines of quadruples of Q (at most 100 states), use number i to represent qi , output format should be the same as sample output. The solution may be not unique, just output any proper one.

Sample Input

1 2
1 0

Sample Output

case 1:
1 * L 7
7 B L 8
8 B * 3
10 * L 5
case 2:
1 * B 2

Hint

Hint for case 1:
M is to compute f(x)=x+2 , now let M to compute f(3) , the initial configuration of M would be
           ↓(q1)
……B B B B * * * * B B B B ……
and final configuration would be
       ↓(q3)
……B B * B * * * * B B B B ……
since there’re 5 ‘*’s at last , so output of M is 5, which is equal to f(3)=3+2=5.

“10 * L 5” is actually unnecessary.

// Problem#: 1720
// Submission#: 3585243
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<stdio.h>
int main() {
    int t,a,b,i;
    t=0;
    while (scanf("%d %d",&a,&b)!=EOF) {
        t++;
        printf("case %d:\n",t);
        if (a==0) {
            printf("1 * B 1\n");
            printf("1 B R 2\n");
            printf("2 * * 1\n");
            printf("2 B B 50\n");
            for (i=0;i<b;i++) {
                printf("%d B * %d\n",i+50,i+50);
                printf("%d * R %d\n",i+50,i+51);
            }
        } else {
            printf("1 * B 1\n");
            printf("1 B R 2\n");
            printf("2 * B 3\n");
            printf("3 B L 4\n");
            printf("4 B * 5\n");
            printf("5 * L 5\n");
            printf("5 B B 6\n");
            for (i=0;i<a-1;i++) {
                printf("%d B * %d\n",i+6,i+6);
                printf("%d * L %d\n",i+6,i+7);
            }
            printf("%d B R 20\n",i+6);
            printf("20 * R 20\n");
            printf("20 B R 2\n");
            printf("2 B R 30\n");
            for (i=0;i<b;i++) {
                printf("%d B * %d\n",i+30,i+30);
                printf("%d * R %d\n",i+30,i+31);
            }
        }
    }
    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值