Codeforces Round 859 (Div. 4),F

Problem - F - Codeforces

思路:暴力模拟

  1. 虽然n,m范围是2e4,但是却有n*m<=5e4,所以我们完全可以暴力模拟最多走n*m次,如果走这么多还没答案显然是走不到。
#include <bits/stdc++.h>
using namespace std;
#define ll     long long
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
typedef pair<int, int> pii;

//double 型memset最大127,最小128
//std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
const int INF = 0x3f3f3f3f;         //int型的INF
const ll llINF = 0x3f3f3f3f3f3f3f3f;//ll型的llINF
const int N = 2e5 + 10;

int main()
{
	std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t,n,m,x,y,ex,ey;
	string s;
	cin>>t;
	while (t--)
		{
			int dx,dy;
			cin>>n>>m>>x>>y>>ex>>ey>>s;
			if (s[0]=='D')dx=1;
			else dx=-1;
			if (s[1]=='R')dy=1;
			else dy=-1;
			int ans=0;
			while (x!=ex||y!=ey)
				{
					int f=0;
					if (x+dx>n||x+dx<1)ans++,dx=-dx,f++;
					if (y+dy>m||y+dy<1)ans++,dy=-dy,f++;//f记录一回转向次数
					x+=dx,y+=dy;
					ans-=(f==2);//如果x,y同时转两次,那么ans多加了一次
					if (ans>m*n)break;
				}
			if (ans>m*n)cout<<-1<<endl;
			else cout<<ans<<endl;
		}

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值