HDU 5319 Painter (2015 Multi-University Training Contest 3 2015多校聯合)

2015 Multi-University Training Contest 3  杭電2015多校聯合的題~題意是一個人要畫畫,要從白紙畫成輸入的樣子。R是紅色、B是藍色、G是綠色。G是由紅色和藍色組成的。然後紅色只能\這樣畫,藍色只能/這樣畫。(不要想太多,就是斜率爲1或-1)'.'是沒畫的。問你畫成輸入的圖,需要畫幾筆。這裏有個坑,輸入的是行數,並不包括列數。列是根據輸入的圖畫而定的。題目輸入起到了誤導的作用- -成功的認爲是正方形,然而是矩形- -好吧。。題目描述的很清楚,人家說的很明白是矩形,輸入的是行數,只怪自己咯╮(╯◇╰)╭ 白WA了好幾次,給出題人跪了Orz。

思路呢,我和官方題解的不太一樣~其實思路有很多啦。我的思路是用dfs的思想~ 也就是遞歸。我寫了兩個dfs函數,分別代表畫紅色和畫藍色的。(其實也能合併起來啦,不過還是分開寫清楚一點。。) dfs函數裏面的內容呢,用紅色來舉例子。首先傳上去x,y的值,也就是座標【我是從數組下標爲1開始的,並不是0,後面會說原因】。首先寫跳出條件,如果x>n或者y>n,那麼return跳出。否則的話,我們就要去看右下角的那一個,也就是dfs(x+1,y+1)。那麼怎麼統計筆畫數呢?因爲G是R和B組成的,所以既能當成R也能當成B。那麼我們想,只有當你當前dfs的這個位置是R或者G,並且下一個既不是R也不是G的時候,筆畫數+1,所以把這句加進去加以判斷就可以了,但是無論成立不成立,依然是要繼續看右下角的那一個的,也就是說,無論什麼情況,接下來都要進行dfs(x+1,y+1),除非越界跳出dfs。藍色也是同樣,只不過是看左下角的那一個,即dfs(x+1,y-1)。現在來解釋一下爲什麼下標從0開始。因爲首先我要把圖畫初始化成'.',也就是說,當你輸入了圖畫之後,其實在數組裏,圖畫周圍是被'.'包圍的。這樣的話,就適用於上面的”只有當目前是R或G(或者B或G),而下一個不是“的判斷了,因爲如果下一個越界了,它是'.',適用於判斷,然後再次dfs的時候,就會因爲越界被return。

Painter

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


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
題目鏈接:HDU5319 Painter

下面上AC代碼(15ms):

Problem : 5319 ( Painter )     Judge Status : Accepted
RunId : 14228528    Language : G++    Author : Moressette
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char maze[55][55];
__int64 res=0;
int len;
int n;
void dfs1(int x,int y)
{
    if(x>n||y>len)
        return;
    else
    {
        if(maze[x][y]=='R' || maze[x][y]=='G')
        {
            if(maze[x+1][y+1]!='R' && maze[x+1][y+1]!='G')
                res++;
        }
    }
    dfs1(x+1,y+1);
}
void dfs2(int x,int y)
{
    if(x>n||y<1)
        return;
    else
    {
        if(maze[x][y]=='B' || maze[x][y]=='G')
        {
           if(maze[x+1][y-1]!='B' && maze[x+1][y-1]!='G')
                res++;
        }
    }
    dfs2(x+1,y-1);
}
int main()
{
    int T;
    string s;
    scanf("%d",&T);
    while(T--)
    {
        memset(maze,'.',sizeof(maze));
        scanf("%d",&n);
        res=0;
        int j=1;
        for(int i=1;i<=n;i++)
        {
            cin>>s;
            len=s.length();
            for(int j=1;j<=len;j++)
            {
                maze[i][j]=s[j-1];
            }
        }
        for(int i=1;i<=n;i++)
            dfs1(i,1);
        for(int i=2;i<=len;i++)
            dfs1(1,i);
        for(int i=1;i<=n;i++)
            dfs2(i,len);
        for(int i=len-1;i>=1;i--)
            dfs2(1,i);
        printf("%I64u\n",res);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章