• <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>
            posts - 14,  comments - 11,  trackbacks - 0
                  單純的bfs(),當(dāng)然你也可以用dfs,只要你不怕超時(shí)或者你的剪枝夠強(qiáng)大
                  最好開(kāi)一個(gè)三維的數(shù)組,記錄每一個(gè)格子的每一個(gè)方向上的最小值,直到bfs完成
                  具體看代碼,不做過(guò)多的解釋。
              1 #include <iostream>
              2 #include <queue>
              3 using namespace std;
              4 struct node
              5 {
              6        int x, y;
              7        int step, dir;
              8 };
              9 int n, m;
             10 int xi[4= {01-10};
             11 int yi[4= {100-1};
             12 int vist[82][82][4];
             13 char map[82][82];
             14 node start;
             15 queue <node> q;
             16 bool check(int dx, int dy)
             17 {
             18      if(dx >= 1 && dx <= n && dy >= 1 && dy <= m) return true;
             19      else return false;
             20 }
             21 bool find(node a)
             22 {
             23      if ((a.x == 1 || a.x == n || a.y == 1 || a.y == m)) return true;
             24      else return false;
             25 }
             26 int bfs()
             27 {
             28      while (!q.empty())q.pop();
             29      memset(vist, 0sizeof(vist));
             30      start.dir = -1;
             31      start.step = 0;
             32      q.push(start);
             33      node now, next;
             34      bool flag = true;
             35      int tmp;
             36      while (!q.empty())
             37      {
             38            now = q.front();
             39            q.pop();
             40            if (find(now)) return now.step;
             41            
             42            flag = false;
             43            for (int i = 0 ; i < 4; i++)
             44            {
             45               if (now.dir == i) continue;
             46               if (now.dir >=0 && 3-now.dir == i) continue;
             47               next.x = now.x + xi[i];
             48               next.y = now.y + yi[i];
             49               next.step = now.step + 1;
             50               if (check(next.x, next.y) && map[next.x][next.y] == '.' )
             51               {
             52                  if (vist[next.x][next.y][i] == 0)  
             53                  {
             54                     vist[next.x][next.y][i] = next.step;
             55                     next.dir = i;
             56                     q.push(next);
             57                  }
             58                  else if (vist[next.x][next.y][i] > next.step)
             59                  {
             60                     vist[next.x][next.y][i] = next.step;
             61                     next.dir = i;
             62                     q.push(next);
             63                  }
             64                  flag = true;
             65               }
             66            }
             67            if (!flag)
             68            {
             69               int i = now.dir;
             70               if (i < 0return 0;
             71               next.x = now.x + xi[i];
             72               next.y = now.y + yi[i];
             73               next.step = now.step + 1;
             74               if (check(next.x, next.y) && map[next.x][next.y] == '.' )
             75               {
             76                  if (vist[next.x][next.y][i] == 0)  
             77                  {
             78                     vist[next.x][next.y][i] = next.step;
             79                     next.dir = i;
             80                     q.push(next);
             81                  }
             82                  else if (vist[next.x][next.y][i] > next.step)
             83                  {
             84                     vist[next.x][next.y][i] = next.step;
             85                     next.dir = i;
             86                     q.push(next);
             87                  }
             88                  flag = true;
             89               }
             90            }
             91      }
             92      return -1;
             93 }
             94 int main()
             95 {
             96     int cas;
             97     scanf("%d"&cas);
             98     while (cas--)
             99     {
            100           scanf("%d%d"&n, &m);
            101           int i, j;
            102           for (i = 1; i <= n; i++)
            103               scanf("%s", map[i]+1);
            104           for (i = 1; i <= n; i++)
            105           for (j = 1; j <= m; j++)
            106           {
            107               if (map[i][j] == '@')
            108               {
            109                  start.x = i;
            110                  start.y = j;
            111                  map[i][j] = '.';
            112               }
            113           }
            114           int ans = bfs();
            115           printf("%d\n", ans);
            116     }
            117 return 0;
            118 }
            119 
            posted on 2012-04-13 18:34 路修遠(yuǎn) 閱讀(1315) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): 路修遠(yuǎn)
            <2011年4月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            轉(zhuǎn)載,請(qǐng)標(biāo)明出處!謝謝~~

            常用鏈接

            留言簿(1)

            隨筆分類(lèi)

            隨筆檔案

            文章檔案

            搜索

            •  

            最新評(píng)論

            • 1.?re: HDU 2433 最短路
            • @test
              的確這組數(shù)據(jù)應(yīng)該輸出20的
            • --YueYueZha
            • 2.?re: HDU 2433 最短路
            • 這方法應(yīng)該不對(duì)。 看下面這組數(shù)據(jù)
              4 4
              1 2
              2 3
              3 4
              2 4

              畫(huà)個(gè)圖,刪去最后一條邊 2 4 后的結(jié)果應(yīng)該是20,但是此方法的輸出是19
            • --test
            • 3.?re: HDU 2433 最短路
            • ans = ans + sum_u + sum_v - sum[u] - sum[v],
              這個(gè)公式不是很理解啊,不知道博主怎么想的啊,謝謝咯
            • --姜
            • 4.?re: HDU 2433 最短路
            • @attacker
              the i-th line is the new SUM after the i-th road is destroyed
            • --路修遠(yuǎn)
            • 5.?re: HDU 2433 最短路
            • 你這樣可以AC????刪除<U,V>不僅改變 u,v最短路啊、、、求解
            • --attacker

            閱讀排行榜

            評(píng)論排行榜

            国内精品欧美久久精品| 久久精品国产精品国产精品污| 国产亚洲精品美女久久久| 亚洲AV无一区二区三区久久| 欧美国产精品久久高清| 久久激情五月丁香伊人| 久久久久婷婷| 中文精品99久久国产| 香蕉久久影院| 中文字幕无码av激情不卡久久| 欧美久久综合九色综合| 日本精品久久久久影院日本| 久久人搡人人玩人妻精品首页 | 免费观看久久精彩视频| 久久婷婷五月综合色高清| 99精品久久久久久久婷婷| 亚洲级αV无码毛片久久精品| 狠狠色综合网站久久久久久久高清 | 久久九九有精品国产23百花影院| 成人妇女免费播放久久久| 久久精品成人免费看| 久久精品国产福利国产琪琪| 精品久久久久成人码免费动漫| 欧美熟妇另类久久久久久不卡| 国产成人久久激情91| 久久久久亚洲AV无码去区首| 久久99国产精品久久99小说 | 久久久91人妻无码精品蜜桃HD| 欧洲性大片xxxxx久久久| 久久久久青草线蕉综合超碰| 国产亚洲婷婷香蕉久久精品| 久久精品一区二区三区中文字幕| 国内精品伊人久久久久妇| 国产精品久久久久jk制服| 三级韩国一区久久二区综合| 无遮挡粉嫩小泬久久久久久久| 国产成人精品久久亚洲| 伊人久久大香线蕉av不卡| 国内精品久久久久久久涩爱| 亚洲愉拍99热成人精品热久久| 国产亚洲精午夜久久久久久|