কম্পিউটার

একটি 2D সমতলে অন্য বিন্দু থেকে একটি বিন্দু পড়ার চালগুলি খুঁজে বের করার জন্য C++ প্রোগ্রাম


ধরুন, একটি 2D সমতল a এবং b-এ দুটি বিন্দু রয়েছে যার স্থানাঙ্ক রয়েছে যথাক্রমে (x1, y1) এবং (x2, y2)। বর্তমানে, আমরা 'a' বিন্দুতে আছি এবং আমরা উল্লম্বভাবে বা অনুভূমিকভাবে 1 দূরত্বে চলতে পারি। আমরা একটি বিন্দু থেকে বি পয়েন্টে চলে যাই, তারপর একটি বিন্দুতে ফিরে যাই এবং আমরা আবার বি বিন্দুতে যাই। ক এবং খ বিন্দু ব্যতীত একই বিন্দুর মধ্য দিয়ে একাধিকবার চলাচলের অনুমতি নেই। আমাদের এই পুরো ট্রিপে আমরা যে পদক্ষেপগুলি করব তা খুঁজে বের করতে হবে এবং এটি আউটপুট করতে হবে। যদি আমরা ডানদিকে সরে যাই, তাহলে আমরা 'R', বামে গেলে 'L', উপরের দিকে গেলে 'U', এবং নিচে গেলে 'D' প্রিন্ট করি। আমাদের একটি জিনিস লক্ষ্য করতে হবে যে x2> x1 এবং y2> y1।

সুতরাং, যদি ইনপুটটি হয় x1 =0, y1 =1, x2 =3, y2 =4, তাহলে আউটপুট হবে UUURRRDDDLLLLUUUURRRRDDDDLLLU

এটি সমাধান করতে, আমরা এই পদক্ষেপগুলি অনুসরণ করব -

s := a blank string
for initialize i := 0, when i < y2 - y1, update (increase i by 1), do:
   add "U" at the end of s
for initialize i := 0, when i < x2 - x1, update (increase i by 1), do:
   add "R" at the end of s
for initialize i := 0, when i < y2 - y1, update (increase i by 1), do:
   add "D" at the end of s
for initialize i := 0, when i < x2 - x1, update (increase i by 1), do:
   add "L" at the end of s
   add "LU" at the end of s
for initialize i := 0, when i < y2 - y1, update (increase i by 1), do:
   add "U" at the end of s
for initialize i := 0, when i < x2 - x1, update (increase i by 1), do:
   add "R" at the end of s
   add "RD" at the end of s
   add "RD" at the end of s
for initialize i := 0, when i < y2 - y1, update (increase i by 1), do:
   add "D" at the end of s
for initialize i := 0, when i < x2 - x1, update (increase i by 1), do:
   add "L" at the end of s
   add "LU" at the end of s
return s

উদাহরণ

আরো ভালোভাবে বোঝার জন্য আসুন নিচের বাস্তবায়ন দেখি -

#include <bits/stdc++.h>
using namespace std;

string solve(int x1, int y1, int x2, int y2){
   string s = "";
   for(int i = 0; i < y2 - y1; i++)
      s.append("U");
   for(int i = 0; i < x2 - x1; i++)
      s.append("R");
   for(int i = 0; i < y2 - y1; i++)
      s.append("D");
   for(int i = 0; i < x2 - x1; i++)
      s.append("L");
   s.append("LU");
   for(int i = 0; i < y2 - y1; i++)
      s.append("U");
   for(int i = 0; i < x2 - x1; i++)
      s.append("R");
      s.append("RD");
      s.append("RD");
   for(int i = 0; i < y2 - y1; i++)  
      s.append("D");
   for(int i = 0; i < x2 - x1; i++) s.append("L");
      s.append("LU");
   return s;
}
int main() {
   int x1 = 0, y1 = 1, x2 = 3, y2 = 4; cout<< solve(x1, y1, x2, y2);
   return 0;
}

ইনপুট

0, 1, 3, 4

আউটপুট

UUURRRDDDLLLLUUUURRRRDRDDDDLLLLU

  1. একটি প্রদত্ত ম্যাট্রিক্স থেকে একটি প্যালিনড্রোমিক ম্যাট্রিক্স তৈরি করা যায় কিনা তা খুঁজে বের করার জন্য C++ প্রোগ্রাম

  2. একটি গ্রাফে সুপার শীর্ষবিন্দুগুলি খুঁজে বের করার জন্য C++ প্রোগ্রাম

  3. একটি গ্রিডে আলোকিত কোষের সংখ্যা খুঁজে বের করার জন্য C++ প্রোগ্রাম

  4. একটি গ্রাফ থেকে সর্বাধিক স্কোর কমানো যেতে পারে তা খুঁজে বের করতে C++ প্রোগ্রাম