当前位置: 首页 > 工具软件 > go-snake > 使用案例 >

2021 ICPC新疆省赛 G - cocktail with snake (思维+模拟)

毋举
2023-12-01

The favorite game of Mr.Cocktail is dota2. Serpentine walking is a useful skill to dodge the enemy’s attack. Serpentine walking refers to
the path of movement that keeps swing direction like a snake. Suppose
the map is a n*m two-dimensional plane. Mr.Cocktail at the point (1,1)
in initial.

He will first go to the right to the end, that is, point (n,1). Then turn and go one step upwards to point (n,2). Go to the left again
to the end, that is, point (1,2),. Then go one step up, and go to the
right again to the end.
The picture above is the action line when n=3 and m=4.

Now the map size is n*m . The number of steps k of Cocktail’s movement are known. Find the Manhattan distance from the position
after k steps to the starting point according to the rule.

Manhattan distance Definition: The distance between two points measured along axes at right angles. In a plane with p1 at (x1, y1)
and p2 at (x2, y2), it is |x1 - x2| + |y1 - y2|. Input

The first line contains a positive integer T, which represents the number of groups to be tested. (1≤T≤104)

Next contains T lines, each row contains three integers
n,m,k(1≤n,m≤1018,0≤k≤min(n∗m−1,1018))

Output

For each test data, output a line of one coordinate to represent the answer. (A positive integer separated by two spaces represents the
x and y coordinates respectively) Example
Input

5
3 3 1
3 3 2
3 3 3
3 3 4
3 3 5

Output

1
2
3
2
1

思路

首先无论如何,x坐标永远都是k/n。

接下来推公式,可以分两种情况讨论

分奇数行和偶数行。偶数行从左往右,奇数行从右往左。然后输出行号+列号就没了。

#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,k;
        cin>>n>>m>>k;
        ll x,y;
        if(k<=n-1)
        {
            x=k+1;
            y=1;
        }
        else
        {
            ll temp=(k-(n-1));
            if(temp%n==0)
            {
                y=1+temp/n;
                if(temp/n%2==1)
                {
                    x=1;
                }
                else
                {
                    x=n;
                }
            }
            else
            {
                y=2+temp/n;
                if(temp/n%2==0)
                {
                    x=n-temp%n+1;
                }
                else
                {
                    x=temp%n;
                }
               
            }
        }
        //cout<<x<<" "<<y<<endl;
        ll ans;
        ans=(y-1)+(x-1);
        cout<<ans<<endl;
        
    }
}
 类似资料: