解题报告 之 HDU5319 Painter

本文介绍了一道经典的编程题——HDU5319 Painter的解题思路与实现方法。该题要求计算绘制特定图案所需的最少画笔次数。文章通过详细分析并采用深度优先搜索算法来解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解题报告 之 HDU5319 Painter


Description

Mr. Hdu is an painter, as we all know, painters need ideas to innovate , one day, he got stuck in rut and the ideas dry up, he took out a drawing board and began to draw casually. Imagine the board is a rectangle, consists of several square grids. He drew diagonally, so there are two kinds of draws, one is like ‘\’ , the other is like ‘/’. In each draw he choose arbitrary number of grids to draw. He always drew the first kind in red color, and drew the other kind in blue color, when a grid is drew by both red and blue, it becomes green. A grid will never be drew by the same color more than one time. Now give you the ultimate state of the board, can you calculate the minimum time of draws to reach this state.
 

Input

The first line is an integer T describe the number of test cases. 
Each test case begins with an integer number n describe the number of rows of the drawing board. 
Then n lines of string consist of ‘R’ ‘B’ ‘G’ and ‘.’ of the same length. ‘.’ means the grid has not been drawn. 
1<=n<=50 
The number of column of the rectangle is also less than 50. 
Output 
Output an integer as described in the problem description. 
 

Output

Output an integer as described in the problem description.
 

Sample Input

2 4 RR.B .RG. .BRR B..R 4 RRBB RGGB BGGR BBRR
 

Sample Output

3 6

题目大意:一张图上每个格子有无色,红色, 蓝色,绿色四种颜色。无色是没涂,红色是从左上到右下斜着涂(\),蓝色是从右上到左下斜着涂(/),绿色是红色和蓝色交叠的部分,一个格子不可能被同一种颜色涂两次,问最少要涂几笔才能涂成这样?

分析:一开始没看到方向限定,感觉是很难的一道题,想不出来又读题之后发现是一道水题。其实就是一道搜索,之前有一个很经典的问题是问说一块地皮上有几个油田。具体方法是从上到下,从左到右扫描。遇到颜色之后则根据不同颜色往左下或者右下一直搜索,直到没有为止,搜索过程中将路过的格子全部去掉对应颜色。然后每次新遇到一个颜色表示是新的一笔,ans++即可。

上代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

const int MAXN = 50 + 10;
char map[MAXN][MAXN];
int n, m;

void dfs( int i, int j,char key )
{
	if(map[i][j] != key&&map[i][j] != 'G') return;

	if(map[i][j] == key)
		map[i][j] = '.';
	else if(map[i][j] == 'G')
	{
		if(key == 'R') map[i][j] = 'B';
		else	map[i][j] = 'R';
	}

	if(key == 'R')
		dfs( i + 1, j + 1, key );
	else if(key == 'B')
		dfs( i + 1, j - 1, key );
}

int main()
{
	int kase;
	cin >> kase;
	while(kase--)
	{
		int ans = 0;
		scanf( "%d", &n );
		for(int i = 0; i < n; i++)
		{
			scanf( "%s", map[i] );
		}
		m = strlen( map[0] );
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < m; j++)
			{
				if(map[i][j] == 'R')
				{
					ans++;
					dfs( i, j, 'R' );
				}
				else if(map[i][j] == 'B')
				{
					ans++;
					dfs( i, j, 'B' );
				}
				else if(map[i][j] == 'G')
				{
					ans += 2;
					dfs( i, j, 'R' );
					dfs( i, j, 'B' );
				}
			}
		}
		printf( "%d\n", ans );
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值