2015轻院校赛 B 迷宫 (bfs)

http://acm.zznu.edu.cn/problem.php?id=1967

这套题的有毒   我交了好多遍才对

坑:机关要按照顺序走 并且在走这个机关之前不能走这个机关  但是能穿过这个机关   所以不能把机关刚开始设成墙

要设成其他符号

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>

using namespace std;
#define memset(a,b) memset(a,b,sizeof(a))
#define N 110

struct node
{
    int x,y,step;
}e[N];

char maps[N][N];
int n,m;
int dis[8][2]={{1,0},{-1,0},{0,1},{0,-1},{-1,-1},{1,1},{-1,1},{1,-1}};
int vis[N][N];

int IF(int x,int y,int nx,int ny,int k)
{
    if(maps[x][y]==‘*‘ || maps[x][y]==‘#‘ || x<0 || x>=n || y<0 || y>=m || vis[x][y]==1)
        return 0;
    if(k>=4)
    {
        if(k==4)
        {
            if(maps[x][y+1]==‘#‘&&maps[x+1][y]==‘#‘)
                return 0;
        }
        if(k==5)
        {
            if(maps[x][y-1]==‘#‘ && maps[x-1][y]==‘#‘)
                return 0;
        }
        if(k==6)
        {
            if(maps[x][y-1]==‘#‘ && maps[x+1][y]==‘#‘)
                return 0;
        }
        if(k==7)
        {
            if(maps[x][y+1]==‘#‘ && maps[x-1][y]==‘#‘)
                return 0;
        }
    }
    return 1;
}

int bfs(node s,node o)
{
    memset(vis,0);
    queue <node>Q;
    Q.push(s);
    vis[s.x][s.y]=1;
    while(Q.size())
    {
        node p,q;
        p=Q.front();
        Q.pop();
        if(p.x==o.x && p.y==o.y)
            return p.step;
        for(int i=0;i<=7;i++)
        {
            q.x=p.x+dis[i][0];
            q.y=p.y+dis[i][1];
            if(IF(q.x,q.y,p.x,p.y,i)==1)
            {
                q.step=p.step+1;
                Q.push(q);
                vis[q.x][q.y]=1;
            }
        }
    }
    return -1;
}

int main()
{
    int T,k;
    scanf("%d",&T);
    while(T--)
    {
        int flag=0;
        scanf("%d %d %d",&n,&m,&k);
        for(int i=0;i<n;i++)
        {
            scanf("%s",maps[i]);
        }
        for(int i=0;i<=k;i++)
        {
            scanf("%d %d",&e[i].x,&e[i].y);
            e[i].step=0;
            if(i!=0 && i!=1)
            {
                if(e[i].x==e[0].x && e[i].y==e[0].y)
                    flag=1;
            }
        }
        if(flag==1)
        {
            printf("-1\n");
            continue;
        }
        int ans=0,sum=0;;
        for(int i=1;i<=k;i++)
        {
            for(int j=i+1;j<=k;j++)
            {
                maps[e[j].x-1][e[j].y-1]=‘*‘;
            }
            node S,E;
            S.x=e[i-1].x-1;
            S.y=e[i-1].y-1;
            S.step=0;
            E.x=e[i].x-1;
            E.y=e[i].y-1;
            E.step=0;
            ans=bfs(S,E);
            if(ans==-1)
            {
                sum=-1;
                break;
            }
            else
                sum+=ans;
            for(int j=i+1;j<=k;j++)
            {
                maps[e[j].x-1][e[j].y-1]=‘.‘;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}
时间: 2024-08-26 08:39:24

2015轻院校赛 B 迷宫 (bfs)的相关文章

2015轻院校赛 D 社交网络(排列组合)

http://acm.zznu.edu.cn/problem.php?id=1964 题目描述 输入 输出 样例输入 2 2 1 0 1 1 0 3 1 0 1 1 1 0 1 1 1 0 样例输出 0.500 1.125 提示 之前想了一个公式  就是0.5*pow(0.5,k)*C(k,n); k是至少认识k个人   n是认识n个人 后来队友都把所有的东西都写出来了我才去验证第二个测试数据  发现是错的  当时真的想自己从五楼上跳下来 正确的公式应该是 for(i=k;i<=n;i++) {

第九届玲珑轻院校赛随笔

迎着小雨,我来到了翻车圣地---轻工业学院,我是来这几次翻几次,所以一开始心情就十分忐忑,开始的时候特别高大上,弄得跟省赛一样,还有个参赛证,不过因为意外,比赛推迟了一个小时,然后我们提前10分钟拿到了题目,因为是组队赛,所以与一开始我们分别看了三道题,,当他们两个讨论的时候,,我发现了,,以我的水平做不了分给我的那道题,然后我就在写生日party和烫饭那道题,我主要是自己单扣,生日party那道题模拟出来了但是卡常数,就TEL了,,然后就到中场吃东西时间啦,午餐很丰富,然后我就在饱腹感中AC了

2015北京网络赛 G Boxes BFS+打表

G Boxes 题意:n个位置摆有n个箱子,每次移动只能把相邻的垒起来,且上面的必须小于下面的.求摆成升序需要移动多少步. 思路:这里的n很小,只有7.但是bfs最快的情况需要2s左右,所以就打表了. 诡异的是n = 6时居然都跑不出来都超时,连6也打了个表. 1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <

【DFS+堆的二叉树结构】15轻院校赛-J-堆

[题目链接:J-堆] 1734: 堆 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 239  Solved: 113 SubmitStatusWeb Board Description Input Output Sample Input 3 1 10 3 10 5 3 1 2 1 3 5 1 2 3 4 5 3 1 2 1 2 4 2 5 Sample Output Yes No Yes [思路] 堆:堆最重要的性质就是儿子的值一定不小于父亲的值.

2016轻院校赛E作死报告

#include <stdio.h>int nums[1000005], judge[1000005];///nums数组表示从1900年到某年期间的闰年个数int days[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};void Init(){ for(int i=1900; i<=1000000; i

【二叉树、堆】15轻院校赛-J-堆

原题:http://acm.zzuli.edu.cn/problem.php?cid=1099&pid=9 [描述] [输入] [输出] Sample Input 3 1 10 3 10 5 3 1 2 1 3 5 1 2 3 4 5 3 1 2 1 2 4 2 5 Sample Output Yes No Yes [代码] 转自:http://www.cnblogs.com/chenchengxun/p/4443560.html 1 #include<stdio.h> 2 #incl

ZOJ 1649 Rescue(有敌人迷宫BFS)

题意 求迷宫中从a的位置到r的位置需要的最少时间  经过'.'方格需要1s  经过'x'方格需要两秒  '#'表示墙 由于有1s和2s两种情况  需要在基础迷宫bfs上加些判断 令到达每个点的时间初始为无穷大  当从一个点到达该点用的时间比他本来的时间小时  更新这个点的时间并将这个点入队  扫描完全图就得到答案咯 #include<cstdio> #include<cstring> #include<queue> using namespace std; const

2015北京网络赛A题The Cats&#39; Feeding Spots

题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<memory.h> 6 using namespace std; 7 const i

2015北京网络赛 Couple Trees 倍增算法

2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道.  解法来自 qscqesze ,这个其实之前如果了解过倍增的话还是不是很难,不过这题的数据也不是很给力,极限数据理论上是过不了的.  其他解法有树链剖分?并不是很清楚.就这样水过了吧... 1 #include <iostream> 2 #include <cstdio> 3 #include &l