SGU109 奇偶性 problem of parity

题意: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<n<101。所以我们第一步特殊处理,走N步去掉右下的一个三角形,再依次走奇数步。这样K最大为299。小数据特判一下。下图是N=6时的解法。

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<n<101. So we do something special in the first step: walk N steps to remove a lower-right triangle and then walks odd steps in the following turns. In this case the largest Ki will be 299. Note that it needs special judge when n is small. The following picture is a solution when N=6.


#include <iostream>
#include <stdio.h>
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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值