N. Find a way
来源:互联网 发布:大疆一键全景软件下载 编辑:程序博客网 时间:2024/04/30 09:57
题目网址:http://www.bnuoj.com/v3/contest_show.php?cid=6453#problem/N
题目描述:
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Imput:
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output:
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
Sample Input:
4 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#Sample Output:
668866
解题思路:两次广搜加枚举到每个KFC店的最短距离,初始化时注意全部为最大,因为存在不可能到达的KFC店,所以如果初始化为0,则枚举时最小距离为0,因此最开始应看成所有的KFC店都不能到达,初始化一个很大的值。就是这点错误,这题WA了4次~~
源代码如下:
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;#define MAXN 200 + 10struct Node { int x, y;};char MAPT[MAXN][MAXN];int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};int vis[MAXN][MAXN], mdist[MAXN][MAXN], ydist[MAXN][MAXN];int M, N;int check(int xx, int yy) { int flag = 1; if (xx < 0 || xx >= M || yy < 0 || yy >= N) flag = 0; if (vis[xx][yy]) flag = 0; return flag;}int BFS(int sx, int sy, char ch) { queue<Node> q; Node now, pre; int i, j; now.x = sx; now.y = sy; q.push(now); vis[now.x][now.y] = 1; while (!q.empty()) { pre = q.front(); q.pop(); for (i = 0; i < 4; i++) { now.x = pre.x + dir[i][0]; now.y = pre.y + dir[i][1]; if (check(now.x, now.y) && (MAPT[now.x][now.y] != '#')) { q.push(now); vis[now.x][now.y] = 1; if (ch == 'Y') ydist[now.x][now.y] = ydist[pre.x][pre.y] + 1; if (ch == 'M') mdist[now.x][now.y] = mdist[pre.x][pre.y] + 1; //if (MAPT[now.x][now.y] == ch) return dist[now.x][now.y]; } } } return 0;}int main() { //FILE *p = freopen("test.txt", "r", stdin); int i, j; while (scanf("%d%d", &M, &N) != EOF) { for (i = 0; i < M; i++) { scanf("%s", MAPT[i]); } int sx = 0, sy = 0, ex = 0, ey = 0; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { if (MAPT[i][j] == 'Y') { sx = i; sy = j; } if (MAPT[i][j] == 'M') { ex = i; ey = j; } } } memset(vis, 0, sizeof(vis)); memset(ydist, 0, sizeof(ydist)); BFS(sx, sy, 'Y'); memset(vis, 0, sizeof(vis)); memset(mdist, 0, sizeof(mdist)); BFS(ex, ey, 'M'); int ans = 11111111; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { if (MAPT[i][j] == '@' && mdist[i][j] != 0 && ydist[i][j] != 0) { ans = min(ans, mdist[i][j] + ydist[i][j]); } } } printf("%d\n", ans * 11); } return 0;}
- N. Find a way
- N - Find a way HDU 2612 基础BFS
- HDU 2612 Find a way(双BFS) (N)
- Find a way
- Find a way hdu2612
- Find a way
- Find a way 2612
- HDU2612---Find a way
- Find a way
- hdu2612---Find a way
- Find a way
- HDU2612 Find a way
- Find a way--hdoj
- hdu Find a way
- Find a way
- Find a way
- hdu Find a way
- Find a way
- 【西祠日志】【08】周五周五,一周又结束,有点困惑未了
- [Leetcode 229, Medium] Majority Element II
- 如何搭建Struts2项目
- Android开发笔记--WIFI状态判断
- Leetcode 7 Reverse Integer
- N. Find a way
- 数据挖掘工程师笔试及答案整理
- HDU空心三角形
- Win7/Win8下双击运行jar程序的方法
- 1212 Big Number
- 11.2.0.4.1RAC Installation Guid for RHEL6
- Vuforia SDK---- AR开发vuforia实现拍照截屏功能
- 阿里电话面试问题----100万个URL如何找到出现频率最高的前100个?
- Ubuntu 与 cocos2d-x