Puzzle,UVa227

来源:互联网 发布:淘宝客服要做什么 编辑:程序博客网 时间:2024/06/13 22:27

这道题因为输入输出折腾了许久....

我也是醉了...

还看了网上找到的AC样例....->这里是链接


这里需要注意的是如果你是直接复制粘贴测试样例到txt中,有可能原本边界处的空白字符会不见了...见了...了...

#include <stdio.h>#include <iostream>#include <string.h>#define row_max 7#define col_max 7char puzzle[row_max][col_max];//定义puzzle的表达形式char instru[1001];using namespace std;int main(){    memset(puzzle,0,sizeof(puzzle));    int kase = 0;    //while(scanf("%s",puzzle[0])&&(puzzle[0][0]!='Z'))    while(gets(puzzle[0]))    {        if(puzzle[0][0] == 'Z') break;        for(int i=1;i<5;i++)            gets(puzzle[i]);        int coor_x=0,coor_y=0;        //打印一下输入结果        /*        printf("\n");        for(int i=0;i<5;i++)        {            for(int j=0;j<5;j++)            printf("%c ",puzzle[i][j]);            printf("\n");        }        */        for(int i=0;i<5;i++)        {            for(int j=0;j<5;j++)            {                if(puzzle[i][j]==' ')                {                    //printf("find the space\n");                    coor_x = i;                    coor_y = j;                    break;                }            }        }        int kount=0;        while(~scanf("%c",&instru[kount]))            if(instru[kount]!='0') kount++;            else break;        instru[kount] = '0';       // instru[kount+1] ='\0';        getchar();//为了回车键设计的,不会影响到下一次输入        //打印指令        //instru[kount+1]='\0';        //printf("%s\n",instru);        bool isillegal = false;        int temp_x,temp_y;        temp_x = coor_x;        temp_y = coor_y;        //未进入for之前的temp_x,temp_y是否合法:        //printf("temp_x :%d temp_y:%d\nspace:%c\ncoor_x:%d,coor_y:%d\n",temp_x,temp_y,puzzle[temp_x][temp_y],coor_x,coor_y);        for(int i=0;instru[i]!='0';i++)        {            switch(instru[i])            {                case 'A': temp_x=coor_x-1;temp_y=coor_y;break;                case 'B': temp_x=coor_x+1;temp_y=coor_y;break;                case 'L': temp_x=coor_x;temp_y=coor_y-1;break;                case 'R': temp_x=coor_x;temp_y=coor_y+1;break;            }            if(temp_x<0||temp_x>4||temp_y<0||temp_y>4)            {                isillegal=true;break;            }            else            {                //printf("the normal behave\n");                puzzle[coor_x][coor_y] = puzzle[temp_x][temp_y];                puzzle[temp_x][temp_y] = ' ';                coor_x = temp_x;                coor_y = temp_y;            }        }        if(kase++) printf("\n");        printf("Puzzle #%d:\n",kase);        if(isillegal)        {            printf("This puzzle has no final configuration.\n");        }        else        {            for(int i=0;i<5;i++)            {                printf("%c",puzzle[i][0]);                for(int j=1;j<5;j++)                {                    printf(" %c",puzzle[i][j]);                }                printf("\n");            }        }    }    return 0;}



0 0
原创粉丝点击