标签:
Everyday Littile Hi and Little Ho meet in the school cafeteria to have lunch together. The cafeteria is often so crowded that two adjacent seats are hard to find.
School cafeteria can be considered as a matrix of N*M blocks. Each block can be empty or occupied by people, obstructions and seats. Little Hi and Little Ho starts from the same block. They need to find two adjacent seats(two seats are adjacent if and only if their blocks share a common edge) without passing through occupied blocks. Further more, they want the total distance to the seats is minimal.
Little Hi and Little Ho can move in 4 directions (up, down, left, right) and they can not move outside the matrix.
Input cantains a single testcase.
First line contains two integers N, M, the length and width of school cafeteria.
The next is a matrix consists of N lines, each line containing M characters. Each character describe a block: ‘.‘ for empty block, ‘P‘ for people, ‘#‘ for obstructions, ‘S‘ for seats and ‘H‘ for Little Hi and Little Ho‘s starting position.
10 <= N, M <= 100
Output the minimal distance they need to move to reach two adjacent seats. If no such adjacent seats output a line "Hi and Ho will not have lunch." without quotes.
Sample Input
10 10 ########## #...P##..# #S#...#.P# #S#..#...# #...#.#### #.#...#.H# ##......## #..P#..S.# ##.......# ##########
Sample Output
25
Solution:
最短路径算法的 简单应用。TALK IS CHEAP......
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <climits> 5 #include <cmath> 6 using namespace std; 7 8 char g[100][100]; 9 int N, M; 10 11 int d[100][100]; 12 bool spt[100][100]; 13 int Hi,Hj; 14 15 int minDistance() 16 { 17 int minm = INT_MAX, min_index; 18 for (int i = 0; i < N; ++i) { 19 for (int j = 0; j < M; ++j) { 20 if(spt[i][j] == false && d[i][j] <= minm) 21 minm = d[i][j], min_index = i*N + j; 22 } 23 } 24 25 return min_index; 26 } 27 28 void dijkstra() 29 { 30 for (int i = 0; i < N; ++i) 31 for (int j = 0; j < M; ++j) { 32 d[i][j] = INT_MAX, spt[i][j] = false; 33 } 34 35 d[Hi][Hj] = 0; 36 int V = N*M; 37 for (int k = 0; k < V-1; ++k) { 38 int u = minDistance(); 39 40 spt[u/N][u%N] = true; 41 42 for (int i = 0; i < N; ++i) 43 for (int j = 0; j < M; ++j) { 44 if (!spt[i][j] && ((g[i][j] == ‘.‘) && abs(i-u/N) + abs(j-u%N) == 1) 45 && d[u/N][u%N] != INT_MAX && (d[u/N][u%N] + 1 < d[i][j]) ) { 46 d[i][j] = d[u/N][u%N] + 1; 47 } 48 } 49 } 50 } 51 52 53 bool hasSeats() 54 { 55 for (int i = 0; i < N; ++i) { 56 for (int j = 0; j < M; ++j) { 57 if (g[i][j] == ‘S‘) { 58 if ((i+1 < N && g[i+1][j] == ‘S‘) 59 || (j+1 < M && g[i][j+1] == ‘S‘) 60 || (i-1 >= 0 && g[i-1][j] == ‘S‘) 61 || (j-1 >= 0 && g[i][j-1] == ‘S‘)) { 62 return true; 63 } 64 } 65 } 66 } 67 68 return false; 69 } 70 71 int solve() 72 { 73 int minm = INT_MAX; 74 for (int i = 0; i < N; ++i) { 75 for (int j = 0; j < M; ++j) { 76 if (g[i][j] == ‘S‘) { 77 if ((i+1 < N && g[i+1][j] == ‘S‘)) { 78 int d1 = INT_MAX; 79 if (i-1>=0) { 80 d1 = min(d1, d[i-1][j]); 81 } 82 if (j-1>=0) { 83 d1 = min(d1, d[i][j-1]); 84 } 85 if (j+1<M) { 86 d1 = min(d1, d[i][j+1]); 87 } 88 89 int d2 = INT_MAX; 90 if (j-1>=0) { 91 d2 = min(d2, d[i+1][j-1]); 92 } 93 if (j+1 < M) { 94 d2 = min(d2, d[i+1][j+1]); 95 } 96 if (i+2 < N) { 97 d2 = min(d2, d[i+2][j]); 98 } 99 if (d1 != INT_MAX && d2 != INT_MAX) 100 minm = min(d1+d2+2, minm); 101 } 102 if (j+1 < M && g[i][j+1] == ‘S‘) { 103 int d1 = INT_MAX; 104 int d2 = INT_MAX; 105 106 if (i-1>=0) { 107 d1 = min(d1, d[i-1][j]); 108 } 109 if (j-1>=0) { 110 d1 = min(d1, d[i][j-1]); 111 } 112 if (i+1 < N) { 113 d1 = min(d1, d[i+1][j]); 114 } 115 116 if (i-1>=0) { 117 d2 = min(d2, d[i-1][j+1]); 118 } 119 if (j+2 < M) { 120 d2 = min(d2, d[i][j+2]); 121 } 122 if (i+1 < N) { 123 d2 = min(d2, d[i+1][j+1]); 124 } 125 126 if (d1 != INT_MAX && d2 != INT_MAX) 127 minm = min(d1+d2+2, minm); 128 129 } 130 } 131 } 132 } 133 134 return minm; 135 } 136 137 int main() 138 { 139 scanf("%d %d", &N, &M); 140 for (int i = 0; i < N; ++i) { 141 for (int j = 0; j < M; ++j) { 142 scanf(" %c", g[i] + j); 143 if (g[i][j] == ‘H‘) { 144 Hi = i, Hj = j; 145 } 146 } 147 } 148 149 if (!hasSeats()) { 150 cout << "Hi and Ho will not have lunch." << endl; 151 } 152 else { 153 dijkstra(); 154 155 int dist = solve(); 156 if (dist == INT_MAX) { 157 cout << "Hi and Ho will not have lunch." << endl; 158 } 159 else { 160 cout << dist << endl; 161 } 162 } 163 }
hihoCoder #1092 Have Launch Together
标签:
原文地址:http://www.cnblogs.com/liew/p/4398015.html