Description
最近ACM小组养了一条贪食蛇,他们把它养在一个可看成二维空间的盒子里(因为它不会向高处爬嘛)。今天小组里的人都不知道出去干什么了,只留了n个食物 在箱子里,但是它又必须按照小组成员给出的1..n的顺序将食物吃完,贪食蛇的行进方式只能是向前,向后,向左,或者向右,而不能斜着走。请你帮它计算一 下它吃完这些食物总共要走过的路程长度。(PS这是一条不会长大的蛇,且它的身体并不会影响到自己的前进;)
Input
输入包含若干组数据,每组数据的第一行一个正整数n(1<=n<=100000)表示盒子里总共有n个食物;第二行两个正整数Sx和Sy,以 空格隔开,表示贪食蛇开始时的位置,第3至3+n-1行的数据中按顺序给出了n个食物的位置X_i和Y_i,表示第i个食物的位置 (|Sx,Sy,X_i,Y_i| <= 100000 )。
读入以EOF结束。
Output
每组数据对应一行输出,输出贪食蛇要按顺序吃完所有食物所走的路程长度。
Sample Input
2 1 1 2 2 3 3
Sample Output
4
- 题解:水题一个,想多了。。。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <queue> #define LL long long using namespace std; int main() { LL ans=0; int n; int sx,sy,ex,ey,tx,ty; while(~scanf("%d",&n)){ ans=0; scanf("%d %d",&sx,&sy); tx=sx,ty=sy; for(int i=0;i<n;i++){ scanf("%d %d",&ex,&ey); ans+=(abs(ex-tx)+abs(ey-ty)); tx=ex,ty=ey; } printf("%lld ",ans); } return 0; }