hdu 2074 输出图形

15 篇文章 0 订阅
4 篇文章 0 订阅

</pre>叠筐</h1><strong><span style="font-family:Arial;font-size:12px;color:green;font-weight:bold">Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13712    Accepted Submission(s): 3569</span></strong><div class="panel_title" align="left">Problem Description</div><div class="panel_content">需要的时候,就把一个个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。这个工作现在要让计算机来完成,得看你的了。</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Input</div><div class="panel_content">输入是一个个的三元组,分别是,外筐尺寸n(n为满足0<n<80的奇整数),中心花色字符,外筐花色字符,后二者都为ASCII可见字符;</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Output</div><div class="panel_content">输出叠在一起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐相叠时,最外筐的角总是被打磨掉。叠筐与叠筐之间应有一行间隔。</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Sample Input</div><div class="panel_content"><pre><div style="font-family:Courier New,Courier,monospace">11 B A
5 @ W</div>
 

Sample Output
   
   
AAAAAAAAA ABBBBBBBBBA ABAAAAAAABA ABABBBBBABA ABABAAABABA ABABABABABA ABABAAABABA ABABBBBBABA ABAAAAAAABA ABBBBBBBBBA AAAAAAAAA @@@ @WWW@ @W@W@ @WWW@ @@@
 

Author
qianneng
 

Source
 


开始时以为是深搜,最后发现直接规律输出即可

//@auther yangZongJun
/********************************************//**
Date  : 2014/02/17
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2074
题    意:搜索
解题思路:
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 85;
const int INF = 2100000000;
//深搜错误代码

int n;
char center, surround;
char graph[MAXN][MAXN];

void dfs(int x, int y)
{
//    if(! (0<=x&&x<n || 0<=y&&y<n || graph[x][y]==' ')) return ;
    for(int i = -1; i <= 1; i++)
    {
        for(int j=-1; j<= 1; j++)
        {
            int xx = x+i, yy = y+j;
            if(i == 0 && j== 0) continue;
            //if(! (0<=xx&&xx<n || 0<=yy&&yy<n || graph[xx][yy]==' ')) return ;
            //if(!(0<=xx&&xx<n && 0<=yy&&yy<n) &&(graph[xx][yy]==center || graph[xx][yy]==surround)) return ;
            if(0<=xx&&xx<n && 0<=yy&&yy<n && graph[xx][yy]==' ')
            {
                if(graph[x][y] == center)
                {
                    graph[xx][yy] = surround;
                }
                else
                {
                    graph[xx][yy] = center;
                }
                dfs(xx, yy);
            }
        }
    }
    return ;
}

int main()
{
    freopen("input.txt", "r", stdin);
    while(~scanf("%d %c %c", &n, ¢er, &surround))
    {
        //cout << center << surround << endl;
        for(int i = 0; i <= n; i++)
            for(int j = 0; j <= n; j++)
            graph[i][j] = ' ';
        graph[n/2][n/2] = center;
        dfs(n/2, n/2);
        graph[0][0] = ' ';
        graph[0][n-1] = ' ';
        graph[n-1][0] = ' ';
        graph[n-1][n-1] = ' ';
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                printf("%c", graph[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

AC代码:

//@auther yangZongJun
/********************************************//**
Date  : 2014/02/17
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2074
题    意:输出图形
解题思路:
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <set>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 85;
const int INF = 2100000000;

int n;
char center, surround;
char graph[MAXN][MAXN];

int main()
{
//    freopen("input.txt", "r", stdin);
    int cas = 0;
    while(~scanf("%d %c %c", &n, &center, &surround))
    {
        if(cas) printf("\n");//没两个样例之间输出空格
        if(n == 1)//1时特判一下
        {
            if(cas) printf("%c\n", center);continue;
        }
        //cout << center << surround << endl;
        for(int i = 0; i <= n; i++)
            for(int j = 0; j <= n; j++)
            graph[i][j] = ' ';
        graph[n/2][n/2] = center;
        for(int i=1; i<=n/2; i++)
        {
            int temp = n/2-i;
            if(temp==-1) break;
            char ch;
            if(i%2==1)
            {
                ch = surround;
            }
            else ch = center;
            for(int j=0; j<2*i+1; j++)
            {
                graph[temp][temp+j] = ch;
            }
            int tt = n/2+i;
            for(int j=0; j<2*i+1; j++)
            {
                graph[tt][temp+j] = ch;
            }
            int t3 = n/2-i;
            for(int j=0; j<2*i+1; j++)
            {
                graph[t3+j][t3] = ch;
            }
            int t4 = n/2+i;
            for(int j=0; j<2*i+1; j++)
            {
                graph[t3+j][t4] = ch;
            }
        }
        graph[0][0] = ' ';
        graph[0][n-1] = ' ';
        graph[n-1][0] = ' ';
        graph[n-1][n-1] = ' ';
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                printf("%c", graph[i][j]);
            }
            printf("\n");
        }
        //printf("\n");
        cas++;
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值