UVa 227 Puzzle(习题3-5)

这题用了不少的时间,反正就是模拟吧,最主要的地方就是字符串获取以及一些细节的地方,有的地方一不注意就会出问题

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

char map[6][6];
int a[4][2]={{-1,0},{1,0},{0,-1},{0,1}};  //简化代码 
int x,y,cnt = 0;
		
bool order(char c)           //执行命令 
{
	int now,nowx,nowy;
	char tmp = ' ';
	if(c=='A') now = 0;
	else if(c=='B') now = 1;
	else if(c=='L') now = 2;
	else if(c=='R') now = 3;
	else return 0;
	nowx = x + a[now][0];
	nowy = y + a[now][1];
	if(nowx<0||nowx>4||nowy<0||nowy>4) return 0;
	else
	{
		map[x][y] = map[nowx][nowy];
		map[nowx][nowy] = ' ';
		x = nowx; y = nowy;
	}
	return true;
}

int main()
{
	//freopen("std.in","r",stdin);
	//freopen("std.out","w",stdout);
	char c;
	while(gets(map[0])) 
	{
		if(map[0][0]=='Z') break;  //获取字符串 
		if(cnt>0) cout<<endl; 
	    int flag = 0,sign = 0;
        for(int i = 1;i<5;i++) gets(map[i]);
        for(int i= 0;i<5;i++)
           for(int j=0;j<5;j++)
              if(map[i][j]>'Z'||map[i][j]<'A'){x=i;y=j;}
		while(1)
		{
			c = getchar();
			if(c=='\n') continue;
			if(c=='0') break;
			if(order(c)) continue;
			else {sign = 1; continue;}
		}
		c = getchar();
		cout<<"Puzzle #"<<++cnt<<":"<<endl;
		if(sign) cout<<"This puzzle has no final configuration."<<endl;
		else
		{
			for(int i = 0;i<5;i++)
			{
			      for(int j = 0;j<4;j++)
			      cout<<map[i][j]<<" ";
			      cout<<map[i][4]<<endl;
			}
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值