Atcoder Beginner Contest 341C Takahashi Gets Lost

23 篇文章 0 订阅

problem link

Since n ≤ 500 n\le 500 n500, an brute force implementation of complexity O ( n 3 ) \mathcal O(n^3) O(n3) would suffice. For each legal starting point, we caculate its position on each of the n n n steps and check if they are legal.

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
const int Maxn=510;
const int dx[]={1,-1,0,0},dy[]={0,0,-1,1};
char a[Maxn][Maxn],s[Maxn];
int f[Maxn];
int n,m,q,ans;
inline int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
bool work(int x,int y)
{
	if(a[x][y]=='#')return 0;
	for(int i=1;i<=q;++i)
	{
		x+=dx[f[i]],y+=dy[f[i]];
		if(a[x][y]=='#')return 0;
	}
	return 1;
}
int main()
{
	// freopen("in.txt","r",stdin);
	n=read(),m=read(),q=read();
	scanf("%s",s+1);
	for(int i=1;i<=q;++i)
	{
		if(s[i]=='L')f[i]=2;
		if(s[i]=='R')f[i]=3;
		if(s[i]=='U')f[i]=1;
		if(s[i]=='D')f[i]=0;
	}
	for(int i=1;i<=n;++i)
	scanf("%s",a[i]+1);
	for(int i=1;i<=n;++i)
	for(int j=1;j<=m;++j)
	if(work(i,j))++ans;
	printf("%d\n",ans);
	return 0;
}
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值