UVa227

作为一只考研狗,突然觉得作为SE专业的学生没学过ACM大学好像少了点什么,所以还是利用平日的空余时间来刷一刷这方面的东西,给自己的大学留下完整的回忆。

UVa227是水题,但要注意两个case间输出一个空行,最后一行注意只能输出一个换行。

#include<stdio.h>
#include<string.h>
#define se 5
const char*msg="This puzzle has no final configuration.";
char borad[se][se]; //保存网格内容
bool move(int&x,int&y,char c){  //移动空格,注意传入参数形式
	int x1=x,y1=y;
	char t;
	if(c=='A') x--;
	if(c=='B') x++;
	if(c=='L') y--;
	if(c=='R') y++;
	if(x>=0&&x<se&&y>=0&&y<se) { t=borad[x][y]; borad[x][y]=borad[x1][y1]; borad[x1][y1]=t; return true;}
	return false;
}
int main()
{
	int kase=1;
	while(true){
	int x=-1,y=-1,ok=1;
	char c;
	for(int i=0;i<se;i++){
		for(int j=0;j<se;j++){
			if((borad[i][j]=getchar())==' '){ x=i;y=j;}
			if(borad[i][j]=='Z') return 0;
		}
		getchar();
	}
	while((c=getchar())!='0'){
		if(c!='\n'){ if(!move(x,y,c)) ok=0; }
	}
	getchar();
	if(kase>1) printf("\n");
	printf("Puzzle #%d:\n",kase);
	if(ok){
		for(int i=0;i<se;i++){
			for(int j=0;j<se-1;j++){ printf("%c ",borad[i][j]);}
			printf("%c\n",borad[i][se-1]);
		}		
	}
	else { printf("%s\n",msg);}
	kase++;
}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值