关于昨天CCF软件认证上的ACS|| Art的题目

DeliverLee 2015-12-21 05:36:42
题目:(凭记忆)
输入:
m,n,q (m表示图的长度,n表示图的高度,q表示操作次数)
x1,y1,x2,y2(表示点(x1,y1)和(x2,y2))
x,y,c(表示点(x,y)和字符c)

作图分为两种:1.连线:当输入0时,表示连线
从点(x1,y1)连到(x2,y2)(仅有x1 = x2且y1≠y2或x1 ≠ x2且y1 = y2)(即只能画竖线|或横线-)
若有-与|交汇,则用+表示
2.填充:当输入1时,表示填充
从点(x,y)开始向四连通方向填充字符c,直到遇到边界或者已画的连线为止

样例输入:
4 4 5
0 0 0 3 0
0 0 3 3 3
0 0 0 0 3
0 3 0 3 3
1 1 1 A

样例输出:
+--+
|AA|
|AA|
+--+

下面是我写的代码,连线的功能已经解决了,填充的功能还没好,是模仿四连通填充算法写的,测试过会无限递归,看了一下午没想出哪里不对劲,求解答


#include <stdio.h>
#define MAX 100

char graph[MAX][MAX];
char c;
int weigh, heigh;

void fill(int x,int y)
{
if (x < weigh && y < heigh && x >= 0 && y >= 0 && graph[y][x] != '+' && graph[y][x] != '|' && graph[y][x] != '-')
{
graph[y][x] = c;
fill(x+1,y);
fill(x-1,y);
fill(x,y+1);
fill(x,y-1);
}
}

int main()
{
int count, choice;
int x1, y1, x2, y2;
int x, y;
int i, j;
scanf("%d %d %d", &weigh, &heigh, &count);
for (i = 0; i < heigh; i++)
for (j = 0; j < weigh; j++)
graph[i][j] = '.';
while (count--)
{
scanf("%d", &choice);
switch (choice)
{
case 0:
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
if (y1 == y2)
{
if (graph[y1][x1] == '|')
graph[y1][x1] = '+';
else
graph[y1][x1] = '-';
if (graph[y2][x2] == '|')
graph[y2][x2] = '+';
else
graph[y2][x2] = '-';
while ((++x1) != x2)
graph[y1][x1] = '-';
}
else if (x1 == x2)
{
if (graph[y1][x1] == '-')
graph[y1][x1] = '+';
else
graph[y1][x1] = '|';
if (graph[y2][x2] == '-')
graph[y2][x2] = '+';
else
graph[y2][x2] = '|';
while ((++y1) != y2)
graph[y1][x1] = '|';
}
break;
case 1:
scanf("%d %d %c", &x, &y, &c);
fill(x,y);
break;
}
}
for (i = 0; i < heigh; i++)
for (j = 0; j < weigh; j++)
{
if (j == weigh-1)
printf("%c\n", graph[i][j]);
else
printf("%c", graph[i][j]);
}
return 0;
}

...全文
62 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
DeliverLee 2015-12-21
  • 打赏
  • 举报
回复

#include <stdio.h>
#define MAX 100

char graph[MAX][MAX];
char c;
int weigh, heigh;
int visited[MAX][MAX];

void fill(int x,int y)
{
    if (x < weigh && y < heigh && x >= 0 && y >= 0 && graph[y][x] != '+' && graph[y][x] != '|' && graph[y][x] != '-' && visited[y][x])
    {
        visited[y][x] = 0;
        graph[y][x] = c;
        fill(x+1,y);
        fill(x-1,y);
        fill(x,y+1);
        fill(x,y-1);
    }
}

int main()
{
    int count, choice;
    int x1, y1, x2, y2;
    int x, y;
    int i, j;
    scanf("%d %d %d", &weigh, &heigh, &count);
    for (i = 0; i < heigh; i++)
        for (j = 0; j < weigh; j++)
        {
            graph[i][j] = '.';
            visited[i][j] = 1;
        }
    while (count--)
    {
        scanf("%d", &choice);
        switch (choice)
        {
            case 0:
                scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
                if (y1 == y2)
                {
                    if (graph[y1][x1] == '|')
                        graph[y1][x1] = '+';
                    else
                        graph[y1][x1] = '-';
                    if (graph[y2][x2] == '|')
                        graph[y2][x2] = '+';
                    else
                        graph[y2][x2] = '-';
                    while ((++x1) != x2)
                        graph[y1][x1] = '-';
                }
                else if (x1 == x2)
                {
                    if (graph[y1][x1] == '-')
                        graph[y1][x1] = '+';
                    else
                        graph[y1][x1] = '|';
                    if (graph[y2][x2] == '-')
                        graph[y2][x2] = '+';
                    else
                        graph[y2][x2] = '|';
                    while ((++y1) != y2)
                        graph[y1][x1] = '|';
                }
                break;
            case 1:
                scanf("%d %d %c", &x, &y, &c);
                fill(x,y);
                break;
        }
    }
    for (i = 0; i < heigh; i++)
        for (j = 0; j < weigh; j++)
        {
            if (j == weigh-1)
                printf("%c\n", graph[i][j]);
            else
                printf("%c", graph[i][j]);
        }
    return 0;
}
CSDN保佑 一发上来就自己解决了 加了个visited[MAX][MAX]标志数组就好了

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧