Description:
Alice有一个N*N的格子,把1-N^2按照从上到下从左到右的顺序填进表格中,允许在表格上进行两种操作:
(1) 旋转行——这一行的数向右移动一个位置,而最后一列的数会移到第一列;
(2) 旋转列——这一列的数向下移动一个位置,最后一行的数会移到第一行。
Alice想把数X移到(R,C)处可以采用以下方法:
•如果X不在C这一列,通过旋转行操作把X移到C这一列;
•如果X不在R这一行,通过旋转列操作把X移到R这一行。
下面是一个把6移到(3,4)的例子:
![]()
Alice现在想采用上述方法,依次把K个数移到各自的目标位置,编程计算每个数需要几次操作。
Input
输入1:
4 1
6 3 4
输入2:
4 2
6 3 4
6 2 2
输入3:
5 3
1 2 2
2 2 2
12 5 5
Output
输出1:
3
输出2:
3
5
输出3:
2
5
3
Solution
模拟(W)
Many people think that we must renew the array K times.
许多人认为我们要更新矩阵K次。
In fact,we just have to renew the position of the K numbers.
实际上,我们只需要更新K个数的坐标。
先列后行(认真读题)。
Program
#include<iostream>
#include<fstream>
#include<cstdio>
using namespace std;
int n,k,t,ans,x[10001],y[10001],r[10001],c[10001];
int main()
{
freopen("init.in","r",stdin);
scanf("%d%d",&n,&k);
for (int i=1;i<=k;i++)
{
scanf("%d%d%d",&,&r[i],&c[i]);
x[i]=(t+n-1)/n;
y[i]=t-(x[i]-1)*n;
}
for (int i=1;i<=k;i++)
{
ans=0;
t=c[i]-y[i];
if (t<0)
t+=n;
for (int j=1;j<=k;j++)
if (x[i]==x[j])
{
y[j]+=t;
if (y[j]>n)
y[j]-=n;
}
/*
ans:=0;
t:=c[i]-y[i];
if (t<0)
t:=t+n;
for j:=1 to k do
if x[i]=x[j] then
begin
y[j]=y[j]+t;
if y[j]>n
y[j]:=y[j]-n;
end;
*/
ans+=t;
t=r[i]-x[i];
if (t<0)
t+=n;
for (int j=1;j<=k;j++)
if (y[i]==y[j])
{
x[j]+=t;
if (x[j]>n)
x[j]-=n;
}
ans+=t;
printf("%d\n",ans);
}
}