UVa 227 Puzzle(谜题)

#include 
#include 
#include 
char map[6][7];
int blanx,blany;//空格处
int change(char ch)
{

      if(ch=='A'){
        if(blanx==1)
            return 0;
        map[blanx][blany]=map[blanx-1][blany];
        map[blanx-1][blany]=' ';
        blanx=blanx-1;
     }
      else if(ch=='B'){
        if(blanx==5)
            return 0;
        map[blanx][blany]=map[blanx+1][blany];
        map[blanx+1][blany]=' ';
        blanx=blanx+1;
      }
      else if(ch=='L'){
        if(blany==1)
            return 0;
        map[blanx][blany]=map[blanx][blany-1];
        map[blanx][blany-1]=' ';
        blany=blany-1;
      }
      else if(ch=='R'){
        if(blany==5)
            return 0;
        map[blanx][blany]=map[blanx][blany+1];
        map[blanx][blany+1]=' ';
        blany=blany+1;
      }
      else
        return 0;
    return 1;
}
int main()
{
    int count=0;
    int getre;
    while(gets(map[1]+1)!=EOF){
        count++;
        getre=1;
        if(map[1][1]=='Z')
            break;
        for(int i=2;i<=5;i++)
            gets(map[i]+1);
        for(int i=1;i<=5;i++)
          for(int j=1;j<=5;j++){
            if(map[i][j]==' '){
                blanx=i;
                blany=j;
                break;
            }
        }
        char ch;
        while((ch=getchar())!='0'){
            if(ch!='\n'&&ch!=' '&&ch!='\r'&&getre)
                getre=change(ch);
        }
        getchar();
        if(count>1)
            printf("%c",'\n');
        printf("Puzzle #%d:\n",count);
        if(getre==0)
            printf("This puzzle has no final configuration.\n");
        else{
          for(int i=1;i<=5;i++){
             for(int j=1;j<=5;j++)
               printf("%c%c",map[i][j],j==5?'\n':' ');
         }
        }
    }
    return 0;
}

你可能感兴趣的:(刘汝佳算法竞赛入门+UVa)