UVa 11831 Sticker Collector Robots

11831

Sticker Collector Robots

One of the favorite sports in RoboLand is the Robots Rally. This rally is practiced in a giant rectangular arena of square cells with dimensions N rows by M columns. Some cells are empty, some contain a sticker for the World Footbal Cup Album (much appreciated by artificial intelligences in RoboLand) and some are occupied by pillars which support the roof of the arena. During the rally the robots can occupy any cell in the arena, except those containing pillars, since they block the robot movement. The path of the robot in the arena during the rally is determined by a sequence of instructions. Each instruction is represented by one of the following characters: ‘D’, ‘E’ and ‘F’, meaning, respectively, “turn 90 degrees to the right”, “turn 90 degrees to the left” and “move forward one cell”. Robots start the rally in some initial position in the arena and follow closely the sequence of instructions given (after all, they are robots!). Whenever a robot occupies a cell that contains a Cup sticker he collects it. Stickers are not replaced, that is, each scticker can be collected only once. When a robot tries to move into a cell which contains a pillar he stalls, remaining in the cell where he was, with the same orientation. The same happens when a robot tries to leave the arena.

Given the map of the arena, describing the position of pillars and sctickers, and the sequence of instructions of a robot, you must write a program to determine the number of stickers collected by the robot.

Input

The input contains several test cases. The first line of a test case contains three integers N, M and S (1 ≤ N, M ≤ 100, 1 ≤ S ≤ 5 × 104 ), separated by white spaces, indicating respectively the number of rows, the number of columns of the arena and the number of instructions to the robot. Each of the following N lines describes a cell line of the arena and contains a string with M characters. The first line to appear in the description of the arena is the one more to the North, the first column to appear in description of a cell line of the arena is the one more to the West.

Each cell in the arena is described by one of the following characters:

• ‘.’ — normal cell;

• ‘*’ — cell which contains a sticker;

• ‘#’ — cell which contains a pillar;

• ‘N’, ‘S’, ‘L’, ‘O’ — cell where the robot starts the rally (only one in the arena). The letter represents the initial robot orientation (North, South, East and West, respectively).

The last line in the input contains a sequence of S characters among ‘D’, ‘E’ and ‘F’, representing the instructions to the robot. The last test case is followed by a line which contains only three numbers zero separated by a blank space.

Output

For each rally described in the input your program must print a single line containing a single integer indicating the number of stickers that the robot collected during the rally.

Sample Input

3 3 2

***

*N*

***

DE

4 4 5

...#

*#O.

*.*.

*.#.

FFEFF

10 10 20

....*.....

.......*..

.....*....

..*.#.....

...#N.*..*

...*......

..........

..........

..........

..........

FDFFFFFFEEFFFFFFEFDF

0 0 0

Sample Output

0

1

3

要测样例自己去复制

文档里面的回车啥的应该会有错

#include <stdio.h>
/*
机器人的状态需要4个变量来描述
现有的积分,面对的方向,所在的行、列
上,下,左,右为1234 
左转为1,右转为2,前进为0 
wall=-1,star=1,blank=0,起始点=2 (离开起始点后将其值赋为0 
F有四种情况,遇到wall,遇到边界,遇到star,遇到blank 
F设为viod型
*/

int map[110][110];
int rob[4];
int ope[50010];
int n,m; 

void move();
void moveon();

int main()
{
	int s;
	while(scanf("%d %d %d",&n,&m,&s)){
		getchar();
		if(n==m&&m==s&&s==0) break;
		
		int startrow,startcol,startdir; 
		for(int i=0;i<n;i++){//地图输入 
			for(int j=0;j<m;j++){
				char temp;
				scanf("%c",&temp);
				if(temp=='.') map[i][j]=0;
				else if(temp=='*') map[i][j]=1;
				else if(temp=='#') map[i][j]=-1;
				else {
					map[i][j]=2;
					startrow=i;
					startcol=j;
					if(temp=='N') startdir=1;
					else if(temp=='S') startdir=2; 
					else if(temp=='O') startdir=3; 
					else if(temp=='L') startdir=4; 
				}
			}
			getchar();
		}
		
		rob[0]=0; rob[1]=startrow; rob[2]=startcol; rob[3]=startdir;
		//机器人初始化
		
		for(int i=0;i<s;i++){//操作符输入 
			char temp;
			scanf("%c",&temp);
			if(temp=='D') ope[i]=2;
			else if(temp=='E') ope[i]=1;
			else if(temp=='F') ope[i]=0;
		} 
		
		for(int i=0;i<s;i++){
			if(ope[i]==2){//右转 
				//1234上下左右,rob[3]为方向
				if(rob[3]==1) rob[3]=4;
				else if(rob[3]==2) rob[3]=3;
				else if(rob[3]==3) rob[3]=1;
				else if(rob[3]==4) rob[3]=2; 
			}
			else if(ope[i]==1){//左转 
				if(rob[3]==1) rob[3]=3;
				else if(rob[3]==2) rob[3]=4;
				else if(rob[3]==3) rob[3]=2;
				else if(rob[3]==4) rob[3]=1; 
			}
			else if(ope[i]==0){//前进! 
				move(); 
			}
		}
		printf("%d\n",rob[0]);
	}
	return 0;
} 
void move(){
/*
上,下,左,右为1234 
wall=-1,star=1,blank=0,起始点=2 (离开起始点后将其值赋为0 
F有四种情况,遇到wall #ok#,遇到边界 #ok#,遇到star,遇到blank 
*/
	if(rob[3]==1){
		if(rob[1]==0||map[rob[1]-1][rob[2]]==-1) {
			return ; 
		}
		rob[1]--;
		moveon();
	}
	if(rob[3]==2){
		if(rob[1]==n-1||map[rob[1]+1][rob[2]]==-1) {
			return ; 
		}
		rob[1]++;
		moveon();
	}
	if(rob[3]==3){
		if(rob[2]==0||map[rob[1]][rob[2]-1]==-1) {
			return ; 
		}
		rob[2]--;
		moveon();
	}
	if(rob[3]==4){
		if(rob[2]==m-1||map[rob[1]][rob[2]+1]==-1) {
			return ; 
		}
		rob[2]++;
		moveon();
	}
} 
void moveon(){
	int row,col;
	row=rob[1];
	col=rob[2];
	if(map[row][col]==1) {
		rob[0]++;
		map[row][col]=0;
	}
}

非常小白的思维,就照着题意写的

写了半天才过,也没看题解

有啥能优化的或者啥看不懂的尽管问

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值