zoukankan      html  css  js  c++  java
  • 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
    ", ans);
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    单独的数字
    设计模式之原型模式
    设计模式之策略模式
    设计模式之单例模式
    泛型入门
    iOS中关于.pch的新建与配置问题
    iOS开发中遇到的头文件找不到的问题解决办法
    iOS中NSJSONSerialization的使用 小记
    6.线程、进程、协程基础篇
    5.装饰器进阶篇
  • 原文地址:https://www.cnblogs.com/Apro/p/4695752.html
Copyright © 2011-2022 走看看