কম্পিউটার

সি প্রোগ্রামে স্ক্রিনে একটি স্ট্রিং প্রিন্ট করার জন্য সবচেয়ে ছোট পথ প্রিন্ট করুন।


একটি স্ট্রিং দেওয়া হলে, প্রোগ্রামটিকে অবশ্যই সংক্ষিপ্ততম পথটি প্রদর্শন করতে হবে যা সেই সংক্ষিপ্ততম পথটি ব্যবহার করে স্ক্রিনের উপর স্ট্রিংটি প্রিন্ট করবে৷

লাইক স্ক্রীন বর্ণমালা ফরম্যাটে সংরক্ষণ করবে

A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
Z

উদাহরণ

Input: HUP
Output : Move Down
Move Down
Move Down
destination reached
Move Left
Move Left
Move Down
Move Down
Move Down
destination reached
Move Up
destination reached

এখানে ব্যবহৃত পদ্ধতিটি হল n x n ম্যাট্রিক্সে অক্ষরগুলি সংরক্ষণ করা এবং নিম্নলিখিত ক্রিয়াকলাপ সম্পাদন করা -

If row difference is negative then move up
If row difference is positive then move down
If column difference is negative then go left
If column difference is positive then we go right

অ্যালগরিদম

START
Step 1 -> Declare Function void printpath(char str[])
   Declare variable int i = 0 and cx=0 and cy=0
   Loop While str[i] != '\0'
      Declare variable as int n1 = (str[i] - 'A') / 5
      Declare variable as int n2 = (str[i] - 'B' + 1) % 5
      Loop while cx > n1
         Print move up
         cx—
      End
      Loop while cy > n2
         Print Move Left
         Cy—
      End
      Loop while cx < n1
         Print move down
         Cx++
      End
      Loop while cy < n2
         Print move down
         Cy++
      End
      Print destination reached
      I++
Step 2 -> in main()
   Declare char str[] = {"HUP"}
   Call printpath(str)
STOP

উদাহরণ

#include <stdio.h>
void printpath(char str[]){
   int i = 0;
   // start from character 'A' present at position (0, 0)
   int cx = 0, cy = 0;
   while (str[i] != '\0'){
      // find cordinates of next character
      int n1 = (str[i] - 'A') / 5;
      int n2 = (str[i] - 'B' + 1) % 5;
      // Move Up if destination is above
      while (cx > n1){
         printf("Move Up\n");
         cx--;
      }
      // Move Left if destination is to the left
      while (cy > n2){
         printf("Move Left\n");
         cy--;
      }
      // Move down if destination is below
      while (cx < n1){
         printf("Move Down\n");
            cx++;
      }
      // Move Right if destination is to the right
      while (cy < n2){
         printf("Move Down\n");
         cy++;
      }
      // At this point, destination is reached
      printf("destination reached\n");
      i++;
   }
}
int main(int argc, char const *argv[]){
   char str[] = {"HUP"};
   printpath(str);
   return 0;
}

আউটপুট

যদি আমরা উপরের প্রোগ্রামটি রান করি তাহলে এটি নিম্নলিখিত আউটপুট −

উৎপন্ন করবে
Move Down
Move Down
Move Down
destination reached
Move Left
Move Left
Move Down
Move Down
Move Down
destination reached
Move Up
destination reached

  1. C প্রোগ্রামে 'X' ফরম্যাটে বিজোড় দৈর্ঘ্যের স্ট্রিং প্রিন্ট করুন।

  2. 0-1 BFS (একটি বাইনারি ওজন গ্রাফের সংক্ষিপ্ত পথ) সি প্রোগ্রামে?

  3. পাইথন প্রোগ্রাম একটি প্রদত্ত স্ট্রিং এর সমস্ত স্থানান্তর প্রিন্ট করতে

  4. পাইথন প্রোগ্রাম একটি স্ট্রিং মধ্যে এমনকি দৈর্ঘ্য শব্দ মুদ্রণ