技术0
经验12187
魅力9
人气176
分享52
原创1
注册时间2022-7-31
最后登录2025-8-7
阅读权限100
在线时间643 小时
主题253
回帖566

版主
禁止访问
  
- 积分
- 7734
- 人气
- 176
- 分享
- 52
 
|
需求:
在教室中,有8列6行共48个座位,你们班有48人,学号为1~48,他们每个人都随机坐在一个座位上(有这样一个二位数组classRoom[8][6],共有48个元素,每个元素为1~48中任意一个整数,且每个数只能出现一次),你是一名课代表,手上有每一名同学的共48本作业,你从教室门口开始发作业,但是你每次只能看到最顶上三本(有这样一个homeworks[48]数组,共有48个元素,每个元素为1~48中任意一个整数,且每个数只能出现一次,你每次只能读取homeworks[0],homeworks[1],homeworks[2]三个元素),如果同学在你左右两侧或前后两侧,你可以把作业发给他(假设下标位于classRoom[x][y],如果classRoom[x][y],classRoom[x+1][y],classRoom[x-1][y],classRoom[x][y+1],classRoom[x][y-1]中任意一个数据与homeworks[0],homeworks[1],homeworks[2]中任意一个数据相等,那么就可以弹出homeworks[0],homeworks[1],homeworks[2]中与classRoom中相等的数据)。你的移动方法:每次只能移动一步(假设下标位于classRoom[x][y],那么每次移动只能出现classRoom[x+1][y],classRoom[x-1][y],classRoom[x][y+1],classRoom[x][y-1]四种情况)。课代表最少需要移动多少步才能发完作业(下标需要移动多少次才能使homeworks数组中的所有元素都被弹出)
我感觉还是挺有用的,我一个课代表天天发作业发得头晕
文心一言结果:
[C++] 纯文本查看 复制代码 #include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>
#include <tuple>
using namespace std;
struct State {
int x, y, steps;
vector<int> hw; // Remaining homeworks
};
int minStepsToDistributeHomework(vector<vector<int>>& classRoom, vector<int>& homeworks) {
int directions[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // right, left, down, up
int rows = classRoom.size();
int cols = classRoom[0].size();
unordered_set<int> hwSet(homeworks.begin(), homeworks.end());
queue<State> q;
unordered_set<tuple<int, int, string>> visited; // (x, y, hw state hash)
// Start from the door (0, 0) with all homeworks
q.push({0, 0, 0, homeworks});
visited.insert({0, 0, to_string(hwSet)});
while (!q.empty()) {
State curr = q.front();
q.pop();
int x = curr.x;
int y = curr.y;
int steps = curr.steps;
vector<int> hw = curr.hw;
// If all homeworks are distributed, return the steps taken
if (hw.empty()) {
return steps;
}
// Try to match and distribute homework in the current position
for (int i = 0; i < hw.size(); ++i) {
if (hw[i] == classRoom[x][y]) {
hw.erase(hw.begin() + i);
break;
}
}
// If homework is distributed, update the set and continue
hwSet.clear();
for (int hwId : hw) {
hwSet.insert(hwId);
}
// Try moving in all four directions
for (auto& dir : directions) {
int nx = x + dir[0];
int ny = y + dir[1];
if (nx >= 0 && nx < rows && ny >= 0 && ny < cols) {
// Try to match and distribute homework in the new position
vector<int> newHw = hw;
for (int i = 0; i < newHw.size(); ++i) {
if (newHw[i] == classRoom[nx][ny]) {
newHw.erase(newHw.begin() + i);
break;
}
}
// If new state is not visited, add to queue
string newHwStr = to_string(unordered_set<int>(newHw.begin(), newHw.end()));
if (visited.find({nx, ny, newHwStr}) == visited.end()) {
q.push({nx, ny, steps + 1, newHw});
visited.insert({nx, ny, newHwStr});
}
}
}
}
// If we exit the loop, it means we couldn't distribute all homeworks (should never happen with valid input)
return -1;
}
int main() {
vector<vector<int>> classRoom = {
// Example initialization, you should replace it with random seating arrangement
{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}
};
vector<int> homeworks = {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};
// Shuffle homeworks array if needed to simulate randomness
random_shuffle(homeworks.begin(), homeworks.end());
int steps = minStepsToDistributeHomework(classRoom, homeworks);
cout << "Minimum steps to distribute all homework: " << steps << endl;
return 0;
}
deepseek结果:
[C++] 纯文本查看 复制代码 服务器繁忙,请稍后再试 
|
评分
-
查看全部评分
|