Problem D Froggie(模拟)t

2018 ICPC North America Qualifier Contest

Problem D Froggie(模拟)

#pragma warning(disable:4996)
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int sit[15][5]{ 0 };
pair<int, int>pos;//记录小乌龟的坐标
pair<int, int>dir[4];//方向
int main()
{
	memset(sit, 0, sizeof(sit));
	int m, n;
	cin >> m >> n;
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			scanf("%d", &sit[i][j]);
		}
		if (i % 2 != 0)
		{
			sit[i][0] = n - sit[i][0] - 1;
		}
	}
	pos.first = m;
	cin >> pos.second;
	string s;
	cin >> s;
	int len = s.size();
	dir[0].first = -1, dir[0].second = 0;//上
	dir[1].first = 1, dir[1].second = 0;//下
	dir[2].first = 0, dir[2].second = -1;//左
	dir[3].first = 0, dir[3].second = 1;//右
	for (int i = 0; i < len; i++)
	{
		switch (s[i])//更改小乌龟坐标
		{
		case 'U':pos.first += dir[0].first, pos.second += dir[0].second; break;
		case 'D':pos.first += dir[1].first, pos.second += dir[1].second; break;
		case 'L':pos.first += dir[2].first, pos.second += dir[2].second; break;
		case 'R':pos.first += dir[3].first, pos.second += dir[3].second; break;
		}
		if (pos.first == -1) { printf("safe\n"); return 0; }
		if (pos.first >= m) { continue; }//加这个过滤条件
		if (pos.second < 0 || pos.second >= n)return -1;//错误路线
		int step;//记录当前行第一辆车的位置
		if (pos.first % 2 == 0)//如果所在行的车往右走
		{
			step = sit[pos.first][0] + sit[pos.first][2] * (i + 1);//调整车的位置
		}
		else
		{
			step = sit[pos.first][0] - sit[pos.first][2] * (i + 1);
		}
		if ((step - pos.second) % sit[pos.first][1] == 0)//sit[pos.dirst][1]这里会崩
		{
			printf("squish\n");
			return 0;
		}
	}
	printf("squish\n");
	return 0;
}

基本思路:

数组sit[15][5]存储m个三元组,

遇见朝左行进的汽车sit[i][0]存储最后一辆车的位置

pair<int, int>pos保存小乌龟的坐标

m次循环

​ 每一次循环:

​ 更新小乌龟的坐标

​ 如果pos.first==-1安全到达,打印safe

​ 否则,更新车的坐标

​ if (乌龟所在行的车朝右走,计算sit[i][0]的车更新后的位置step

​ else 同上

​ if ((step - pos.second) % sit[pos.first][1] == 0)//这只乌龟要被撞

​ { printf(“squish\n”); }avoiding cars的情况

其余情况: printf(“squish\n”)

发布了21 篇原创文章 · 获赞 8 · 访问量 2747

猜你喜欢

转载自blog.csdn.net/shen253135371/article/details/96169067