#include<deque>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
//每次蛇移动只会有3种情况:
// //1.撞墙(坐标越界)直接返回长度;
//2.空格 第一步:将空格坐标body.push_front({x,y}),将该坐标设置为'H',第二步:将尾巴坐标设置为'E',然后弹出尾巴坐标body.pop_back()。
//3.食物 直接body.push_front({x,y}),设置坐标为'H';
/*D G L G G
3 3
F F F
F F H
E F E*/
/*U G G
3 3
F F F
F F H
E F E*/
/*DGL GUG RG
3 3
F F F
F F H
E F E*/ 
using namespace std;
int main() {
	string op;
	getline(cin,op);
	int n = 0;
	int m = 0;
	string t;
	cin >> n >> m; getline(cin, t);
	string c;
	int x=0;
	int y = 0;
	vector<string> matrix(n,string());
	for (int i = 0; i < n; ++i) {
		getline(cin, c);
		for (char j:c) {
			if (j != ' ') {
				matrix[i].push_back(j);
			}
		}
	}
	for (int i = 0; i < matrix.size();++i) {
		for (int j = 0;j<matrix[i].size(); ++j) {
			if (matrix[i][j] == 'H') {
				x = i;
				y = j;
			}
		}
	}
	pair<int, int> dir{-1,0};
	int len = 1;
	deque<pair<int, int>> body{{x,y}};
	for (auto ch : op) {
		if (ch == 'L') {
			 dir.first=0;
			 dir.second = -1;
		}
		else if (ch == 'R') {
			dir.first = 0;
			dir.second = 1;
		}
		else if (ch == 'U') {
			dir.first = -1;
			dir.second = 0;
		}
		else if (ch == 'D') {
			dir.first = 1;
			dir.second = 0;
		}
		else if (ch == 'G') {
			x=x+dir.first;
			y=y+dir.second;
			cout<<x<<' ' << y << endl;
			if (x < 0 || x >= n || y < 0 || y >= m) {
				cout << len;
				return 0;
			}
			if (matrix[x][y] == 'F') {
				++len;
				matrix[x][y] = 'H';
				body.push_back({ x,y });
			}
			else if (matrix[x][y] == 'H') {
				cout << len;
				return 0;
			}
			else if (matrix[x][y] == 'E') {
				auto tempx = body.back().first;
				auto tempy = body.back().second;
				body.push_front({x,y});
				body.pop_back();
				matrix[tempx][tempy] = 'E';
				matrix[x][y] = 'H';
			}
		}

	}
	cout << len;
	return 0;
}

原文地址:http://www.cnblogs.com/chunbai11/p/16881532.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性