Crashing Robots POJ 2632(模拟法+代码量较大)

问题:Crashing RobotsPOJ - 2632

分析:

debug用的数据:点击打开链接

下面是我的代码,wa了两次,要不是有数据,估计还要更久,实际就是一些小错误。。。发现只要有粘贴代码,就很容易有东西忘记修改。。。

在下面是另一位OJER的代码,在poj的discuss上找到的,地址是点击打开链接

好好学习编程艺术!!!

代码(我的):

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>   
#include<set>  
#include<bitset>  
#include<list>
#include<fstream>
 
#define UP(i,x,y) for(int i=x;i<=y;i++)  
#define DOWN(i,x,y) for(int i=x;i>=y;i--)  
#define MEM(a,x) memset(a,0,sizeof(a))
#define W(a) while(a) 
#define ll long long  
#define INF 0x3f3f3f3f  
#define EXP 1e-10  
#define lowbit(x) (x&-x)
 
using namespace std;
struct point{
	int id;
	int dir;
}p[110][110];
struct robot{
	int x,y;
}robs[105];
int main(){
	//ofstream fout("out.txt",ios::out);
	int k;
	cin>>k;
	while(k--){
		int a,b;cin>>b>>a;
		MEM(p,0);
		MEM(robs,0);
		int n,m;cin>>n>>m;
		for(int i=1;i<=n;i++){
			int x,y,direction;cin>>y>>x;
			robs[i].x=x;robs[i].y=y;
			char ch;cin>>ch;
			if(ch=='N')direction=0;
			else if(ch=='E')direction=1;
			else if(ch=='S')direction=2;
			else if(ch=='W')direction=3;
			p[x][y].id=i;p[x][y].dir=direction;
			//cout<<"direction:"<<direction<<endl;
		}
		int ok=1;
		for(int j=1;j<=m;j++){
			int no;cin>>no;
			int x=robs[no].x,y=robs[no].y;
			char ch;cin>>ch;
			int r;cin>>r;
			if(!ok)continue;
			if(ch=='L'){
				for(int i=1;i<=r;i++){
					int d=p[x][y].dir;
					d=((d-1)%4+4)%4;
					p[x][y].dir=d;
				}
			}
			else if(ch=='R'){
				for(int i=1;i<=r;i++){
					int d=p[x][y].dir;
					d=((d+1)%4+4)%4;
					p[x][y].dir=d;
				}
			}
			else if(ch=='F'){
				int d=p[x][y].dir;
				if(d==2){
					//cout<<r<<endl;
					for(int i=1;i<=r;i++){
						//cout<<x<<' '<<p[x-1][y].id<<endl;
						if(x>1&&p[x-1][y].id==0){
							p[x-1][y].id=p[x][y].id;
							p[x][y].id=0;
							p[x-1][y].dir=p[x][y].dir;
							x--;
							robs[p[x][y].id].x=x;
							//cout<<"x:"<<x<<" y:"<<y<<endl;
						}
						else if(x-1<1){
							ok=0;
							cout<<"Robot "<<p[x][y].id<<" crashes into the wall"<<endl;
							break;
						}
						else if(p[x-1][y].id!=0){
							ok=0;
							cout<<"Robot "<<p[x][y].id<<" crashes into robot "<<p[x-1][y].id<<endl;
							break;
						}
					}
				}
				else if(d==1){
					//cout<<"d1hihi"<<endl;
					for(int i=1;i<=r;i++){
						if(y<b&&p[x][y+1].id==0){
							//cout<<"x:"<<x<<" y:"<<y<<" id:"<<p[x][y+1].id<<endl;
							p[x][y+1].id=p[x][y].id;
							p[x][y].id=0;
							p[x][y+1].dir=p[x][y].dir;
							y++;
							robs[p[x][y].id].y=y;
							//cout<<"x:"<<x<<" y:"<<y<<endl;
						}
						else if(y>=b){
							ok=0;
							cout<<"Robot "<<p[x][y].id<<" crashes into the wall"<<endl;
							break;
						}
						else if(p[x][y+1].id!=0){
							ok=0;
							cout<<"Robot "<<p[x][y].id<<" crashes into robot "<<p[x][y+1].id<<endl;
							break;
						}
					}
				}
				else if(d==0){
					//cout<<"hi"<<endl;
					//cout<<r<<endl;
					for(int i=1;i<=r;i++){
						//cout<<x<<' '<<p[x+1][y].id<<endl;
						if(x<a&&p[x+1][y].id==0){
							p[x+1][y].id=p[x][y].id;
							p[x][y].id=0;
							p[x+1][y].dir=p[x][y].dir;
							x++;
							robs[p[x][y].id].x=x;
							//cout<<"x:"<<x<<"y:"<<y<<endl;
						}
						else if(x>=a){
							ok=0;
							cout<<"Robot "<<p[x][y].id<<" crashes into the wall"<<endl;
							break;
						}
						else if(p[x+1][y].id!=0){
							ok=0;
							cout<<"Robot "<<p[x][y].id<<" crashes into robot "<<p[x+1][y].id<<endl;
							break;
						}
					}
				}
				else if(d==3){
					//cout<<"d=3hihi"<<endl;
					for(int i=1;i<=r;i++){
						//cout<<"x:"<<x<<" y:"<<y<<' ';
						//cout<<"id:"<<p[x][y-1].id<<endl;
						if(y>1&&p[x][y-1].id==0){
							p[x][y-1].id=p[x][y].id;
							p[x][y].id=0;
							p[x][y-1].dir=p[x][y].dir;
							y--;
							robs[p[x][y].id].y=y;
							//cout<<"x:"<<x<<" y:"<<y<<endl;
							//cout<<"id:"<<p[x][y].id<<endl;
						}
						else if(y-1<1){
							ok=0;
							cout<<"Robot "<<p[x][y].id<<" crashes into the wall"<<endl;
							break;
						}
						else if(p[x][y-1].id!=0){
							ok=0;
							cout<<"Robot "<<p[x][y].id<<" crashes into robot "<<p[x][y-1].id<<endl;
							break;
						}
					}
				}
			}
		}
		if(ok){
			cout<<"OK"<<endl;
		}
	}
	return 0;
}

代码2:

#include <cstdio>

using namespace std;

const int N = 105;

struct rob
{
	int x, y, d;
}rob[N];

struct move
{
	int id, tap, times;
}ms[N];

int on[N][N];
int t, m, n, nums, moves;

void input()
{
	scanf("%d %d", &m, &n);
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			on[i][j] = 0;
	char c;
	scanf("%d %d", &nums, &moves);
	for (int i = 1; i <= nums; ++i)
	{
		scanf("%d %d %c", &rob[i].y, &rob[i].x, &c);
		on[rob[i].x][rob[i].y] = i;
		switch(c)
		{
			case 'S':rob[i].d = 0;break;
			case 'E':rob[i].d = 1;break;
			case 'N':rob[i].d = 2;break;
			case 'W':rob[i].d = 3;break;
		}
	}
	for (int i = 1; i <= moves; ++i)
	{
		scanf("%d %c %d", &ms[i].id, &c, &ms[i].times);
		switch(c)
		{
			case 'L':ms[i].tap = 1;break;
			case 'R':ms[i].tap = 2;break;
			case 'F':ms[i].tap = 3;break;
		}
	}
}

void output(int one, int two, int w)
{
	if (w == 1)
		printf("Robot %d crashes into the wall\n", one);
	else
		printf("Robot %d crashes into robot %d\n", one, two);
}

bool move(int id, int inx, int iny, int times)
{
	int x = rob[id].x;
	int y = rob[id].y;
	on[x][y] = 0;
	for (int i = 0; i < times; ++i)
	{
		x += inx;
		y += iny;
		if (x < 1 || x > n || y < 1 || y > m)
		{
			output(id, 0, 1);
			return false;
		}
		if (on[x][y] != 0)
		{
			output(id, on[x][y], 2);
			return false;
		}
	}
	on[x][y] = id;
	rob[id].x = x;
	rob[id].y = y;
	return true;
}

bool change(int id, int tap, int times)
{
	switch(tap)
	{
		case 1:rob[id].d = (rob[id].d + times) % 4;break;
		case 2:rob[id].d = (rob[id].d + 3 * times) % 4;break;
		case 3:
			switch(rob[id].d)
			{
				case 0:
					if (!move(id, -1,  0, times))
						return false;
					break;
				case 1:
					if (!move(id,  0,  1, times))
						return false;
					break;
				case 2:
					if (!move(id,  1,  0, times))
						return false;
					break;
				case 3:
					if (!move(id,  0, -1, times))
						return false;
					break;
			}
			break;
	}
	return true;
}

void solve()
{
	for (int i = 1; i <= moves; ++i)
		if (!change(ms[i].id, ms[i].tap, ms[i].times))
			return;
	puts("OK");
}

int main()
{
	scanf("%d", &t);
	while (t--)
	{
		input();
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值