标签:
input | output |
---|---|
7 6 ####### #.#.### #.#.### #.#.#.# #.....# ####### |
8 |
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define MIT (2147483647) 21 #define INF (1000000001) 22 #define MLL (1000000000000000001LL) 23 #define sz(x) ((int) (x).size()) 24 #define clr(x, y) memset(x, y, sizeof(x)) 25 #define puf push_front 26 #define pub push_back 27 #define pof pop_front 28 #define pob pop_back 29 #define ft first 30 #define sd second 31 #define mk make_pair 32 33 inline int Getint() 34 { 35 int Ret = 0; 36 char Ch = ‘ ‘; 37 bool Flag = 0; 38 while(!(Ch >= ‘0‘ && Ch <= ‘9‘)) 39 { 40 if(Ch == ‘-‘) Flag ^= 1; 41 Ch = getchar(); 42 } 43 while(Ch >= ‘0‘ && Ch <= ‘9‘) 44 { 45 Ret = Ret * 10 + Ch - ‘0‘; 46 Ch = getchar(); 47 } 48 return Flag ? -Ret : Ret; 49 } 50 51 const int N = 1010; 52 const int DX[] = {-1, 0, 1, 0}, DY[] = {0, -1, 0, 1}; 53 int n, m; 54 char graph[N][N]; 55 int dp[N][N]; 56 queue<pair<int, int> > que; 57 58 inline void Input() 59 { 60 scanf("%d%d", &m, &n); 61 for(int i = 0; i < n; i++) scanf("%s", graph[i]); 62 } 63 64 inline bool Check(int x, int y) 65 { 66 if(x < 0 || y < 0 || x >= n || y >= m) return 0; 67 if(graph[x][y] != ‘.‘) return 0; 68 return 1; 69 } 70 71 inline void Bfs(int sx, int sy) 72 { 73 for(int i = 0; i < n; i++) 74 for(int j = 0; j < m; j++) 75 dp[i][j] = INF; 76 que.push(mk(sx, sy)); 77 dp[sx][sy] = 0; 78 while(sz(que)) 79 { 80 int ux = que.front().ft, uy = que.front().sd; 81 que.pop(); 82 for(int t = 0; t < 4; t++) 83 { 84 int vx = ux + DX[t], vy = uy + DY[t]; 85 if(Check(vx, vy) && dp[vx][vy] > dp[ux][uy] + 1) 86 { 87 dp[vx][vy] = dp[ux][uy] + 1; 88 que.push(mk(vx, vy)); 89 } 90 } 91 } 92 } 93 94 inline void GetMax(int &px, int &py) 95 { 96 int mx = -INF; 97 for(int i = 0; i < n; i++) 98 for(int j = 0; j < m; j++) 99 if(mx < dp[i][j] && dp[i][j] < INF) 100 { 101 mx = dp[i][j]; 102 px = i, py = j; 103 } 104 } 105 106 inline void Solve() 107 { 108 bool flag = 0; 109 for(int i = 0; i < n && !flag; i++) 110 for(int j = 0; j < m && !flag; j++) 111 if(graph[i][j] == ‘.‘) 112 { 113 Bfs(i, j); 114 flag = 1; 115 } 116 117 int px, py; 118 GetMax(px, py); 119 Bfs(px, py); 120 121 GetMax(px, py); 122 printf("%d\n", dp[px][py]); 123 } 124 125 int main() 126 { 127 freopen("a.in", "r", stdin); 128 Input(); 129 Solve(); 130 return 0; 131 }
ural 1145. Rope in the Labyrinth
标签:
原文地址:http://www.cnblogs.com/StupidBoy/p/5077624.html