0
点赞
收藏
分享

微信扫一扫

zoj 1056 The Worm Turns

落拓尘嚣 2022-04-01 阅读 69

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

#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 条评论