Last active
December 2, 2017 05:19
-
-
Save rvndbalaji/6e1206d133f82d784f0d87436daa4d81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* package codechef; // don't place package name! */ | |
import java.util.Scanner; | |
import java.util.Stack; | |
/* Name of the class has to be "Main" only if the class is public. */ | |
class Codechef | |
{ | |
public static void main (String[] args) throws java.lang.Exception | |
{ | |
Scanner sc = new Scanner(System.in); | |
int T = sc.nextInt(); | |
for (int i = 0; i < T; i++) | |
{ | |
int N = sc.nextInt(); | |
int M = sc.nextInt(); | |
int downcost = 0; | |
int upcost = 0; | |
Stack<Character> stack = new Stack<>(); | |
String strLine = ""; | |
for (int j = 0; j < N; j++) | |
{ | |
strLine = strLine + sc.next(); | |
} | |
char line[] = strLine.toCharArray(); | |
char top = line[0]; | |
stack.push(top); | |
for (int j = 1; j < line.length ; j++) | |
{ | |
if(line[j]==top) | |
{ | |
if(line[j]=='R') | |
{ | |
upcost = upcost + 5; | |
top = 'G'; | |
} | |
else | |
{ | |
upcost = upcost + 3; | |
top = 'R'; | |
} | |
stack.push(line[j]); | |
continue; | |
} | |
top = line[j]; | |
stack.push(line[j]); | |
} | |
char prev = stack.pop(); | |
while (!stack.isEmpty()) | |
{ | |
char cur = stack.pop(); | |
if(cur==prev) | |
{ | |
if(cur=='R') | |
{ | |
downcost = downcost + 5; | |
prev = 'G'; | |
} | |
else | |
{ | |
downcost = downcost + 3; | |
prev = 'R'; | |
} | |
continue; | |
} | |
prev = cur; | |
} | |
System.out.println((downcost<upcost)?downcost:upcost); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment