⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2107.cpp

📁 这是哈尔滨工业大学acmOJ的源代码
💻 CPP
字号:
/*  This Code is Submitted by wywcgs for Problem 2107 on 2005-12-12 at 12:29:46 */ 
#include <cstdio>

const int MAX = 128;
const char di[] = "NESW";

class Point {
public:
	int x, y;
	int dir;
	void init(int, int, int);
	void operator +=(const Point&);
	bool crash(const int, const int) const;
};
void Point::init(int bx, int by, int bd = -1) {
	x = bx; y = by; dir = bd;
}
void Point::operator +=(const Point& p) {
	x += p.x; y += p.y;
}
bool Point::crash(const int w, const int h) const {
	return (x == 0 || x == w+1 || y == 0 || y == h+1);
}

const Point dir[4] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} };

int main()
{
	Point robot[MAX];
	int t, T, i, j;
	int w, h, n, m;
	char act;

	scanf("%d", &T);
	for(t = 0; t < T; t++) {
		scanf("%d %d", &w, &h);
		scanf("%d %d", &n, &m);
		int map[MAX][MAX] = { 0 };
		for(i = 1; i <= n; i++) {
			int x, y;
			scanf("%d %d %c", &x, &y, &act);
			map[x][y] = i;
			for(j = 0; j < 4; j++) {
				if(di[j] == act) {
					robot[i].init(x, y, j);
					break;
				}
			}
		}
		bool crash = false;
		for(i = 0; i < m; i++) {
			int o, re;
			scanf("%d %c %d", &o, &act, &re);
			if(!crash) {
				switch(act) {
				case 'L':
					robot[o].dir -= re;
					while(robot[o].dir < 0) {
						robot[o].dir += 4;
					}
					break;
				case 'R':
					robot[o].dir = (robot[o].dir + re) % 4;
					break;
				case 'F':
					for(j = 0; j < re && !crash; j++) {
						map[robot[o].x][robot[o].y] = 0;
						robot[o] += dir[robot[o].dir];
						if(robot[o].crash(w, h)) {
							printf("Robot %d crashes into the wall\n", o);
							crash = true;
						} else if(map[robot[o].x][robot[o].y] != 0) {
							printf("Robot %d crashes into robot %d\n", o, 
								map[robot[o].x][robot[o].y]);
							crash = true;
						} else {
							map[robot[o].x][robot[o].y] = o;
						}
					}
					break;
				}
			}
		}
		if(!crash) {
			printf("OK\n");
		}
	}
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -