Форум программистов, компьютерный форум, киберфорум
С++ для начинающих
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
Рейтинг 4.78/9: Рейтинг темы: голосов - 9, средняя оценка - 4.78
0 / 0 / 1
Регистрация: 16.06.2016
Сообщений: 7
1

Игра - сокровища пирата Ларри

10.11.2017, 19:59. Показов 1753. Ответов 2
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
Всем привет, не знал где именно расположить тему, так что сорян если что не так.
Помогите составить алгоритм или псевдо-код для вот такой задачки

Problem

Following an old map, you have stumbled upon the Dread Pirate Larry's secret treasure trove!

The treasure trove consists of N locked chests, each of which can only be opened by a key of a specific type. Furthermore, once a key is used to open a chest, it can never be used again. Inside every chest, you will of course find lots of treasure, and you might also find one or more keys that you can use to open other chests. A chest may contain multiple keys of the same type, and you may hold any number of keys.

You already have at least one key and your map says what other keys can be found inside the various chests. With all this information, can you figure out how to unlock all the chests?

For example, suppose the treasure trove consists of four chests as described below, and you began with exactly one key of type 1:

Chest Number | Key Type To Open Chest | Key Types Inside
--------------+--------------------------+------------------
1 | 1 | None
2 | 1 | 1, 3
3 | 2 | None
4 | 3 | 2
You can open all the chests in this example if you do them in the order 2, 1, 4, 3. If you start by opening chest #1 first, then you will have used up your only key, and you will be stuck.
Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a single line containing two positive integers K and N, representing the number of keys you start with and the number of chests you need to open.

This is followed by a line containing K integers, representing the types of the keys that you start with.

After that, there will be N lines, each representing a single chest. Each line will begin with integers Ti and Ki, indicating the key type needed to open the chest and the number of keys inside the chest. These two integers will be followed by Ki more integers, indicating the types of the keys contained within the chest.

Output

For each test case, output one line containing "Case #x: C1 C2 ... CN", where x is the case number (starting from 1), and where Ci represents the index (starting from 1) of the ith chest that you should open.

If there are multiple ways of opening all the chests, choose the "lexicographically smallest" way. In other words, you should choose to make C1 as small as possible, and if there are multiple ways of making C1 as small as possible, choose the one that makes C2 as small as possible, and so on.

If there is no way to open all the chests, you should instead output one line containing "Case #x: IMPOSSIBLE".

Limits

1 ≤ T ≤ 25.
1 ≤ K.
All key types will be integers between 1 and 200 inclusive.
Small dataset

1 ≤ N ≤ 20.
In each test case, there will be at most 40 keys altogether.
Large dataset

1 ≤ N ≤ 200.
In each test case, there will be at most 400 keys altogether.
Sample


Input

Output

3
1 4
1
1 0
1 2 1 3
2 0
3 1 2
3 3
1 1 1
1 0
1 0
1 0
1 1
2
1 1 1
Case #1: 2 1 4 3
Case #2: 1 2 3
Case #3: IMPOSSIBLE

Ссыль на оригинал: https://code.google.com/codeja... board#s=p3

Не могу понять что конкретно надо делать. Вроде есть массивы с типами ключей и сундуков, сами ключи и т.д . Но все равно не соображаю(
P.S. Входные данные идут с файлаD-small-practice.rar
0
Лучшие ответы (1)
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
10.11.2017, 19:59
Ответы с готовыми решениями:

Написать программы:1)расставить сокровища по экрану 2)Двигать паучка по 4м направлениям.На сокровища пока не р
Написать программы:1)расставить сокровища по экрану 2)Двигать паучка по 4м направлениям.На...

Создать в графическом режиме Pascal игру Сокровища
Случайным образом разбросать сокровища (например,окружность) на экране.Реализовать движение паучка...

Выведите 2 числа — количество банок, не простреленных Гарри и Ларри соответственно.
Бандиты Гарри и Ларри отдыхали на природе. Решив пострелять, они выставили на бревно несколько...

программа в Pascal Abc Вывод координат сокровищ из файла и расставить сокровища по экрану
Написать программу вывода координат сокровищ из файла и расставить сокровища по экрану

2
управление сложностью
1687 / 1300 / 259
Регистрация: 22.03.2015
Сообщений: 7,545
Записей в блоге: 5
13.11.2017, 07:48 2
А вы сами-то текст задачи переводили ? Какая задача стоит ?
0
0 / 0 / 1
Регистрация: 16.06.2016
Сообщений: 7
17.11.2017, 18:13  [ТС] 3
Лучший ответ Сообщение было отмечено FARADEYua как решение

Решение

Додумал решение, слегка кривое, не идеальное.
Милости прошу дорабатывать, комментировать.
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <iterator>
#pragma warning(disable:4996)
 
using namespace std;
//Структура с инфорамации о тестовых данных
struct Keys
{
    
    int iKey_chest_num[2];  // кол-во ключей и сундуков, первая строка
    int *iKey_val;          //Значения начальных ключей 
    int **Chest_info = new int*[30]; //двухмерный динамический массив с информацией о самих сундуках 
                                    // Каким ключем открывается, сколько в нем ключей и какого типа они
 
};
 
 
void main()
{
    // Переменные и подключение файла, чтение данных
    int *tmp_arr = new int[40];
    int test_c = 0;
    int size = 0;
    int k = 0;
    setlocale(LC_ALL, "rus");
    ifstream fin("D-small-practice.in", ios_base::in); 
    ofstream fout("D_small_solution.in", ios_base::trunc);
    char buff[256];
    fout.close();
    fin.close();
    fin.open("D-small-practice.in");
    fout.open("D_small_solution.in");
    if (!fin.is_open())
    {
        cout << "Can't open file" << endl;
        system("Pause");
        return;
    }
    else
    {
        fin.getline(buff, 50);
        test_c = atoi(buff);
        
        cout << "Test cases = " << test_c << endl;
    
    }
    // циклично и динамично считываем данные с файла заносим в массив структур
    Keys *full_info = new Keys[test_c];
    for (k = 0; k < test_c;k++)
    {
 
        // Получаем количество начальных ключей и сундуков
        fin.getline(buff, 50);
        char * pch;
        pch = strtok(buff, " ");
        int i = 0;
        while (pch != NULL)
        {
            full_info[k].iKey_chest_num[i] = atoi(pch);
            i++;
            pch = strtok(NULL, " ");
        }
        for (int i = 0; i < 2; i++)
            cout << full_info[k].iKey_chest_num[i] << " ";
        cout << endl;
 
 
 
        //Получаем значения начальных ключей в динамический масив
        fin.getline(buff, 50);
        pch = strtok(buff, " ");
        i = 0;
        while (pch != NULL)
        {
            tmp_arr[i] = atoi(pch);
            i++;
            //cout << i << " ";
            pch = strtok(NULL, " ");
        }
        size = full_info[k].iKey_chest_num[0];
        full_info[k].iKey_val = new int[size];
        for (int i = 0; i < size; i++)
        {
            full_info[k].iKey_val[i] = tmp_arr[i];
            cout << full_info[k].iKey_val[i] << " ";
        }
        cout << endl;
 
 
 
        for (int z = 0; z < full_info[k].iKey_chest_num[1] + 1; z++)
        {
            full_info[k].Chest_info[z] = new int[];
        }
 
        
        // Получаем данные о самых сундуках, все вносится в двухмерный массив
        // full_info[k].chest_info[j]
        for (int j = 0; j < full_info[k].iKey_chest_num[1]; j++)
        {
            delete []full_info[k].Chest_info[j];
            fin.getline(buff, 50);
            pch = strtok(buff, " ");
            int i = 0;
            while (pch != NULL)
            {
                
                tmp_arr[i] = atoi(pch);
                int tmp = tmp_arr[i];                       
                pch = strtok(NULL, " ");
                i++;
            }
            
            size = tmp_arr[1] + 2;
            full_info[k].Chest_info[j] = new int[size];
            for (int i = 0; i < size; i++)
            {
                full_info[k].Chest_info[j][i] = tmp_arr[i];
                cout << full_info[k].Chest_info[j][i] << " ";
            }
            cout << endl;
 
        }
        cout << endl;
        
        
    } 
    
    
    //разбираем ключи и возможности открытия сундуков 
        k = 0;
    multiset<int> Keys;
    std::set<int>::iterator it;
    int max_count = 0, Keys_num = 0, num = 0, op_chest = 0, i_tmp = 0;
    string str;
    char c_tmp[50];
    int i = 0, j = 0;
    
    // циклично для каждого элемента массива структуры делаем разбор
    for (k = 0; k < test_c; k++)
    {
        //k = 9;
        Keys.erase(Keys.begin(),Keys.end());
        i = 0;
        j = 0;
        op_chest = 0;
        cout << "CASE# " << k + 1 << ": ";
 
        for (i = 0; i < full_info[k].iKey_chest_num[0]; i++)
        {
            Keys.insert(full_info[k].iKey_val[i]);
        }
 
        for ( i = 0; i < full_info[k].iKey_chest_num[1]; i++)
        {
            Keys_num += full_info[k].Chest_info[i][1];
        }
        Keys_num += full_info[k].iKey_chest_num[0]; // кол-во ключей в целом за цикл
 
        if (Keys_num < full_info[k].iKey_chest_num[1]) // Предпроверка, если всего сундуков больше чем ключей то все открыть не получиться
        {
            cout << "Imposible" << endl;
        }
        //Используется метод жадного обхвата, ищем максимальное кол-во ключей в каком либо сундуке,
        //который можем изначально открыть
        for (j = 0; j < full_info[k].iKey_chest_num[1]; j++)
        {
            max_count = 0;
            num = 0;
            for (i = 0; i < full_info[k].iKey_chest_num[1]; i++)
            {
                
                if (Keys.find(full_info[k].Chest_info[i][0]) != Keys.end())
                if (full_info[k].Chest_info[i][1] >= max_count)
                {
                    max_count = full_info[k].Chest_info[i][1];
                    num = i;
                }
 
            }
            // найдя максимальное кол-во ключей открываем сундук
            if (Keys.find(full_info[k].Chest_info[num][0]) != Keys.end())
            {
 
                for ( i = 2; i < full_info[k].Chest_info[num][1] + 2; i++)
                {
                    Keys.insert(full_info[k].Chest_info[num][i]);
 
                }
                it = Keys.find(full_info[k].Chest_info[num][0]);
                Keys.erase(it); // удаляем использованный ключ
                full_info[k].Chest_info[num][0] = -1; //обозначаем что сундук открыт 
                full_info[k].Chest_info[num][1] = -1; // и больше по нему не проводить поиск
                op_chest++; 
                itoa(num + 1, c_tmp, 10);
                str += c_tmp;
                str += " ";
 
            }
        // Если открыты все сундуки выводим последовательность            
        }
        if (op_chest == full_info[k].iKey_chest_num[1])
        {
            fout << str;
            cout << str;
            
        }
        else
        // Если нет, значит для данного тестового набора открыть все сундуки не возможно
        {
            fout << "Impossible";
            cout << "Impossible";
        }
        
        str = "";
        fout << endl;
        cout << endl;
    }
    // Задержка экрана, закрытие файла, удаление динамических переменных
    cout << endl;
    fout << endl;
    system("pause");
    fin.close();
    fout.close();
    for (int z = 0; z < full_info[k].iKey_chest_num[1] + 1; z++)
    {
        delete[] full_info[k].Chest_info[z];
    }
    delete [] full_info;
    delete [] tmp_arr;
 
    return;
}
ifstream fin("D-small-practice.in", ios_base::in);
файл прикреплен к изначальному сообщению.


Просьба сильно не ругаться =)
0
17.11.2017, 18:13
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
17.11.2017, 18:13
Помогаю со студенческими работами здесь

Определите по этим данным, сколько банок не прострелил Гарри и сколько банок не прострелил Ларри?
Ковбои Гарри и Ларри отдыхали на природе. Решив пострелять, они выставили на бревно несколько банок...

Бюджет 4500 гр. Конфигурация работа в Office, AutoCAD, игра Assassin, онлайн игра World of Tanks
Собираю компьютер для сестры. Основные требования: работа в Microsoft Office, AutoCAD, игра...

Игра в загадки. Загадать загадку. Если ответ верен – поздравить пользователя. Затем сообщить, что игра окончена.
Всем привет! Меня зовут VitoScaletta, совсем недавно начал обучаться JS, но очень тяжело в голову...

Определите по данным, сколько банок не прострелил Гарри и сколько банок не прострелил Ларри.
Бандиты Гарри и Ларри отдыхали на природе. Решив пострелять, они выставили на бревно несколько...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
3
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru