UVa227 谜题 Puzzle

来源:互联网 发布:淘宝买家版下载 编辑:程序博客网 时间:2024/06/06 18:06

题目描述:


题目分析:

题目本身很简单,模拟就能过,但是要注意这里的输入输出格式,极容易WA和RE


AC代码:

/*by_superxdtime_2016年9月22日 19:38:40*/#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#define LIM (sx>=0&&sx<=4&&sy>=0&&sy<=4)using namespace std;char map[5][5];int sx,sy,x,y;//记录空白处的位置int cnt;//第cnt组数据char step[1005];//记录处理的次序char sh,ch;int num;//记录step数组的长度int main(){    cnt=1;    while(gets(map[0]))    {        if(map[0][0]=='Z')            break;        for(int i=1;i<=4;i++)            {                gets(map[i]);            }        for(int i=0;i<5;i++)        {            for(int j=0;j<5;j++)            {                if(map[i][j]==' ')                {                    sx=i;                    sy=j;                    break;                }            }        }        num=0;        while(ch=getchar())        {            step[++num]=ch;            if(ch=='0')                break;        }        int k;        for(k=1;k<num;k++)        {            if(step[k]=='A')            {                sx-=1;                if(LIM)                {                    map[sx+1][sy]=map[sx][sy];                    map[sx][sy]=' ';                }                else                {                    break;                }            }            else if(step[k]=='B')            {                sx+=1;                if(LIM)                {                    map[sx-1][sy]=map[sx][sy];                    map[sx][sy]=' ';                }                else                    {                        break;                    }            }            else if(step[k]=='L')            {                sy-=1;                if(LIM)                {                    map[sx][sy+1]=map[sx][sy];                    map[sx][sy]=' ';                }                else                {                    break;                }            }            else if(step[k]=='R')            {                sy+=1;                if(LIM)                {                    map[sx][sy-1]=map[sx][sy];                    map[sx][sy]=' ';                }                else                {                    break;                }            }        }        if(cnt!=1)            printf("\n");        printf("Puzzle #%d:\n",cnt++);        if(k==num)        {            for(int i=0;i<=4;i++)            {                for(int j=0;j<=3;j++)                {                    printf("%c ",map[i][j]);                }                printf("%c\n",map[i][4]);            }        }        else        {            printf("This puzzle has no final configuration.\n");        }        getchar();    }    return 0;}


0 0
原创粉丝点击