zoukankan      html  css  js  c++  java
  • hdoj 5319 Painter(模拟题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5319

    思路分析:假设颜色R表示为1,颜色B表示为2,颜色G表示为3,因为数据量较小,采用暴力解法即可,即每次扫描对角线,看每条对角线需要画多少笔,统计所有对角线的笔数和即可;

    代码如下:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    const int MAX_N = 50 + 10;
    int map[MAX_N][MAX_N];
    
    int Solve(int n, int m)
    {
        // m 表示行, n表示列数
        int count = 0;
        bool flag = false;
        for (int k = m - 1; k >= 1 - n; -- k)
        {
            flag = false;
            for (int j = 0; j < n; ++ j)
            {
                int i = j + k;
                if (0 <= i && i < m && 0 <= j && j < n)
                {
                    if (map[i][j] & 1)
                    {
                        map[i][j] = map[i][j] - 1;
                        if (!flag)
                        {
                            flag = true;
                            count++;
                        }
                    }  else if ((map[i][j] & 1) == 0 && flag)
                        flag = false;
                }
            }
        }
        flag = false;
        int t = m + n;
        for (int k = 0; k < t; ++ k)
        {
            flag = false;
            for (int j = 0; j < n; ++ j)
            {
                int i = -j + k;
                if (0 <= i && i < m && 0 <= j && j < n)
                {
                    if (map[i][j] & 2)
                    {
                        map[i][j] = map[i][j] - 2;
                        if (!flag)
                        {
                            flag = true;
                            count++;
                        }
                    } else if ((map[i][j] & 2) == 0 && flag)
                        flag = false;
                }
            }
        }
        return count;
    }
    
    int main()
    {
        int case_times;
        char str[MAX_N];
        int n, m;
    
        scanf("%d", &case_times);
        while (case_times--)
        {
            scanf("%d", &n);
            memset(map, 0, sizeof(map));
            for (int i = 0; i < n; ++ i)
            {
                scanf("%s", str);
                int len = m = strlen(str);
                for (int j = 0; j < len; ++ j)
                {
                    if (str[j] == '.')
                        map[i][j] = 0;
                    if (str[j] == 'R')
                        map[i][j] = 1;
                    if (str[j] == 'B')
                        map[i][j] = 2;
                    if (str[j] == 'G')
                        map[i][j] = 3;
                }
            }
    
            int ans = Solve(m, n);
            printf("%d
    ", ans);
    
        }
        return 0;
    }
  • 相关阅读:
    VIM
    函数指针
    《BOOST程序库完全开发指南》 第13章 编程语言支持
    《BOOST程序库完全开发指南》 第01章 Boost程序库总论
    gtest
    《BOOST程序库完全开发指南》 第04章 实用工具
    distcc配置
    《BOOST程序库完全开发指南》 第08章 算法
    Makefile
    《BOOST程序库完全开发指南》 第03章 内存管理
  • 原文地址:https://www.cnblogs.com/tallisHe/p/4683643.html
Copyright © 2011-2022 走看看