POJACM3752

来源:互联网 发布:java date setmonth 编辑:程序博客网 时间:2024/06/06 04:06

/*
  Description:  字母旋转游戏
  POJ__ACM  3752  http://poj.org/problem?id=3752

*/
#include<stdio.h>
#include<stdlib.h>
char **s;
void Molloc(int m, int n)
{
    s = (char **)malloc( (m+1)*sizeof(char *) );
    int i ;
    for(i = 0; i <= m; ++i)
      s[i] = (char *)malloc ((n+1)*sizeof(char) );
    //printf("Successfully Malloc!\n");
}

//char s[255][255];
char map(int index)
{
    //printf("map = %c\n", ('A' + index%26));
    return ('A' + index%26);
}
void Print(int, int);
void Display(int left, int up, int right, int down, int nstep)
{
     int i;
    if(left > right || up > down)
      return;
    //printf("left = %d, up = %d\n", left, up);
    if(left == right){
       for(i = up; i <= down; ++i)
         s[i][left] = map(nstep++);
       return ;
    }
    else if(up == down){
       for(i = left; i <= right; ++i)
         s[up][i] = map(nstep++);
       return;
    }
    for(i = left; i < right; ++i)
      s[up][i] = map(nstep++);
    for(i = up ; i < down; ++i)
      s[i][right] = map(nstep++);
    for(i = right; i > left; --i)
      s[down][i] = map(nstep++);   
    for(i = down  ; i > up; --i)
      s[i][left] = map(nstep++);
    Display(left+1, up+1, right-1, down-1, nstep);
}
void Print(int m, int n)
{
    int i, j;
    for(i = 1; i <= m; ++i){
      for(j = 1; j <= n; ++j)
        printf("%4c", s[i][j]);
      printf("\n");
   }
}
int main()
{
  int m, n;
  while(scanf("%d%d", &m, &n) != EOF){
      Molloc(m, n);
      Display(1, 1, n, m, 0);
      Print(m, n);                   
  } 
  ///system("pause");
  return 0;
}
 

本文出自 “东方快翔” 博客,请务必保留此出处http://hustluy.blog.51cto.com/1792080/607562

原创粉丝点击