HDU 5319 Painter

题意:红色从左上向右下涂,蓝色从右上向左下涂,既涂红色又涂蓝色就变成绿色,问最少涂几下能变成给的图。

 

解法:模拟一下就好了,注意细节。

 

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int main()
{
    int T;
    while(~scanf("%d", &T))
    {
        while(T--)
        {
            int n;
            scanf("%d", &n);
            string s[55];
            for(int i = 0; i < n; i++)
                cin >> s[i];
            int m = s[0].size();
            int ans = 0;
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < m; j++)
                {
                    if(s[i][j] == 'R')
                    {
                        ans++;
                        int x = i, y = j;
                        while(x < n && y < m && (s[x][y] == 'R' || s[x][y] == 'G'))
                        {
                            if(s[x][y] == 'R')
                                s[x][y] = '.';
                            else
                                s[x][y] = 'B';
                            x++;
                            y++;
                        }
                    }
                    else if(s[i][j] == 'B')
                    {
                        ans++;
                        int x = i, y = j;
                        while(x < n && y >= 0 && (s[x][y] == 'B' || s[x][y] == 'G'))
                        {
                            if(s[x][y] == 'B')
                                s[x][y] = '.';
                            else
                                s[x][y] = 'R';
                            x++;
                            y--;
                        }
                    }
                    else if(s[i][j] == 'G')
                    {
                        ans += 2;
                        int x = i, y = j;
                        while(x < n && y < m && (s[x][y] == 'R' || s[x][y] == 'G'))
                        {
                            if(s[x][y] == 'R')
                                s[x][y] = '.';
                            else
                                s[x][y] = 'B';
                            x++;
                            y++;
                        }
                        x = i, y = j;
                        while(x < n && y >= 0 && (s[x][y] == 'B' || s[x][y] == 'G'))
                        {
                            if(s[x][y] == 'B')
                                s[x][y] = '.';
                            else
                                s[x][y] = 'R';
                            x++;
                            y--;
                        }
                    }
                }
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}

  

posted @ 2015-08-02 14:45  露儿大人  阅读(153)  评论(0编辑  收藏  举报