列表

详情


NC24796. Grid

描述

Give you a rectangular gird which is h cells high and w cells wide.
Each grid is black initially. You can turn some grids into white.
A grid A(x,y) is connected with grid B if the coordinate of B is (x+1, y),(x-1, y),(x, y+1) or (x, y-1).
And your task is to propose a plan of the gird which has exactly n connected components of black part.
If there is no valid plan containing n connected components of black part, output -1.

输入描述

Three integers h, w, n as described above.

输出描述

Print h rows and w columns, '#' represents a black grid and '*' represents a white grid, indicating your solution.

示例1

输入:

1 10 5

输出:

#*#*#*#*#*

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C(clang 3.9) 解法, 执行用时: 3ms, 内存消耗: 380K, 提交时间: 2019-04-13 12:41:01

#include <stdio.h>
int main()
{
	long i,j,n,h,w;
	scanf("%ld %ld %ld",&h,&w,&n);
	if(2*n> h*w)
	{
		printf("-1\n");
		return 0;
	}
	for(i=0;i<h;i++)
	{for(j=0;j<w;j++)
	{
		{	if(i%2==0)
			{	if (j%2==0&&((n--)>0))printf("#");
			else printf("*");
			}
		
			else
			{	if (j%2!=0&&((n--)>0))printf("#");
			else printf("*");
			}
		}

	}	printf("\n");
	}


	return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 5ms, 内存消耗: 488K, 提交时间: 2019-04-13 10:16:19

#include<bits/stdc++.h>
using namespace std;
int h,w,n,tp;
int main()
{
	scanf("%d%d%d",&h,&w,&n);
	if((h*w+1)/2<n){puts("-1");return 0;}
	for(int i=1;i<=h;i++)
	{
		tp^=1;
		for(int j=1;j<=w;j++)
		if(!n)printf("*");
		else if((j&1)==tp)printf("#"),n--;
		else printf("*");
		puts("");
	}
}

C++14(g++5.4) 解法, 执行用时: 7ms, 内存消耗: 492K, 提交时间: 2019-04-13 15:22:54

#include<iostream>
using namespace std;
int h,w,n,i,j;
int main()
{
	cin>>h>>w>>n;
	if(h*w%2==0&&h*w/2>=n||h*w%2&&h*w/2>=n-1)
		for(;i<h;i++)
			for(j=0;j<w;j++)
				n?((i+j)%2?putchar('*'):(n--,putchar('#'))):putchar('*'),j==w-1?puts(""):0;
	else
		puts("-1");
}

上一题