find a way

https://vjudge.net/contest/389717#problem/F
分析:裸的BFS不用优化剪枝也能过。。。
比赛的时候竟然打错了一堆东西。。。
具体的思路就是从两人的家开始遍历走两次BFS
之后遍历一遍去寻找KFC位置并取其中的最小值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=205;
char mp[maxn][maxn];
int n,m,yx,yy,mx,my;
int vis[maxn][maxn];
int dir[4][2]={0,1,1,0,-1,0,0,-1};
int mstep[maxn][maxn],ystep[maxn][maxn];
struct node
{
    int x,y;
};

void bfs(int sx,int sy,int flag)
{
    node start,next;
    int i,j;
    queue<node>q;
    start.x=sx;
    start.y=sy;
    vis[sx][sy]=1;
    mstep[sx][sy]=0;
    ystep[sx][sy]=0;
    q.push(start);

    while(!q.empty())
    {
        start=q.front();
        q.pop();
        for(i=0;i<4;i++)
        {
            next.x=start.x+dir[i][0];
            next.y=start.y+dir[i][1];
            if(next.x<1||next.x>n||next.y<1||next.y>m) continue;

            if(mp[next.x][next.y]!='#'&&vis[next.x][next.y]==0)
            {
                vis[next.x][next.y]=1;
                q.push(next);
                if(flag==1) ystep[next.x][next.y]=ystep[start.x][start.y]+1;
                else mstep[next.x][next.y]=mstep[start.x][start.y]+1;
            }
        }

    }
    return;
}
int main()
{
    int i,j,mn;
    while(cin>>n>>m)
    {
        mn=99999;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                cin>>mp[i][j];
                if(mp[i][j]=='Y')
                {
                    yx=i;
                    yy=j;
                }
                if(mp[i][j]=='M')
                {
                    mx=i;
                    my=j;
                }
            }
        }


        memset(vis,0,sizeof(vis));
        bfs(yx,yy,1);

        memset(vis,0,sizeof(vis));
        bfs(mx,my,2);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(mp[i][j]=='@')
                {
                    mn=min(mn,mstep[i][j]+ystep[i][j]);
                }
            }
        }
        printf("%d\n",mn*11);
    }
    return 0;
}
You and your team have worked tirelessly until you have a sequence a1,a2,…,a2n+1 of positive integers satisfying these properties. 1≤ai≤1018 for all 1≤i≤2n+1 . a1,a2,…,a2n+1 are pairwise distinct. a1=a2−a3+a4−a5+…+a2n−a2n+1 . However, the people you worked with sabotaged you because they wanted to publish this sequence first. They deleted one number from this sequence and shuffled the rest, leaving you with a sequence b1,b2,…,b2n . You have forgotten the sequence a and want to find a way to recover it. If there are many possible sequences, you can output any of them. It can be proven under the constraints of the problem that at least one sequence a exists.    Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104 ). The description of the test cases follows. The first line of each test case contains one integer n (1≤n≤2⋅105 ). The second line of each test case contains 2n distinct integers b1,b2,…,b2n (1≤bi≤109 ), denoting the sequence b . It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .    Output For each test case, output 2n+1 distinct integers, denoting the sequence a (1≤ai≤1018 ). If there are multiple possible sequences, you can output any of them. The sequence a should satisfy the given conditions, and it should be possible to obtain b after deleting one element from a and shuffling the remaining elements. Example InputCopy 4 1 9 2 2 8 6 1 4 3 99 2 86 33 14 77 2 1 6 3 2 OutputCopy 7 9 2 1 8 4 6 9 86 99 2 77 69 14 33 4 6 1 2 3
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值