hdu 1986

来源:互联网 发布:音视频格式转换软件 编辑:程序博客网 时间:2024/05/16 04:37

 http://acm.hdu.edu.cn/showproblem.php?pid=1986

要注意 怎么蛇形填数,还有  如何输入带空格的字符串。

下面看代码:

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;const char biao[27][6]={{"00000"},{"00001"},{"00010"},{"00011"},{"00100"},{"00101"},{"00110"},{"00111"},{"01000"},{"01001"},{"01010"},{"01011"},{"01100"},{"01101"},{"01110"},{"01111"},{"10000"},{"10001"},{"10010"},{"10011"},{"10100"},{"10101"},{"10110"},{"10111"},{"11000"},{"11001"},{"11010"}};int map[30][30];string s;bool m[1000];int main(){    int T,r,c;    scanf("%d",&T);    for(int t=1;t<=T;t++)    {        memset(m,0,sizeof(m));        memset(map,-1,sizeof(map));        scanf("%d%d",&r,&c);        getchar();       /// gets(s);      //把s的类型由string 换为char s[100],用gets(s);也可以。        getline(cin,s);        int sum=0;        for( int i = 0; s[i]!='\0' ; i++ )        {            int k=0;            if(s[i]>='A' && s[i]<='Z') k=(int)(s[i]-'A')+1;            for(int j=0;j<5;j++){                 m[++sum] = biao[k][j]-'0';            }        }        int tot=0,x,y;        x=0;y=0;        map[0][0] = m[++tot];        while( tot < sum )  ///蛇行填数        {            while(y+1<c && map[x][y+1]==-1) map[x][++y] = m[++tot];///右            while(x+1<r && map[x+1][y]==-1) map[++x][y] = m[++tot];///下            while(y-1>=0&& map[x][y-1]==-1) map[x][--y] = m[++tot];///左            while(x-1>=0&& map[x-1][y]==-1) map[--x][y] = m[++tot];///上        }        printf("%d ",t);        for(int i=0;i<r;i++)            for(int j=0;j<c;j++)            {                if(map[i][j]==-1) printf("0");                else printf("%d",map[i][j]);            }              printf("\n");    }    return 0;}



原创粉丝点击