• <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 閱讀(829) 評論(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 
            久久免费美女视频| 亚洲国产精品无码久久| 久久这里只精品国产99热 | 久久免费高清视频| 韩国三级中文字幕hd久久精品| 精品久久人人爽天天玩人人妻| 欧美一级久久久久久久大| 亚洲女久久久噜噜噜熟女| 99热热久久这里只有精品68| 久久亚洲国产最新网站| 青青国产成人久久91网| 久久久久久久久波多野高潮| 色成年激情久久综合| 欧美黑人激情性久久| 久久乐国产精品亚洲综合| 久久久亚洲欧洲日产国码aⅴ| 无码国内精品久久人妻麻豆按摩| 精品国际久久久久999波多野| 综合久久给合久久狠狠狠97色| 久久国产一区二区| 麻豆一区二区99久久久久| 亚洲精品97久久中文字幕无码| 91精品婷婷国产综合久久| 久久精品一本到99热免费| 久久精品国产免费观看三人同眠| 久久精品不卡| 久久久久亚洲AV综合波多野结衣 | 亚洲人成无码www久久久| 国产免费福利体检区久久| 成人久久综合网| 久久亚洲精品成人AV| 亚洲精品乱码久久久久久蜜桃不卡| 思思久久99热只有频精品66| 中文字幕精品久久久久人妻| 久久精品一区二区影院| 久久露脸国产精品| 欧美精品国产综合久久| 亚洲精品无码久久千人斩| 亚洲精品美女久久久久99| 2020久久精品国产免费| 国产精品美女久久久久久2018|