送诗一首
《竹石》
作者:郑燮
咬定青山不放松,立根原在破岩中。
千磨万击还坚劲,任尔东西南北风。
题目要求
分析
这题有毛病诶,你看他说“抵达终点的最短时间”,我觉得正常人的解读就应该是没到终点的所有时间吧……
但实际上根据我WA的情况来看,应该是说“选择顺风行进的时间”……
这题意啊,真的垃圾。
那就没啥可说的了,就算一算位移就行,到了就到了,没到就没到。
到了的话直接坐标相减即可,大可不必计数。
我代码写的有点啰嗦,凑合着看吧……
AC代码(Java语言描述)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x1 = scanner.nextInt(), y1 = scanner.nextInt(), x2 = scanner.nextInt(), y2 = scanner.nextInt();
int x = x2 - x1, y = y2 - y1, result = Math.abs(x) + Math.abs(y);
int num = scanner.nextInt();
for (int i = 0; i < num && !(x == 0 && y == 0); i++) {
String next = scanner.next();
switch (next) {
case "E":
if (x > 0) {
x--;
}
break;
case "W":
if (x < 0) {
x++;
}
break;
case "N":
if (y > 0) {
y--;
}
break;
default:
if (y < 0) {
y++;
}
break;
}
}
scanner.close();
if (x == 0 && y == 0) {
System.out.println(result);
} else {
System.out.println(-1);
}
}
}