首页 > 其他 > 详细

HDU 5319 Painter

时间:2015-07-29 14:08:40      阅读:191      评论:0      收藏:0      [点我收藏+]

Painter

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 634    Accepted Submission(s): 291


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.
 

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

Sample Output
3 6
 

Source



题目大意:给出一个矩形是矩形而不是正方形,即长和宽是不一定相等的,在这个矩形当中包含4种字符,其中

字符‘.‘表示没有画线,‘R‘表示画了红线,‘B‘表示画了蓝线,‘G‘表示红线和蓝线同时画了,每一个位置最多画过两次

线,并且画的红线(\)是从左上到右下画出的,画的蓝线(/)是从左下到右上画出的,问至少要画多少条线才能形

成给定的矩形的涂色情况。


解题思路:根据题目描述可以确定给定的矩形是唯一对应一种画线图案的,‘.‘对应的小格子没任何线,‘R‘对应的小格

子只有一条左上至右下的线,‘B‘对应的小格子只有一条左下至右上的线,‘G‘对应的格子左上至右下、左下至右上都有

线,那么问题就转化为求最后的线条图案形成了多少个线段。具体来说,只需要对图案中每条对角线都从前往后扫一

遍,如果当前小方格上有线而它的前一个位置没有当前位置上的线或者当前小方格是第一个,那么结果加1。


例如:





代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-4)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1e9+7
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
int main()
{
    int i,j,k,m,n,t,a[55][55];
    char s[55];
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));//初始地图值0
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            m=strlen(s);
            for(j=0;j<m;j++)
            {
                if(s[j]=='R')//红线赋值1
                    a[i+1][j+1]=1;
                if(s[j]=='B')//蓝线赋值2
                    a[i+1][j+1]=2;
                if(s[j]=='G')//红蓝都有赋值3
                    a[i+1][j+1]=3;
            }
        }
        /*
        //输出地图,最外周赋值为0,作为判定周边为起点
        for(i=0;i<=n+1;i++)
        {
            for(j=0;j<=m+1;j++)
            {
                printf("%d",a[i][j]);
            }
            printf("\n");
        }
        */
        n=max(n,m);//
        int sum=0;//
        //遍历矩形左上三角,即蓝线
        //printf("矩形左上三角:\n");
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=i;j++)
            {
                //printf("[%d,%d]\n",i-j+1,j);//
                if(a[i-j+1][j]&2)
                {
                    if(!(a[i-j+2][j-1]&2))
                        sum++;
                }
            }
        }
        //遍历矩形右下三角,即蓝线
        //printf("矩形右下三角:\n");
        for(i=2;i<=n;i++)
        {
            for(j=i;j<=n;j++)
            {
                //printf("[%d,%d]\n",n-j+i,j);//
                if(a[n-j+i][j]&2)
                {
                    if(!(a[n-j+i+1][j-1]&2))
                        sum++;
                }
            }
        }
        //遍历矩形右上三角,即红线
        //printf("矩形右上三角:\n");
        for(i=1;i<=n;i++)
        {
            for(j=i;j<=n;j++)
            {
                //printf("[%d,%d]\n",j-i+1,j);//
                if(a[j-i+1][j]&1)
                {
                    if(!(a[j-i][j-1]&1))
                        sum++;
                }
            }
        }
        //遍历矩形左下三角,即红线
        //printf("矩形左下三角:\n");
        for(i=2;i<=n;i++)
        {
            for(j=1;j<=n-i+1;j++)
            {
                //printf("[%d,%d]\n",i+j-1,j);//
                if(a[i+j-1][j]&1)
                {
                    if(!(a[i+j-2][j-1]&1))
                        sum++;
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5319 Painter

原文:http://blog.csdn.net/yanghuaqings/article/details/47124865

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!