链接:https://codeforces.com/contest/1130/problem/C

题意:

给一个n*n的图,0表示地面,1表示水,给出起点和终点,

现要从起点到达终点,有一次在两个坐标间创立隧道的机会,消耗为(x1 - x2)^2 + (y1 - y1)^2。

求出最小的消耗,如果直接能走到,则消耗为0。

思路:

bfs求出起点和终点分别能到达的点,枚举每一种情况,取最小值即可。

代码:

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long LL;
  6.  
  7. int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  8. const int MAXN = 50 + 5;
  9. char Map[MAXN][MAXN];
  10. int vis[MAXN][MAXN];
  11. int n, r1, r2, c1, c2;
  12.  
  13. int Bfs()
  14. {
  15. queue<pair<int, int>> que;
  16. vector<pair<int, int>> start;
  17. vector<pair<int, int>> ends;
  18. que.push(make_pair(r1, c1));
  19. vis[r1][c1] = 1;
  20. while (!que.empty())
  21. {
  22. int x = que.front().first;
  23. int y = que.front().second;
  24. start.push_back(make_pair(x, y));
  25. if (x == r2 && y == c2)
  26. return 0;
  27. for (int i = 0;i < 4;i++)
  28. {
  29. int nx = x + Next[i][0];
  30. int ny = y + Next[i][1];
  31. if (nx < 1 || nx > n || ny < 1 || ny > n)
  32. continue;
  33. if (vis[nx][ny] || Map[nx][ny] == '1')
  34. continue;
  35. vis[nx][ny] = 1;
  36. que.push(make_pair(nx, ny));
  37. }
  38. que.pop();
  39. }
  40. que.push(make_pair(r2, c2));
  41. vis[r2][c2] = 1;
  42. while (!que.empty())
  43. {
  44. int x = que.front().first;
  45. int y = que.front().second;
  46. ends.push_back(make_pair(x, y));
  47. for (int i = 0;i < 4;i++)
  48. {
  49. int nx = x + Next[i][0];
  50. int ny = y + Next[i][1];
  51. if (nx < 1 || nx > n || ny < 1 || ny > n)
  52. continue;
  53. if (vis[nx][ny] || Map[nx][ny] == '1')
  54. continue;
  55. vis[nx][ny] = 1;
  56. que.push(make_pair(nx, ny));
  57. }
  58. que.pop();
  59. }
  60. int res = 9999;
  61. /*
  62. for (int i = 0;i < start.size();i++)
  63. cout << start[i].first << ' ' << start[i].second << endl;
  64. cout << endl;
  65. for (int i = 0;i < ends.size();i++)
  66. cout << ends[i].first << ' ' << ends[i].second << endl;
  67. cout << endl;
  68. */
  69. for (int i = 0;i < start.size();i++)
  70. {
  71. for (int j = 0;j < ends.size();j++)
  72. {
  73. int cx = start[i].first - ends[j].first;
  74. int cy = start[i].second - ends[j].second;
  75. int cost = cx * cx + cy * cy;
  76. res = min(res, cost);
  77. }
  78. }
  79. return res;
  80. }
  81.  
  82. int main()
  83. {
  84. cin >> n;
  85. cin >> r1 >> c1 >> r2 >> c2;
  86. for (int i = 1;i <= n;i++)
  87. {
  88. for (int j = 1; j <= n; j++)
  89. cin >> Map[i][j];
  90. }
  91. cout << Bfs() << endl;
  92.  
  93. return 0;
  94. }

  

Codeforces Round #542(Div. 2) C.Connect的更多相关文章

  1. Codeforces Round 542 (Div. 2)

    layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  2. Codeforces Round #542(Div. 2) CDE 思维场

    C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...

  3. Codeforces Round #542(Div. 2) B.Two Cakes

    链接:https://codeforces.com/contest/1130/problem/B 题意: 给定n和 2 * n个数,表示i位置卖ai层蛋糕, 有两个人在1号,必须严格按照1-n的顺序买 ...

  4. Codeforces Round #542(Div. 2) A.Be Positive

    链接:https://codeforces.com/contest/1130/problem/A 题意: 给n个数,找出一个非0整数d,使所有n个数除以整数d后,数组中正数的数量>= n/2. ...

  5. Codeforces Round #542(Div. 2) D1.Toy Train

    链接:https://codeforces.com/contest/1130/problem/D1 题意: 给n个车站练成圈,给m个糖果,在车站上,要被运往某个位置,每到一个车站只能装一个糖果. 求从 ...

  6. Codeforces Round #542 (Div. 1) 题解

    开学了住校了打不了深夜场 好难受啊QwQ A 显然对于每个起点,我们只需要贪心记录这个起点出发出去的糖果数量以及离自己最近的糖果 因为这个起点最后一次装载糖果一定是装载终点离自己最近的那个糖果 $ O ...

  7. Codeforces Round #542 Div. 1

    A:显然对于起点相同的糖果,应该按终点距离从大到小运.排个序对每个起点取max即可.读题花了一年还wa一发,自闭了. #include<iostream> #include<cstd ...

  8. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解

    Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

随机推荐

  1. C++类中使用new及delete小例子

    //默认复制构造函数的不足//尽管有默认的复制构造函数来解决一般对象与对象之间的初始化问题, 但是在有些情况下我们必须手动显式的去定义复制构造函数, 例如: #include <iostream ...

  2. Android Webview的测试

    1.查看当前的所有窗口: Set contexts= driver.getContextHandles(); System.out.println(contexts); 打印出当前所有的窗口 Set& ...

  3. js之substr和substring的差别

    今天有人在群里问这两个的差别,借这个机会在这罗列下 substring(from,to) 開始和结束的位置,从零開始的索引 參数     描写叙述 from     必需. 一个非负的整数,规定要提取 ...

  4. PR修改例子

    DATA: lt_items_old    LIKE TABLE OF bapiebanv   WITH HEADER LINE.   DATA: lt_items_new    LIKE TABLE ...

  5. C++11 条件变量

    C++11中的条件变量提供了用户等待的同步机制,在同步队列的应用中有很大的便利. 简单同步队列代码如下(SimpleSyncQueue.h): #ifndef SIMPLESYNCQUEUE_H #d ...

  6. C# 自定义控件及引用自动义控件

    1.http://www.cnblogs.com/hjxzjp/p/7823292.html   优先考虑从现有的控件中进行派生,并添加所需要的功能. 在解决方案资源管理器窗口中设置:引用----&g ...

  7. MessageBox_ swt

    SWT有不同类型的对话框.有些对话框具有特殊的属性. MessageBox messageBox = new MessageBox(shell, SWT.OK|SWT.CANCEL); if (mes ...

  8. 重入锁ReentrantLock用法以及如何实现重进入

    在java多线程中,可以使用synchronized实现线程之间的同步互斥,但在jdk1.5中增加了ReentrantLock类也能达到同样的效果,而且在使用上更加灵活,扩展功能上更加强大. 创建My ...

  9. bleve搜索引擎源码分析之索引——mapping和lucene一样,也有_all

    例子: package main import ( "fmt" "github.com/blevesearch/bleve" ) func main() { / ...

  10. RxJava RxBinding 按钮(Button) 点击(click)

    /********************************************************************* * RxJava RxBinding 按钮(Button) ...