zoukankan      html  css  js  c++  java
  • HDU 2612 Find a way

    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

    开始以为用双向bfs,两个人都走到的就直接输出答案,但实际对于到当前点,虽然Y和M都是最短,但总路程不一定最短,所以可以持续更新,也可以最后遍历一遍。

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    using namespace std;
    const int N = 205;
    int n,m;
    char a[N][N];
    
    int dx[4] = {-1,0,0,1};
    int dy[4] = {0,-1,1,0}; 
    int dis[3][N][N];
    int ans;
    
    struct node {
    	int x,y,id;
    }s1,s2;
    
    void bfs() {
    	queue <node> q;
    	q.push(s1); q.push(s2);
    	while(!q.empty()) {
    		node cur = q.front(); q.pop();
    		int x = cur.x,y = cur.y;
    		if(dis[1][x][y] && dis[2][x][y] && a[x][y] == '@') {
    			//printf("%d %d
    ",x,y);//debuging
    			//cout << (dis[1][x][y]+dis[2][x][y])*11 << endl;//debug dis1+dis2 not max(dis1,dis2);
    			//break;
    			ans = min(ans,dis[1][x][y]+dis[2][x][y]);
    		}
    		for(int i = 0;i < 4; i++) {
    			int nx = x+dx[i],ny = y+dy[i],id = cur.id;
    			if(nx < 1 || nx > n || ny < 1 || ny > m || a[nx][ny] == '#' || dis[id][nx][ny]) continue;//debug ny > m not < m 
    			node pt; pt.x = nx,pt.y = ny,pt.id = id;
    			q.push(pt); dis[id][nx][ny] = dis[id][x][y]+1;//debug +1 do not write
    		}
    	}
    } 
    
    int main() {
    	while(cin >> n >> m) {
    		ans = 1<<30;//debug init ans;
    		for(int i = 1;i <= n; i++) {
    			for(int j = 1;j <= m; j++) {
    				cin >> a[i][j]; dis[1][i][j] = dis[2][i][j] = 0;//debug not init dis
    				if(a[i][j] == 'Y') s1.x = i,s1.y = j,s1.id = 1;
    				if(a[i][j] == 'M') s2.x = i,s2.y = j,s2.id = 2; 
    			}
    		}
    		bfs();
    		cout << ans*11 << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    DPDK — 网卡初始化流程(Intel 82599 ixgbe 网卡驱动示例)
    leetcode 3. 无重复字符的最长子串
    20193120 实验四 Python综合实践
    hadoop常用的端口号
    Django学习笔记
    ORACLE EBS AP invoice 到付款的数据流
    EBS 系统标准职责定义MAP
    Advanced Pricing
    Oracle Advanced Pricing White Papers
    增加AP INVOICE 行&分配行
  • 原文地址:https://www.cnblogs.com/mzg1805/p/15012499.html
Copyright © 2011-2022 走看看