UVa227 Puzzle

#include <stdio.h>
#include <string.h>
#define maxn 100

char p[7][7];
char s[maxn];

int exchange(char p[7][7], int &x, int &y, char option)
{
    char temp;
    int flag = 1;
    if(option == 'A')
    {
        temp = p[x][y];
        if( (x-1) != 0)
        {
            temp = p[x][y];
            p[x][y] = p[x-1][y];
            p[x-1][y] = temp;
            x = x-1;
        }
        else
        {
            printf("This puzzle has no final configuration.\n");
            flag = 0;
        }
    }
    else if(option == 'B')
    {
        temp = p[x][y];
        if( (x+1) != 6)
        {
            temp = p[x][y];
            p[x][y] = p[x+1][y];
            p[x+1][y] = temp;
            x = x+1;
        }
        else
        {
            printf("This puzzle has no final configuration.\n");
            flag = 0;
        }
    }
    else if(option == 'L')
    {
        temp = p[x][y];
        if( (y-1) != 0)
        {
            temp = p[x][y];
            p[x][y] = p[x][y-1];
            p[x][y-1] = temp;
            y = y-1;
        }
        else
        {
            printf("This puzzle has no final configuration.\n");
            flag = 0;
        }
    }
    else if(option == 'R')
    {
        temp = p[x][y];
        if( (y+1) != 6)
        {
            temp = p[x][y];
            p[x][y] = p[x][y+1];
            p[x][y+1] = temp;
            y = y+1;
        }
        else
        {
            printf("This puzzle has no final configuration.\n");
            flag = 0;
        }
        
    }
    return flag;    
}

int main()
{
    int num = 0;
    while(1){
    scanf("%c", &p[1][1]);
    if(p[1][1] == 'Z')
      break;
      
    int x, y;
    for(int i = 1; i < 6; i++)
    {
      for(int j = 1; j < 6; j++)
      {
          if(i != 1 || j != 1)
          scanf("%c", &p[i][j]);
        if(p[i][j] == ' ')
        {
            x = i;
            y = j;
        }
      }
      getchar();
    }
    
    int count = 0;
    int flag = 1;
    while(1)
    {
        scanf("%c", &s[count]);
        if(s[count] == '0')
          break;
        else if(s[count] == '\n')
        {
        }
        else
          count++;
    }
    int len = count;
    count = 0;
    num++;
    if(num > 1)
      printf("\n");
    printf("Puzzle #%d:\n", num);
    while(count < len && flag == 1)
    {
        flag = exchange(p, x, y, s[count]);
        count++;
    }
    
    if(flag == 1){
    
    for(int i = 1; i < 6; i++)
    {
      for(int j = 1; j < 5; j++)
      {
          printf("%c ", p[i][j]);
      }
      printf("%c", p[i][5]);
      printf("\n");
    }
    }
    getchar();
    }
    return 0;
}


你可能感兴趣的:(c,uva)