/*
예제1
@$@@
A 4
5
A @ @ A 1
A $ $ B 1
B @ @ C -1
C $ @ C 1
C @ $ B 1

출력 : @@@$

예제 2
@$@@
A 4
4
A @ @ A 1
A $ $ B 1
B @ @ A -1
B $ $ B 1

출력 : CONTINUE

예제 3
$@$@
A 4
3
A $ @ B 1
B @ $ C 1
C $ $ C 1

출력 : @$$@
*/

#include <stdio.h>
#include <string.h>

struct S {
    char wS, nSt;
    int nloc;
};

struct Visited {
    char tape[20];
    char st;
    int ptr;
};

struct Visited visited[5000]; 
int visited_count = 0;

int idx(char c) {
    return (c == '$') ? 0 : 1;
}

int is_visited(char tape[20], char st, int ptr) {
    for(int i = 0; i < visited_count; i++) {
        if(visited[i].st == st &&
           visited[i].ptr == ptr &&
           strcmp(visited[i].tape, tape) == 0)
            return 1;
    }
    return 0;
}

void add_visited(char tape[20], char st, int ptr) {
    strcpy(visited[visited_count].tape, tape);
    visited[visited_count].st = st;
    visited[visited_count].ptr = ptr;
    visited_count++;
}

struct Rule {
    char cur, read, write, next;
    int move;
};

int main() {

    int n, L, ptr = 0;
    char st;
    struct S state[20][2] = {0};
    char tape[20];

    // 테이프 입력
    scanf("%s", tape);

    // 초기 상태 / 테이프 길이
    scanf(" %c %d", &st, &L);

    // 프로그램 행 수
    scanf("%d", &n);

    struct Rule rules[20];

    // 프로그램 읽기
    for(int i=0; i<n; i++) {
        char curSt, readCh, writeCh, nextSt;
        int nloc;
        scanf(" %c %c %c %c %d",
              &curSt, &readCh, &writeCh, &nextSt, &nloc);

        state[curSt - 'A'][idx(readCh)] =
            (struct S){writeCh, nextSt, nloc};

        rules[i] = (struct Rule){curSt, readCh, writeCh, nextSt, nloc};
    }

    while(1) {

        if(ptr < 0 || ptr >= L) {
            printf("%s\n", tape);
            return 0;
        }

        char tmpst = st;
        char tmpsym = tape[ptr];

        if(is_visited(tape, tmpst, ptr)) {
            printf("CONTINUE\n");
            return 0;
        }

        add_visited(tape, tmpst, ptr);

        int found = 0;
        for(int i = 0; i < n; i++) {
            if(rules[i].cur == tmpst && rules[i].read == tmpsym) {
                tape[ptr] = rules[i].write;
                st = rules[i].next;
                ptr += rules[i].move;
                found = 1;
                break;
            }
        }
        if(!found) {
            printf("%s\n", tape);
            return 0;
        }

    }

    return 0;
}

/*

----------------- 개선 후 ----------------------

#include <stdio.h>
#include <string.h>

struct S {
    char wS, nSt;
    int nloc;
};

struct Visited {
    char tape[20];
    char st;
    int ptr;
};

struct Visited visited[5000]; 
int visited_count = 0;

int idx(char c) {
    return (c == '$') ? 0 : 1;
}

int is_visited(char tape[20], char st, int ptr) {
    for(int i = 0; i < visited_count; i++) {
        if(visited[i].st == st &&
           visited[i].ptr == ptr &&
           strcmp(visited[i].tape, tape) == 0)
            return 1;
    }
    return 0;
}

void add_visited(char tape[20], char st, int ptr) {
    strcpy(visited[visited_count].tape, tape);
    visited[visited_count].st = st;
    visited[visited_count].ptr = ptr;
    visited_count++;
}

int main() {

    int n, L, ptr = 0;
    char st;
    struct S state[20][2] = {0};

    char tape[20];

    // 테이프 입력
    scanf("%s", tape);

    // 초기 상태 / 테이프 길이
    scanf(" %c %d", &st, &L);

    // 프로그램 행 수
    scanf("%d", &n);

    // 프로그램 읽기
    for(int i=0; i<n; i++) {
        char curSt, readCh, writeCh, nextSt;
        int nloc;
        scanf(" %c %c %c %c %d",
              &curSt, &readCh, &writeCh, &nextSt, &nloc);

        state[curSt - 'A'][idx(readCh)] =
            (struct S){writeCh, nextSt, nloc};
    }

    while(1) {

        if(ptr < 0 || ptr >= L) {
            printf("%s\n", tape);
            return 0;
        }

        char tmpst = st;
        char tmpsym = tape[ptr];

        if(is_visited(tape, tmpst, ptr)) {
            printf("CONTINUE\n");
            return 0;
        }

        add_visited(tape, tmpst, ptr);

        // 문자 쓰기
        char w = state[tmpst - 'A'][idx(tmpsym)].wS;
        tape[ptr] = w;

        // 다음 상태
        st = state[tmpst - 'A'][idx(tmpsym)].nSt;

        // 다음 위치
        ptr += state[tmpst - 'A'][idx(tmpsym)].nloc;
    }

    return 0;
}
*/

Intégrer sur un site web

Pour intégrer ce projet à votre site web, copiez le code suivant et collez-le dans le HTML de votre site web :