• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj2488

            A Knight's Journey

            Time Limit: 1000MS Memory Limit: 65536K
            Total Submissions: 18085 Accepted: 6095

            Description

            Background
            The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey
            around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans?

            Problem
            Find a path such that the knight visits every square once. The knight can start and end on any square of the board.

            Input

            The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .

            Output

            The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.
            If no such path exist, you should output impossible on a single line.

            Sample Input

            3
            1 1
            2 3
            4 3

            Sample Output

            Scenario #1:
            A1
            
            Scenario #2:
            impossible
            
            Scenario #3:
            A1B3C1A2B4C2A3B1C3A4B2C4
            
            這個題目要求給出遍歷棋盤的字典序,所以要給擴展一個順序,我也不明白為什么這樣是字典序 








            dx[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};

            這個搜索順序總是先找字母數(shù)字小的,即以左上角為原點,當前為(i,j)總是找比先找比該點行或列小的,在找行或列大的,
            行列中總是先找行
            這里的行要按字母順序,列要按數(shù)字順序,樣例很糾結、




            如果是存在能遍歷棋盤上所有點的解的話,那從棋盤上任意一點出發(fā)總能遍歷棋盤上所有的點,、
            這里要
             1#include<stdio.h>
             2#include<string.h>
             3#include<math.h>
             4int dx[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
             5int stack[100][2];
             6int n,m,tot;
             7int mark[27][27];
             8short flag;
             9void print()
            10{
            11    int i;
            12    for (i=1;i<=tot ;i++ )
            13    {
            14        printf("%c%d",stack[i][0]+64,stack[i][1]);
            15    }

            16    printf("\n");
            17}

            18void dfs(int num)
            19{
            20    int i,xn,yn,x,y,numn;
            21    if (num==tot&&!flag)
            22    {
            23        print();
            24        flag=1;
            25        return;
            26    }

            27    x=stack[num][0];
            28    y=stack[num][1];
            29    for (i=0;i<8 ;i++ )
            30    if (flag==0)
            31    {
            32        xn=x+dx[i][0];
            33        yn=y+dx[i][1];
            34        if ((xn>0)&&(xn<=n)&&(yn>0)&&(yn<=m)&&(mark[xn][yn]==0))
            35        {
            36            numn=num+1;
            37            mark[xn][yn]=1;
            38            stack[numn][0]=xn;stack[numn][1]=yn;
            39            dfs(numn);
            40            mark[xn][yn]=0;
            41        }

            42    }

            43    else return;
            44}

            45int main()
            46{
            47    int t,i;
            48    scanf("%d",&t);
            49    for (i=1;i<=t ;i++ )
            50    {
            51        scanf("%d%d",&m,&n);
            52        memset(mark,0,sizeof(mark));
            53        memset(stack,0,sizeof(stack));
            54        mark[1][1]=1;
            55        tot=n*m;
            56        flag=0;
            57        stack[1][0]=1;
            58        stack[1][1]=1;
            59        printf("Scenario #%d:\n",i);
            60        dfs(1);
            61        if (!flag)
            62        {
            63            printf("impossible\n");
            64        }

            65        //if (i!=t)
            66        {
            67            printf("\n");
            68        }

            69    }

            70    return 0;
            71}

            72
            求字典序
            所以應從(1,1)開始遍歷































            posted on 2012-02-28 13:19 jh818012 閱讀(1822) 評論(0)  編輯 收藏 引用

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統(tǒng)計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內(nèi)容較長,點擊標題查看
            • --王私江
            91精品国产色综久久| 久久久久久亚洲精品成人 | 亚洲精品tv久久久久久久久| 2021国产精品午夜久久| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 波多野结衣久久一区二区| 无码精品久久久天天影视| 99久久99这里只有免费的精品| 99热都是精品久久久久久| 国产精品久久久久久久人人看 | 久久久久亚洲?V成人无码| 久久久久久久久久久精品尤物| 国产韩国精品一区二区三区久久| 久久免费视频一区| 国产精品久久影院| 久久综合给合久久狠狠狠97色69 | 久久精品国产乱子伦| 久久久青草青青亚洲国产免观| 久久久这里有精品| 久久精品国产国产精品四凭| 久久免费的精品国产V∧ | 国内精品久久人妻互换| 国产精品乱码久久久久久软件| 777米奇久久最新地址| 综合网日日天干夜夜久久 | 国产精品一久久香蕉国产线看| 亚洲国产成人久久笫一页| 久久久久久免费一区二区三区| 亚洲va中文字幕无码久久不卡| 日批日出水久久亚洲精品tv| 国产精品久久自在自线观看| 97久久精品无码一区二区| 久久99国产综合精品免费| 97久久精品无码一区二区| 99re久久精品国产首页2020| 国产欧美久久久精品| 国产精品禁18久久久夂久| AV无码久久久久不卡蜜桃| 国产午夜免费高清久久影院| 久久国产成人精品麻豆| 亚洲狠狠综合久久|