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

📅  最后修改于: 2021-09-24 05:01:48             🧑  作者: Mango

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

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

例子:

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


Javascript


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


Javascript


输出:

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

蟒蛇3

# 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

Javascript


输出:
Victory