Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct node{
- int estado[6];
- int sequencia[6];
- int transicao_come_from;
- struct node *brother;
- struct node *son;
- struct node *father;
- }node;
- node* create_node(int estado, int *sequencia,int transicao_come_from,int places);
- void add_son(node* father,node* new_son);
- void print(node* root,int places);
- node* checkSequence(node* atual, node* node, int num_places);
- int main()
- {
- int places,transitions,check=1,flag=0;
- int i;
- int matriz_val[1000][3];
- char teste[10],teste1[10],teste2[10];
- char state_inicial[20];
- int len, index=0,indice_matriz=0,indice_state=0;
- int matriz_state[6]={0};
- struct node *head;
- scanf("%d %d",&places, &transitions);
- int arrayTransitons[transitions][2][places];
- memset(arrayTransitons,0, sizeof(arrayTransitons));
- while(check==1){
- if(flag==1){
- for(i=0;i<places;i++){
- scanf("%s",&state_inicial);
- if(isdigit(state_inicial[0])){
- matriz_state[i]=atoi(state_inicial);
- //indice_state++;
- }
- }
- check=0;
- }else{
- scanf("%s",&teste);
- if(isdigit(teste[0])){
- scanf("%s %s",&teste1,&teste2);
- matriz_val[index][0]=atoi(teste);
- matriz_val[index][1]=atoi(teste1);
- matriz_val[index][2]=atoi(teste2);
- if(atoi(teste2) == 1){
- arrayTransitons[atoi(teste)-1][1][atoi(teste1)-1] = 1;
- }else{
- arrayTransitons[atoi(teste1)-1][0][atoi(teste)-1] = 1;
- }
- index++;
- }else{
- //printf("flag=1\n");
- flag=1;
- }
- }
- }
- printf("\n%d %d\n",places,transitions);
- for(i=0;i<index;i++){
- printf("%d %d %d\n",matriz_val[i][0],matriz_val[i][1],matriz_val[i][2]);
- }
- printf("STATE\n");
- for(i=0;i<places;i++){
- printf("%d ",matriz_state[i]);
- }
- printf("\n\n");
- int k,l;
- printf("[");
- for(i = 0; i<transitions; i++){
- printf("[");
- for(k =0;k<2;k++){
- printf("[");
- for(l = 0; l<places;l++){
- printf("%d, ", arrayTransitons[i][k][l]);
- }
- printf("],");
- }
- printf("],");
- }
- printf("]\n");
- //considero aqui que do place 1 sair sempre o estado 1
- head=create_node(1,matriz_state,1,places);
- node * aux= head;
- while(addNextTransition(aux, transitions, places, arrayTransitons,head)){
- printf("\n-------------->AQUI");
- };
- printf("---------------------------------------------------PAI: ");
- for(i = 0; i<places; i++){
- printf("%d ", head->sequencia[i]);
- }
- printf("\n-------------------------------------------FILHO: ");
- for(i = 0; i<places; i++){
- printf("%d ", head->son->sequencia[i]);
- }
- while(addNextTransition(head->son, transitions, places, arrayTransitons, head) !=0);
- printf("\n----------------------------------------------FILHO FILHO: ");
- for(i = 0; i<places; i++){
- printf("%d ", head->son->son->sequencia[i]);
- }
- printf("\n-----------------------------------------FILHO FILHO IRMAO: ");
- for(i = 0; i<places; i++){
- printf("%d ", head->son->son->brother->sequencia[i]);
- }
- while(addNextTransition( head->son->son, transitions, places, arrayTransitons,head) !=0);
- printf("\n-------------------------------------FILHO FILHO FILHO: ");
- for(i = 0; i<places; i++){
- printf("%d ", head->son->son->son->sequencia[i]);
- }
- while(addNextTransition( head->son->son->son, transitions, places, arrayTransitons,head) !=0);
- printf("\n--------------------------------------FILHO FILHO FILHO FILHO: ");
- for(i = 0; i<places; i++){
- printf("%d ", head->son->son->son->son->sequencia[i]);
- }
- printf("\n-------------------------------------------FILHO FILHO FILHO FILHO IRMAO: ");
- for(i = 0; i<places; i++){
- printf("%d ", head->son->son->son->son->brother->sequencia[i]);
- }
- int check = 0;
- while(addNextTransition(aux,transitions,places,arrayTransitons) != 0){ //quanto tiver w talvez dê
- int i = 0;
- printf("OI + %d: ", i);
- for(i = 0; i<places; i++){
- printf("%d ", aux->sequencia[i]);
- }
- aux = aux->son;
- if(aux->son == NULL){
- aux = aux->father;
- check = 1
- }
- }
- // memcpy(head->sequencia,matriz_state,sizeof(matriz_state));
- // head->estado=1;
- // head->transicao_come_from=1;
- //create_node(1,matriz_state,1,places);
- /*int num=1;//ja considero a transicao 1
- int atual=1;
- int first=0;
- node* atual_node=head;
- node* aux_node=head;
- while(num<transitions){
- //vou ver por transicoes
- if(matriz_val[num][0]!=num){
- }else{
- while(num==matriz_val[atual][0]){//mesma transicao
- if(first==0){
- //para verificar e garantir que a head começa no inicio da transicao 1
- if(matriz_val[atual][0]==1 && matriz_val[atual][2]==2){
- head->estado=matriz_val[atual][1];
- first=1;
- }
- }
- if(matriz_val[atual][2]==1){
- matriz_state[matriz_val[atual][1]-1]+=1;
- }else if(matriz_val[atual][2]==2){
- matriz_state[matriz_val[atual][1]-1]-=1;
- }
- if((matriz_val[atual][1]>matriz_val[atual][0])&&matriz_val[atual][2]==1){
- add_son(atual_node,create_node(matriz_val[atual][1],matriz_state,matriz_val[atual][0],places));
- }
- atual++;
- }
- if(atual_node->brother!=NULL){
- atual_node=atual_node->brother;
- }else if(aux_node->son!=NULL){
- atual_node=aux_node->son;
- aux_node=aux_node->son;
- }else{
- break;
- }
- }
- num++;
- }*/
- //add_son(head,create_node(2,matriz_state,2,places));
- // printf("\nsequencia:\n");
- // for(i=0;i<places;i++){
- // printf("%d ",head->son->sequencia[i]);
- // }
- // printf("\nestado:\n");
- // printf("%d \n",head->son->estado);
- // printf("transicao:\n");
- // printf("%d \n",head->son->transicao_come_from);
- //
- // add_son(head,create_node(3,matriz_state,3,places));
- // printf("\n----brother---\nsequencia:\n");
- //
- /* printf("print\n");
- print(head,places, arrayTransitons, places);
- printf("fim_print\n");*/
- // for(i=0;i<places;i++){
- // printf("e1: %d \n",head->son->estado);
- // printf("s1: %d \n",head->son->sequencia[i]);
- // printf("e2: %d \n",head->son->brother->estado);
- // printf("s2: %d \n",head->son->brother->sequencia[i]);
- // }
- // printf("\nestado:\n");
- // printf("%d \n",head->son->brother->estado);
- // printf("transicao:\n");
- // printf("%d \n",head->son->brother->transicao_come_from);
- return 0;
- }
- void print_seq( int* seq,int places){
- int i;
- for(i=0;i<places;i++){
- printf("%d ",seq[i]);
- }
- printf("\n");
- }
- node* checkSequence(node* atual, node* node, int num_places){
- int i;
- printf("\nbeginCHECK_Sequence\n");
- print_seq(atual->sequencia,num_places);
- print_seq(node->sequencia,num_places);
- for(i =0; i<num_places; i++){
- if((atual->sequencia[i] < node->sequencia[i]) && atual->sequencia[i] !=-1){
- //printf("\n-----------------------------------------------------É MAIOR2222222222222222222222222\n");
- printf("--------!!_-----atual: ");print_seq(atual->sequencia,num_places);
- printf("--------!!_-----node: ");print_seq(node->sequencia,num_places);
- atual->transicao_come_from = -99;
- return atual;
- }
- }
- printf("--->segue segundo teste\n");
- for(i = 0;i<num_places; i++){
- if(atual->sequencia[i] > node->sequencia[i]){
- //printf("\n-----------------------------------------------------É MAIOR\n");
- atual->sequencia[i] = -1;
- }
- }
- //print_seq(atual->sequencia,num_places);
- return atual;
- }
- int search_node(node* atual, node* procurado, int places){
- int i;
- int flag = 1;
- if(atual != NULL && procurado != NULL){
- // printf("\nAQUI---<\n");
- for(i=0;i<places;i++){
- /*printf("\nAQUI FOR---<\n");
- printf("\nSEQ1: %d ; SEQ2: %d", atual->sequencia[i], procurado->sequencia[i]);
- printf("\nAQUI FOR2---<\n");*/
- if(atual->sequencia[i] != procurado->sequencia[i]){
- //printf("\nAQUI IF---<\n");
- flag = 0;
- break;
- }
- }
- if(flag == 0){
- if(atual->son != NULL){
- search_node(atual->son,procurado,places);
- //printf("\nAQUI DEPOIS1---<\n");
- }
- else{
- if(atual->brother !=NULL){
- search_node(atual->brother,procurado,places);
- //printf("\nAQUI DEPOIS2---<\n");
- }
- }
- }
- }
- return flag;
- }
- node* create_node(int estado, int *sequencia,int transicao_come_from, int places){
- int i;
- //printf("seq:\n");
- /*for(i=0;i<places;i++){
- printf("%d ",sequencia[i]);
- }*/
- node* new_node;
- new_node= (node *)malloc(sizeof(node));
- new_node->father = NULL;
- if(estado != -1){
- memset(new_node->estado, 0, sizeof(new_node->estado));
- new_node->estado[estado-1]=1;
- for(i=0; i<places; i++){
- new_node->sequencia[i] = sequencia[i];
- }
- new_node->transicao_come_from=transicao_come_from;
- //printf("\ncriou no\n\n");
- }
- return new_node;
- }
- int addNextTransition(node* father, int num_transitions, int num_places, int arrayTransitions[num_transitions][2][num_places], node* root){
- int i,j,k,p,s;
- int done=0;
- int sequencia_aux[6];
- int estado = get_estado(father, num_places);
- int finalState = 0;
- printf("\n");
- printf("begin\n");
- if(estado != -1){
- //printf("\nSEQUENCIA: ");
- for(i = 0; i<num_places;i++){
- sequencia_aux[i] = father->sequencia[i];
- printf("%d ", sequencia_aux[i]);
- }
- printf("\tESTADO: %d\n", estado);
- done = 1;
- // printf("\nestado: %d\n", estado+1);
- for(i=0; i<num_transitions; i++){
- //printf("\nTRANSICAO: %d\n", i+1);
- if(arrayTransitions[i][0][estado] ==1){
- node *new_node = create_node(-1,sequencia_aux, 0,num_places);
- for(j = 0; j<num_places; j++){
- if(arrayTransitions[i][0][j] == 1){
- if(sequencia_aux[j] == 0){
- finalState = 1;
- }
- if(sequencia_aux[j]>0){
- sequencia_aux[j]-=1;
- }
- }
- }
- for(j = 0; j<num_places; j++){
- if(arrayTransitions[i][1][j]==1){
- //printf("\n--->adiciona no estado: %d\n", j+1);
- if(sequencia_aux[j] == -1){
- new_node->sequencia[j] = -1;
- new_node->estado[j] = 1;
- }else{
- sequencia_aux[j]+=1;
- new_node->estado[j]=1;
- }
- }else{
- if(sequencia_aux[j] == -1){
- new_node->estado[j] = -1;
- new_node->sequencia[j] = -1;
- }else{
- new_node->estado[j] = -1;
- }
- //sequencia_aux[j] = 0;
- }
- }
- memset(new_node->sequencia, 0, sizeof(new_node->sequencia));
- for(k=0; k<num_places; k++){
- new_node->sequencia[k] = sequencia_aux[k];
- }
- new_node->transicao_come_from = i;
- new_node->father = father;
- add_son(father, new_node);
- node * aux = new_node->father;
- node * aux2 = (node *)malloc(sizeof(node));
- aux2 = checkSequence(new_node, aux, num_places);
- printf("antes while\n");
- print_seq(aux->sequencia,num_places);
- while(aux !=NULL&&(aux2->transicao_come_from==-99)){
- aux = aux->father;
- if(aux!=NULL){
- print_seq(aux->sequencia,num_places);
- aux2 = checkSequence(new_node, aux, num_places);
- printf("inside\n");
- print_seq(aux->sequencia,num_places);
- print_seq(aux2->sequencia,num_places);
- }
- }
- printf("depois while\n");
- for(s = 0; s<num_places; s++){
- new_node->sequencia[i] = aux2->sequencia[i];
- }
- printf("depois for\n");
- //printf("\nSEQ1: %d ; SEQ2: %d", root->sequencia[0], new_node->sequencia[0]);
- if(search_node(root, new_node, num_places) ==0){//0 nao encontra, 1 encontra
- printf("dentro search do if\n");
- if(finalState == 1){
- done = 0;
- }else{
- done = 1;
- }
- printf("1-done: %d",done);
- return done;
- }else{
- done = 0;
- }
- printf("2-done: %d",done);
- return done;
- }
- }
- memset(sequencia_aux, 0, sizeof(sequencia_aux));
- for(p = 0; p<num_places;p++){
- sequencia_aux[p] = father->sequencia[p];
- }
- }else{
- printf("3-done: %d",done);
- return done;
- }
- printf("4-done: %d",done);
- return done;
- }
- int get_estado(node* father, int num_places){
- int i=0;
- for(i=0; i<num_places; i++){
- if(father->estado[i] != 0 && father->estado[i] != -1){
- //printf("\nFATHER ESTADO: %d\n", i+1);
- father->estado[i] = -1;
- return i;
- }
- }
- return-1;
- }
- void add_son(node* father,node* new_son){
- int i;
- // printf("\n---add_son\n");
- //printf("aqui: %d\n",new_son->estado);
- if(father->son!=NULL){
- node* aux;
- //aux= (node *)malloc(sizeof(node));
- aux=father->son;
- // printf("ja tem pelo menos 1 filho\n");
- while(aux->brother!=NULL){
- //erro AQUIIIIIIIIIIIIIIIIII
- //printf("tem filho\n");
- aux=aux->brother;
- //printf("continuacao\n");
- }
- //printf("adiciona novo filho\n");
- aux->brother=new_son;
- aux=aux->brother;
- // printf("estado: %d",aux->estado);
- //
- // printf("seq:\n");
- // for(i=0;i<4;i++){
- // printf("%d ",aux->sequencia[i]);
- // }
- //printf("\nsai add\n");
- }else{
- //printf("primeiro filho\n");
- father->son=new_son;
- }
- }
- void print(node* root,int places){
- node* aux=root;
- node* aux2=root;
- int i;
- while(aux->sequencia[0]!=NULL){
- for(i=0;i<places;i++){
- printf("%d ",aux->sequencia[i]);
- }
- printf("\n");
- printf("estado: %d\n\n",aux->estado);
- if(aux->brother!=NULL){
- printf("vai para irmao \n");
- aux=aux->brother;
- }else{
- if(aux2->son!=NULL){
- aux=aux2->son;
- aux2=aux2->son;
- }else{
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment