标签:
#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; const int maxn = 2 * 1e5 + 7; typedef long long ll; #define re(i,n) for(int i=0;i<n;i++) char a[107][107]; int b[107][107]; int n, m; int sx, sy; struct Point{ int x, y; }; struct Q{ Point a[10000]; int h, r; void init(){ h = r = 0; } void enq(int x, int y){ a[r].x = x, a[r].y = y, r++; } Point deq(){ return a[h++]; } }q; int dir[4][2] = {0,1,1,0,0,-1,-1,0}; void go(){ memset(b, -1, sizeof(b)); b[sx][sy] = 0; q.init(); q.enq(sx, sy); while (q.h != q.r){ Point me = q.deq(); re(i, 4){ int x = me.x + dir[i][0], y = me.y + dir[i][1]; if (b[x][y] != -1)continue; if (a[x][y] == ‘.‘){ b[x][y] = b[me.x][me.y] + 1; q.enq(x, y); } else if (a[x][y] == ‘S‘){ b[x][y] = b[me.x][me.y] + 1; } } } } int main(){ freopen("in.txt", "r", stdin); cin >> n >> m; re(i, n)re(j, m){ cin >> a[i + 1][j + 1]; if (a[i + 1][j + 1] == ‘P‘)a[i + 1][j + 1] = ‘#‘; else if (a[i + 1][j + 1] == ‘H‘){ sx = i + 1, sy = j + 1; a[sx][sy] = ‘.‘; } } re(i, n + 1)a[i][m + 1] = a[i][0] = ‘#‘; re(i, m + 1)a[0][i] = a[n + 1][i] = ‘#‘; go();/* re(i, n + 1){ re(j, m + 1){ printf("%3d", b[i][j]); } puts(""); }*/ int ans = 1e5; for (int i = 1; i <= n; i++){ for (int j = 1; j <= m; j++){ if (a[i][j] == ‘S‘){ if (b[i][j] == -1)continue; re(k, 4){ int x = i + dir[k][0], y = j + dir[k][1]; if (a[x][y] == ‘S‘){ if (b[x][y] != -1){ int now = b[x][y] + b[i][j]; ans = min(now, ans); } } } } } } if (ans == 1e5){ puts("Hi and Ho will not have lunch."); } else{ printf("%d\n", ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/weidiao/p/4855064.html