https://www.codechef.com/DEC17/problems/GIT01

#include<cstdio>
#include<algorithm> using namespace std; #define N 101 char s[N]; int main()
{
int T;
scanf("%d",&T);
int n,m;
int OddG,OddR,EvenG,EvenR;
int ans;
while(T--)
{
OddG=OddR=EvenG=EvenR=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i)
{
scanf("%s",s+);
for(int j=;j<=m;++j)
{
if(s[j]=='G')
{
if((i+j)&) OddG++;
else EvenG++;
}
else
{
if((i+j)&) OddR++;
else EvenR++;
}
}
}
ans=*OddR+*EvenG;
ans=min(ans,*OddG+*EvenR);
printf("%d\n",ans);
}
}

All submissions for this problem are available.

Read problems statements in Mandarin chineseRussian andVietnamese as well.

Chef’s girlfriend's birthday is near, so he wants to surprise her by making a special cake for her. Chef knows that his girlfriend likes cherries on the cake, so he puts cherries on the top of the cake, but he was not satisfied. Therefore, he decided to replace some of the cherries to make a beautiful pattern. However, Chef has a lot of other work to do so he decided to ask for your help.

The cherries are of two colors red and green. Now Chef wants the cherries to be placed in such a way that each cherry of one color must be adjacent to only cherries of the other color, two cherries are adjacent if they share a side. Now Chef has asked for your help in making that pattern on the cake.

You can replace any cherry of given color with the other color. But there is a cost for each replacement: if you replace a red cherry with a green one, the cost is 5 units and if you replace a green cherry with a red one, the cost is 3 units.

Help your friend Chef by making the cake special with minimum cost.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains two space-separated integers N and M, where N × M are the dimensions of the cake.
  • Each of the next N lines contains a string of length M.

Output

For each test case, output the minimum cost required to make the cake special.

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ N, M ≤ 100
  • each string consists only of letters 'R' and 'G' denoting red and green cherries respectively

Example

Input:

2
4 5
RGRGR
GRGRG
RGRGR
GRGRG
2 3
RRG
GGR Output: 0
8

CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake的更多相关文章

  1. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

  2. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

    https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #incl ...

  3. CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds

    https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #includ ...

  4. CF&&CC百套计划2 CodeChef December Challenge 2017 Penalty Shoot-out

    https://www.codechef.com/DEC17/problems/CPLAY #include<cstdio> #include<algorithm> using ...

  5. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

  6. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence

    http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...

  7. CF&&CC百套计划1 Codeforces Round #449 C. Willem, Chtholly and Seniorious (Old Driver Tree)

    http://codeforces.com/problemset/problem/896/C 题意: 对于一个随机序列,执行以下操作: 区间赋值 区间加 区间求第k小 区间求k次幂的和 对于随机序列, ...

  8. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods

    http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...

  9. CF&&CC百套计划1 Codeforces Round #449 A. Nephren gives a riddle

    http://codeforces.com/contest/896/problem/A 第i个字符串嵌套第i-1个字符串 求第n个字符串的第k个字母 dfs #include<map> # ...

随机推荐

  1. beta冲刺(6/7)

    目录 组员情况 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内最新成果 团队签入记 ...

  2. Codeforces Round #235 (Div. 2) D. Roman and Numbers 状压dp+数位dp

    题目链接: http://codeforces.com/problemset/problem/401/D D. Roman and Numbers time limit per test4 secon ...

  3. sprint初步计划(第一天)

    一.现状 小组成员初步了解四则运算程序编写大概内容,进行简单的讨论.只知道大概的流程,实际还没做出.现在明确目标是把我们写Java的四则运算变成一个手机APP,关于手机ap,我们还不是很了解,所以需要 ...

  4. 关于javascript异步编程的理解

    在开发手机app的时候,要使用ajax想向后台发送数据.然后遇到了一个bug,通过这个bug,理解了ajax异步请求的工作原理.下面是登录页面的源代码. <!DOCTYPE html> & ...

  5. vue.js常用指令

    本文摘自:http://www.cnblogs.com/rik28/p/6024425.html Vue.js的常用指令 上面用到的v-model是Vue.js常用的一个指令,那么指令是什么呢? Vu ...

  6. Scala入门系列(三):数组

    Array 与Java的Array类似,也是长度不可变的数组,此外,由于Scala与Java都是运行在JVM中,双方可以互相调用,因此Scala数组的底层实际上是Java数组. 注意:访问数组中元素使 ...

  7. [转帖]22个必须学习的Linux安全命令

    22个必须学习的Linux安全命令 http://os.51cto.com/art/201808/581401.htm Linux系统的安全性涉及很多方面,从设置帐户到确保用户合法,限制比完成工作所需 ...

  8. myisam和innodb 删除一条记录后,再次添加后,最大记录值

  9. day02--Python基础二(基础数据类型)

    一.数据与数据类型 1 什么是数据? x=10,10是我们要存储的数据 2 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3 数据类型 数字(int) 字符串 ...

  10. 利用stream对map集合进行过滤

    最近公司在大张旗鼓的进行代码审核,从中也发现自己写代码的不好习惯.一次无意的点到了公司封装的对map集合过滤的方法,发现了stream.于是研究了一下.并对原有的代码再次结合Optional进行重构下 ...