POJ2632
来源:互联网 发布:程序员该怎么建立博客 编辑:程序博客网 时间:2024/05/01 16:21
摘要:模拟题,水
#include <iostream>
using namespace std;
const int size = 100;
typedef struct Position{
int machine;
int dir;
}Position;
typedef struct Machine{
int x;
int y;
}Machine;
Machine robot[size];
Position graph[size+1][size+1];
int getDir(char d)
{
int dir = 0;
switch(d){
case 'N':
dir = 1;
break;
case 'E':
dir = 2;
break;
case 'S':
dir = 3;
break;
case 'W':
dir = 4;
break;
default:
dir = 0;
break;
}
return dir;
}
void getNewPosi(int x, int y, int & new_x, int & new_y)
{
switch(graph[x][y].dir){
case 1:
new_x = x;
new_y = y+1;
break;
case 2:
new_x = x+1;
new_y = y;
break;
case 3:
new_x = x;
new_y = y-1;
break;
case 4:
new_x = x-1;
new_y = y;
break;
default:
break;
}
}
bool checkNewPosi(int r, int new_x, int new_y, int ew, int ns, bool & crash)
{
if( new_x>ew || new_x<1 || new_y>ns || new_y<1 ){
crash = true;
cout << "Robot " << r << " crashes into the wall" << endl;
return false;
}
if( graph[new_x][new_y].machine != 0 ){
crash = true;
cout << "Robot " << r << " crashes into robot " << graph[new_x][new_y].machine << endl;
return false;
}
return true;
}
void move(int r, int new_x, int new_y)
{
int temp_m = graph[robot[r].x][robot[r].y].machine;
int temp_d = graph[robot[r].x][robot[r].y].dir;
graph[robot[r].x][robot[r].y].machine = 0;
graph[robot[r].x][robot[r].y].dir = 0;
robot[r].x = new_x;
robot[r].y = new_y;
graph[new_x][new_y].machine = temp_m;
graph[new_x][new_y].dir = temp_d;
}
int main()
{
int n;
cin >> n;
for(int i=1; i<=n; i++){
int ew, ns;
cin >> ew >> ns;
for(int j=1; j<=ew; j++){
for(int k=1; k<=ns; k++){
graph[j][k].machine = 0;
graph[j][k].dir = 0;
}
}
//cout << "graph init finish" << endl;
int N, M;
cin >> N >> M;
for(int j=1; j<=N; j++){
int x, y;
char dir;
cin >> x >> y >> dir;
graph[x][y].machine = j;
graph[x][y].dir = getDir(dir);
robot[j].x = x;
robot[j].y = y;
}
//cout << "graph, robot assignment finish" << endl;
bool crash = false;
for(int j=1; j<=M; j++){
int r, steps;
char op;
cin >> r >> op >>steps;
//if( crash ){
// continue;
//}
for(int k=1; k<=steps; k++){
if( crash ){
continue;
}
if( op == 'F' ){
int new_x = 0;
int new_y = 0;
getNewPosi(robot[r].x, robot[r].y, new_x, new_y);
if( !checkNewPosi(r, new_x, new_y, ew, ns, crash) ){
//crash = true;
break;
}
move(r, new_x, new_y);
continue;
}
if( op == 'L' ){
graph[robot[r].x][robot[r].y].dir--;
if( graph[robot[r].x][robot[r].y].dir ==0 ){
graph[robot[r].x][robot[r].y].dir = 4;
}
continue;
}
if( op == 'R' ){
graph[robot[r].x][robot[r].y].dir++;
if( graph[robot[r].x][robot[r].y].dir == 5){
graph[robot[r].x][robot[r].y].dir = 1;
}
continue;
}
}
//cout << "finish opration:" << j << endl;
}
if( !crash ){
cout << "OK" << endl;
}
}
return 0;
}
- POJ2632
- POJ2632
- poj2632
- POJ2632
- POJ2632
- poj2632
- poj2632
- POJ2632
- poj2632
- poj2632
- poj2632--模拟
- poj2632(模拟)
- Crashing Robots POJ2632
- poj2632 Crashing Robots
- POJ2632--Crashing Robots--模拟
- POJ2632 Crashing Robots
- poj2632--Crashing Robots
- POJ2632 Crashing Robots
- 浅谈SUN JVM内存管理与应用服务器的优化 之SUN JVM内存管理
- 利用InstallShield12制作安装包之三:判断操作系统类型的多种方法
- VC++中什么是BSTR、LPSTR以及LPWSTR呢
- POJ2418
- Eclipse3.2.2中安装Spring IDE2.0.1
- POJ2632
- db_cache_size、shared_pool_size一些初始化参数为什么是0?
- 2440PWMTimer启动流程
- 明朝时代
- Word效率工具(WET)
- 日本产业研究--读后感二 关于原材料和组装加工业 从钢铁到切割钢铁的。嘿嘿
- 经常下载的朋友注意了,教你怎样硬盘零伤害!
- C++ 资源网站
- 去配眼镜