• <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 
            国产免费久久精品99re丫y| 国内精品久久久久久久coent | 精品999久久久久久中文字幕| 欧美久久久久久| 欧美日韩精品久久久久| 久久九九久精品国产| A级毛片无码久久精品免费| 久久久久国产一级毛片高清版| 2021精品国产综合久久| 99精品国产在热久久无毒不卡| 久久亚洲精品成人AV| 久久综合给合久久狠狠狠97色69| 国产成人精品综合久久久久| 东方aⅴ免费观看久久av| 香蕉久久夜色精品升级完成| 一本一道久久综合狠狠老| 无码国内精品久久人妻蜜桃 | 久久综合中文字幕| 91精品国产91久久久久久| 成人精品一区二区久久| 久久精品国产国产精品四凭| 亚洲国产成人久久综合一区77| 99久久这里只精品国产免费| 亚洲午夜久久久久久噜噜噜| 久久99国产乱子伦精品免费| 亚洲天堂久久精品| 色婷婷久久综合中文久久一本| 久久人人爽人人爽人人片AV麻烦 | 国产欧美久久久精品| 久久久国产精品福利免费| 精品久久久久久无码中文野结衣 | 中文字幕热久久久久久久| 精品综合久久久久久888蜜芽| 国产精品成人99久久久久 | 久久人人爽爽爽人久久久| 精品999久久久久久中文字幕| 久久夜色精品国产亚洲av| 伊人久久久AV老熟妇色| 国产亚州精品女人久久久久久 | 亚洲国产成人精品无码久久久久久综合| 亚洲欧洲中文日韩久久AV乱码|