作业疯了 - 部分题目代码

之前看到老番茄玩所以关注了一下这个游戏……
一开始bug很多,结果和室友直播就,经常切出来写代码……
分享几个题吧。

点灯问题

点灯问题的话这个已经是被很多人研究过的问题了
https://github.com/pmneila/Lights-Out

之前想写分析的但是没有成功orz
简单的操作是如果行列有奇数的话,先把最中间那一列点亮然后再调整。

数字

1-9组成五位数减四位数,得到33333.

int main()
{
	for (int i = 33333; i < 50000; i++) {
		int t = i - 33333;
		string s = to_string(i) + to_string(t);
		sort(s.begin(), s.end());
		if (s == "123456789")
			cout << i << " - " << t << endl;
	}
	return 0;
}

倒水

就,杯子来回倒水使得平分那题。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
#define ll long long


// 杯子容量
vector cups({ 8, 5, 3 });

// 把i杯里的水倒入j
vector pour(vector before, int i, int j) {
	int all = before[i] + before[j];
	if (all > cups[j]) {
		before[j] = cups[j];
		before[i] = all - cups[j];
	}
	else {
		before[i] = 0;
		before[j] = all;
	}
	return before;
}


int main()
{
	const int waternum = 8;
	// 初始水位
	vector waterbegin({ waternum, 0, 0 });
	// 记录是否访问过
	set> rec;
	// 记录前一个数据
	map, vector> before;


	queue> que;
	que.push(waterbegin);
	vector last; // 记录结束状态	
	while (!que.empty()) {
		vector water = que.front();
		que.pop();

		auto s = rec.find(water);
		if (s != rec.end()) continue;
		if (water[0] * 2 == waternum || water[1] * 2 == waternum || water[2] * 2 == waternum) {
			last = water;
			break;
		}

		// pour water in cup[i] to cup[j]
		for (int i = 0; i < 3; i++) {
			if (water[i] == 0) continue;
			for (int j = 0; j < 3; j++) {
				if (i == j || water[j] == cups[j]) continue;
				auto newwater = pour(water, i, j);
				if (rec.find(newwater) != rec.end()) continue;
				assert(newwater != water);
				que.push(newwater);
				before[newwater] = water;
			}

		}
		rec.insert(water);
	}
	// 倒着输出
	stack> mystack;
	while (last != waterbegin) {
		mystack.push(last);
		last = before[last];
	}

	while (!mystack.empty()) {
		auto t = mystack.top();
		mystack.pop();
		cout << t[0] << t[1] << t[2] << endl;
	}
	return 0;
}

蜡烛照明

对于一个N*M的格子,每个蜡烛可以照亮十字形的五个格子。求问怎么摆放。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
#define ll long long

class board {
public:
	vector> lighted;
	int lightnum = 0;
	board() : lighted(6, vector(7, 0)) {}
	
	void setlight(int x, int y) {
		lightnum++;
		lighted[x][y] = 2;
		int setX[] = { 1, -1, 0, 0 };
		int setY[] = { 0, 0, 1, -1 };
		for (int i = 0; i < 4; i++) {
			int newX = x + setX[i]; 
			int newY = y + setY[i];
			if (newX < 0 || newY < 0 || newX >= 6 || newY >= 7) continue;
			lighted[newX][newY] = 1;
		}
	}

	bool search() {
		if (lightnum > 11) return false;
		bool setted = true;
		int x, y;
		for (int i = 0; i < 6; i++) {
			if (!setted) break;
			for (int j = 0; j < 7; j++) {
				if (!setted) break;
				if (lighted[i][j] == 0) {
					setted = false;
					x = i;
					y = j;
					break;
				}			
			}
		}
		if (setted) {
			print();
			return true;
		}
		int setX[] = { 1, -1, 0, 0, 0 };
		int setY[] = { 0, 0, 0, 1, -1 };
		for (int i = 0; i < 5; i++) {
			int newX = x + setX[i]; 
			int newY = y + setY[i];
			if (newX < 0 || newY < 0 || newX >= 6 || newY >= 7) continue;
			if (lighted[newX][newY] == 1) continue;
			class board newboard = *this;
			newboard.setlight(newX, newY);
			newboard.search();
		}
		return false;
		
	}
	void print() {
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				if (lighted[i][j] == 2) cout << "O";
				else cout << '.';
			}
			cout << endl;
		}
		cout << endl;
	}

};

int main()
{
	class board myboard;
	myboard.search();
	return 0;
}

你可能感兴趣的:(杂文)