• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

  • 联系
  • 订阅
  • 管理

View Post

Sicily 1797. Do solve it directly

水题,看明白题目就可以了。

题意是,控制一个机器人行动,s,r,l为控制机器人操作

给一个函数,先按它给的定义计算出该函数,然后解析该字符串

 

#include <iostream>
#include <string>
using namespace std;

string z(int , int);

struct point
{
	int x;
	int y;
	int now_direction;
};

point analysis(string);

int main()
{
	int n;
	int a,b;
	point p;

	//freopen("C:\\Users\\Haojian\\Desktop\\test.txt", "r", stdin);
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		cin >> a >> b;
		p = analysis(z(a,b));
		cout << p.x << " " << p.y << endl;
	}
	return 0;

	return 0;
}

point analysis(string s)
{
	point p= {0,0,0};
	for (int i = 0; i < s.size(); i++)
	{
		switch (s[i])
		{
		case 'r':
			p.now_direction = (p.now_direction+1)%4;
			break;
		case 'l':
			p.now_direction = (p.now_direction-1)%4;		
			break;
		case 's':
			switch (p.now_direction)
			{
				case 0:
					p.y++;
					break;
				case 1:
					p.x++;
					break;
				case 2:
					p.y--;
					break;
				case 3:
					p.x--;
					break;
			}		
			break;
		}
	}
	return p;

}
string z(int a, int b)
{
	string s;
	if (a <= 0 || b <= 0 )
	{
		s = "";
		return (s);
	}
	else
	{
		s = "s" + z(a-b,b) + "r" + z(b-a,a);
		return s;
	}
}

posted on 2010-09-26 19:50  sysuwhj  阅读(261)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3