NC24004. [USACO 2016 Jan B]Mowing the Field
描述
输入描述
The first line of input contains N (1≤N≤100). Each of the remaining N lines contains a single statement and is of the form 'D S', where D is a character describing a direction (N=north, E=east, S=south, W=west) and S is the number of steps taken in that direction (1≤S≤10).
输出描述
Please determine the maximum value of x such that FJ never steps on a cell with cut grass. If FJ never visits any cell more than once, please output -1.
示例1
输入:
6 N 10 E 2 S 3 W 4 S 5 E 8
输出:
10
说明:
In this example, FJ steps on a cell at time 17 that he stepped on earlier at time 7; therefore, x must be at most 10 or else the grass from his first visit would not yet have grown back. He also steps on a cell at time 26 that he also visited at time 2; hence x must also be at most 24. Since the first of these two constraints is tighter, we see that x can be at most 10.C++ 解法, 执行用时: 8ms, 内存消耗: 4352K, 提交时间: 2022-07-11 16:51:16
#include <bits/stdc++.h> using namespace std; int a[2100][2100],n,t;//t是时间 int x,y,v,maxx=-1; char c; string s="NESW";//使用find函数,返回0,1,2,3 int dx[4]={1,0,-1,0 }; int dy[4]={0,1,0 ,-1}; int main() { cin>>n; x=1000; y=1000; for(int i=1;i<=n;++i) { cin>>c>>v; int k=s.find(c); for(int j=1;j<=v;++j) { t++; x+=dx[k]; y+=dy[k]; if(a[x][y]!=0) { int tmp=t-a[x][y]; if(maxx==-1) maxx=tmp; else { if(tmp<maxx)maxx=tmp; } } a[x][y]=t; } } cout<<maxx; return 0; }