uva 11831(ad hoc 模拟)

题意:有一个棋盘,然后有一个机器人在上面走,我们需要根据指令命令这个机器人。

思路:明明很简单的一题,结果,理解错题意写的异常臃肿(其实就是多了几个vector插入)。:(

只要维护两个变量,分别是机器人的位置和机器人的朝向,然后建坐标系的时候注意一下,不要走错方向了就行。

AC代码:

#include <bits/stdc++.h>
#define MAXR 200
#define MAXC 200
using namespace std;
int C;
void cpos_d(int &robd,pair<int,int> & robp,int nxd,int nxr,int nxc){
	if(nxd == 2){robd+=1;robd%=4;}
	else if(nxd == 0){
		robd = robd==0?3:robd-1;
	}
	robp.first = nxr;
	robp.second = nxc;
}
int main(){
	int N,M,S;
	while(cin>>N>>M>>S && N && M && S){
	int Gra[MAXR][MAXC];
	pair<int,int> robp;
	int robd;
	for(int i=0;i<N;i++)
		for(int j=0;j<M;j++){
			char a;
			cin>>a;
			if(a == '.')Gra[i][j]=0;
			else if(a == '*')Gra[i][j] = 1;
			else if(a == '#')Gra[i][j] = 2;
			else if(a == 'N' || a == 'S'|| a=='L'|| a=='O'){
				Gra[i][j]=0;
				robp.first =i;
				robp.second = j;
				if(a == 'N')robd = 1;
				else if(a == 'L')robd = 2;
				else if(a == 'S')robd = 3;
				else if(a == 'O')robd = 0;
			}else assert(0);
		}
	C = M;
	vector<vector<pair<int,int>>> dir(4);
	dir[0].emplace_back(0,0);//E
	dir[0].emplace_back(0,-1);//F
	dir[0].emplace_back(0,0);//D
	
	dir[1].emplace_back(0,0);
	dir[1].emplace_back(-1,0);
	dir[1].emplace_back(0,0);//
	
	dir[2].emplace_back(0,0);//
	dir[2].emplace_back(0,1);//
	dir[2].emplace_back(0,0);//
	
	dir[3].emplace_back(0,0);//
	dir[3].emplace_back(1,0);//
	dir[3].emplace_back(0,0);//
	queue<char> q;
	for(int i=0;i<S;i++){
		char t;
		cin>>t;
		q.push(t);
	}
	int sti_c = 0;
	while(!q.empty()){
		char nd;
		nd = q.front();q.pop();
		int nxd;
		
		if(nd == 'E'){
			nxd = 0;
		}else if(nd == 'F')nxd = 1;
		else nxd = 2;
		int robotd = robd;
		pair<int,int> robot_nowp = robp;
		int nxr = dir[robotd][nxd].first + robot_nowp.first;
		int nxc = dir[robotd][nxd].second + robot_nowp.second;
		if(nxr < 0 || nxc <0 || nxr>N-1 || nxc > M -1)continue;
		else if(Gra[nxr][nxc] == 0){
			cpos_d(robd,robp,nxd, nxr, nxc);
			continue;
		}else if(Gra[nxr][nxc] == 2)continue;
		else if(Gra[nxr][nxc] == 1){
			cpos_d(robd,robp,nxd, nxr, nxc);
			sti_c++;
			Gra[nxr][nxc] = 0;
			continue;
		}
	}
	cout<<sti_c<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值