USACO 1.5.4 Checker Challenge

本文介绍了一种解决跳棋挑战问题的有效算法。该算法通过深度优先搜索和多种优化手段找到了所有可能的解,并能高效处理不同规模的跳棋棋盘。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Checker Challenge

Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more than one in any diagonal. (Diagonals run from southeast to northwest and southwest to northeast and include all diagonals, not just the major two.)

Column
1 2 3 4 5 6
-------------------------
1 | | O | | | | |
-------------------------
2 | | | | O | | |
-------------------------
3 | | | | | | O |
-------------------------
4 | O | | | | | |
-------------------------
5 | | | O | | | |
-------------------------
6 | | | | | O | |
-------------------------

The solution shown above is described by the sequence 2 4 6 1 3 5, which gives the column positions of the checkers for each row from 1 to 6:

ROW123456
COLUMN246135

This is one solution to the checker challenge. Write a program that finds all unique solution sequences to the Checker Challenge (with ever growing values of N). Print the solutions using the column notation described above. Print the the first three solutions in numerical order, as if the queen positions form the digits of a large number, and then a line with the total number of solutions.

Special note: the larger values of N require your program to be especially efficient. Do not precalculate the value and print it (or even find a formula for it); that's cheating. Work on your program until it can solve the problem properly. If you insist on cheating, your login to the USACO training pages will be removed and you will be disqualified from all USACO competitions. YOU HAVE BEEN WARNED.

TIME LIMIT: 1 CPU second

PROGRAM NAME: checker

INPUT FORMAT

A single line that contains a single integer N (6 <= N <= 13) that is the dimension of the N x N checkerboard.

SAMPLE INPUT (file checker.in)

6

OUTPUT FORMAT

The first three lines show the first three solutions found, presented as N numbers with a single space between them. The fourth line shows the total number of solutions found.

SAMPLE OUTPUT (file checker.out)

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

跳棋的挑战

译 by Jeru



检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行,每列,每条对角线(包括两条主对角线的所有对角线)上都至多有一个棋子。


列号
1 2 3 4 5 6
-------------------------
1 | | O | | | | |
-------------------------
2 | | | | O | | |
-------------------------
3 | | | | | | O |
-------------------------
4 | O | | | | | |
-------------------------
5 | | | O | | | |
-------------------------
6 | | | | | O | |
-------------------------


上面的布局可以用序列2 4 6 1 3 5来描述,第i个数字表示在第i行的相应位置有一个棋子,如下:

行号 1 2 3 4 5 6
列号 2 4 6 1 3 5


这只是跳棋放置的一个解。请遍一个程序找出所有跳棋放置的解。并把它们以上面的序列方法输出。解按字典顺序排列。请输出前3个解。最后一行是解的总个数。

特别注意: 对于更大的N(棋盘大小N x N)你的程序应当改进得更有效。不要事先计算出所有解然后只输出,这是作弊。如果你坚持作弊,那么你登陆USACO Training的帐号将被无警告删除

PROGRAM NAME: checker

INPUT FORMAT
一个数字N (6 <= N <= 13) 表示棋盘是N x N大小的。

SAMPLE INPUT(checker.in)
6

OUTPUT FORMAT
前三行为前三个解,每个解的两个数字之间用一个空格隔开。第四行只有一个数字,表示解的总数。

SAMPLE OUTPUT(checker.out)
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4



一开始用DFS很明显会TLE...状态数太多了

于是考虑优化

标准的8皇后的优化,开个Y[k]表示k列是否有棋子了;

优化的结果,依然TLE...FT!

继续优化:

考虑到斜边上的坐标和

(/类型的斜变)是定值;

(/类型的)行,列之差是定值(有可能<0..);

优化完居然还是TLE...

无语继续优化,搜第一行的一半,利用对称性质,注意n是奇数的时候需要对第1行的中间列单独考虑.

最后面前过了....0.6xs..这题目很不错,虽然对我这新人来说BT了点...(-__-被虐啊)..


USACO的C1已经过了..(我菜-_-!)

考试咯,考试完慢慢刷,争取寒假通关..


  1. /*
  2. ID: chenh193
  3. PROG: checker
  4. LANG: C++
  5. */
  6. #include<iostream>
  7. #include<algorithm>
  8. #include<time.h>
  9. using namespace std;
  10. int n,ans[14],tot=0;
  11. bool xx[28],yy[28],Y[14],quit;
  12. void dfs(int x,int id)
  13. {
  14.     int i,j,tmp;
  15.     if(quit)return;
  16.     if(x==n+1)
  17.     {
  18.         tot++;
  19.         if(tot<=3)
  20.             for(i=0;i<id;i++)
  21.                 printf("%d%c",ans[i],i==id-1?'/n':' ');
  22.             else quit=true;
  23.         return;
  24.     }
  25.     for(i=1;i<=n;i++)
  26.     {
  27.         if(Y[i]||yy[x+i]||xx[tmp=x-i+14])continue;
  28.         Y[i]=true;
  29.         xx[tmp]=true;
  30.         yy[x+i]=true;
  31.         ans[id]=i;
  32.         dfs(x+1,id+1);
  33.         Y[i]=false;
  34.         xx[tmp]=false;
  35.         yy[x+i]=false;
  36.     }
  37. }
  38. void Dfs(int x)
  39. {
  40.     int i,j,tmp;
  41.     if(x==n+1)
  42.     {
  43.         tot++;
  44.         return;
  45.     }
  46.     for(i=1;i<=n;i++)
  47.     {
  48.         if(Y[i]||yy[x+i]||xx[tmp=x-i+14])continue;
  49.         Y[i]=true;
  50.         xx[tmp]=true;
  51.         yy[x+i]=true;
  52.         Dfs(x+1);
  53.         Y[i]=false;
  54.         xx[tmp]=false;
  55.         yy[x+i]=false;
  56.     }
  57. }
  58. int main()
  59. {
  60.     //freopen("checker.out","w",stdout);
  61.     //freopen("checker.in","r",stdin);
  62.     int i;
  63.     scanf("%d",&n);
  64.     dfs(1,0);
  65.     tot=0;
  66.     memset(xx,false,sizeof(xx));
  67.     memset(yy,false,sizeof(yy));
  68.     memset(Y,false,sizeof(Y));
  69.     for(i=1;i<=n/2;i++)
  70.     {
  71.         Y[i]=true;
  72.         xx[1-i+14]=true;
  73.         yy[1+i]=true;
  74.         Dfs(2);
  75.         Y[i]=false;
  76.         xx[1-i+14]=false;
  77.         yy[1+i]=false;
  78.     }
  79.     tot*=2;
  80.     if(n&0x1)
  81.     {
  82.         Y[n/2+1]=true;
  83.         xx[1-(n/2+1)+14]=true;
  84.         yy[1+n/2+1]=true;
  85.         Dfs(2);
  86.     }
  87.     printf("%d/n",tot);
  88.     return 0;
  89. }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值