最后一个街区的方向

给定一个RxC(1<=R,C<=100000000)网格,初始位置为左上角,方向为东。现在我们开始向前运行,穿过矩阵的每个方块。每当我们发现死胡同或到达一个已经被访问过的单元时,我们就向右走,因为我们不能再穿过访问过的方形街区。告诉我们到达最后一个街区的方向。 例如: 考虑R=3,C=3的情况。接下来的路径将是(0,0)-(0,1)-(0,2)-(1,2)-(2,2)-(2,0)-(1,0)-(1,1)。在这一点上,所有的广场都已经参观过了,并且都面向右边。 例如:

null
Input  :  R = 1, C = 1Output :  RightInput   :  R = 2, C = 2Output  :  LeftInput   :  R = 3, C = 1Output  :  DownInput  :  R = 3, C = 3Output :  Right

简单的解决方案: 这个问题的一个简单解决方案是,使其RxC矩阵初始化为零,并以螺旋形式遍历它,然后取一个变量“Dir”,告诉当前方向。当我们在任何行和列的末尾时,取“Right”并根据当前方向更改“Dir”的值。现在遵循给定的条件:

  • 如果你正在穿过第一排,那么你当前的方向是“正确的”。
  • 如果你是对的,那么你当前的方向是“向下”。
  • 如果您正在穿过最下面一行,那么您当前的方向是“左”。
  • 如果你正在穿越左列,那么你当前的方向是“向上”。

当我们到达最后一个方块时,只需打印当前方向,即;“Dir”变量的值。 这个问题的时间和空间复杂度是O(RxC),这只适用于较小的R,C值,但这里R和C太大,所以对于太大的R和C值,创建RxC矩阵是不可能的。 有效方法: 这种方法需要很少的观察和一些笔墨工作。在这里,我们必须考虑所有可能的情况下,R和C,那么我们只需要把if条件为所有可能的情况。以下是所有可能的条件:

  1. R!=C和R是偶数,C是奇数,R
  2. R!=C和R是奇数,C是偶数,R
  3. R!=C和R是均匀的,C是均匀的,R
  4. R!=C和R是奇数,C是奇数,R
  5. R!=C和R是偶数,C是奇数,R>C,方向将是“向下”。
  6. R!=C和R是奇数,C是偶数,R>C,方向将是“向上”。
  7. R!=C和R是均匀的,C是均匀的,R>C,方向将是“向上”。
  8. R!=C和R是奇数,C是奇数,R>C,方向将是“向下”。
  9. R==C,R为偶数,C为偶数,方向为“左”。
  10. R==C,R为奇数,C为奇数,方向为“右”。

下面是上述想法的实现。

C++

// C++ program to tell the Current direction in
// R x C grid
#include <iostream>
using namespace std;
typedef long long int ll;
// Function which tells the Current direction
void direction(ll R, ll C)
{
if (R != C && R % 2 == 0 && C % 2 != 0 && R < C) {
cout << "Left" << endl;
return ;
}
if (R != C && R % 2 != 0 && C % 2 == 0 && R > C) {
cout << "Up" << endl;
return ;
}
if (R == C && R % 2 != 0 && C % 2 != 0) {
cout << "Right" << endl;
return ;
}
if (R == C && R % 2 == 0 && C % 2 == 0) {
cout << "Left" << endl;
return ;
}
if (R != C && R % 2 != 0 && C % 2 != 0 && R < C) {
cout << "Right" << endl;
return ;
}
if (R != C && R % 2 != 0 && C % 2 != 0 && R > C) {
cout << "Down" << endl;
return ;
}
if (R != C && R % 2 == 0 && C % 2 == 0 && R < C) {
cout << "Left" << endl;
return ;
}
if (R != C && R % 2 == 0 && C % 2 == 0 && R > C) {
cout << "Up" << endl;
return ;
}
if (R != C && R % 2 == 0 && C % 2 != 0 && R > C) {
cout << "Down" << endl;
return ;
}
if (R != C && R % 2 != 0 && C % 2 == 0 && R < C) {
cout << "Right" << endl;
return ;
}
}
// Driver program to test the Cases
int main()
{
ll R = 3, C = 1;
direction(R, C);
return 0;
}


JAVA

// Java program to tell the Current direction in
// R x C grid
import java.io.*;
class GFG {
// Function which tells the Current direction
static void direction( int R, int C)
{
if (R != C && R % 2 == 0 && C % 2 != 0 && R < C) {
System.out.println( "Left" );
return ;
}
if (R != C && R % 2 != 0 && C % 2 == 0 && R > C) {
System.out.println( "Up" );
return ;
}
if (R == C && R % 2 != 0 && C % 2 != 0 ) {
System.out.println( "Right" );
return ;
}
if (R == C && R % 2 == 0 && C % 2 == 0 ) {
System.out.println( "Left" );
return ;
}
if (R != C && R % 2 != 0 && C % 2 != 0 && R < C) {
System.out.println( "Right" );
return ;
}
if (R != C && R % 2 != 0 && C % 2 != 0 && R > C) {
System.out.println( "Down" );
return ;
}
if (R != C && R % 2 == 0 && C % 2 == 0 && R < C) {
System.out.println( "Left" );
return ;
}
if (R != C && R % 2 == 0 && C % 2 == 0 && R > C) {
System.out.println( "Up" );
return ;
}
if (R != C && R % 2 == 0 && C % 2 != 0 && R > C) {
System.out.println( "Down" );
return ;
}
if (R != C && R % 2 != 0 && C % 2 == 0 && R < C) {
System.out.println( "Right" );
return ;
}
}
// Driver code
public static void main(String[] args)
{
int R = 3 , C = 1 ;
direction(R, C);
}
}
// This code is contributed by KRV.


Python3

# Python3 program to tell the Current
# direction in R x C grid
# Function which tells the Current direction
def direction(R, C):
if (R ! = C and R % 2 = = 0 and
C % 2 ! = 0 and R < C):
print ( "Left" )
return
if (R ! = C and R % 2 = = 0 and
C % 2 = = 0 and R > C):
print ( "Up" )
return
if R = = C and R % 2 ! = 0 and C % 2 ! = 0 :
print ( "Right" )
return
if R = = C and R % 2 = = 0 and C % 2 = = 0 :
print ( "Left" )
return
if (R ! = C and R % 2 ! = 0 and
C % 2 ! = 0 and R < C):
print ( "Right" )
return
if (R ! = C and R % 2 ! = 0 and
C % 2 ! = 0 and R > C):
print ( "Down" )
return
if (R ! = C and R % 2 = = 0 and
C % 2 ! = 0 and R < C):
print ( "Left" )
return
if (R ! = C and R % 2 = = 0 and
C % 2 = = 0 and R > C):
print ( "Up" )
return
if (R ! = C and R % 2 ! = 0 and
C % 2 ! = 0 and R > C):
print ( "Down" )
return
if (R ! = C and R % 2 ! = 0 and
C % 2 ! = 0 and R < C):
print ( "Right" )
return
# Driver code
R = 3 ; C = 1
direction(R, C)
# This code is contributed by Shrikant13


C#

// C# program to tell the Current
// direction in R x C grid
using System;
class GFG
{
// Function which tells
// the Current direction
static void direction( int R, int C)
{
if (R != C && R % 2 == 0 &&
C % 2 != 0 && R < C)
{
Console.WriteLine( "Left" );
return ;
}
if (R != C && R % 2 != 0 &&
C % 2 == 0 && R > C)
{
Console.WriteLine( "Up" );
return ;
}
if (R == C && R % 2 != 0 &&
C % 2 != 0)
{
Console.WriteLine( "Right" );
return ;
}
if (R == C && R % 2 == 0 &&
C % 2 == 0)
{
Console.WriteLine( "Left" );
return ;
}
if (R != C && R % 2 != 0 &&
C % 2 != 0 && R < C)
{
Console.WriteLine( "Right" );
return ;
}
if (R != C && R % 2 != 0 &&
C % 2 != 0 && R > C)
{
Console.WriteLine( "Down" );
return ;
}
if (R != C && R % 2 == 0 &&
C % 2 == 0 && R < C)
{
Console.WriteLine( "Left" );
return ;
}
if (R != C && R % 2 == 0 &&
C % 2 == 0 && R > C)
{
Console.WriteLine( "Up" );
return ;
}
if (R != C && R % 2 == 0 &&
C % 2 != 0 && R > C)
{
Console.WriteLine( "Down" );
return ;
}
if (R != C && R % 2 != 0 &&
C % 2 == 0 && R < C)
{
Console.WriteLine( "Right" );
return ;
}
}
// Driver code
static public void Main ()
{
int R = 3, C = 1;
direction(R, C);
}
}
// This code is contributed by m_kit


PHP

<?php
// PHP program to tell the Current
// direction in R x C grid
// Function which tells
// the Current direction
function direction( $R , $C )
{
if ( $R != $C && $R % 2 == 0 &&
$C % 2 != 0 && $R < $C )
{
echo "Left" , "" ;
return ;
}
if ( $R != $C && $R % 2 != 0 &&
$C % 2 == 0 && $R > $C )
{
echo "Up" , "" ;
return ;
}
if ( $R == $C && $R % 2 != 0
&& $C % 2 != 0)
{
echo "Right" , "" ;
return ;
}
if ( $R == $C && $R % 2 == 0
&& $C % 2 == 0)
{
echo "Left" , "" ;
return ;
}
if ( $R != $C && $R % 2 != 0 &&
$C % 2 != 0 && $R < $C )
{
echo "Right" , "" ;
return ;
}
if ( $R != $C && $R % 2 != 0 &&
$C % 2 != 0 && $R > $C )
{
echo "Down" , "" ;
return ;
}
if ( $R != $C && $R % 2 == 0 &&
$C % 2 == 0 && $R < $C )
{
echo "Left" , "" ;
return ;
}
if ( $R != $C && $R % 2 == 0 &&
$C % 2 == 0 && $R > $C )
{
echo "Up" , "" ;
return ;
}
if ( $R != $C && $R % 2 == 0 &&
$C % 2 != 0 && $R > $C )
{
echo "Down" , "" ;
return ;
}
if ( $R != $C && $R % 2 != 0 &&
$C % 2 == 0 && $R < $C )
{
echo "Right" , "" ;
return ;
}
}
// Driver Code
$R = 3; $C = 1;
direction( $R , $C );
// This code is contributed by aj_36
?>


Javascript

<script>
// Javascript program to tell the Current
// direction in R x C grid
// Function which tells
// the Current direction
function direction(R, C)
{
if (R != C && R % 2 == 0 &&
C % 2 != 0 && R < C)
{
document.write( "Left" );
return ;
}
if (R != C && R % 2 != 0 &&
C % 2 == 0 && R > C)
{
document.write( "Up" );
return ;
}
if (R == C && R % 2 != 0 &&
C % 2 != 0)
{
document.write( "Right" );
return ;
}
if (R == C && R % 2 == 0 &&
C % 2 == 0)
{
document.write( "Left" );
return ;
}
if (R != C && R % 2 != 0 &&
C % 2 != 0 && R < C)
{
document.write( "Right" );
return ;
}
if (R != C && R % 2 != 0 &&
C % 2 != 0 && R > C)
{
document.write( "Down" );
return ;
}
if (R != C && R % 2 == 0 &&
C % 2 == 0 && R < C)
{
document.write( "Left" );
return ;
}
if (R != C && R % 2 == 0 &&
C % 2 == 0 && R > C)
{
document.write( "Up" );
return ;
}
if (R != C && R % 2 == 0 &&
C % 2 != 0 && R > C)
{
document.write( "Down" );
return ;
}
if (R != C && R % 2 != 0 &&
C % 2 == 0 && R < C)
{
document.write( "Right" );
return ;
}
}
let R = 3, C = 1;
direction(R, C);
</script>


输出:

Down

时间复杂性: O(1) 辅助空间: O(1) 参考: http://www.spoj.com/problems/TRGRID/ 本文由 沙申克·米什拉(古卢) .这篇文章由Geeksforgeks团队审阅。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享