其他分享
首页 > 其他分享> > poj 3752(水题)

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;
}

 

标签:direction,ch,水题,int,3752,visit,dfs,poj,data
来源: https://www.cnblogs.com/stevenzrx/p/15646450.html