Puzzle UVA - 227

这道题...输出格式令人好无语,用c写不下去了,一直AW,心态爆炸改用c++重写,改了贼多次,终于搞定

#include <iostream>
#include <string>

using namespace std;

string strs[5];
int x, y;  // 记录空格的坐标
int first = 1;  // 判断是否为第一个输出
int kcase = 1;  // 判断操作次数
int flag = 1;  // 判断操作是否非法

int input();
int output();

int main() {
	while (true) {
		input();
		if (strs[0][0] == 'Z')
			return 0;
		output();
	}
	return 0;
}

int input() {
	char c;
	for (int i = 0; i < 5 ; i++) {  // 输入5行数据
		getline(cin, strs[i]);
		if (strs[0][0] == 'Z')  // 若第一个字符是Z则退出
			return 0;
	}
	
	for (int i = 0; i < 5 ; i++)  // 记录空格的位置
		for (int j = 0; j < 5; j++)
			if (strs[i][j] == ' ') {
				x = i; y = j;
			}

	flag = 1;
	while ((c = getchar()) != '0') {
		if (flag && c == 'A') {
			if (x > 0) {
				strs[x][y] = strs[x-1][y];
				strs[x-1][y] = ' ';
				x -= 1;
			}
			else
				flag = 0;
		}
		else if (flag && c == 'B') {
			if (x < 4) {
				strs[x][y] = strs[x+1][y];
				strs[x+1][y] = ' ';
				x += 1;
			}
			else
				flag = 0;
		}
		else if (flag && c == 'L') {
			if (y > 0) {
				strs[x][y] = strs[x][y-1];
				strs[x][y-1] = ' ';
				y -= 1;
			}
			else
				flag = 0;
		}
		else if (flag && c == 'R') {
			if (y < 4) {
				strs[x][y] = strs[x][y+1];
				strs[x][y+1] = ' ';
				y += 1;
			}
			else
				flag = 0;
		}
		else if (flag && c != '\n')
			flag = 0;
		else
			continue;
	}
	cin.get();  // 接收换行符

	return 0;
}

int output() {
	if (first)
		first = 0;
	else
		putchar('\n');
	cout << "Puzzle #" << kcase++ << ':' << endl;
	if (flag == 0)
		cout << "This puzzle has no final configuration." << endl;
	else
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				if (j == 4)
					cout << strs[i][j];
				else
					cout << strs[i][j] << ' ';
			}
			cout << endl;
		}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值