hdoj-1987-Decoding

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
int t,n,m,map[25][25],temp[450],top;
void dfs(int x,int y)
{
    if(top==n*m)
        return;
    while(y+1<m&&map[x][y+1]<2)
    {
        temp[top++]=map[x][y+1];
        map[x][y+1]=2;
        y++;
        if(top==n*m)
        return;
    }
    while(x+1<n&&map[x+1][y]<2)
    {
        temp[top++]=map[x+1][y];
        map[x+1][y]=2;
        x++;
        if(top==n*m)
        return;
    }
    while(y-1>=0&&map[x][y-1]<2)
    {
        temp[top++]=map[x][y-1];
        map[x][y-1]=2;
        y--;
        if(top==n*m)
        return;
    }
    while(x-1>=0&&map[x-1][y]<2)
    {
        temp[top++]=map[x-1][y];
        map[x-1][y]=2;
        x--;
        if(top==n*m)
        return;
    }
    dfs(x,y);
}
int main()
{
    int i,j,k;
    char ch;
    scanf("%d",&t);
    k=t;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        getchar();
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf("%c",&ch);
                map[i][j]=ch-'0';
            }
        }
        top=0;
        temp[top++]=map[0][0];
        map[0][0]=2;
        dfs(0,0);
        printf("%d ",k-t);
        int flag=1,tve=0;
        for(i=0;i+4<top;i+=5)
        {
            int sum=0;
            sum=temp[i]*16+temp[i+1]*8+temp[i+2]*4+temp[i+3]*2+temp[i+4];
            if(sum>=0&&sum<=26)
            {
                if(sum==0&&!flag)
                tve++;
                if(sum>0&&sum<=26)
                {
                    if(tve>0)
                    {
                        for(int j=0;j<tve;j++)
                        {
                            printf(" ");
                        }
                        tve=0;
                    }
                    printf("%c",'A'+sum-1);
                    flag=0;
                }
            }

        }
        printf("
");
    }
    return 0;
}

比赛pe8次啊     注意空格   除了字母间有空格外   开头  结尾不允许有空格

原文地址:https://www.cnblogs.com/nylg-haozi/p/3219011.html