• <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>

            superman

            聚精會神搞建設 一心一意謀發展
            posts - 190, comments - 17, trackbacks - 0, articles - 0
               :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            POJ 1915 - Knight Moves

            Posted on 2008-06-21 14:37 superman 閱讀(822) 評論(0)  編輯 收藏 引用 所屬分類: POJ
             1 /* Accepted 2488K 688MS G++ 3131B */
             2 #include <queue>
             3 #include <iostream>
             4 
             5 using namespace std;
             6 
             7 struct point { int x, y; } ;
             8 
             9 point dir[8= {
            10     {-2+1}, {-1+2}, {+1+2}, {+2+1},
            11     {+2-1}, {+1-2}, {-1-2}, {-2-1}
            12 };
            13 
            14 int n, a[300][300], b[300][300];
            15 
            16 inline bool inside(const int x, const int y)
            17 {
            18     if(x >= 0 && x < n && y >= 0 && y < n)
            19         return true;
            20     return false;
            21 }
            22 
            23 int bfs(int sx, int sy, int tx, int ty)
            24 {
            25     for(int i = 0; i < n; i++)
            26     for(int j = 0; j < n; j++)
            27         a[i][j] = b[i][j] = -1;
            28     
            29     a[sx][sy] = b[tx][ty] = 0;
            30     
            31     queue <point> l, r;
            32     point sp = { sx, sy };
            33     point tp = { tx, ty };
            34     l.push(sp); r.push(tp);
            35     
            36     point cp;
            37     while(l.empty() == false || r.empty() == false)
            38     {
            39         if(l.empty() == false)
            40         {
            41             cp = l.front(); l.pop();
            42             for(int i = 0; i < 8; i++)
            43             {
            44                 int x = cp.x + dir[i].x;
            45                 int y = cp.y + dir[i].y;
            46                 if(inside(x, y))
            47                 {
            48                     if(a[x][y] == -1)
            49                     {
            50                         a[x][y] = a[cp.x][cp.y] + 1;
            51                         point np = { x, y };
            52                         l.push(np);
            53                     }
            54                     if(b[x][y] != -1)
            55                         return a[x][y] + b[x][y];
            56                 }
            57             }
            58         }
            59         if(r.empty() == false)
            60         {
            61             cp = r.front(); r.pop();
            62             for(int i = 0; i < 8; i++)
            63             {
            64                 int x = cp.x + dir[i].x;
            65                 int y = cp.y + dir[i].y;
            66                 if(inside(x, y))
            67                 {
            68                     if(b[x][y] == -1)
            69                     {
            70                         b[x][y] = b[cp.x][cp.y] + 1;
            71                         point np = { x, y };
            72                         r.push(np);
            73                     }
            74                     if(a[x][y] != -1)
            75                         return a[x][y] + b[x][y];
            76                 }
            77             }
            78         }
            79     }
            80 }
            81 
            82 int main()
            83 {
            84     cin >> n;
            85     while(cin >> n)
            86     {
            87         int sx, sy, tx, ty;
            88         cin >> sx >> sy >> tx >> ty;
            89         
            90         if(sx == tx && sy == ty)
            91             cout << 0 << endl;
            92         else
            93             cout << bfs(sx, sy, tx, ty) << endl;
            94     }
            95     
            96     return 0;
            97 }
            98 
            久久亚洲私人国产精品vA| 2021久久精品国产99国产精品| 久久久久99精品成人片 | 热re99久久6国产精品免费| 人人狠狠综合久久88成人| 久久精品夜夜夜夜夜久久| 国产精品综合久久第一页| 久久婷婷是五月综合色狠狠| 亚洲AV无码久久精品成人| 人人狠狠综合久久亚洲婷婷| 久久亚洲AV无码精品色午夜 | 久久综合精品国产一区二区三区 | 伊人久久大香线蕉影院95| 欧美午夜A∨大片久久| 久久ZYZ资源站无码中文动漫| 7国产欧美日韩综合天堂中文久久久久 | 午夜精品久久久久久久无码| 国产精品久久国产精品99盘| 久久精品日日躁夜夜躁欧美| 国产91久久综合| 久久91综合国产91久久精品| 国产成人精品综合久久久久| 久久精品国产一区二区电影| 久久久久久久综合日本亚洲| 亚洲伊人久久精品影院| 日韩影院久久| 久久精品成人免费国产片小草 | 亚洲va久久久噜噜噜久久男同| 99精品久久久久久久婷婷| 久久这里只有精品18| 久久婷婷色综合一区二区| 一本色道久久88综合日韩精品 | 一级做a爰片久久毛片16| 久久超乳爆乳中文字幕| 久久精品亚洲日本波多野结衣 | 久久精品国产清自在天天线| 97久久精品人人做人人爽| 久久国产成人午夜aⅴ影院 | 亚洲综合伊人久久大杳蕉| 99久久国产宗和精品1上映 | 亚洲午夜久久久|