poj 3752(水题)

#include<iostream>
#include<cstring>
using namespace std;
int m,n,num;
bool visit[100][100];
char data[100][100],ch;
void dfs(int direction,int i,int j){
    //direction=1向东,2向南,3向西,4向北 
    ch = (ch-'A'+1)%26+'A';
    if(direction==1){
        if(!visit[i][j+1]){
            visit[i][j+1] = 1;
            data[i][j+1] = ch;
            dfs(1,i,j+1);
        }
        else if(!visit[i+1][j]){
            visit[i+1][j] = 1;
            data[i+1][j] = ch;
            dfs(2,i+1,j);
        }
    }
    if(direction==2){
        if(!visit[i+1][j]){
            visit[i+1][j] = 1;
            data[i+1][j] = ch;
            dfs(2,i+1,j);
        }
        else if(!visit[i][j-1]){
            visit[i][j-1] = 1;
            data[i][j-1] = ch;
            dfs(3,i,j-1);
        }
    }
    if(direction==3){
        if(!visit[i][j-1]){
            visit[i][j-1] = 1;
            data[i][j-1] = ch;
            dfs(3,i,j-1);
        }
        else if(!visit[i-1][j]){
            visit[i-1][j] = 1;
            data[i-1][j] = ch;
            dfs(4,i-1,j);
        }
    }
    if(direction==4){
        if(!visit[i-1][j]){
            visit[i-1][j] = 1;
            data[i-1][j] = ch;
            dfs(4,i-1,j);
        }
        else if(!visit[i][j+1]){
            visit[i][j+1] = 1;
            data[i][j+1] = ch;
            dfs(1,i,j+1);
        }
    }
}
int main(){
    scanf("%d%d",&m,&n);
    num = 0;
    ch = 'Z';
    memset(visit,0,sizeof visit);
    visit[1][n+1] = visit[m+1][n] = visit[m][0] = visit[0][1] = 1;
    dfs(1,1,0);
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            printf("   %c",data[i][j]);
        }
        printf("\n");
    }
    return 0;
}

 

上一篇:【CSS】外部样式表及文本类样式


下一篇:Enterprise Architect去掉元素背景渐变效果