zoj 1056 The Worm Turns

这题注意一个细节,尾巴和头是同时移动的
所以也就是说,若头的下一个位置是尾巴目前的位置,那么这次移动不会咬到自己

#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<vector>
#include<set>
#include<queue>
#include<list>
#include<map>
using namespace std;

struct node {
	int x, y;
};

int main() {
	string state;
	int n;
	while (cin >> n && n) {
		cin >> state;
		deque<node> deq;
		node head;
		bool flag = true;
		for (int i = 11; i < 30; i++) {
			node n;
			n.x = 25;
			n.y = i;
			deq.push_front(n);
		}
		head.x = 25;
		head.y = 30;
		for (int i = 0; i < state.size(); i++) {




			char dre = state[i];
			node t;
			t.x = head.x;
			t.y = head.y;
			deq.push_front(t);
			deq.pop_back();
			
			if (dre == 'N') {
				head.x = head.x + 1;
			} else if (dre == 'S') {
				head.x = head.x - 1;
			} else if (dre == 'E') {
				head.y = head.y + 1;
			} else {
				head.y = head.y - 1;
			}

			for (deque<node>::iterator it = deq.begin(); it != deq.end(); it++) {
				if (it->x == head.x && it->y == head.y) {
					printf("The worm ran into itself on move %d.\n", i + 1);
					flag = false;
					break;
				}
			}

			if (head.x < 1 || head.x > 50 || head.y < 1 || head.y > 50) {
				printf("The worm ran off the board on move %d.\n", i + 1);
				flag = false;
				break;
			}

			if (!flag) {
				break;
			}

		}

		if (flag) {
			printf("The worm successfully made all %d moves.\n", state.size());
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值