AtCoder AT_abc405_d ABC405D - Escape Route

打印 上一主题 下一主题

主题 1705|帖子 1705|积分 5115

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
前言

BFS 算法在 AtCoder 比赛中照旧会考的,因为不常训练导致没想到,不仅错误 TLE 了很多,还影响了心态,3 发罚时后才 AC。
思路

起首,我们把全部位置和出口的隔断算出来(用 BFS),记为                                              d                                       x                               ,                               y                                                 d_{x,y}                  dx,y​,顺便求出离它近来的出口坐标,记为                                    (                                   X                                       x                               ,                               y                                            ,                                   Y                                       x                               ,                               y                                            )                              (X_{x,y},Y_{x,y})                  (Xx,y​,Yx,y​)。我们发现这个必要在队列里记下这个点的近来出口位置以及具体坐标。
然后我们像荡漾一样扩散着用 BFS 去求方向。找每个位置的上一步,然后判断是否是一条路上的(即近来出口相同且隔断大于这个点的隔断),假如是,那么修改方向并压入队列,否则忽略。
似乎很成功地做完了,那么有哪些易错点呢?


  • 更新方向的时候肯定要注意隔断是否大于当前点的隔断。注意:必须是严格大于,等于也不可以,因为加上这一步之后就不是最优。
  • 记得把安全疏散出口的近来出口位置设为它自己。
  • 肯定要用 BFS,而不是 DFS,两个函数都得用 BFS。
代码

AC 提交记录:Submission #65683293。
TLE 提交记录:第一发、第二发、第三发。
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <queue>
  6. using namespace std;
  7. int h, w, d[1010][1010];
  8. char a[1010][1010];
  9. pair<int, int> p[1010][1010];
  10. int dx[] = {-1, 1, 0, 0};
  11. int dy[] = {0, 0, -1, 1};
  12. char cc[] = {'v', '^', '>', '<'};
  13. void work()
  14. {
  15.         queue<pair<pair<int, int>, pair<int, int> > > q;
  16.         for (int x = 1; x <= h; x++)
  17.                 for (int y = 1; y <= w; y++)
  18.                         if (a[x][y] == 'E')
  19.                         {
  20.                                 p[x][y] = make_pair(x, y);
  21.                                 q.push(make_pair(make_pair(x, y), make_pair(x, y)));
  22.                                 d[x][y] = 0;
  23.                         }
  24.         while (q.size())
  25.         {
  26.                 int fx = q.front().first.first;
  27.                 int fy = q.front().first.second;
  28.                 int xx = q.front().second.first;
  29.                 int yy = q.front().second.second;
  30.                 q.pop();
  31.                 for (int i = 0; i < 4; i++)
  32.                 {
  33.                         int nx = fx + dx[i];
  34.                         int ny = fy + dy[i];
  35.                         if (nx < 1 || nx > h)
  36.                                 continue;
  37.                         if (ny < 1 || ny > w)
  38.                                 continue;
  39.                         if (a[nx][ny] != '.')
  40.                                 continue;
  41.                         if (d[nx][ny] > d[fx][fy] + 1)
  42.                         {
  43.                                 d[nx][ny] = d[fx][fy] + 1;
  44.                                 p[nx][ny] = make_pair(xx, yy);
  45.                                 q.push(make_pair(make_pair(nx, ny), make_pair(xx, yy)));
  46.                         }
  47.                 }
  48.         }
  49. }
  50. void calc()
  51. {
  52.         queue<pair<int, int> > q;
  53.         for (int x = 1; x <= h; x++)
  54.                 for (int y = 1; y <= w; y++)
  55.                         if (a[x][y] == 'E')
  56.                                 q.push(make_pair(x, y));
  57.         while (q.size())
  58.         {
  59.                 int fx = q.front().first;
  60.                 int fy = q.front().second;
  61.                 q.pop();
  62.                 for (int i = 0; i < 4; i++)
  63.                 {
  64.                         int nx = fx + dx[i];
  65.                         int ny = fy + dy[i];
  66.                         if (nx < 1 || nx > h)
  67.                                 continue;
  68.                         if (ny < 1 || ny > w)
  69.                                 continue;
  70.                         if (a[nx][ny] != '.')
  71.                                 continue;
  72.                         if (p[nx][ny] != p[fx][fy])
  73.                                 continue;
  74.                         if (d[nx][ny] <= d[fx][fy])
  75.                                 continue;
  76.                         a[nx][ny] = cc[i];
  77.                         q.push(make_pair(nx, ny));
  78.                 }
  79.         }
  80. }
  81. int main()
  82. {
  83.         cin >> h >> w;
  84.         for (int i = 1; i <= h; i++)
  85.                 for (int j = 1; j <= w; j++)
  86.                         cin >> a[i][j];
  87.         memset(d, 0x3f, sizeof(d));
  88.         work();
  89.         calc();
  90.         for (int i = 1; i <= h; i++)
  91.         {
  92.                 for (int j = 1; j <= w; j++)
  93.                         cout << a[i][j];
  94.                 cout << endl;
  95.         }
  96.         return 0;
  97. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

羊蹓狼

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表