实现谜题的空格字符换位

有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母。一共有4种指令:A,B,L,R,分别表示把空格上、下、左右的相邻字母移到空格种。输入初始网格和指令序列(以数字0结束),输出指令执行完毕后的网格。如果有非法指令,应输出“This puzzle has no final configuration.”。注意指令要以0结束输出,例如:AAALQRR0

#include<stdio.h>

int main()

{

char a[5][5], b[100]; int n, m;

for (int i = 0; i < 5; i++)

for (int j = 0; j < 5; j++)

{

a[i][j] = getchar();//输入字母矩阵

}

for (int i = 0; i < 5; i++)//打印字母矩阵,顺手找空格位置(n,m)

{

for (int j = 0; j < 5; j++)

{

printf("%c ", a[i][j]); if (a[i][j] == ' ') { n = i; m = j; }

}printf("\n");

}

scanf("%[^0]", b);//输入除了0以外的指令,输入0结束输出。

for (int t = 0; t < strlen(b); t++)//读取指令,例如A就会让空格被赋值为即将被移动的位置的

{//字符,再将需移动到位置字符赋值为空格,即可实现换位。且字符(n,m)始终指向空格位置。

if (b[t] == 'A' && n != 0) { a[n][m] = a[n - 1][m]; a[--n][m] = ' '; }

else if (b[t] == 'B' && n != 4) { a[n][m] = a[n + 1][m]; a[++n][m] = ' '; }

else if (b[t] == 'L' && m != 0) { a[n][m] = a[n][m - 1]; a[n][--m] = ' '; }

else if (b[t] == 'R' && m != 4) { a[n][m] = a[n][m + 1]; a[n][++m] = ' '; }

else

printf("This puzzle no final configuration\n");

return 0;

}for (int i = 0; i < 5; i++)//打印换位的字母矩阵。

{

for (int j = 0; j < 5; j++)

printf("%c ", a[i][j]);

printf("\n");

}

return 0;

}

三壶谜题是一个经典的逻辑问题,通过使用三个容量不同的水壶,来实现特定容量的水的测量。以下是C++编程实现三壶谜题的一种可能解决方案: ```cpp #include <iostream> #include <queue> #include <set> using namespace std; struct State { int x, y, z; string path; }; void solveThreeJugs(int a, int b, int c, int d) { queue<State> q; set<pair<int, int>> visited; State init = {0, 0, c, ""}; q.push(init); visited.insert(make_pair(0, c)); while (!q.empty()) { State curr = q.front(); q.pop(); if (curr.x == d || curr.y == d || curr.z == d) { cout << curr.path << endl; return; } // 从x壶向y壶倒水 if (curr.x > 0) { int pour = min(curr.x, b - curr.y); State next = {curr.x - pour, curr.y + pour, curr.z, curr.path + "X" + to_string(pour) + "Y"}; if (visited.find(make_pair(next.x, next.y)) == visited.end()) { q.push(next); visited.insert(make_pair(next.x, next.y)); } } // 从x壶向z壶倒水 if (curr.x > 0) { int pour = min(curr.x, c - curr.z); State next = {curr.x - pour, curr.y, curr.z + pour, curr.path + "X" + to_string(pour) + "Z"}; if (visited.find(make_pair(next.x, next.z)) == visited.end()) { q.push(next); visited.insert(make_pair(next.x, next.z)); } } // 从y壶向x壶倒水 if (curr.y > 0) { int pour = min(curr.y, a - curr.x); State next = {curr.x + pour, curr.y - pour, curr.z, curr.path + "Y" + to_string(pour) + "X"}; if (visited.find(make_pair(next.x, next.y)) == visited.end()) { q.push(next); visited.insert(make_pair(next.x, next.y)); } } // 从y壶向z壶倒水 if (curr.y > 0) { int pour = min(curr.y, c - curr.z); State next = {curr.x, curr.y - pour, curr.z + pour, curr.path + "Y" + to_string(pour) + "Z"}; if (visited.find(make_pair(next.y, next.z)) == visited.end()) { q.push(next); visited.insert(make_pair(next.y, next.z)); } } // 从z壶向x壶倒水 if (curr.z > 0) { int pour = min(curr.z, a - curr.x); State next = {curr.x + pour, curr.y, curr.z - pour, curr.path + "Z" + to_string(pour) + "X"}; if (visited.find(make_pair(next.x, next.z)) == visited.end()) { q.push(next); visited.insert(make_pair(next.x, next.z)); } } // 从z壶向y壶倒水 if (curr.z > 0) { int pour = min(curr.z, b - curr.y); State next = {curr.x, curr.y + pour, curr.z - pour, curr.path + "Z" + to_string(pour) + "Y"}; if (visited.find(make_pair(next.y, next.z)) == visited.end()) { q.push(next); visited.insert(make_pair(next.y, next.z)); } } } cout << "无法得到目标容量的水" << endl; } int main() { int a, b, c, d; cout << "请输入三个水壶的容量(以空格分隔):"; cin >> a >> b >> c; cout << "请输入目标容量:"; cin >> d; solveThreeJugs(a, b, c, d); return 0; } ``` 这段代码使用了广度优先搜索算法来遍历所有可能的状态,直到找到目标容量的水或者无法得到目标容量的水。程序会输出一系列操作,其中X表示从x壶倒水,Y表示从y壶倒水,Z表示从z壶倒水。如果无法得到目标容量的水,则输出"无法得到目标容量的水"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值