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

来源:互联网 发布:ubuntu服务器密码修改 编辑:程序博客网 时间:2024/05/29 09:58

题目地址: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 0
原创粉丝点击