Uva227 - Puzzle

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
int case1 = 0;
int last = 1;
int main(){
	while (1){
		char p[5][7];
		char buf[7];
		int m, n, ilg = 1, first;
		char c[1000];
		for (int i = 0; i < 5; i++){
			gets(buf);
			if (buf[0] != '\0') strcpy(p[i], buf);
			else i--;
			if (strcmp(p[0], "Z") == 0) return 0;
			for (int j = 0; j < 5; j++){
				if (p[i][j] == ' '){
					m = i;
					n = j;
				}
			}
		}
		char ch;
		for (int i = 0;; i++){
			ch = getchar();
			if (ch != '\n')
				c[i] = ch;
			else i--;
			if (ch == '0'){
				c[i + 1] = '\0';
				break;
			}
		}
		for (int k = 0; k<strlen(c); k++){
			while (c[k] != '0')
			{
				if (c[k] == 'A'&&m - 1 > -1){
					p[m][n] = p[m - 1][n];
					p[m - 1][n] = ' ';
					m--; break;
				}
				else if (c[k] == 'B'&&m + 1 < 5){
					p[m][n] = p[m + 1][n];
					p[m + 1][n] = ' ';
					m++; break;
				}
				else if (c[k] == 'L'&&n - 1 > -1){
					p[m][n] = p[m][n - 1];
					p[m][n - 1] = ' ';
					n--; break;
				}
				else if (c[k] == 'R'&&n + 1 < 5){
					p[m][n] = p[m][n + 1];
					p[m][n + 1] = ' ';
					n++; break;
				}
				else{
					ilg = 0;
					break;
				}
			}
		}
		if (last) last = 0;
		else printf("\n");
		printf("Puzzle #%d:\n", ++case1);
		if (ilg){
			for (int i = 0; i < 5; i++){
				first = 1;
				for (int j = 0; j < 5; j++){
					if (first) first = 0;
					else printf(" ");
					printf("%c", p[i][j]);
				}
				printf("\n");
			}
		}
		else printf("This puzzle has no final configuration.\n");
		memset(c, '\0', 1000 * sizeof(char));
	}
	return 0;
}
题目不难,就是输入输出麻烦。竟然因为This puzzle has no final configuration.少打了个final而wrong answer,坑了几个小时。。。。  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值