搜索---广搜N - 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. 

InputThe 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 
OutputFor 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去@这个地点约会花费的最少时间。

思路:

 可以分开bfs(),先把Y去@所花费的时间遍历一遍在step1[][],然后再把M去@的时间遍历一遍,存在数组step2[][]里面。

最后可以求得到每个地方的总时间(let the total time to it be most smallest,不可以同时走),求最小值即可。

#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef struct node
{
    int x;
    int y;
}node;

node s,t;
char a[201][201];
int step[201][201];
int m1[201][201];
int m2[201][201];
bool vis[201][201];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int n,m;


int judge(node cur)
{
	if(cur.x<1||cur.x>n||cur.y<1||cur.y>m||vis[cur.x][cur.y]||a[cur.x][cur.y]=='#')
	   return 0;
	else
	  return 1;
}

void bfs(node cur)
{
    queue <node> q;    
    node next;
    q.push(cur);  
    memset(vis,0,sizeof(vis));
    vis[cur.x][cur.y] = true;
    step[cur.x][cur.y] = 0;
    while(!q.empty())
	{
        cur = q.front();
        q.pop();
        for(int i=0;i<4;i++)
		{
            next.x = cur.x + dir[i][0];
            next.y = cur.y + dir[i][1];
            if( judge(next) )     
               { 
                   step[next.x][next.y] = step[cur.x][cur.y] + 1; 
                   vis[next.x][next.y] = true;
                   q.push(next);
                  
			   }
            
        }
    }
}
int main()
{
    while(cin>>n>>m)
	{
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
			{
                cin>>a[i][j];
                if(a[i][j]=='Y')
				{   
                    s.x=i;
                    s.y=j;
                }
                else if(a[i][j]=='M')
				{    
                    t.x=i;
                    t.y=j;
                }
            }
        int ans = INF;
        bfs(s);   
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
			{
                m1[i][j]=step[i][j];
            }
        bfs(t);  
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
			{
                m2[i][j]=step[i][j];
            }
        for(int i=1;i<=n;i++)    
            for(int j=1;j<=m;j++)
			{
                step[i][j]=m1[i][j] + m2[i][j];
                if(a[i][j]=='@')
				{
                    ans =min(ans,step[i][j]);
                }
            }
        cout<<ans*11<<endl; 
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值