101 The Blocks Problem

两块砖位置相同时不进行操作

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Blocks
{
public:
	void ReadBlocks();
	void ComputeBlocks();
	void OutputResult();
private:
	void moveBlocks(const int& iFrom, const int& iTo);
private:
	int m_iNumber = 0;
	vector<string> m_vecCommands;
	vector<int> m_vecFrom;
	vector<string> m_vecOperation;
	vector<int> m_vecTo;
	vector<vector<int>> m_vecBlocks;
	vector<int> m_vecBlocksPosition;
};

void Blocks::ReadBlocks()
{
	cin >> m_iNumber;
	for (int i = 0; i < m_iNumber; ++i)
	{
		vector<int> vecNumber;
		vecNumber.push_back(i);
		m_vecBlocks.push_back(vecNumber);
		m_vecBlocksPosition.push_back(i);
	}
	string strCommand;
	while (true)
	{
		cin >> strCommand;
		if (strCommand.compare("quit") == 0)
		{
			return;
		}
		m_vecCommands.push_back(strCommand);
		int iNumber;
		cin >> iNumber;
		m_vecFrom.push_back(iNumber);
		cin >> strCommand;
		m_vecOperation.push_back(strCommand);
		cin >> iNumber;
		m_vecTo.push_back(iNumber);
	}
}

void Blocks::moveBlocks(const int& iFrom, const int& iTo)
{
	bool isMove = false;
	for (auto it = m_vecBlocks[m_vecBlocksPosition[iFrom]].begin(); it != m_vecBlocks[m_vecBlocksPosition[iFrom]].end();)
	{
		if (isMove)
		{
			int iPosition = 0;
			if (iTo == -1)
			{
				iPosition = *it;
			}
			else
			{
				iPosition = m_vecBlocksPosition[iTo];
			}
			m_vecBlocksPosition[*it] = iPosition;
			m_vecBlocks[iPosition].push_back(*it);
			it = m_vecBlocks[m_vecBlocksPosition[iFrom]].erase(it);
		}
		else
		{
			if (*it == iFrom)
			{
				isMove = true;
				if (iTo != -1)
				{
					m_vecBlocks[m_vecBlocksPosition[iTo]].push_back(iFrom);
					it = m_vecBlocks[m_vecBlocksPosition[iFrom]].erase(it);
					continue;
				}
			}
			++it;
		}
	}
	if (iTo != -1)
	{
		m_vecBlocksPosition[iFrom] = m_vecBlocksPosition[iTo];
	}
}

void Blocks::ComputeBlocks()
{
	for (size_t i = 0; i < m_vecCommands.size(); ++i)
	{
		if (m_vecBlocksPosition[m_vecFrom[i]] != m_vecBlocksPosition[m_vecTo[i]])
		{
			if (m_vecCommands[i].compare("move") == 0)
			{
				if (m_vecOperation[i].compare("onto") == 0)
				{
					this->moveBlocks(m_vecTo[i], -1);
					this->moveBlocks(m_vecFrom[i], m_vecTo[i]);
				}
				else if (m_vecOperation[i].compare("over") == 0)
				{
					this->moveBlocks(m_vecFrom[i], m_vecTo[i]);
				}
			}
			else if (m_vecCommands[i].compare("pile") == 0)
			{
				if (m_vecOperation[i].compare("onto") == 0)
				{
					this->moveBlocks(m_vecTo[i], -1);
					this->moveBlocks(m_vecFrom[i], m_vecTo[i]);
				}
				else if (m_vecOperation[i].compare("over") == 0)
				{
					this->moveBlocks(m_vecFrom[i], m_vecTo[i]);
				}
			}
		}
	}
}


void Blocks::OutputResult()
{
	for (size_t i = 0; i < m_vecBlocks.size(); ++i)
	{
		cout << i << ":";
		for (auto it : m_vecBlocks[i])
		{
			cout << " " << it;
		}
		cout << endl;
	}
}

int main()
{
	Blocks blocks;
	blocks.ReadBlocks();
	blocks.ComputeBlocks();
	blocks.OutputResult();
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The Readers-Writer problem is a classic synchronization problem in computer science. In this problem, multiple threads need access to a shared resource. However, some of these threads only need to read from the resource, while others need to write to it. To ensure correctness and avoid data corruption, we need to implement a solution that allows concurrent reading but exclusive writing. One common solution to this problem is to use a semaphore or a lock to coordinate access to the shared resource between readers and writers. Here's an implementation of the Readers-Writer problem using a mutex and a condition variable in C++: ```c++ #include <mutex> #include <condition_variable> class ReadersWriter { public: ReadersWriter() : readers_count(0), writer_active(false) {} void start_read() { std::unique_lock<std::mutex> lock(m); while (writer_active) { // wait until no writer is active cv_read.wait(lock); } ++readers_count; } void end_read() { std::unique_lock<std::mutex> lock(m); --readers_count; if (readers_count == 0) { cv_write.notify_one(); // notify waiting writers } } void start_write() { std::unique_lock<std::mutex> lock(m); while (writer_active || readers_count > 0) { // wait until no readers or writers are active cv_write.wait(lock); } writer_active = true; } void end_write() { std::unique_lock<std::mutex> lock(m); writer_active = false; cv_write.notify_one(); // notify waiting writers cv_read.notify_all(); // notify waiting readers } private: std::mutex m; std::condition_variable cv_read; std::condition_variable cv_write; int readers_count; bool writer_active; }; ``` In this implementation, we use a mutex `m` to ensure exclusive access to the shared variables `readers_count` and `writer_active`. We also use two condition variables `cv_read` and `cv_write` to coordinate access between readers and writers. When a thread wants to start reading, it first acquires the lock `m` and then waits on the condition variable `cv_read` until there are no active writers. If there are active writers, the thread blocks until it's notified by a writer that it has finished writing. Once it's safe to read, the thread increases the `readers_count` and releases the lock `m`. When a thread wants to end reading, it first acquires the lock `m` and then decreases the `readers_count`. If there are no more readers, the thread notifies waiting writers using the condition variable `cv_write` and releases the lock `m`. When a thread wants to start writing, it first acquires the lock `m` and then waits on the condition variable `cv_write` until there are no active readers or writers. If there are active readers or writers, the thread blocks until it's notified by a reader or writer that it has finished accessing the shared resource. Once it's safe to write, the thread sets the `writer_active` flag to true and releases the lock `m`. When a thread wants to end writing, it first acquires the lock `m` and then sets the `writer_active` flag to false. The thread then notifies waiting writers using the condition variable `cv_write` and waiting readers using the condition variable `cv_read`, and releases the lock `m`.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值