hdu 5319(多校联合赛)

本文介绍了一道关于画家在网格上染色的算法题目,通过模拟染色过程,求解达到特定状态所需的最少染色次数。文章给出了详细的题目描述及解决方案。

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


                                                                                              painter

Problem 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.
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5319

大概题意:

     输入一个长为n宽度自定的矩形表格,在每个单位表格中染色,主对角线方向染红色,副对角线方向染蓝色,相同颜色不能进行重复染色,如果一个单位表格中既有红色又有蓝色,则显示为绿色,给出染色结果,求最小的染色次数。


题目分析:

     模拟题,根据结果倒推答案。怎样求最小的染色次数呢?每一次可以在对角线上染任意数量的表格,每次还原到所能还原的最长对角线即为最短次数。

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s[55];
int n;
int simulation(){
    int tot = 0;
    int m = s[0].size();
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(s[i][j] == 'R'){
                ++tot;
                for(int k = 1; k + i < n && k + j < m; ++k){    //红色还原主对角线;
                    if(s[k + i][k + j] == 'R')
                        s[k + i][k + j] = '.';
                    else if(s[k + i][k + j] == 'G')
                        s[k + i][k + j] = 'B';
                    else 
                        break;
                }
            }
            else if(s[i][j] == 'B'){
                ++tot;
                for(int k = 1; k + i < n && j - k >= 0; ++k){   //蓝色还原副对角线;
                    if(s[k + i][j - k] == 'B')
                        s[k + i][j - k] = '.';
                    else if(s[k + i][j - k] == 'G')
                        s[k + i][j - k] = 'R';
                    else        //到达所能还原的最长长度,结束;                   
                        break;
                }
            }
            else if(s[i][j] == 'G'){
                tot += 2;
                for(int k = 1; k + i < n && k + j < m; ++k){    //绿色相当于染色两次,一次红色,一次蓝色;
                     if(s[k + i][k + j] == 'R')
                        s[k + i][k + j] = '.';
                    else if(s[k + i][k + j] == 'G')
                        s[k + i][k + j] = 'B';
                    else
                        break;
                }
                for(int k = 1; k + i < n && j - k >= 0; ++k){
                    if(s[k + i][j - k] == 'B')
                        s[k + i][j - k] = '.';
                    else if(s[k + i][j - k] == 'G')
                        s[k + i][j - k] = 'R';
                    else
                        break;
                }
            }
        }
    }
    return tot;
}
int main()
{
    int T;
    cin >> T;
    while(T--){
        cin >> n;
        for(int i = 0; i < n; ++i){
            cin >> s[i];
        }
        int ans = simulation();
        cout << ans << endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值