码迷,mamicode.com
首页 > 其他好文 > 详细

BFS POJ 2150 Crossing Prisms

时间:2015-08-03 22:31:49      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

  1 /*
  2     BFS:这题我的做法是先找出所有草地,然后枚举所有两兄弟烧的地点进行BFS,然后去最小值
  3 */
  4 /************************************************
  5 Author        :Running_Time
  6 Created Time  :2015-8-3 19:23:42
  7 File Name     :FZU_2150.cpp
  8 *************************************************/
  9 
 10 #include <cstdio>
 11 #include <algorithm>
 12 #include <iostream>
 13 #include <sstream>
 14 #include <cstring>
 15 #include <cmath>
 16 #include <string>
 17 #include <vector>
 18 #include <queue>
 19 #include <deque>
 20 #include <stack>
 21 #include <list>
 22 #include <map>
 23 #include <set>
 24 #include <bitset>
 25 #include <cstdlib>
 26 #include <ctime>
 27 using namespace std;
 28 
 29 #define lson l, mid, rt << 1
 30 #define rson mid + 1, r, rt << 1 | 1
 31 typedef long long ll;
 32 const int MAXN = 15;
 33 const int INF = 0x3f3f3f3f;
 34 const int MOD = 1e9 + 7;
 35 struct Point    {
 36     int x, y, step;
 37 };
 38 char maze[MAXN][MAXN];
 39 bool vis[MAXN][MAXN];
 40 pair<int, int> gra[MAXN*MAXN];
 41 int n, m, ans;
 42 int dx[4] = {-1, 1, 0, 0};
 43 int dy[4] = {0, 0, -1, 1};
 44 
 45 bool judge(int x, int y)    {
 46     if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || maze[x][y] != #) return false;
 47     return true;
 48 }
 49 
 50 void BFS(int x1, int y1, int x2, int y2)    {
 51     queue<Point> Q; 
 52     Q.push ((Point) {x1, y1, 0});   Q.push ((Point) {x2, y2, 0});
 53     vis[x1][y1] = true; vis[x2][y2] = true;
 54     int res = 0;
 55     while (!Q.empty ()) {
 56         Point p = Q.front ();   Q.pop ();
 57         for (int i=0; i<4; ++i) {
 58             int tx = p.x + dx[i], ty = p.y + dy[i];
 59             if (!judge (tx, ty))    continue;
 60             res = p.step + 1;   Q.push ((Point) {tx, ty, p.step + 1});
 61             vis[tx][ty] = true;
 62         }
 63     }
 64 
 65     for (int i=1; i<=n; ++i)    {
 66         for (int j=1; j<=m; ++j)    {
 67             if (maze[i][j] == # && !vis[i][j])    {
 68                 res = INF;  break;
 69             }
 70         }
 71         if (res == INF) break;
 72     }
 73     ans = min (ans, res);
 74 }
 75 
 76 int main(void)    {       //POJ 2150 Crossing Prisms
 77     int T, cas = 0;  scanf ("%d", &T);
 78     while (T--) {
 79         scanf ("%d%d", &n, &m);
 80         for (int i=1; i<=n; ++i)    {
 81             scanf ("%s", maze[i] + 1);
 82         }
 83 
 84         printf ("Case %d: ", ++cas);
 85         int tot = 0;
 86         for (int i=1; i<=n; ++i)    {
 87             for (int j=1; j<=m; ++j)    {
 88                 if (maze[i][j] == #)  gra[++tot] = make_pair (i, j);
 89             }
 90         }
 91         if (tot == 0)   {
 92             puts ("-1");    
 93         }
 94         ans = INF;
 95         for (int i=1; i<=tot; ++i)  {
 96             for (int j=i; j<=tot; ++j)  {
 97                 memset (vis, false, sizeof (vis));
 98                 int x1 = gra[i].first, y1 = gra[i].second;
 99                 int x2 = gra[j].first, y2 = gra[j].second;
100                 BFS (x1, y1, x2, y2);
101             }
102         }
103 
104         if (ans == INF) puts ("-1");
105         else    printf ("%d\n", ans);
106     }
107 
108     return 0;
109 }

 

BFS POJ 2150 Crossing Prisms

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4700378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!