SGU109 奇偶性 problem of parity

2014-11-24 07:35:53 · 作者: · 浏览: 0

题意:N*N的方格,第一个人从1号格(左上)开始走K1步(上下左右),然后去掉他永远不可能走到的格子。第二个人接着第一个人的终点走K2步,再去掉他永远走不到的格子。以此类推,直到有一个人被困在唯一一个格子里。求给出K1到Ke,以及每次去掉了哪些格子(e是自己定的,符合题意就好)。

Problem: Give you a N*N girds. The first person walks K1 steps(up,down,left,right) from grid number 1(upper left), then the grids he would never arrive will be removed. The second person walks K2 steps from the end point and so on, until a person will be trapped in the only one grid. It asks you to give K1...Ke, and the grids removed in each turn(as long as e in accordance with the problem).

解法:本能的想到了奇偶性。如果将方格染成黑白相间的,每次走奇数步,显然不会回到相同颜色中。这样我们可以一斜行一斜行的去掉,最后只剩一个1号格。然而题目有限制条件Ki<>Kj和N<=Ki<300, 1

Solution: Obviously, a problem of parity. If we color the grids black and white, we will not step into the grid with the same color as the start point in odd steps. So we can remove the grids one diagonal by one, until the grid number one left. However, the problem has some restrictions as Ki<>Kj和N<=Ki<300, 1

\

#include 
  
   
#include 
   
     using namespace std; int n,m; int main() { while (~scanf("%d",&n)) { if (n==2) { printf("3 4\n5 2 3\n"); continue; } printf("%d",n); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (i+j>n+2) printf(" %d",(i-1)*n+j); printf("\n"); if (n%2==0) m = n+1; else m = n+2; for (int i=n+2;i>=3;i--) { printf("%d",m); m += 2; for (int j=1;j<=n;j++) { int tt = i-j; if (tt>=1 && tt<=n) printf(" %d",(j-1)*n+tt); } printf("\n"); } } return 0; }