打印十字图 queue 搞定

题目描述

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

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

输入格式

一个正整数 n (n<30) 表示要求打印图形的层数。

输出

对应包围层数的该标志。

样例输入

3

样例输出

..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..

最近发现对容器比较敏感,老往那方面想,哈哈,又解决一个:还需要八方向搜索

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <queue>
 4 #include <cstring>
 5 using namespace std;
 6 int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};//八方向搜索
 7 char s[100][100];
 8 struct state
 9 {
10     int x,y;
11 }num[35],ms,me;
12 queue < state >nnn;
13 queue < state >mmm;
14 void fun(int a,int b)//存入基本十字架
15 {
16       s[a][b]='$';
17     ms.x=a;ms.y=b;
18      nnn.push(ms);
19      s[a][b+1]='$';s[a][b+2]='$';s[a][b-1]='$';s[a][b-2]='$';
20      ms.x=a;ms.y=b+1;nnn.push(ms);ms.x=a;ms.y=b+2;nnn.push(ms);ms.x=a;ms.y=b-1;nnn.push(ms);
21      ms.x=a;ms.y=b-2;nnn.push(ms);
22      s[a+1][b]='$';s[a+2][b]='$';s[a-1][b]='$';s[a-2][b]='$';
23      ms.x=a+1;ms.y=b;nnn.push(ms);ms.x=a+2;ms.y=b;nnn.push(ms);ms.x=a-1;ms.y=b;nnn.push(ms);
24      ms.x=a-2;ms.y=b;nnn.push(ms);
25 }
26 int main()
27 {
28 
29     int i,n,xx,yy;
30     num[1].x=5;num[1].y=5;
31     for(i=2;i<35;i++)
32     {
33         num[i].x=num[i-1].x+2;
34         num[i].y=num[i-1].y+2;
35     }
36  state st;
37    memset(s,'0',sizeof(s));
38     cin>>n;
39     int nn=n;
40     fun(num[n].x,num[n].y);
41    while(n--)
42     {
43        while(!nnn.empty())//打印外圈的*
44        {
45            st=nnn.front();
46            nnn.pop();
47            for(i=0;i<8;i++)
48            {
49                xx=st.x+dir[i][0];
50                yy=st.y+dir[i][1];
51                if(s[xx][yy]=='0')
52                  {
53                      s[xx][yy]='.';
54                      ms.x=xx;ms.y=yy;
55                      mmm.push(ms);
56                  }
57            }
58        }
59        while(!mmm.empty())//再外一层的&
60        {
61           st=mmm.front();
62            mmm.pop();
63            for(i=0;i<8;i++)
64            {
65                xx=st.x+dir[i][0];
66                yy=st.y+dir[i][1];
67                if(s[xx][yy]=='0')
68                {
69                      s[xx][yy]='$';
70                      ms.x=xx;ms.y=yy;
71                      nnn.push(ms);
72                }
73            }
74        }
75     }
76     for( i=1;i<num[nn].x*2;i++)//输出整个图形
77     {
78         for(int j=1;j<num[nn].y*2;j++)
79         {
80             if(s[i][j]=='0')
81               cout<<".";
82         else
83              cout<<s[i][j];
84         }
85                  cout<<endl;
86     }
87     return 0;
88 }
原文地址:https://www.cnblogs.com/lovychen/p/3612002.html