কম্পিউটার

C++ এ একটি বুলিয়ান ম্যাট্রিক্স প্রশ্ন?


এখানে আমরা একটি আকর্ষণীয় বুলিয়ান ম্যাট্রিক্স সমস্যা দেখতে পাব। একটি বুলিয়ান ম্যাট্রিক্স দেওয়া হয়েছে যাতে 0 এবং 1 রয়েছে। আমাদের লক্ষ্য হল যেখানে 1 চিহ্নিত করা আছে তা খুঁজে বের করা। যদি 1 পজিশন ম্যাট[i,j]-এ চিহ্নিত করা হয়, তাহলে আমরা i এবং কলাম j-এর সারির 1-এ সমস্ত এন্ট্রি করব। আসুন একটি উদাহরণ দেখি। ম্যাট্রিক্স নিচের মত হলে −

1 0 0 1
0 0 0 0
0 0 0 0
0 1 0 0

তারপর পরিবর্তন করার পরে, এটি হবে −

1 1 1 1
1 1 0 1
1 1 0 1
1 1 1 1

অ্যালগরিদম

ম্যাট্রিক্সআপডেট(ম্যাট্রিক্স[R,C])

begin
   define two matrices row[R] and col[C], and fill them with 0
   mark the row and column indices of mat[R,C] where 1 is placed into the row[R] and col[C] matrices
   check row[R] and col[C] if the place is marked, then fill all places of that row and column with 1’s.
end

উদাহরণ

#include <iostream>
#define R 4
#define C 4
using namespace std;
void updateMatrix(bool mat[R][C]) {
   bool row[R];
   bool col[C];
   int i, j;
   for (int i = 0; i < R; i++) { //set all elements of row matrix as 0
      row[i] = 0;}
      for (i = 0; i < C; i++) { //set all elements of col matrix as 0
         col[i] = 0;}
         for (int i = 0; i < R; i++) { //mark row and col matrix to identify where 1 is present
            for (int j = 0; j < C; j++) {
               if (mat[i][j] == 1) {
                  row[i] = 1;
                  col[j] = 1;
               }
            }
         }
         for (i = 0; i < R; i++) { //set all 1s to the row and col, where 1 is marked
            for (j = 0; j < C; j++) {
               if ( row[i] == 1 || col[j] == 1 ) {
                  mat[i][j] = 1;
               }
            }
         }
      }
      void displayMatrix(bool mat[R][C]) {
         for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
               cout << mat[i][j];
            }
            cout << endl;
         }
      }
      main() {
         bool mat[R][C] = { {1, 0, 0, 1},
         {0, 0, 0, 0},
         {0, 0, 0, 0},
         {0, 1, 0, 0}
      };
      cout << "Given Matrix" << endl;
      displayMatrix(mat);
      updateMatrix(mat);
      cout << "Updated Matrix" << endl;
      displayMatrix(mat);
}

আউটপুট

Given Matrix
1001
0000
0000
0100
Updated Matrix
1111
1101
1101
1111

  1. একটি ম্যাট্রিক্স সম্ভাব্যতা প্রশ্ন?

  2. C++ এ ম্যাট্রিক্সের সারি-ভিত্তিক বনাম কলাম-ভিত্তিক ট্রাভার্সাল

  3. C++ এ স্পাইরাল ম্যাট্রিক্স III

  4. C++ তে বিসিমেট্রিক ম্যাট্রিক্স?