HDU 2612 Find a way

本文探讨了一道算法题,旨在寻找两人从各自位置出发,到达最近的KFC餐厅的总时间。通过广度优先搜索算法,计算两人分别到达所有KFC的最短路径,最终找出总时间最小的方案。

题目链接:传送门

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

题目大意:输入Y,M表示接下来有一个Y*M的矩阵,Y和M分别表示两个人的地点,@表示KFC,#表示墙,问两个人到达KFC的时间相加最短是多少。

坑点:有的KFC可能在墙内,不能到达。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int m[202][202];
int n[202][202];
int w[202][202];
char s[202][202];
int Y,M;
int x1,x2,y1,y2;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int a,b,c;
};
void bfs(int x,int y,int ss[][202])
{
    int i;
	node st,ed;
	queue<node>q;
	st.a=x;
	st.b=y;
	st.c=0;
	q.push(st);
	while(!q.empty())
	{
		st=q.front();
		q.pop();
		for(i=0;i<4;i++)
		{
			ed.a=st.a+dir[i][0];
			ed.b=st.b+dir[i][1];
			if(ed.a<0||ed.b<0||ed.a>=Y||ed.b>=M||s[ed.a][ed.b]=='#'||w[ed.a][ed.b])
                continue;
			ed.c=st.c+1;
			w[ed.a][ed.b]=1;
		    if(s[ed.a][ed.b]=='@')
			{
				ss[ed.a][ed.b]=ed.c;
			}
			q.push(ed);
		}
	}
}
int main()
{
    int a,b,c,d,e,f,g;
    while(cin>>Y>>M)
    {
        for(a=0;a<Y;a++)
            cin>>s[a];
        for(b=0;b<Y;b++)
        {
            for(c=0;c<M;c++)
            {
                if(s[b][c]=='Y')
                {
                    x1=b;
                    y1=c;
                }
                if(s[b][c]=='M')
                {
                    x2=b;
                    y2=c;
                }
            }
        }
        memset(n,0,sizeof(n));
        memset(m,0,sizeof(m));
        memset(w,0,sizeof(w));
        w[x1][y1]=1;
        bfs(x1,y1,n);
        memset(w,0,sizeof(w));
        w[x2][y2]=1;
        bfs(x2,y2,m);
        int min=0x6ffffff;
        for(d=0;d<Y;d++)
        {
            for(e=0;e<M;e++)
            {
                if(min>n[d][e]+m[d][e]&&n[d][e]&&m[d][e])
                    min=n[d][e]+m[d][e];
            }
        }
        cout<<min*11<<endl;
    }
    return 0;
}

 

 

 

第五届广西省大学生程序设计竞赛K Kirby's challenge(AC代码) 分数 300 作者 Colin 单位 杭州电子科技大学 Description Recently, Colin bought a Switch for Eva. And they are playing "Kirby and the Forgotten Land". In a challenge mission, Kirby is in a 4×n grid. The row of it is numbered from 1 to 4, and the column of it is numbered from 1 to n. There are many keys in this grid. Let a x,y ​ represent the status of cell (x,y). If a x,y ​ =1, there is a key in (x,y). If a x,y ​ =0, there is no key in (x,y). Kirby starts at (1,1), and should reach (4,n). Moreover, Kirby must collect all the keys in the grid to open the door in (4,n). Kirby will collect the key at (x,y) when Kirby reach (x,y). Of course, Kirby will collect the key at (1,1) at the beginning. In a second, Kirby can move from (x,y) to (x+1,y),(x,y+1),(x−1,y). Or Kirby can stay at (x,y) and throw a returnable flying weapon(boomerang) to collect keys in the flying path. Kirby has two ways to throw the weapon. As the picture shows: image-20220604213457062.png The flying path is represented as the grey cells, so keys in the grey cells can be collected by the weapon. In a second, Kirby can only choose one way to throw the weapon, but Kirby can throw the weapon multiple times at (x,y) if necessary. Notice: Kirby can't get off the grid, but the weapon can fly outside the grid and keep the flying path. Please write a program to help Colin and Eva find the shortest time to complete the challenge mission, so that they can get more rewards. Input The first line contains one integer n (1≤n≤100). In the next 4 lines, the x-th line contains n integers a x,1 ​ ,a x,2 ​ ,⋯,a x,n ​ (0≤a x,y ​ ≤1). Output Print one integer representing the minimum number of seconds required to complete the challenge mission. Sample input 1 5 1 1 1 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 output 1 8 The best solution is: Spend 1 second to throw the weapon in the second way at (1,1), and spend 7 seconds to reach (4,5). 代码长度限制 16 KB 时间限制 1000 ms 内存限制 512 MB 栈限制 131072 KB
最新发布
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值