달팽이

언어/c언어2016. 4. 20. 17:29

//작성자: 김준현
//작성일: 2016 04 20
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define _SIZE_ 4
typedef struct Mat {
 int** A_RRAY; // 정사각행렬
 int** A_RRAY_temp; // 정사각행렬
 int array_size; // 배열에 크기
 int start_number;// 1부터 n까지
 int row_temp; int col_temp;// 일시적으로
 int choice[_SIZE_];// 1:오른쪽  2:아랫쪽  3: 왼쪽  4: 윗쪽
}Mat;
int row_index_global; int col_index_global; int k_index_global; // index
int j_index_global; int i_index_global; int i; int j;
//Function prototype
//[1]------------------------------
void initiallize(Mat*);
//[2]------------------------------
void info_array(Mat*);
//[3]------------------------------
void data_input(Mat*);
// Main----------------------------
int main(void) {
 Mat start_;
 initiallize(&start_);
 info_array(&start_);
 //출력 테스트 확인 후 주석으로 막을것 --------------------------------------------------
 for (row_index_global = 0; row_index_global < start_.array_size; row_index_global++) {
  for (col_index_global = 0; col_index_global < start_.array_size; col_index_global++) {
   printf("%2d ",start_.A_RRAY[row_index_global][col_index_global]);
   if (col_index_global == start_.array_size - 1) {
    printf("\n"); //개행
   }
  }
 }
 //------------------------------------------------------------------------------------
 data_input(&start_);
 
 for (row_index_global = 0; row_index_global < start_.array_size; row_index_global++) {
  for (col_index_global = 0; col_index_global < start_.array_size; col_index_global++) {
   printf("%2d ", start_.A_RRAY[row_index_global][col_index_global]);
   if (col_index_global == start_.array_size - 1) {
    printf("\n"); //개행
   }
  }
 }
 
 return 0;
}
//-------------------------------
//[1] 초기화
void initiallize(Mat* Mat_temp) {
 Mat_temp->A_RRAY = NULL;
 Mat_temp->A_RRAY_temp = NULL;
 Mat_temp->array_size = 0;
 Mat_temp->start_number = 1;
 Mat_temp->row_temp = 0; // 일시적으로 쓸 행
 Mat_temp->col_temp = 0; // 일시적으로 쓸 열
 for (row_index_global = 0; row_index_global < 4; row_index_global++) {
  Mat_temp->choice[row_index_global] = row_index_global + 1;
 }
}
//[2] 배열형성 및 값 입력
void info_array(Mat* Mat_temp) {
 printf("Enter Mat size: ");
 scanf_s("%d", &Mat_temp->array_size); // 배열의 크기 지정
 Mat_temp->A_RRAY = (int**)malloc(sizeof(int*)*Mat_temp->array_size); // 배열의 행구성
 Mat_temp->A_RRAY_temp = (int**)malloc(sizeof(int*)*Mat_temp->array_size); // 배열의 행구성
  //배열의 열 구성
 for (row_index_global = 0; row_index_global < Mat_temp->array_size; row_index_global++) {
  Mat_temp->A_RRAY[row_index_global] = (int*)malloc(sizeof(int)*Mat_temp->array_size);
  Mat_temp->A_RRAY_temp[row_index_global] = (int*)malloc(sizeof(int)*Mat_temp->array_size);
 }
 //배열을 0 으로 초기화 값이 잘되어 있는지 확인하기 위해 넣는다.
 for (row_index_global = 0; row_index_global < Mat_temp->array_size; row_index_global++) {
  for (col_index_global = 0; col_index_global < Mat_temp->array_size; col_index_global++) {
   Mat_temp->A_RRAY[row_index_global][col_index_global] = 0;
   Mat_temp->A_RRAY_temp[row_index_global][col_index_global] = 0;
  }
 }
}
//[3]
void data_input(Mat* Mat_temp) {
 int temp_col = -1;
 int temp_row = 0;
 
 for (k_index_global = 1; k_index_global <= (Mat_temp->array_size *Mat_temp->array_size); k_index_global++) {
  for (j_index_global = 0; j_index_global < 4; j_index_global++) {
   //case[1]--------------------------------------------------------------------
   if (Mat_temp->choice[j_index_global] == 1) {// [RIGHT] 열 이동
    temp_col++; // 0
    for (i= temp_col; i< Mat_temp->array_size; i++) {
     if (Mat_temp->A_RRAY_temp[temp_row][i] == 1) {
      goto gogosing;
     }
     else {//Mat_temp->A_RRAY_temp[Mat_temp->row_temp][i] == 0
      Mat_temp->A_RRAY[temp_row][i] = Mat_temp->start_number++;
      Mat_temp->A_RRAY_temp[temp_row][i] = 1; //  
      temp_col = i;//3
      if (i == Mat_temp->array_size - 1) {
       goto gogosing;
      }
     }
    }
   }
   //case[2]--------------------------------------------------------------------
   else if (Mat_temp->choice[j_index_global] == 2) { // [DOWN] 행 이동
    temp_row++; // 1
    for (i = temp_row; i< Mat_temp->array_size; i++) {
     if (Mat_temp->A_RRAY_temp[i][temp_col] ==1 ) {
      goto gogosing;
     }
     else {//Mat_temp->A_RRAY_temp[Mat_temp->row_temp][i] == 0
      Mat_temp->A_RRAY[i][temp_col] = Mat_temp->start_number++;
      Mat_temp->A_RRAY_temp[i][temp_col] = 1;//
      temp_row = i; // 3
      if (i == Mat_temp->array_size - 1) {
       goto gogosing;
      }
     }
    }
   }
   //case[3]--------------------------------------------------------------------
   else if (Mat_temp->choice[j_index_global] == 3) { // [LEFT] 열 이동
    temp_col--;
    for (i = temp_col; i>=0; i--) {
     if (Mat_temp->A_RRAY_temp[temp_row][i] == 1) {
      goto gogosing;
     }
     else {//Mat_temp->A_RRAY_temp[Mat_temp->row_temp][i] == 0
      Mat_temp->A_RRAY[temp_row][i] = Mat_temp->start_number++;
      Mat_temp->A_RRAY_temp[temp_row][i] = 1;//
      temp_col = i; // 0
      if (i == 0) {
       goto gogosing;
      }
     }
    }
   }
   //case[4]--------------------------------------------------------------------
   else { //[UP] 위로이동
    temp_row--;
    for (i = temp_row; i >= 0; i--) {
     if (Mat_temp->A_RRAY_temp[i][temp_col] == 1) {
      goto gogosing;
     }
     else {//Mat_temp->A_RRAY_temp[Mat_temp->row_temp][i] == 0
      Mat_temp->A_RRAY[i][temp_col] = Mat_temp->start_number++;
      Mat_temp->A_RRAY_temp[i][temp_col] = 1;//
      temp_row = i; // 3
      if (i == 0) {
       goto gogosing;
      }
     }
    }
   }
  gogosing:;
  }
 }
}

'언어 > c언어' 카테고리의 다른 글

queue ver1  (0) 2016.05.05
linkedlist  (0) 2016.05.01
행렬 회전  (0) 2016.04.19
가중 그래프  (0) 2016.04.18
포인터  (0) 2016.04.18