• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hug_Sea
博客园    首页    新随笔    联系   管理    订阅  订阅

POJ-1941 The Sierpinski Fractal

题意:如图所示画出图形。

 

思路:递归~

题目连接:http://poj.org/problem?id=1941

 

View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iostream>
 8 using namespace std;
 9 const int N=3000;
10 
11 char map[N][N];
12 
13 void dfs(int n,int x,int y){
14     if(n==1){
15         map[x][y]=map[x-1][y+1]='/';
16         map[x][y+1]=map[x][y+2]='_';
17         map[x][y+3]=map[x-1][y+2]='\\';
18         return ;
19     }
20     int size=1<<(n-1);
21     dfs(n-1,x,y);    
22     dfs(n-1,x-size,y+size);
23     dfs(n-1,x,y+2*size);
24 }
25 
26 int main(){
27     
28 //    freopen("data.in","r",stdin);
29 //    freopen("data.out","w",stdout);
30     
31     int n;
32     while(scanf("%d",&n),n){
33         memset(map,' ',sizeof(map));
34         int size=1<<n;
35         dfs(n,size,1);
36         for(int i=1;i<=size;i++){
37             for(int j=1;j<=size*2;j++)
38                 printf("%c",map[i][j]);
39             puts("");
40         }
41         puts("");
42     }
43     return 0;
44 }

 

posted @ 2012-05-30 22:48  Hug_Sea  阅读(346)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3