POJ 1573 *** Robot Motion

题意:有一个ROW*COL的矩形,其中每一个1*1的小块上有NSWE四个方向,一个机器人站在这上面,按照指定方向前进,问最后小机器人能不能出去或者在里面怎么循环。

想法:建一个矩形,然后按照提示每一步走就行了。。这几天做的模拟题都好水。。。

代码如下:

#pragma warning(disable:4996)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<stack>
#include<cstring>
#include<sstream>
#include<set>
#include<string>
#include<iterator>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

int mmap[15][15];
char m[15][15];
int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int row, col,step,x,y;
	char sign;
	string str[15];
	while (cin >> col >> row >> x, col || row || x) {
		memset(mmap, 0, sizeof(mmap));
		for (int i = col; i >= 1; --i)
			for (int j = 1; j <= row; ++j)
				cin >> m[j][i];
		step = 1;
		y = col;
		while (x > 0 && x <= row&&y > 0 && y <= col&&(!mmap[x][y])) {
			mmap[x][y] = step;
			sign = m[x][y];
			if (sign == 'N') y++;
			else if (sign == 'S')y--;
			else if (sign == 'W') x--;
			else if (sign == 'E') x++;
			step++;
		}
		if (mmap[x][y])
			cout << mmap[x][y] - 1 << " step(s) before a loop of " << step - mmap[x][y] << " step(s)" << endl;
		else cout << step-1<<" step(s) to exit" << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值