回 帖 发 新 帖 刷新版面

主题:各位高手们帮小弟做一道题好不好  我会给高分

Problem
在N*N的棋盘上(1<=N<=10)填入1,2,...N*N共N*N个数,使得任意两个相邻的数之和为素数.

例如,当N=2时,有

1 2

4 3

Input
输入第一行为一整数T,表示有T组测试数据.

每组测试数据一行,为一整数N(1<=N<=10)

要求:
输出满足条件的最小序列的方案。
最小序列即将每一行连接起来组成一行,然后使前面的尽可能小,当第一个数字相同时则比较下面一个,依次类推。

比如当N=2时,序列为1 2 4 3,当无满足条件的方案时输出"NO"。

输入样例
1
2

输出样例
1 2
4 3

谢谢

回复列表 (共14个回复)

沙发

我会

板凳

对不起我不会

3 楼

妈的,见不得人的勾当!我最讨厌了,终于明白……

4 楼

既然会,那就发程序!

5 楼

发个无敌点的程序(过了的)
program p1025(input,output);
var t,n,i:integer;
  begin
   read(t);
    for i:=1 to t do
     begin
      read(n);
       case n of
        1:writeln(1);
        2:begin
           writeln(1,' ',2);
           writeln(4,' ',3);
          end;
        3:writeln('NO');
        4:begin
           writeln(1,' ',2,' ',11,' ',12);
           writeln(4,' ',9,' ',8,' ',5);
           writeln(7,' ',10,' ',3,' ',14);
           writeln(6,' ',13,' ',16,' ',15);
          end;
         5:begin
           writeln(1,' ',2,' ',3,' ',4,' ',7);
           writeln(6,' ',5,' ',8,' ',15,' ',22);
           writeln(25,' ',18,' ',23,' ',14,' ',9);
           writeln(16,' ',13,' ',24,' ',17,' ',20);
           writeln(21,' ',10,' ',19,' ',12,' ',11);
           end;
          6:begin
             writeln(1,' ',2,' ',3,' ',4,' ',7,' ',6);
             writeln(10,' ',21,' ',16,' ',13,' ',24,' ',5);
             writeln(19,' ',22,' ',25,' ',18,' ',23,' ',14);
             writeln(12,' ',31,' ',36,' ',35,' ',8,' ',15);
             writeln(29,' ',30,' ',17,' ',26,' ',33,' ',28);
             writeln(32,' ',11,' ',20,' ',27,' ',34,' ',9);
            end;
          7:begin
             writeln(1,' ',2,' ',3,' ',4,' ',7,' ',6,' ',5);
             writeln(10,' ',9,' ',8,' ',15,' ',16,' ',13,' ',18);
             writeln(19,' ',22,' ',21,' ',46,' ',37,' ',24,' ',23);
             writeln(12,' ',49,' ',40,' ',43,' ',30,' ',29,' ',14);
             writeln(35,' ',48,' ',31,' ',36,' ',17,' ',44,' ',39);
             writeln(38,' ',41,' ',42,' ',11,' ',26,' ',45,' ',28);
             writeln(33,' ',20,' ',47,' ',32,' ',27,' ',34,' ',25);
            end;
           8:begin
              writeln(1,' ',2,' ',3,' ',4,' ',7,' ',6,' ',5,' ',8);
              writeln(10,' ',9,' ',14,' ',15,' ',16,' ',13,' ',18,' ',11);
              writeln(19,' ',22,' ',39,' ',28,' ',25,' ',34,' ',49,' ',12);
              writeln(24,' ',37,' ',64,' ',33,' ',46,' ',55,' ',54,' ',17);
              writeln(23,' ',60,' ',43,' ',40,' ',61,' ',42,' ',29,' ',44);
              writeln(56,' ',41,' ',30,' ',31,' ',36,' ',47,' ',32,' ',27);
              writeln(57,' ',26,' ',53,' ',48,' ',35,' ',62,' ',21,' ',52);
              writeln(50,' ',63,' ',20,' ',59,' ',38,' ',45,' ',58,' ',51);
             end;
            9:begin
               writeln(1,' ',2,' ',3,' ',4,' ',7,' ',6,' ',5,' ',8,' ',9);
               writeln(10,' ',21,' ',16,' ',13,' ',24,' ',17,' ',12,' ',11,' ',20);
               writeln(19,' ',22,' ',15,' ',28,' ',43,' ',30,' ',29,' ',18,' ',23);
               writeln(34,' ',25,' ',46,' ',33,' ',40,' ',31,' ',42,' ',41,' ',38);
               writeln(27,' ',76,' ',37,' ',64,' ',49,' ',48,' ',59,' ',68,' ',69);
               writeln(52,' ',61,' ',36,' ',67,' ',60,' ',53,' ',44,' ',39,' ',70);
               writeln(79,' ',78,' ',35,' ',72,' ',77,' ',74,' ',63,' ',50,' ',81);
               writeln(58,' ',73,' ',66,' ',65,' ',62,' ',75,' ',26,' ',57,' ',32);
               writeln(55,' ',54,' ',47,' ',14,' ',45,' ',56,' ',71,' ',80,' ',51);
              end;
            10:begin
                writeln(1,' ',2,' ',3,' ',4,' ',7,' ',6,' ',5,' ',8,' ',9,' ',10);
                writeln(12,' ',11,' ',20,' ',27,' ',16,' ',13,' ',18,' ',23,' ',14,' ',33);
                writeln(17,' ',26,' ',21,' ',32,' ',15,' ',28,' ',19,' ',24,' ',29,' ',38);
                writeln(30,' ',41,' ',62,' ',35,' ',44,' ',39,' ',22,' ',37,' ',42,' ',59);
                writeln(31,' ',48,' ',65,' ',36,' ',53,' ',50,' ',51,' ',46,' ',25,' ',54);
                writeln(40,' ',61,' ',66,' ',43,' ',60,' ',47,' ',56,' ',57,' ',82,' ',49);
                writeln(63,' ',76,' ',73,' ',58,' ',91,' ',90,' ',83,' ',74,' ',75,' ',88);
                writeln(86,' ',81,' ',100,' ',79,' ',72,' ',77,' ',80,' ',99,' ',52,' ',85);
                writeln(93,' ',70,' ',97,' ',34,' ',67,' ',96,' ',71,' ',68,' ',45,' ',64);
                writeln(98,' ',69,' ',94,' ',55,' ',84,' ',95,' ',78,' ',89,' ',92,' ',87);
              end;
           end;
          end;
     end.

6 楼

用搜索做嘛!太容易了!
数据大点就用动态规划做嘛!

7 楼

dfs + 剪枝……反正时限大

8 楼

同意~

9 楼


[img]http://bbs.cqreview.com/UploadFile/2005-7/2005712154831382.gif[/img]

10 楼

我狂汗楼上……

我来回复

您尚未登录,请登录后再回复。点此登录或注册