RGYB(color) Slots Game 猜猜正确颜色的正确插槽
假设您有四个插槽,每个插槽将分别包含红色 (R)、黄色 (Y)、绿色 (G)、蓝色 (B)。例如,如果您选择 YGGR(插槽 1 为黄色,插槽 2 和 -3 为绿色,插槽 -4 为红色)。您事先不知道插槽的颜色。您将猜测颜色。例如,您可能会猜测 YRGB。
当您猜出正确插槽的正确颜色时,您会得到“命中” 如果您猜测的颜色存在但位于错误的插槽中,则会得到“伪命中:”请注意,命中的插槽永远不会算作伪命中。
给定一个猜测和一个解决方案,你的任务是编写一个程序来计算命中数和伪命中数。
例子:
Input: solution -> RGBY, Guess -> GGRR
Output: hit -> 1, pseudohit -> 1
Input: solution -> RGYB, Guess -> YGRR
Output: hit -> 1, pseudohit -> 2
Input: solution -> RGYB, Guess -> GGYR
Output: hit -> 2, pseudohit -> 1
一个简单的解决方案是同时遍历两个字符串并检查两个字符串的字符。如果两个字符串具有相同的字符,则它是一个命中,因此会增加命中数。如果字符串的字符在某个位置不匹配,则再次遍历解决方案字符串,以查看猜测中的字符是否出现在解决方案字符串中的任何位置,如果是则增加伪命中数。
时间复杂度: O(N*N) ,其中 N 是字符串的长度。
一个有效的解决方案是创建一个频率数组,该数组存储每个字符在解决方案中出现的次数,不包括插槽被“命中”的次数。然后,我们迭代guess来计算伪命中的数量。
以下是上述方法的实现:
C++
// CPP code to solve RGYB game
#include
#include
using namespace std;
int hits = 0;
int pseudoHits = 0;
// maximum no of colors are 4 RGYB
int MAX_COLORS = 4;
// Function to assign code to a slot
// based on the color they have
int codeOfColor(char c)
{
switch (c)
{
case 'B': return 0;
case 'G': return 1;
case 'R': return 2;
case 'Y': return 3;
default:
return -1;
}
}
// Function to calculate number of hits
// and pseudo hits
void calcResult(string guess, string solution)
{
hits = 0;
pseudoHits = 0;
if(guess.length() != solution.length())
cout<<"Incorrect Input"<= 0 && frequencies[codecolor] > 0
&& guess[i] != solution[i])
{
pseudoHits++;
frequencies[codecolor]--;
}
}
cout << "hits -> " << hits << " Pseudo hits -> "
<< pseudoHits << endl;
}
// Driver code to test above function
int main()
{
string solution = "GGRR";
string guess = "RGBY";
calcResult(solution, guess);
solution = "YGRR";
guess = "RGYB";
calcResult(solution, guess);
}
// This code is contributed by Sumit Ghosh
Java
// Java code to solve RGYB game
import java.util.*;
import java.lang.*;
import java.io.*;
class CalcRes
{
static int hits = 0;
static int pseudoHits = 0;
// Function to assign code to a slot
// based on the color they have
static int codeOfColor(char c)
{
switch (c)
{
case 'B': return 0;
case 'G': return 1;
case 'R': return 2;
case 'Y': return 3;
default:
return -1;
}
}
// maximum no of colors are 4 RGYB
static int MAX_COLORS = 4;
// Function to calculate number of hits
// and pseudo hits
static void calcResult(String guess, String solution)
{
hits = 0;
pseudoHits = 0;
if (guess.length() != solution.length())
System.out.println("Incorrect Input");
// frequency array to store how many times
// each character occurs in solution string
int[] frequencies = new int[MAX_COLORS];
// Compute hits and build frequency table
for (int i = 0; i < guess.length(); i++)
{
if (guess.charAt(i) == solution.charAt(i))
{
hits++;
}
else
{
/*
Only increment the frequency table (which will be
used for pseudo-hits) if it's not a hit. If it's a
hit, the slot has already been "used."
*/
int codeofcolor = codeOfColor(solution.charAt(i));
frequencies[codeofcolor]++;
}
}
// Compute pseudo-hits
for (int i = 0; i < guess.length(); i++)
{
int codeofcolor = codeOfColor(guess.charAt(i));
if (codeofcolor >= 0 && frequencies[codeofcolor] > 0 &&
guess.charAt(i) != solution.charAt(i))
{
pseudoHits++;
frequencies[codeofcolor]--;
}
}
System.out.println("hits -> " + hits +
" Pseudo hits -> " + pseudoHits);
}
// Driver Code
public static void main(String[] args)
{
String solution = "GGRR";
String guess = "RGBY";
calcResult(solution, guess);
solution = "YGRR";
guess = "RGYB";
calcResult(solution, guess);
}
}
Python3
# Python code to solve RGYB game
hits = 0
pseudoHits = 0
# maximum no of colors are 4 RGYB
MAX_COLORS = 4
# Function to assign code to a slot
# based on the color they have
def codeOfColor(c):
if c == 'B':
return 0;
else if c == 'G':
return 1;
else if c == 'R':
return 2;
else if c == 'Y':
return 3;
else:
return -1;
# Function to calculate number of hits
# and pseudo hits
def calcResult(guess, solution):
hits = 0
pseudoHits = 0
if (len(guess) != len(solution)):
print ("Incorrect Input")
# frequency array to store how many times
# each character occurs in solution string
frequencies = [0 for i in range(MAX_COLORS)]
# Compute hits and build frequency table
for i in range(len(guess)):
if (guess[i] == solution[i]):
hits += 1
else:
# Only increment the frequency table (which
# will be used for pseudo-hits) if it's not
# a hit. If it's a hit, the slot has already
# been "used."
codecolor = codeOfColor(solution[i])
frequencies[codecolor] += 1
# Compute pseudo-hits
for i in range(len(guess)):
codecolor = codeOfColor(guess[i])
if codecolor >= 0 and frequencies[codecolor] > 0 and guess[i] != solution[i]:
pseudoHits += 1
frequencies[codecolor] -= 1
print("hits -> ", hits, " Pseudo hits -> ", pseudoHits)
# Driver code to test above function
solution = "GGRR"
guess = "RGBY"
calcResult(solution, guess)
solution = "YGRR"
guess = "RGYB"
calcResult(solution, guess)
#This code is contributed by Sachin Bisht
C#
// C# code to solve RGYB game
using System;
class GFG {
static int hits = 0;
static int pseudoHits = 0;
// Function to assign code to a slot
// based on the color they have
static int codeOfColor(char c)
{
switch (c)
{
case 'B': return 0;
case 'G': return 1;
case 'R': return 2;
case 'Y': return 3;
default: return -1;
}
}
// maximum no of colors are 4 RGYB
static int MAX_COLORS = 4;
// Function to calculate number of hits
// and pseudo hits
static void calcResult(string guess, string solution)
{
hits = 0;
pseudoHits = 0;
if (guess.Length != solution.Length)
Console.Write("Incorrect Input");
// frequency array to store how many
// times each character occurs in
// solution string
int []frequencies = new int[MAX_COLORS];
// Compute hits and build frequency table
for (int i = 0; i < guess.Length; i++)
{
if (guess[i] == solution[i])
{
hits++;
}
else
{
/* Only increment the frequency table
(which will be used for pseudo-hits)
if it's not a hit. If it's a hit, the
slot has already been "used." */
int codeofcolor = codeOfColor(solution[i]);
frequencies[codeofcolor]++;
}
}
// Compute pseudo-hits
for (int i = 0; i < guess.Length; i++)
{
int codeofcolor = codeOfColor(guess[i]);
if (codeofcolor >= 0 &&
frequencies[codeofcolor] > 0 &&
guess[i] != solution[i])
{
pseudoHits++;
frequencies[codeofcolor]--;
}
}
Console.WriteLine("hits -> " + hits +
" Pseudo hits -> " + pseudoHits);
}
// Driver Code
public static void Main()
{
string solution = "GGRR";
string guess = "RGBY";
calcResult(solution, guess);
solution = "YGRR";
guess = "RGYB";
calcResult(solution, guess);
}
}
// This code is contributed by nitin mittal.
Javascript
输出:
hits -> 1 Pseudo hits -> 1
hits -> 1 Pseudo hits -> 2