蓝桥杯 打印十字图

传送门:http://lx.lanqiao.cn/problem.page?gpid=T25

 

 

解题思路:

可以看出图像是一层层的扩展,可以用广搜扩展,然后用二维数组保存图案一下就行了。

实现代码:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5 
 6 const int MAXN=200;
 7 
 8 char map[MAXN][MAXN];
 9 
10 struct node{
11     int x,y;
12     node(int x,int y):x(x),y(y){};
13 };
14 
15 int dx[]={1,-1,0,0,1,-1,1,-1};
16 int dy[]={0,0,-1,1,1,-1,-1,1};
17 void init(){
18     queue<node>q;
19     int cx=MAXN/2;
20     int cy=MAXN/2;
21     memset(map,0,sizeof(map));
22 
23     map[cx][cy]='$';
24     q.push(node(cx,cy));
25 
26     map[cx+1][cy]='$';
27     q.push(node(cx+1,cy));
28     map[cx+2][cy]='$';
29     q.push(node(cx+2,cy));
30       map[cx-1][cy]='$';
31     q.push(node(cx-1,cy));
32     map[cx-2][cy]='$';
33     q.push(node(cx-2,cy));
34 
35     map[cx][cy+1]='$';
36     q.push(node(cx,cy+1));
37     map[cx][cy+2]='$';
38     q.push(node(cx,cy+2));
39       map[cx][cy-1]='$';
40     q.push(node(cx,cy-1));
41     map[cx][cy-2]='$';
42     q.push(node(cx,cy-2));
43 
44     while(!q.empty()){
45         node fr=q.front();
46         q.pop();
47 
48         int x=fr.x;
49         int y=fr.y;
50 
51         for(int i=0;i<8;i++){
52             int tx=x+dx[i];
53             int ty=y+dy[i];
54 
55             if(tx<0||tx>=MAXN||ty<0||ty>=MAXN)
56                 continue;
57 
58             if(map[tx][ty]=='$'||map[tx][ty]=='.')
59                 continue;
60 
61             q.push(node(tx,ty));
62             if(map[x][y]=='$'){
63                 map[tx][ty]='.';
64             }else{
65                 map[tx][ty]='$';
66             }
67         }
68     }
69 
70 }
71 
72 int main(){
73     init();
74     int st=MAXN/2-2;
75     int ed=MAXN/2+2;
76     int n;scanf("%d",&n);
77     st-=n*2;
78     ed+=n*2;
79     map[st][st]=map[st][ed]=map[ed][st]=map[ed][ed]='.';
80     for(int i=st;i<=ed;i++){
81         for(int j=st;j<=ed;j++)
82             cout<<map[i][j];
83         cout<<endl;
84     }
85 
86 
87 }

 

posted on 2017-04-02 17:42  mkfoy  阅读(308)  评论(0编辑  收藏  举报

导航