打印十字图

 问题描述
小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示:

..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..
对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数。

输入格式
一个正整数 n (n<30) 表示要求打印图形的层数。
输出格式
对应包围层数的该标志。
样例输入1
1
样例输出1
..$$$$$..
..$...$..
$$$.$.$$$
$...$...$
$.$$$$$.$
$...$...$
$$$.$.$$$
..$...$..
..$$$$$..
样例输入2
3
样例输出2
..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..
提示
请仔细观察样例,尤其要注意句点的数量和输出位置。
  1. 首先确定二维数组的位数,中间哪一行一定是有5个 n" ” 和 “.” 。也就是说对于输入的n应打印输出一个n*4+5大小的方阵
  2. 记$与.互逆。从中心元素开始搜索,到达某一点时,上下左右置相同值,四角置相反值。
  3. 本来是用广搜做的,一圈一圈的,但是不知道为什么搜索不出来。所以用了深搜
  4. 注意矩阵四个角应先置”.”,否者最后打印的图案这四个角不对
  5. 好像有奇偶行规律,可是我没有找出来,直接就穷举了
  6. 由于图形是左右、中心对称的,所以可以只搜索左上角图形,然后直接对称就得到了整个图案。

//四方向搜索AC代码


import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class Point{
    public int x=0,y=0;
    public Point(int x,int y) {
        this.x = x;
        this.y = y;
    }
}

public class Main {

    public static int n,m;
    public static char[][] grids = null;
    public static Queue<Point> que = new LinkedList<Point>();
    public static final int[] dirx1 = {-1,1,0,0},diry1 = {0,0,-1,1};
    public static final int[] dirx2 = {-1,-1,1,1},diry2 = {-1,1,-1,1};

    public static void show(){
        //System.out.println("Output grids:");
        for(int i=0;i<m;i++){
            for(int j=0;j<m;j++) System.out.print(grids[i][j]);
            System.out.println();
        }
    //  System.out.println();
    }
    public static boolean isAvailable(int nx,int ny){
        if(nx>=0&&nx<m&&ny>=0&&ny<m&&grids[nx][ny]=='#') return true;
        return false;
    }

    public static void DFS(int x,int y,char ch){
        for(int i=-1;i<=1;i++){
            for(int j=-1;j<=1;j++){
                int nx = x+i;
                int ny = y+j;
                if(isAvailable(nx, ny)){
                    if(ch=='$'){
                        if(i*j!=0){
                            grids[nx][ny] = '.';
                        }
                        else if(i*j==0) grids[nx][ny] = '$';
                    }
                    else if(ch=='.'){ 
                        if(i*j!=0){
                            grids[nx][ny] = '$';
                        }
                        else grids[nx][ny] = '.';
                    }
                    if(ch=='$') grids[nx][ny] = '.';
                    else grids[nx][ny] = '$';
                }
            }
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = n*4+5;
        grids = new char[m][m];
        for(int i=0;i<m;i++){
            Arrays.fill(grids[i], '#');
        }
        int x,y,nx=0,ny=0,t=0;
        x = y = m/2;
        grids[0][0] = grids[0][m-1] = grids[m-1][0] = grids[m-1][m-1] = '.';
        for(int i=-2;i<=2;i++) {
            grids[x][y+i]=grids[x+i][y] = '$';
        }
        for(int i=x;i>=0;i--){
            for(int j=y;j>=0;j--){
                if(grids[i][j]=='$') DFS(i,j,'$');
                else if(grids[i][j]=='.') DFS(i,j,'.');
            }
        }
        for(int i=x;i>=0;i--){
            for(int j=y;j<m;j++){
                if(grids[i][j]=='$') DFS(i,j,'$');
                else if(grids[i][j]=='.') DFS(i,j,'.');
            }
        }
        for(int i=x;i<m;i++){
            for(int j=y;j<m;j++){
                if(grids[i][j]=='$') DFS(i,j,'$');
                else if(grids[i][j]=='.') DFS(i,j,'.');
            }
        }
        for(int i=x;i<m;i++){
            for(int j=y;j>=0;j--){
                if(grids[i][j]=='$') DFS(i,j,'$');
                else if(grids[i][j]=='.') DFS(i,j,'.');
            }
        }
        show();
    }

}

//搜索左上角,对称



import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class Point{
    public int x=0,y=0;
    public Point(int x,int y) {
        this.x = x;
        this.y = y;
    }
}

public class Main {

    public static int n,m;
    public static char[][] grids = null;
    public static Queue<Point> que = new LinkedList<Point>();
    public static final int[] dirx1 = {-1,1,0,0},diry1 = {0,0,-1,1};
    public static final int[] dirx2 = {-1,-1,1,1},diry2 = {-1,1,-1,1};

    public static void show(){
        //System.out.println("Output grids:");
        for(int i=0;i<m;i++){
            for(int j=0;j<m;j++) System.out.print(grids[i][j]);
            System.out.println();
        }
    //  System.out.println();
    }
    public static boolean isAvailable(int nx,int ny){
        if(nx>=0&&nx<m&&ny>=0&&ny<m&&grids[nx][ny]=='#') return true;
        return false;
    }

    public static void DFS(int x,int y,char ch){
        //show();
        for(int i=-1;i<=1;i++){
            for(int j=-1;j<=1;j++){
                int nx = x+i;
                int ny = y+j;
                if(isAvailable(nx, ny)){
                    if(ch=='$'){
                        if(i*j!=0){
                            grids[nx][ny] = '.';
                        }
                        else if(i*j==0) grids[nx][ny] = '$';
                    }
                    else if(ch=='.'){ 
                        if(i*j!=0){
                            grids[nx][ny] = '$';
                        }
                        else grids[nx][ny] = '.';
                    }
                    if(ch=='$') grids[nx][ny] = '.';
                    else grids[nx][ny] = '$';
                }
            }
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = n*4+5;
        grids = new char[m][m];
        for(int i=0;i<m;i++){
            Arrays.fill(grids[i], '#');
        }
        int x,y,nx=0,ny=0,t=0;
        x = y = m/2;
        grids[0][0] = grids[0][m-1] = grids[m-1][0] = grids[m-1][m-1] = '.';
        for(int i=-2;i<=2;i++) {
            grids[x][y+i]=grids[x+i][y] = '$';
        }
        for(int i=x;i>=0;i--){
            for(int j=y;j>=0;j--){
                if(grids[i][j]=='$') DFS(i,j,'$');
                else if(grids[i][j]=='.') DFS(i,j,'.');
                grids[i][m-1-j] = grids[i][j];
                grids[m-1-i][j] = grids[i][j];
                grids[m-1-i][m-1-j] = grids[m-1-i][j];
            }
        }
        show();
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值