zoukankan      html  css  js  c++  java
  • HDU

    Find a way

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6157    Accepted Submission(s): 2052


    Problem Description
    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.
     

    Input
    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 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
     

    Sample Output
    66 88 66
     

    Author
    yifenfei
     

    Source
     



    简单的BFS题,bfs来记录Y和M到每一个点的最小步数。这里只是要注意可能会有的KFC不能到达


    AC代码:

    #include <map>
    #include <set>
    #include <cmath>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <cstdio>
    #include <cctype>
    #include <string>
    #include <vector>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define LL long long
    #define INF 0x7fffffff
    using namespace std;
    
    int n, m;
    
    int mp[205][205];
    
    int d[205][205];
    int vis[205][205];
    
    const int dx[4] = {-1, 0, 1, 0};
    const int dy[4] = {0, 1, 0, -1};
    
    struct node {
    	int a;
    	int b;
    	int cnt;
    	node (int x, int y, int z) {
    		a = x;
    		b = y;
    		cnt = z;
    	}
    };
    
    void bfs(int x, int y) {	//bfs遍历来存储M和Y到每一个点的距离 
    	memset(vis, 0, sizeof(vis));
    	queue<node> que;
    	que.push(node(x, y, 0));
    	vis[x][y] = 1;
    	while(!que.empty()) {
    		node t = que.front();
    		que.pop();
    		for(int i = 0; i < 4; i ++) {
    			int xx = t.a + dx[i];
    			int yy = t.b + dy[i];
    			if(xx >= 0 && xx <= n - 1 && yy >= 0 && yy <= m - 1 && mp[xx][yy] != 2 && !vis[xx][yy]) {
    				que.push(node(xx, yy, t.cnt + 1));
    				d[xx][yy] += t.cnt + 1;
    				vis[xx][yy] = 1;
    			}
    		}
    	}
    }
    
    int main() {
    	while(scanf("%d %d", &n, &m) != EOF) {
    		int yx, yy;
    		int mx, my;
    		char s[205];
    		for(int i = 0; i < n; i ++) {
    			scanf("%s", &s);
    			for(int j = 0; j < m; j ++) {
    				if(s[j] == '.') mp[i][j] = 1;
    				else if(s[j] == '#') mp[i][j] = 2;
    				else if(s[j] == '@') {
    					mp[i][j] = 3;
    				}
    				else if(s[j] == 'Y') {
    					mp[i][j] = 4;
    					yx = i; yy = j;
    				}
    				else if(s[j] == 'M') {
    					mp[i][j] = 4;
    					mx = i; my = j;
    				}
    			}
    		}
    		
    //		for(int i = 0; i < n; i ++, cout << endl) {
    //			for(int j = 0; j < m; j ++) {
    //				cout << mp[i][j] << " ";
    //			}
    //		}
    		
    		memset(d, 0, sizeof(d));
    		bfs(yx, yy);
    		bfs(mx, my);
    		
    		int ans = INF;
    		for(int i = 0; i < n; i ++) {
    			for(int j = 0; j < m; j ++) {
    				if(mp[i][j] == 3) {
    					if(d[i][j] != 0) {	//要注意还有可能有的KFC不能到达 
    						ans = min(ans, d[i][j]);
    					}
    				}
    			}
    		}
    		printf("%d
    ", ans * 11);
    	}
    	return 0;
    }
    








  • 相关阅读:
    POJ2785-4 Values whose Sum is 0
    Codeforce 9C
    Codeforces 797C -Minimal string
    程序员自我修养
    异或
    Socket网络编程
    java编写本月日历
    PreparedStatement的用法
    项目导入时报错:The import javax.servlet.http.HttpServletRequest cannot be resolved 解决方法
    Dell灵越 5559笔记本安装固态硬盘 BIOS设置
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6759011.html
Copyright © 2011-2022 走看看