[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612

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
[email protected]
.#...
.#...
@..M.
#...#

Sample Output

66
88
66

开始的时候想都没想就先找出所有的中间点,然后计算每个中间点,Y和M到他们的最短距离,然后求出最短的。果然超时
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 40005
int n,m,dx[]={1,-1,0,0},dy[]={0,0,1,-1},num,sum[maxn],vis[205][205];
char mapn[205][205];
struct point{
    int x,y;
};
point p[maxn];
struct node{
    int x,y,step;
};
void bfs(int a,int b,int c,int d,int cnt){
    node p;
    p.x = a,p.y = b,p.step = 0;
    queue<node> q;
    q.push(p);
    while(!q.empty()){
        node tmp = q.front();
        q.pop();
        if(tmp.x == c && tmp.y == d){
            sum[cnt] += tmp.step;
            return;
        }
        for(int i=0;i<4;i++){
            int xx = tmp.x + dx[i];
            int yy = tmp.y + dy[i];
            if(xx>0 && xx<=n && yy>0 && yy<=m && !vis[xx][yy] && mapn[xx][yy]!=‘#‘){
                vis[xx][yy] = 1;
                node tp;
                tp.x = xx,tp.y = yy,tp.step = tmp.step + 1;
                q.push(tp);
            }
        }
    }
}
int main()
{
    while(cin >> n >> m){
        num = 0;
        memset(sum,0,sizeof(sum));
        if(!n || !m){
            break;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin >> mapn[i][j];
                if(mapn[i][j] == ‘@‘){
                    p[num].x = i,p[num].y = j,num++;
                }
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mapn[i][j] == ‘Y‘ || mapn[i][j] == ‘M‘){
                    for(int k=0;k<num;k++){
                        memset(vis,0,sizeof(vis));
                        bfs(i,j,p[k].x,p[k].y,k);
                    }
                }
            }
        }
        sort(sum,sum+num);
        cout << sum[0] * 11 << endl;
    }
    return 0;
}

后面是先计算好Y,M到每个点的最短距离,这样只需要循环Y,M的个数次数就行,不需再套上中间点个数的循环。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 40005
#define inf 0x1f1f1f1f
int n,m,dx[]={1,-1,0,0},dy[]={0,0,1,-1},num,sum[205][205][2],vis[205][205],z;
char mapn[205][205];
struct node{
    int x,y,step;
};
void bfs(int a,int b){
    memset(vis,0,sizeof(vis));//在这里重置vis数组
    node p;
    p.x = a,p.y = b,p.step = 0;
    vis[p.x][p.y] = 1;
    queue<node> q;
    q.push(p);
    while(!q.empty()){
        node tmp = q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int xx = tmp.x + dx[i];
            int yy = tmp.y + dy[i];
            if(xx>0 && xx<=n && yy>0 && yy<=m && !vis[xx][yy] && mapn[xx][yy]!=‘#‘){
                vis[xx][yy] = 1;
                node tp;
                tp.x = xx,tp.y = yy,tp.step = tmp.step + 1;
                sum[tp.x][tp.y][z] = tp.step;
                q.push(tp);
            }
        }
    }
}
int main()
{
    while(cin >> n >> m){
        num = 0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                sum[i][j][0] = inf;
                sum[i][j][1] = inf;//注意记录距离的sum数组一定要初始化为一个很大的数
            }
        }
        if(!n || !m){
            break;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin >> mapn[i][j];
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mapn[i][j] == ‘Y‘){
                    z = 0;
                    bfs(i,j);
                }
                if(mapn[i][j] == ‘M‘){
                    z = 1;
                    bfs(i,j);
                }
            }
        }
        int ans = inf;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mapn[i][j] == ‘@‘){
                    ans = min(ans,sum[i][j][0] + sum[i][j][1]);
                }
            }
        }
        cout << ans*11 << endl;
    }
    return 0;
}
时间: 2024-08-27 13:07:12

[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612的相关文章

kuangbin带你飞专题一 简单搜索 题解

目录 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题一 简单搜索 总结:用时2天半终于把这个专题刷完了 对于最基础的dfs bfs 路径打印 状态转移也有了一点自己些微的理解 其实2天半可以压缩到1天半的 主要是自己太懒了...慢慢加油刷bin神的专题呀 从大二下学期开始学算法 一开始就知道这个专题 一开始对于这个专题里的所有问题感觉都好难啊..就直接放弃了 看lrj的书 现在看到这个专题还挺唏嘘的吧 突然觉得思维和想法也不是很难 果然是那个时候心不静&还是储量不够吗

[kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple

1 //Memory Time 2 //2236K 32MS 3 4 #include<iostream> 5 using namespace std; 6 7 int mod[524286]; //保存每次mod n的余数 8 //由于198的余数序列是最长的 9 //经过反复二分验证,436905是能存储198余数序列的最少空间 10 //但POJ肯定又越界测试了...524286是AC的最低下限,不然铁定RE 11 12 int main(int i) 13 { 14 int n; 15

[kuangbin带你飞]专题一 简单搜索 棋盘问题

题来:链接https://vjudge.net/problem/OpenJ_Bailian-132 J - 棋盘问题 1.题目: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C.Input 输入含有多组测试数据. 每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目. n <= 8 , k

[kuangbin带你飞]专题一 简单搜索

一直拖.放了放久.受不了 A - 棋盘问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第

[kuangbin带你飞]专题一 简单搜索 A - 棋盘问题

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目. n <= 8 , k <= n 当为-1 -1时表示输入结束. 随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白

Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two m

[kuangbin带你飞]专题一 简单搜索 bfs B - Dungeon Master

B - Dungeon Master You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You c

[kuangbin带你飞]专题一 简单搜索 - M - 非常可乐

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 bool cup[105][105][105]; 8 struct dot 9 { 10 int a; 11 int b; 12 int c; 13 int s; 14 }; 15 int x, y,

[kuangbin带你飞]专题一 简单搜索 - L - Oil Deposits

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 char g[105][105]; 8 int x, y, ans; 9 int dx[3]={1,0,-1}; 10 int dy[3]={1,0,-1}; 11 bool sscanf() 12