📜  三元平衡数字系统中的博弈论(一次移动3k步)

📅  最后修改于: 2021-05-25 08:07:04             🧑  作者: Mango

就像以2为底的二进制数系统(以0和1s为数字)一样,三元(三进制)数字系统是以3为底数的二进制系统,以0、1和-1为数字。
最好使用字母“ Z”代替-1,因为在表示完整的三进制数-1时,在1s和0s之间看起来是奇数。

将十进制转换为平衡三进制:
与在二进制转换中一样,首先将十进制数表示为具有0、1、2作为提醒的普通三元系统。
现在,从最低位开始迭代可以安全地跳过任何0和1,但是将2变成Z并在下一位添加1。以相同的条件将3变成0(此类数字最初不存在于数字中,但在增加约2s后可能会遇到。)
例子:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Numbers are in range of pow(3, 32)
int arr[32];
  
// Conversion of ternary into balanced ternary as
// start iterating from Least Significant Bit (i.e 0th), 
// if encountered 0 or 1, safely skip and pass carry 0 
// further 2, replace it to -1 and pass carry 1 further
// 3, replace it to 0 and pass carry 1 further
void balTernary(int ter)
{
    int carry = 0, base = 10;
    int i = 32;
    while (ter > 0) {
        int rem = ter % base;
        rem = rem + carry;
        if (rem == 0) {
            arr[i--] = 0;
            carry = 0;
        }
        else if (rem == 1) {
            arr[i--] = 1;
            carry = 0;
        }
        else if (rem == 2) {
            arr[i--] = -1;
            carry = 1;
        }
        else if (rem == 3) {
            arr[i--] = 0;
            carry = 1;
        }
        ter = ter / base;
    }
    if (carry == 1)
        arr[i] = 1;
}
  
// Similar to binary conversion
int ternary(int number)
{
    int ans = 0, rem = 1, base = 1;
    while (number > 0) {
        rem = number % 3;
        ans = ans + rem * base;
        number /= 3;
        base = base * 10;
    }
    return ans;
}
  
// Driver code
int main()
{
    int number = 3056;
  
    int ter = ternary(number);
    memset(arr, 0, sizeof(arr));
    balTernary(ter);
  
    int i = 0;
  
    // Moving on to first occupied bit
    while (arr[i] == 0) {
        i++;
    }
  
    // Printing
    for (int j = i; j <= 32; j++) {
  
        // Print 'Z' in place of -1
        if (arr[j] == -1)
            cout << 'Z';
        else
            cout << arr[j];
    }
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Numbers are in range of pow(3, 32)
static int []arr = new int[33];
  
// Conversion of ternary into balanced ternary as
// start iterating from Least Significant Bit (i.e 0th), 
// if encountered 0 or 1, safely skip and pass carry 0 
// further 2, replace it to -1 and pass carry 1 further
// 3, replace it to 0 and pass carry 1 further
static void balTernary(int ter)
{
    int carry = 0, base = 10;
    int i = 32;
    while (ter > 0) 
    {
        int rem = ter % base;
        rem = rem + carry;
        if (rem == 0) 
        {
            arr[i--] = 0;
            carry = 0;
        }
        else if (rem == 1) 
        {
            arr[i--] = 1;
            carry = 0;
        }
        else if (rem == 2)
        {
            arr[i--] = -1;
            carry = 1;
        }
        else if (rem == 3) 
        {
            arr[i--] = 0;
            carry = 1;
        }
        ter = (int)(ter / base);
    }
    if (carry == 1)
        arr[i] = 1;
}
  
// Similar to binary conversion
static int ternary(int number)
{
    int ans = 0, rem = 1, base = 1;
    while (number > 0) 
    {
        rem = number % 3;
        ans = ans + rem * base;
        number = (int)(number/3);
        base = base * 10;
    }
    return ans;
}
  
// Driver code
public static void main(String args[])
{
    int number = 3056;
  
    int ter = ternary(number);
    Arrays.fill(arr,0);
    balTernary(ter);
  
    int i = 0;
  
    // Moving on to first occupied bit
    while (arr[i] == 0) 
    {
        i++;
    }
  
    // Printing
    for (int j = i; j <= 32; j++) 
    {
  
        // Print 'Z' in place of -1
        if (arr[j] == -1)
            System.out.print('Z');
        else
        System.out.print(arr[j]);
    }
}
}
  
// This code is contributed by SURENDRA_GANGWAR


Python3
# Python3 implementation of the approach 
  
# Numbers are in range of pow(3, 32) 
arr = [0] * 32
  
# Conversion of ternary into balanced ternary as 
# start iterating from Least Significant Bit (i.e 0th), 
# if encountered 0 or 1, safely skip and pass carry 0 
# further 2, replace it to -1 and pass carry 1 further 
# 3, replace it to 0 and pass carry 1 further 
def balTernary(ter): 
   
    carry, base, i = 0, 10, 31 
    while ter > 0:
        rem = (ter % base) + carry 
          
        if rem == 0:  
            arr[i] = 0 
            carry, i = 0, i-1 
           
        elif rem == 1:  
            arr[i] = 1 
            carry, i = 0, i-1 
           
        elif rem == 2:  
            arr[i] = -1 
            carry, i = 1, i-1 
           
        elif rem == 3:  
            arr[i] = 0 
            carry, i = 1, i-1 
           
        ter = ter // base 
       
    if carry == 1:
        arr[i] = 1 
   
# Similar to binary conversion 
def ternary(number): 
   
    ans, rem, base = 0, 1, 1 
    while number > 0:
        rem = number % 3 
        ans = ans + rem * base 
        number //= 3 
        base = base * 10 
       
    return ans 
   
# Driver code 
if __name__ == "__main__": 
   
    number = 3056 
    ter = ternary(number) 
    balTernary(ter) 
  
    i = 0 
  
    # Moving on to first occupied bit 
    while arr[i] == 0:  
        i += 1
       
    # Printing 
    for j in range(i, 32):  
  
        # Print 'Z' in place of -1 
        if arr[j] == -1: 
            print('Z', end = "") 
        else:
            print(arr[j], end = "") 
       
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach 
using System;
      
class GFG
{
  
// Numbers are in range of pow(3, 32)
static int []arr = new int[33];
  
// Conversion of ternary into balanced ternary as
// start iterating from Least Significant Bit (i.e 0th), 
// if encountered 0 or 1, safely skip and pass carry 0 
// further 2, replace it to -1 and pass carry 1 further
// 3, replace it to 0 and pass carry 1 further
static void balTernary(int ter)
{
    int carry = 0, b = 10;
    int i = 32;
    while (ter > 0) 
    {
        int rem = ter % b;
        rem = rem + carry;
        if (rem == 0) 
        {
            arr[i--] = 0;
            carry = 0;
        }
        else if (rem == 1) 
        {
            arr[i--] = 1;
            carry = 0;
        }
        else if (rem == 2)
        {
            arr[i--] = -1;
            carry = 1;
        }
        else if (rem == 3) 
        {
            arr[i--] = 0;
            carry = 1;
        }
        ter = (int)(ter / b);
    }
    if (carry == 1)
        arr[i] = 1;
}
  
// Similar to binary conversion
static int ternary(int number)
{
    int ans = 0, rem = 1, b = 1;
    while (number > 0) 
    {
        rem = number % 3;
        ans = ans + rem * b;
        number = (int)(number / 3);
        b = b * 10;
    }
    return ans;
}
  
// Driver code
public static void Main(String []args)
{
    int number = 3056;
  
    int ter = ternary(number);
    balTernary(ter);
  
    int i = 0;
  
    // Moving on to first occupied bit
    while (arr[i] == 0) 
    {
        i++;
    }
  
    // Printing
    for (int j = i; j <= 32; j++) 
    {
  
        // Print 'Z' in place of -1
        if (arr[j] == -1)
            Console.Write('Z');
        else
            Console.Write(arr[j]);
    }
}
}
  
// This code is contributed by Rajput-Ji


C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true
// if the game cannot be won
bool isDefeat(string s1, string s2, int n)
{
    for (int i = 0; i < n; i++) {
        if ((s1[i] == '0' && s2[i] == '1')
            || (s1[i] == '1' && s2[i] == '0'))
            continue;
        else if ((s1[i] == '0' && s2[i] == 'Z')
                 || (s1[i] == 'Z' && s2[i] == '0'))
            continue;
        else {
            return true;
        }
    }
    return false;
}
  
// Driver code
int main()
{
    string s1 = { "01001101ZZ" };
    string s2 = { "10Z1001000" };
  
    // Common length
    int n = 10;
  
    if (isDefeat(s1, s2, n))
        cout << "Defeat";
    else
        cout << "Victory";
  
    return 0;
}


Java
// Java implementation of the approach
class GfG
{
      
// Function that returns true
// if the game cannot be won
static boolean isDefeat(String s1, String s2, int n)
{
    for (int i = 0; i < n; i++)
    {
        if ((s1.charAt(i) == '0' && s2.charAt(i) == '1')
            || (s1.charAt(i) == '1' && s2.charAt(i) == '0'))
            continue;
        else if ((s1.charAt(i) == '0' && s2.charAt(i) == 'Z')
                || (s1.charAt(i) == 'Z' && s2.charAt(i) == '0'))
            continue;
        else 
        {
            return true;
        }
    }
    return false;
}
  
// Driver code
public static void main(String[] args)
{
    String s1 = ("01001101ZZ" );
    String s2 = ("10Z1001000" );
  
    // Common length
    int n = 10;
  
    if (isDefeat(s1, s2, n))
        System.out.println("Defeat");
    else
        System.out.println("Victory");
  
}
}
  
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the approach
  
# Function that returns true
# if the game cannot be won
def isDefeat(s1, s2, n):
  
    for i in range(n):
        if ((s1[i] == '0' and s2[i] == '1') or 
            (s1[i] == '1' and s2[i] == '0')):
            continue
        elif ((s1[i] == '0' and s2[i] == 'Z') or 
              (s1[i] == 'Z' and s2[i] == '0')):
            continue
        else:
            return True
          
    return False
  
# Driver code
s1 = "01001101ZZ"
s2 = "10Z1001000"
  
# Common length
n = 10
  
if (isDefeat(s1, s2, n)):
    print("Defeat")
else:
    print("Victory")
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
  
class GfG
{
      
// Function that returns true
// if the game cannot be won
static bool isDefeat(string s1, string s2, int n)
{
    for (int i = 0; i < n; i++)
    {
        if ((s1[i] == '0' && s2[i] == '1')
            || (s1[i] == '1' && s2[i] == '0'))
            continue;
        else if ((s1[i] == '0' && s2[i] == 'Z')
                || (s1[i] == 'Z' && s2[i]== '0'))
            continue;
        else
        {
            return true;
        }
    }
    return false;
}
  
// Driver code
public static void Main()
{
    string s1 = ("01001101ZZ" );
    string s2 = ("10Z1001000" );
  
    // Common length
    int n = 10;
  
    if (isDefeat(s1, s2, n))
        Console.WriteLine("Defeat");
    else
        Console.WriteLine("Victory");
  
}
}
  
// This code is contributed by Code_Mech


PHP


输出:
111ZZ1ZZ

从平衡的三进制数中恢复原始十进制数:-
过程:-与从二进制到十进制的转换类似
范例:-111ZZ1ZZ
3^7*(1) + 3^6*(1) + 3^5*(1) + 3^4*(-1) + 3^3*(-1) + 3^2*(1) + 3^1*(-1) + 3^0*(-1)
 = 2187 + 729 + 243 - 81 - 27 + 9 - 3 - 1 = 3056

游戏规则:
从0开始,允许两个机器人在x轴上逐步移动。
他们可以从0开始执行多个步骤,但是它们的移动存在一些限制。
k_t_h步进机器人将精确移动3^k距离单位。
在每个步骤中,机器人必须选择向左(x坐标减小)或向右(x坐标增大)两个方向之一,在特定步骤中,只有一个机器人会移动,而另一个机器人会等待。
不允许跳过任何步骤。

陈述:
给定两个整数x1和x2。分别需要机器人1和2覆盖它们各自的距离x1和x2。是否可以??
如果有可能您赢了,否则您输了。

方法:
每个十进制数(此处为距离)只有一个平衡的三进制表示,这意味着只有一种方法可以满足上述规则,以覆盖特定的距离。
因此,如果可以覆盖x1和x2的距离,以使当一个机器人移动时其他机器人仍然静止并且两个机器人不能同时静止不动,那么这就是胜利。

逻辑:
首先使用上述过程将x1和x2表示为平衡三元数。
从LSB检查中迭代:-
在一个时间(步骤)中,只有一个值应为1或Z。
两者不能同时为0(步骤)。

如果任何时候违反规则,那是您的输家,否则您就赢了。

例子:

在两个数组上按位迭代,并在规则中断的任何地方中断。
首先,通过在最短的数组的开头加0来使两个数组的长度相等,以使长度相同。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true
// if the game cannot be won
bool isDefeat(string s1, string s2, int n)
{
    for (int i = 0; i < n; i++) {
        if ((s1[i] == '0' && s2[i] == '1')
            || (s1[i] == '1' && s2[i] == '0'))
            continue;
        else if ((s1[i] == '0' && s2[i] == 'Z')
                 || (s1[i] == 'Z' && s2[i] == '0'))
            continue;
        else {
            return true;
        }
    }
    return false;
}
  
// Driver code
int main()
{
    string s1 = { "01001101ZZ" };
    string s2 = { "10Z1001000" };
  
    // Common length
    int n = 10;
  
    if (isDefeat(s1, s2, n))
        cout << "Defeat";
    else
        cout << "Victory";
  
    return 0;
}

Java

// Java implementation of the approach
class GfG
{
      
// Function that returns true
// if the game cannot be won
static boolean isDefeat(String s1, String s2, int n)
{
    for (int i = 0; i < n; i++)
    {
        if ((s1.charAt(i) == '0' && s2.charAt(i) == '1')
            || (s1.charAt(i) == '1' && s2.charAt(i) == '0'))
            continue;
        else if ((s1.charAt(i) == '0' && s2.charAt(i) == 'Z')
                || (s1.charAt(i) == 'Z' && s2.charAt(i) == '0'))
            continue;
        else 
        {
            return true;
        }
    }
    return false;
}
  
// Driver code
public static void main(String[] args)
{
    String s1 = ("01001101ZZ" );
    String s2 = ("10Z1001000" );
  
    // Common length
    int n = 10;
  
    if (isDefeat(s1, s2, n))
        System.out.println("Defeat");
    else
        System.out.println("Victory");
  
}
}
  
// This code is contributed by Code_Mech

Python3

# Python3 implementation of the approach
  
# Function that returns true
# if the game cannot be won
def isDefeat(s1, s2, n):
  
    for i in range(n):
        if ((s1[i] == '0' and s2[i] == '1') or 
            (s1[i] == '1' and s2[i] == '0')):
            continue
        elif ((s1[i] == '0' and s2[i] == 'Z') or 
              (s1[i] == 'Z' and s2[i] == '0')):
            continue
        else:
            return True
          
    return False
  
# Driver code
s1 = "01001101ZZ"
s2 = "10Z1001000"
  
# Common length
n = 10
  
if (isDefeat(s1, s2, n)):
    print("Defeat")
else:
    print("Victory")
  
# This code is contributed by mohit kumar

C#

// C# implementation of the approach
using System;
  
class GfG
{
      
// Function that returns true
// if the game cannot be won
static bool isDefeat(string s1, string s2, int n)
{
    for (int i = 0; i < n; i++)
    {
        if ((s1[i] == '0' && s2[i] == '1')
            || (s1[i] == '1' && s2[i] == '0'))
            continue;
        else if ((s1[i] == '0' && s2[i] == 'Z')
                || (s1[i] == 'Z' && s2[i]== '0'))
            continue;
        else
        {
            return true;
        }
    }
    return false;
}
  
// Driver code
public static void Main()
{
    string s1 = ("01001101ZZ" );
    string s2 = ("10Z1001000" );
  
    // Common length
    int n = 10;
  
    if (isDefeat(s1, s2, n))
        Console.WriteLine("Defeat");
    else
        Console.WriteLine("Victory");
  
}
}
  
// This code is contributed by Code_Mech

的PHP

输出:
Victory

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。