poj 2632 Crashing Robots

17 篇文章 0 订阅
1 篇文章 0 订阅

模拟题,考验一些细节的东西,但我真的是太笨了,细节方面总是出错。
这一题最关键的是如何用数组表示出这个二维坐标平面,我个人觉得把题目给的图顺时针旋转90度的话,就比较方便。

题源

以下是代码(虽然是从网上看的):

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;
const int MAX = 1e3;
struct node
{
	int x, y;
	int sign;
};

int run[4][2] = {
	{0,1},{-1,0},{0,-1},{1,0}
};
map<char, int> m;
node pos[MAX];
int maze[MAX][MAX] = { {0} };
int A, B, N, M;
int main()
{
	m['N'] = 0, m['W'] = 1, m['S'] = 2, m['E'] = 3;
	int t; cin >> t;
	while (t--)
	{
		memset(maze, 0, sizeof(maze));
		cin >> A >> B >> N >> M; int a, b; char ch; //cout << A << B << N << M;
		for (int i = 1; i <= N; i++)
		{
			//cout << i << endl;
			cin >> pos[i].x >> pos[i].y >> ch;
			maze[pos[i].x][pos[i].y] = i;
			pos[i].sign = m[ch];
		}
		bool f = false;
		while (M--)
		{
			cin >> a >> ch >> b; //cout << a << ch << b << endl;
			while (b--) {
				if (f) break;
				if (ch == 'L') { pos[a].sign = (pos[a].sign + 1) % 4; }
				else if (ch == 'R') { pos[a].sign = (pos[a].sign + 3) % 4; }
				else {
					//cout << a << endl;
					maze[pos[a].x][pos[a].y] = 0;
					int x = pos[a].x+= run[pos[a].sign][0], y = pos[a].y+= run[pos[a].sign][1]; //cout << "first    " << x << " " << y << endl;
					//x , y ;
					//cout << "second   " << x << " " << y << " " <<  B << " " << A << endl;
					if (x < 1 || y < 1 || x > A || y > B) 
					{ 
						f = true;
						printf("Robot %d crashes into the wall\n", a); break;
					}
					else
					{
						if (maze[x][y] && maze[x][y] != a) {
							f = true, printf("Robot %d crashes into robot %d\n", a, maze[x][y]); break;
						}
						else maze[x][y] = a;
							//{ swap(maze[pos[a].x][pos[a].y], maze[x][y]), pos[a].x = x, pos[a].y = y; }
					}
				}
			}
		}
		if (!f)
			cout << "OK" << endl;
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值