Lara Croft and the New Game(CF-ED-43-B)(有趣的规律题)

35 篇文章 0 订阅

B. Lara Croft and the New Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You might have heard about the next game in Lara Croft series coming out this year. You also might have watched its trailer. Though you definitely missed the main idea about its plot, so let me lift the veil of secrecy.

Lara is going to explore yet another dangerous dungeon. Game designers decided to use good old 2D environment. The dungeon can be represented as a rectangle matrix of n rows and m columns. Cell (x, y) is the cell in the x-th row in the y-th column. Lara can move between the neighbouring by side cells in all four directions.

Moreover, she has even chosen the path for herself to avoid all the traps. She enters the dungeon in cell (1, 1), that is top left corner of the matrix. Then she goes down all the way to cell (n, 1) — the bottom left corner. Then she starts moving in the snake fashion — all the way to the right, one cell up, then to the left to the cell in 2-nd column, one cell up. She moves until she runs out of non-visited cells. n and mgiven are such that she always end up in cell (1, 2).

Lara has already moved to a neighbouring cell k times. Can you determine her current position?

Input

The only line contains three integers nm and k (2 ≤ n, m ≤ 109n is always even0 ≤ k < n·m). Note that k doesn't fit into 32-bit integer type!

Output

Print the cell (the row and the column where the cell is situated) where Lara ends up after she moves k times.

Examples
input
Copy
4 3 0
output
Copy
1 1
input
Copy
4 3 11
output
Copy
1 2
input
Copy
4 3 7
output
Copy
3 2
Note

Here is her path on matrix 4 by 3:


这题做了我一天,昨晚做的时候只知道它是个规律题,我就没上心,然后就WA了我13次。刚刚看了题解,才知道原来正解是这样的(趁热快点写题解。。。)

题意:

这个题就是上图所示,给你一个N×M的方格,按上述的方向走,给你一个步数K,问你当走了K步后它的行列坐标为多少。

题解:

我的做法:

把图分成两部分来考虑,第一列加特判,除去第一列后:

0        7        6                                1        2         我可以    k=k-(n-1)+1转化为右边的情况进行考虑

1        4        5       >>>>>            4         3         row -列    容易判断, 就是直接    整除列宽即可。

      3        2       >>>>>            5         6         col- 分奇偶   奇数就是平时那样取余,偶数则要返回来。

3        0        1                               8          7        但是我的做法不能进行取余,因为这个数字不是从0开始的。


 但是蠢人有蠢方法,我可以通过减去当行,来判断,具体看代码。

后来发现要是进行取余的话也是可行的只不过        k        要从0开始, k=k-(n-1).

今天真的长记性了,要是要用取余来算行列坐标一定需要从零开始。

第二个做法:

官方给出的做法:


0      10       11                        7         6    左边红色就加特判即可,而官方的做法在于直接倒着来做;

1        9        8       >>>>>      4         5      k=k-n;            可以得到右图那样的步数图。

       7        6       >>>>>      3         2      row=   n- ( k/(m-1) );行标容易,因为直接整除列宽    m-1

3        4        5                         0         1      col    要分奇偶,其中奇数的    如图所示要倒着的,直接col=m-(k%(m-1));

                                                                      偶数稍微复杂了点。            col=k%(m-1)+2;

 比如第二行        4        5->> (取余列宽后得到)       0    1    因为是顺着的 从0开始且从第二列开始,所以要加   2

下面有三个代码:

第一个:做的最蠢的方法,正着看        通过减去行以上    的数值得到col。

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
    ll n,m,k;
    while(~scanf("%lld%lld%lld",&n,&m,&k)){
        if(k+1<=n){
        printf("%lld %d\n",k+1,1);
        }else{
            k=k-(n-1);
            ll t=(n*(m-1)-k+1);
            ll x=t/(m-1)-(t%(m-1)==0?1:0);
            ll y;
            //printf("t:    %lld \n",t);
            if(x%2==0){
                y=t-(x*(m-1));
                //if(y==1)y++;
            }else{
                y=((x+1)*(m-1)-t)+1;
            }
            //printf("x:   %lld ,y:  %lld \n",x,y);
            printf("%lld %lld\n",x+1,y+1);
        }
    }
    return 0;
}

第二个:正着看,通过整除   得到    row    和    取余    得到    col

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
    ll n,m,k;
    while(~scanf("%lld%lld%lld",&n,&m,&k)){
        if(k+1<=n){
        printf("%lld %d\n",k+1,1);
        }else{
            k=k-(n-1);
            ll t=(n*(m-1)-k);
            ll x=t/(m-1);
            ll y;
            //printf("t:    %lld \n",t);
            if(x%2==0){
                y=t%(m-1)+1;
            }else{
                y=(m-1)-t%(m-1);
            }
            //printf("x:   %lld ,y:  %lld \n",x,y);
            printf("%lld %lld\n",x+1,y+1);
        }
    }
    return 0;
}

第三个官方做法:反着直接看。

#include <bits/stdc++.h>
using namespace std;

int n, m;
long long k;

int main() {
	scanf("%d%d%lld", &n, &m, &k);
	if (k < n){
		printf("%lld 1\n", k + 1);
		return 0;
	}
	k -= n;
	long long row = k / (m - 1);
	printf("%lld ", n - row);
	if (row & 1)
		printf("%lld\n", m - k % (m - 1));
	else
		printf("%lld\n", k % (m - 1) + 2);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值