मैं कुछ प्रकार के एल्गोरिदम का उपयोग "बाढ़ भरने" या पथ खोजने वाले एल्गोरिदम जैसे * * के समान करता हूं। ऊपरी बाएं कोने (मान 1) में शुरू करें, इसे आउटपुट करें और दाएं और नीचे तक "विस्तृत करें" - इसलिए अपनी मान (2 और 5) को एक सूची में जोड़ें। वे दोनों 1 से अधिक होंगे। अब अपनी सूची (मूल्य 2) से सबसे छोटा मूल्य आउटपुट करें और इसे "विस्तृत करें" भी करें। आपको अपनी सूची में 4 और 7 जोड़ा जाएगा, आउटपुट 4 और "इसे विस्तारित करें", और इसी तरह।
ध्यान दें कि सूची को क्रमबद्ध करके, आप तुरंत सबसे छोटे तत्व को आउटपुट कर सकते हैं और एक बार में लगातार मूल्यों के कई "रन" आउटपुट करने में भी सक्षम हो सकते हैं (f.e. 10,11,12)। तो छद्म कोड होगा:
// array a[y][x]
// list L - ordered insertion, additionally stores matrix indices of values
add a[0][0] to L
loop until L is empty
output first element of L
remove first element of L and add its right and bottom neighbors (if any) to L
loop end
संपादित करें: यहां एक कामकाजी सी कार्यान्वयन है।
#include <stdio.h>
#include <stdlib.h>
#define COLS 5
#define ROWS 4
int matrix[ROWS][COLS] = {
1, 5, 10, 15, 20,
2, 7, 12, 17, 22,
4, 9, 18, 25, 28,
11, 14, 21, 26, 31
};
struct entry {
int value;
int x, y;
};
entry list[ROWS+COLS]; // Not sure how big the list can get, but this should be enough
int list_len = 0;
void set_list_entry(int index, int value, int x, int y) {
list[index].value = value;
list[index].x = x;
list[index].y = y;
}
void add_to_list(int x, int y) {
int val = matrix[y][x];
int i, pos = list_len;
for (i = 0; i < list_len; i++) {
if (list[i].value == val) return; // Don't add value that is on the list already
if (list[i].value > val) {
pos = i;
break;
}
}
// Shift the elements after pos
for (i = list_len + 1; i > pos; i--) {
set_list_entry(i, list[i - 1].value, list[i - 1].x, list[i - 1].y);
}
// Insert new entry
set_list_entry(pos, val, x, y);
list_len++;
}
int main() {
int iteration = 0;
add_to_list(0,0);
do {
// output first element of list
printf("%i ", list[0].value);
iteration++;
if ((iteration % COLS) == 0) printf("\n");
// add neighbors of first element of list to the list
if (list[0].x < (COLS - 1)) add_to_list(list[0].x + 1, list[0].y);
if (list[0].y < (ROWS - 1)) add_to_list(list[0].x, list[0].y + 1);
// remove first element of list
for (int i = 0; i < list_len; i++) {
set_list_entry(i, list[i + 1].value, list[i + 1].x, list[i + 1].y);
}
list_len--;
} while (list_len > 0);
return 0;
}
सूची की लंबाई के बारे में टिप्पणी नोट करें। मुझे यकीन है कि कितना बड़ा सूची प्राप्त कर सकते हैं नहीं कर रहा हूँ, लेकिन मुझे लगता COLS+ROWS
पर्याप्त इस सबसे खराब स्थिति को देखकर किया जाना चाहिए:
1 3 5 7 9 ..
2 y y y y
4 y x x x
6 y x x x
8 y x x x
.
.
सभी "सीमा" तत्वों छोटी से छोटी y
मूल्य से कम हैं, तो आप मिल जाएगा प्रक्रिया में y
मानों से भरा एक सूची, जो (ROWS - 1) + (COLS - 1)
तत्वों का लंबा है।
ऐसे सबसे खराब मामलों को देखते हुए, मुझे लगता है कि यह सबसे कुशल समाधान नहीं है, लेकिन मुझे लगता है कि यह एक सुरुचिपूर्ण और संक्षिप्त है।
कृपया सी ++ या सी चुनें सी ++ में कोई बड़ी मानक लाइब्रेरी (या बूस्ट) का लाभ उठा सकता है, इसलिए उत्तर नाटकीय रूप से अलग होंगे। –
यदि यू विशेष रूप से पूछता है। मैं सी – Aiden