跳到正文

题解

题解:P15539 [CCC 2026 J4] Snail Path

The_Dream_Return约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年5月20日
更新
2026年5月20日
篇幅
约 1 分钟
题目
P15539

版本

  1. v2当前
  2. v1

这是一道简单题。

观察一下数据,发现 M≤200000M≤200000 ,字母后面的数不超过 2020 ,所以模拟能过。

现在有个问题:如果我们的起始坐标为 (1,1)(1,1) ,用数组存,有概率越界,那怎么办呢?

1.你可以把初始坐标设为 (4000000,4000000)(4000000,4000000) 。

2.你可以把数组改为 map 。

当然,我选择第二种 (其实是懒) 。

C++
#include<bits/stdc++.h>
using namespace std;
int m,v,x=1,y=1,ans;
char op;
map<int,map<int,int> > ma;
int main(){
	cin>>m;
	ma[1][1]=1;
	while(m--){
		cin>>op>>v;
		if(op=='S'){
			while(v--){
				x++;
				if(ma[x][y]) ans++;
				ma[x][y]=1;
			}
		}else if(op=='N'){
			while(v--){
				x--;
				if(ma[x][y]) ans++;
				ma[x][y]=1;
			}
		}else if(op=='E'){
			while(v--){
				y++;
				if(ma[x][y]) ans++;
				ma[x][y]=1;
			}
		}else{
			while(v--){
				y--;
				if(ma[x][y]) ans++;
				ma[x][y]=1;
			}
		}
	}
	cout<<ans;
}
原文