NOIP 2008 Vijos 1497 立体图 模拟

在Vijos上编译成功

测试数据 #0: Accepted, time = 0 ms, mem = 1340 KiB, score = 10

测试数据 #1: Accepted, time = 0 ms, mem = 1336 KiB, score = 10

测试数据 #2: Accepted, time = 0 ms, mem = 1336 KiB, score = 10

测试数据 #3: Accepted, time = 0 ms, mem = 1328 KiB, score = 10

测试数据 #4: Accepted, time = 0 ms, mem = 1336 KiB, score = 10

测试数据 #5: Accepted, time = 0 ms, mem = 1332 KiB, score = 10

测试数据 #6: Accepted, time = 15 ms, mem = 1336 KiB, score = 10

测试数据 #7: Accepted, time = 0 ms, mem = 1340 KiB, score = 10

测试数据 #8: Accepted, time = 0 ms, mem = 1332 KiB, score = 10

测试数据 #9: Accepted, time = 0 ms, mem = 1336 KiB, score = 10

Accepted, time = 15 ms, mem = 1340 KiB, score = 100

这道题看起来比较难,但实际上还是比较简单的。

首先我们要画出来一个立方体。

void draw(int x, int y)
{
	int i, j;
	for(i = y + 1; i < y + 4; i++)
		canvas[x][i] = canvas[x - 3][i] = canvas[x - 5][i + 2] = '-';
	for(i = x - 1; i > x - 3; i--)
		canvas[i][y] = canvas[i][y + 4] = canvas[i - 2][y + 6] = '|';
	for(i = x - 1; i > x - 3; i--)
		for(j = y + 1; j < y + 4; j++)
			canvas[i][j] = ' ';
	for(i = x - 2; i > x - 4; i--)
		canvas[i][y + 5] = ' ';
	for(i = y + 2; i < y + 5; i++)
		canvas[x - 4][i] = ' ';
	canvas[x][y] = canvas[x - 3][y] = canvas[x][y + 4] = canvas[x - 3][y + 4] = '+';
	canvas[x - 4][y + 1] = canvas[x - 4][y + 5] = canvas[x - 1][y + 5] = '/';
	canvas[x - 5][y + 2] = canvas[x - 5][y + 6] = canvas[x - 2][y + 6] = '+';
}

draw(x,y)表示从在画布上左小角坐标为x,y画立方体。

实际上也可以用常量定义立方体,然后复制到数组里。

然后我们要确定画布的长和宽。

长比较好求,因为每个格子都回至少有1个立方体,现在就是要确定宽。

	width = 4 * n + 1 + 2 * m;
	height = 0;
	for(i = 1; i <= m; i++)
		for(j = 1; j <= n; j++)
			height = max(height, hei[i][j] * 3 + 1 + 2 * (m - i) + 2);


意思比较好理解了。

hei是每个格子的高度。

height是宽,width是长。

然后初始化画布。

现在就是在哪里画立方体。

	for(i = 1; i <= m; i++)
		for(j = 1; j <= n; j++)
		{
			for(k = 1; k <= hei[i][j]; k++)
				draw(height - 2 * (m - i) - ((k-1)*3), 4 * (j - 1) + 1 + (m - i) * 2);
		}



i和j是格子的坐标,k是格子的高度

height-2*(m-i)-((k-1)*3)表示x坐标,4*(j-1)+1+(m-i)*2表示y坐标。

最后打印画布即可。


#include <stdio.h>
#define max(a,b) (a)>(b)?(a):(b)
#define M 50
#define N 50


int n, m;
int i, j, k;
int p, q;
int width, height;
int hei[M][N];
char canvas[900][900];

void draw(int x, int y)
{
	int i, j;
	for(i = y + 1; i < y + 4; i++)
		canvas[x][i] = canvas[x - 3][i] = canvas[x - 5][i + 2] = '-';
	for(i = x - 1; i > x - 3; i--)
		canvas[i][y] = canvas[i][y + 4] = canvas[i - 2][y + 6] = '|';
	for(i = x - 1; i > x - 3; i--)
		for(j = y + 1; j < y + 4; j++)
			canvas[i][j] = ' ';
	for(i = x - 2; i > x - 4; i--)
		canvas[i][y + 5] = ' ';
	for(i = y + 2; i < y + 5; i++)
		canvas[x - 4][i] = ' ';
	canvas[x][y] = canvas[x - 3][y] = canvas[x][y + 4] = canvas[x - 3][y + 4] = '+';
	canvas[x - 4][y + 1] = canvas[x - 4][y + 5] = canvas[x - 1][y + 5] = '/';
	canvas[x - 5][y + 2] = canvas[x - 5][y + 6] = canvas[x - 2][y + 6] = '+';
}

int main()
{
	scanf("%d%d", &m, &n);
	for(i = 1; i <= m; i++)
		for(j = 1; j <= n; j++)
			scanf("%d", &hei[i][j]);
	width = 4 * n + 1 + 2 * m;
	height = 0;
	for(i = 1; i <= m; i++)
		for(j = 1; j <= n; j++)
			height = max(height, hei[i][j] * 3 + 1 + 2 * (m - i) + 2);
	for(i = 1; i <= height; i++)
		for(j = 1; j <= width; j++)
			canvas[i][j] = '.';
	for(i = 1; i <= m; i++)
		for(j = 1; j <= n; j++)
		{
			for(k = 1; k <= hei[i][j]; k++)
				draw(height - 2 * (m - i) - ((k-1)*3), 4 * (j - 1) + 1 + (m - i) * 2);
		}
	for(p = 1; p <= height; p++)
	{
		for(q = 1; q <= width; q++)
			putchar(canvas[p][q]);
		putchar('\n');
	}
	return 0;
}



你可能感兴趣的:(vijos)