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

            聚精會神搞建設(shè) 一心一意謀發(fā)展
            posts - 190, comments - 17, trackbacks - 0, articles - 0
               :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            POJ 1915 - Knight Moves

            Posted on 2008-06-21 14:37 superman 閱讀(821) 評論(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国产精品| 精品久久久久久久国产潘金莲| 99久久精品免费看国产一区二区三区| 亚洲国产精品无码久久久蜜芽 | 久久国产色av免费看| 亚洲综合伊人久久综合| 热久久这里只有精品| 久久天天躁狠狠躁夜夜不卡| 久久99国产精品久久99果冻传媒| 久久综合五月丁香久久激情| 国产精品久久毛片完整版| 午夜精品久久久久久影视riav| 久久精品中文闷骚内射| 丁香狠狠色婷婷久久综合| 少妇被又大又粗又爽毛片久久黑人| 久久AV高潮AV无码AV| 久久久久18| 国产精品欧美久久久久无广告 | 国产精品va久久久久久久| 精品久久久久久中文字幕大豆网| 香蕉久久夜色精品国产小说| 伊人久久大香线蕉av不卡| 色婷婷久久综合中文久久一本| 国产成人久久激情91| 欧美丰满熟妇BBB久久久| 亚洲七七久久精品中文国产| 99久久精品免费看国产| AV无码久久久久不卡蜜桃| 久久精品亚洲AV久久久无码| 国产精品女同一区二区久久| 色综合久久综精品| 日韩精品国产自在久久现线拍| 99久久无色码中文字幕| 国产精品久久久久久久久| 国产精品久久久久aaaa| 色成年激情久久综合| 亚洲一区中文字幕久久|