HDU 5319(2015多校3)-Painter(dfs)

16 篇文章 0 订阅

题目地址:HDU 5319
题意:给一个图n*m,原来全是点(’.’)。 现在要把图染成已给出的样子。 要求当是’\’的情况只用红色,是’/’的情况只用蓝色,当一个格子同时被红色和蓝色染得时候变成绿色。(每个格子只画一次)。
思路:这题只要模拟一下刷的过程就行了,如果出现了R,就刷R刷到底,出现B就刷B,出现G就左右各刷一次。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-8;
char str[60][60];
int n,m;
void dfs_R(int x, int y) {
    if (x>=0&&x<n&&y>=0&&y<m&&(str[x][y]=='R'||str[x][y]=='G')) {
        if (str[x][y]=='G')
            str[x][y]='B';
        else
            str[x][y]='.';
        dfs_R(x-1,y-1);
        dfs_R(x+1,y+1);
    }
    return ;
}
void dfs_B(int x, int y) {
    if(x>=0&&x<n&&0<=y&&y<m&&(str[x][y]=='B'||str[x][y]=='G')) {
        if(str[x][y]=='G')
            str[x][y]='R';
        else
            str[x][y]='.';
        dfs_B(x+1,y-1);
        dfs_B(x-1,y+1);
    }
    return ;
}
int main() {
    int T;
    int cnt;
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%s",str[i]);
        m=strlen(str[0]);
        cnt=0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (str[i][j]=='R') {
                    dfs_R(i, j);
                    cnt++;
                } else if (str[i][j]=='B') {
                    dfs_B(i, j);
                    cnt++;
                } else if (str[i][j]=='G') {
                    dfs_R(i, j);
                    str[i][j]='B';
                    dfs_B(i, j);
                    cnt+= 2;
                }
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rocky0429

一块也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值