kuangbin带你飞《简单搜索》N-find a way题解报告

*最近在刷搜索题,今天做了这个两个点搜索的觉得挺有意思,写一个题解*

题目链接:https://vjudge.net/contest/221801#problem/N
开始我想的时用一个结构体保存两个状态(即Y与M),后来实施的时候发现了问题,不能两个点绑定在一起搜索。就两个Y与M分开搜索原始点到各个kfc的最短距离,用一个二位数组记录每个kfc到两个原点的距离,emmm对还有乘上11,我是最后输出才乘的。
ok talk is cheap

#include<bits/stdc++.h>
using namespace std;
const int d[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct node{
    int x,y,step;
    node(){}
    node(int q,int w,int e){
        x=q;y=w;
        step=e;
    }
};
int vis[220][220];
char mat[220][220];
int s1[220][220];//记录最短路
int n,m,ans=0;
void bfs(int x1,int y1,int x2,int y2);
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie();
    while(cin>>n>>m){
        memset(mat,0,sizeof(mat));
        memset(vis,0,sizeof(vis));
        memset(s1,0,sizeof(s1));
        //memset(s2,0x3f,sizeof(s2));
        //memset(vis2,0,sizeof(vis2));
        int x1,y1,x2,y2;
        for (int i=0;i<n;i++){
            //scanf("%s",mat[i]);
            cin>>mat[i];
            for (int j=0;j<m;j++){
                if (mat[i][j]=='Y'){//记录两个原始点
                    x1=i;
                    y1=j;
                }
                if (mat[i][j]=='M'){
                    x2=i;
                    y2=j;
                }
            }
        }
        bfs(x1,y1,x2,y2);
    }
    return 0;
}
void bfs(int x1,int y1,int x2,int y2){
    queue<node> que;
    que.push(node(x1,y1,0));
    vis[x1][y1]=1;
    while(!que.empty()){
        node now=que.front();
        que.pop();
        if (mat[now.x][now.y]=='@'){
            s1[now.x][now.y]+=now.step;
            //continue;
        }
        int tx,ty;
        for (int i=0;i<4;i++){
            tx=now.x+d[i][0];
            ty=now.y+d[i][1];
            if (tx<0||tx>=n||ty<0||ty>=m||vis[tx][ty]||mat[tx][ty]=='#')
                continue;
            vis[tx][ty]=1;
            que.push(node(tx,ty,now.step+1));
        }
    }
    memset(vis,0,sizeof(vis));
    vis[x2][y2]=1;
    que.push(node(x2,y2,0));
    while(!que.empty()){
        node now=que.front();
        que.pop();
        if (mat[now.x][now.y]=='@')
            s1[now.x][now.y]+=now.step;
        int tx,ty;
        for (int i=0;i<4;i++){
            tx=now.x+d[i][0];
            ty=now.y+d[i][1];
            if (tx<0||tx>=n||ty<0||ty>=m||vis[tx][ty]||mat[tx][ty]=='#')
                continue;
            vis[tx][ty]=1;
            que.push(node(tx,ty,now.step+1));
        }
    }
    int ans=INT_MAX;
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
            if(s1[i][j]>0)
                ans=min(ans,s1[i][j]);
    cout<<ans*11<<"\n";//关掉同步是为了节省时间,则不能用endl啦
}

emmmmm还有一个小东西,最先一次提交re,然后关掉cin同步后就ac以为关掉同步有bug,咨询了一下大佬以后,晓得了关掉同步以后就不能用printf和scanf了。

已标记关键词 清除标记
Problem Description We'll consider an interesting geometric problem here. Given a number of circles with varying radius on the plane, and define the P-value of a point (x, y) on the plane as the number of circles covering this point. Here, by "covering", we mean that the point is either strictly within the circle, or on the boundary of the circle. Given the starting position (Sx, Sy), and the destination position (Tx, Ty), please find a path between the two points, such that every point of the path is on the boundary of one or more circles, and the absolute difference between the maximum P-value and the minimum P-value among all points on the path is minimized. Can you find the minimum absolute value with the help of your computer? Input There are multiple test cases in the input file. Each test case starts with one integer N (1 <= N <= 150), the number of circles, followed by four real numbers, Sx, Sy, Tx, Ty, representing the x-coordinate and y-coordinate of the starting position and the destination. Each of the following N lines consists of three real numbers X, Y and R (R >= 1), indicating that there is a circle at position (X, Y) with radius R. There is a blank line after each test case. Input ends with End-of-File. Note: It is guaranteed that the input data is always legal, i.e. both the starting position and the destination are on the boundary of one or more circles, no two circles will be at the same position, every real number in the input file has at most three digits after the decimal point, and the absolute value of any real number does not exceed 10000. Output For each test case, output one integer on one separate line as requested. If there is no way to reach the destination, output -1 instead. Sample Input 2 -1.000 0.000 1.000 0.000 0.000 0.000 1.000 1.000 0.000 1.000 2 -1.000 0.000 5.000 0.000 -1.000 -1.000 1.000 4.000 0.000 1.000 Sample Output Case 1: 1 Case 2: -1
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页