[hdu5319]二进制表示,简单模拟

题意:给一个矩形,矩形里面画了4种符号,‘.‘表示没画线,‘R‘表示画了红线,‘B‘表示画了蓝线,‘G‘表示红线和蓝线同时画了,并且矩形主对角线上只能画红线,副对角线上只能画蓝线,问最少画多少条线才能形成给定的矩形的涂色情况。

思路:实际上给定的矩形唯一对应一种画线图案,‘.‘对应的格子没任何线,‘R‘对应的格子只有一条左上至右下的线,‘B‘对应的格子只有一条左下至右上的线,‘G‘对应的格子左上至右下、左下至右上都有线,那么问题转化为求最后的线条图案形成了多少线段。具体来说,只需要对每条对角线(包括主副)都从前之后扫一遍,如果对角线方向有线而它的前一个没这种方向的线或者它本身是第一个的话,答案加1。个人觉得用2位二进制来表示这种状态比较优雅。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

/* ******************************************************************************** */

#include <iostream>                                                                 //

#include <cstdio>                                                                   //

#include <cmath>                                                                    //

#include <cstdlib>                                                                  //

#include <cstring>                                                                  //

#include <vector>                                                                   //

#include <ctime>                                                                    //

#include <deque>                                                                    //

#include <queue>                                                                    //

#include <algorithm>                                                                //

using namespace std;                                                                //

                                                                                    //

#define pb push_back                                                                //

#define mp make_pair                                                                //

#define X first                                                                     //

#define Y second                                                                    //

#define all(a) (a).begin(), (a).end()                                               //

#define foreach(i, a) for (typeof(a.begin()) it = a.begin(); it != a.end(); it ++)  //

                                                                                    //

void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}    //

void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>                    //

void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;          //

while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>      //

void print(const T t){cout<<t<<endl;}template<typename F,typename...R>              //

void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>   //

void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}   //

                                                                                    //

typedef pair<intint> pii;                                                         //

typedef long long ll;                                                               //

typedef unsigned long long ull;                                                     //

                                                                                    //

/* -------------------------------------------------------------------------------- */

                                                                                    //

template<typename T>bool umax(T &a, const T &b) {

    return a >= b? false : (a = b, true);

}

int a[123][123];

char s[123];

int main() {

#ifndef ONLINE_JUDGE

    freopen("in.txt""r", stdin);

#endif // ONLINE_JUDGE

    int T;

    cin >> T;

    while (T --) {

        int n, m;

        RI(n);

        memset(a, 0, sizeof(a));

        for (int i = 1; i <= n; i ++) {

            scanf("%s", s);

            m = strlen(s);

            for (int j = 0; j < m; j ++) {

                if (s[j] == ‘.‘continue;

                a[i][j + 1] = s[j] == ‘R‘? 1 : (s[j] == ‘B‘? 2 : 3);

            }

        }

        n = max(n, m);

        int ans = 0;

        for (int i = 1; i <= n; i ++) {

            for (int j = 1; j <= i; j ++) {

                if (a[i - j + 1][j] & 2) {

                    if (!(a[i - j + 2][j - 1] & 2)) ans ++;

                }

            }

        }

        for (int i = 2; i <= n; i ++) {

            for (int j = i; j <= n; j ++) {

                if (a[n - j + i][j] & 2) {

                    if (!(a[n - j + i + 1][j - 1] & 2)) ans ++;

                }

            }

        }

        for (int i = 1; i <= n; i ++) {

            for (int j = i; j <= n; j ++) {

                if (a[j - i + 1][j] & 1) {

                    if (!(a[j - i][j - 1] & 1)) ans ++;

                }

            }

        }

        for (int i = 2; i <= n; i ++) {

            for (int j = 1; j <= n - i + 1; j ++) {

                if (a[i + j - 1][j] & 1) {

                    if (!(a[i + j - 2][j - 1] & 1)) ans ++;

                }

            }

        }

        cout << ans << endl;

    }

    return 0;                                                                       //

}                                                                                   //

                                                                                    //

                                                                                    //

                                                                                    //

/* ******************************************************************************** */

时间: 2024-08-03 04:25:39

[hdu5319]二进制表示,简单模拟的相关文章

HDU-1034-Candy Sharing Game(C++ &amp;&amp; 简单模拟)

Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3703    Accepted Submission(s): 2311 Problem Description A number of students sit in a circle facing their teacher in the cent

Jquery源码分析与简单模拟实现

前言 最近学习了一下jQuery源码,顺便总结一下,版本:v2.0.3 主要是通过简单模拟实现jQuery的封装/调用.选择器.类级别扩展等.加深对js/Jquery的理解. 正文 先来说问题: 1.jQuery为什么能使用$的方式调用,$是什么.$()又是什么.链式调用如何实现的 2.jQuery的类级别的扩展内部是怎样实现的,方法级别的扩展有是怎样实现的,$.fn又是什么 3.jQuery选择器是如何执行的,又是如何将结果包装并返回的 带着这些问题,我们进行jquery的模拟实现,文章下方有

Linux 内核 链表 的简单模拟(2)

接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ #define list_for_each(pos, head) for (pos = (head)->next; pos != (head);

HDU 1048 What Is Your Grade? (简单模拟)

 What Is Your Grade? Problem Description "Point, point, life of student!" This is a ballad(歌谣)well known in colleges, and you must care about your score in this exam too. How many points can you get? Now, I told you the rules which are used in

JavaWeb学习总结(四十九)——简单模拟Sping MVC

在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: 1 /** 2 * 使用Controller注解标注LoginUI类 3 */ 4 @Controller 5 public class LoginUI { 6 7 //使用RequestMapping注解指明forward1方法的访问路径 8 @RequestMapping("LoginUI/Lo

简单模拟Hibernate的主要功能实现

在学习期间接触到Hibernate框架,这是一款非常优秀的O/R映射框架,大大简化了在开发web项目过程中对数据库的操作.这里就简单模拟其底层的实现. /*******代码部分,及其主要注解**********************/1.实体类User:public class User {    private int id;    private String username;    private String password; public int getId() {       

ZOJ 3804 YY&#39;s Minions (简单模拟)

1 /* 2 题意:一个矩阵中有 n*m个宠物,每一个宠物都有一个状态, 1醒着的,0睡着的 3 X离开的!如果这个宠物(醒着的)的周围醒着的个数>3 || <2它就会睡着, 4 如果这个宠物(睡着的)的周围醒着的个数==3就会醒来! 5 每一分钟都会有变换一个状态! 6 其中会有些宠物会在给定的时间内离开! 7 */ 8 #include<iostream> 9 #include<cstring> 10 #include<cstdio> 11 #inclu

Codeforces Round #259 (Div. 2) (简单模拟实现题)

题目链接:http://codeforces.com/problemset/problem/454/A A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine

HDU 4891 简单模拟

The Great Pan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1035    Accepted Submission(s): 355 Problem Description As a programming contest addict, Waybl is always happy to take part in vario

一个简单的Webservice的demo,简单模拟服务

前段时间一直在学习WCF,匆匆忙忙的把<WCF全面解析>和<WCF服务编程>看了一遍,好多东西都不是很懂,又听了一下WCF分布式开发的网络教程,算是马马虎虎的明白点了.回顾了一下Webservice,将二者进行比较学习.考虑到以后的发展,当时决定学习WCF,希望自己在不久的将来能将WCF学的稍微精通点吧.这几天又将Webservice看了一遍,回想当时学习Webservice处处碰到坑,由于没人指点,连最基本地点发布都折腾好长时间,只能一点一点的填坑跳坑.这几天闲了,想写一个简单的