CSL的校园卡

思路:

bfs,用状压表示走过的区域,然后和x1,y1,x2,y2构成所有的状态,然后标记一下就可以了

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head bool vis[(<<) + ][][][][];
int dir[][] = {, , , , , -, -, };
char s[][];
int n, m;
struct node {
int x1, y1, x2, y2, dis, st;
};
int get(int x, int y) {
return (x-)*m + y - ;
}
int bfs() {
queue<node> q;
int st = ;
for (int i = ; i <= n; i++) {
for (int j = ; j <= m; j++) {
if(s[i][j] == 'S' || s[i][j] == 'X') st |= <<get(i, j);
}
}
for (int i = ; i <= n; i++) {
for (int j = ; j <= m; j++) {
if(s[i][j] == 'S') {
q.push(node{i, j, i, j, , st});
break;
}
}
}
while(!q.empty()) {
node now = q.front();
q.pop();
if(now.st == (<<n*m) - ) return now.dis;
if(!vis[now.st][now.x1][now.y1][now.x2][now.y2]) vis[now.st][now.x1][now.y1][now.x2][now.y2] = true;
else continue;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
int x = now.x1 + dir[i][];
int y = now.y1 + dir[i][];
int xx = now.x2 + dir[j][];
int yy = now.y2 + dir[j][];
if( <= x && x <= n && <= y && y <= m && <= xx && xx <= n && <= yy && yy <= m && s[x][y] != 'X' && s[xx][yy] != 'X') {
q.push(node{x, y, xx, yy, now.dis+, now.st|(<<get(x, y))|(<<get(xx, yy))});
}
}
}
}
return ;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = ; i <= n; i++) {
scanf("%s", s[i]+);
}
printf("%d\n", bfs());
return ;
}

牛客小白月赛7 CSL的校园卡的更多相关文章

  1. 树的最长链-POJ 1985 树的直径(最长链)+牛客小白月赛6-桃花

    求树直径的方法在此转载一下大佬们的分析: 可以随便选择一个点开始进行bfs或者dfs,从而找到离该点最远的那个点(可以证明,离树上任意一点最远的点一定是树的某条直径的两端点之一:树的直径:树上的最长简 ...

  2. 牛客网 牛客小白月赛5 I.区间 (interval)-线段树 or 差分数组?

    牛客小白月赛5 I.区间 (interval) 休闲的时候写的,但是写的心情有点挫,都是完全版线段树,我的一个队友直接就水过去了,为啥我的就超内存呢??? 试了一晚上,找出来了,多初始化了add标记数 ...

  3. 牛客小白月赛8 - E - 诡异数字 数位DP

    牛客小白月赛8 - E - 诡异数字 题意: 求区间中,满足限制条件的数字的个数. 限制条件就是某些数字不能连续出现几次. 思路: 比较裸的数位DP, DP数组开一个dp[len][x][cnt] 表 ...

  4. 牛客小白月赛18 Forsaken给学生分组

    牛客小白月赛18 Forsaken给学生分组 Forsaken给学生分组 链接:https://ac.nowcoder.com/acm/contest/1221/C来源:牛客网 ​ Forsaken有 ...

  5. 牛客小白月赛18 Forsaken喜欢数论

    牛客小白月赛18 Forsaken喜欢数论 题目传送门直接点标题 ​ Forsaken有一个有趣的数论函数.对于任意一个数xxx,f(x)f(x)f(x)会返回xxx的最小质因子.如果这个数没有最小质 ...

  6. 牛客小白月赛19 E 「火」烈火燎原 (思维,树)

    牛客小白月赛19 E 「火」烈火燎原 (思维,树) 链接:https://ac.nowcoder.com/acm/contest/2272/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空 ...

  7. 【牛客小白月赛21】NC201604 Audio

    [牛客小白月赛21]NC201604 Audio 题目链接 题目大意: 给出三点 ,求到三点距离相等的点 的坐标. 解析 考点:计算几何基础. 初中蒟蒻表示不会什么法向量.高斯消元..qwq 方法一: ...

  8. 【牛客小白月赛21】NC201605 Bits

    [牛客小白月赛21]NC201605 Bits 题目链接 题目描述 Nancy喜欢做游戏! 汉诺塔是一个神奇的游戏,神奇在哪里呢? 给出3根柱子,最开始时n个盘子按照大小被置于最左的柱子. 如果盘子数 ...

  9. 牛客小白月赛16 小石的妹子 二分 or 线段树

    牛客小白月赛16 这个题目我AC之后看了一下别人的题解,基本上都是线段树,不过二分也可以. 这个题目很自然就肯定要对其中一个进行排序,排完序之后再处理另外一边,另一边记得离散化. 怎么处理呢,你仔细想 ...

随机推荐

  1. 'root'@'127.0.0.1'没有grant privileges

    从另外一台服务器拷贝了个mysql实例过来,给root@'%'授权的时候提示ERROR 1045 (28000): Access denied for user 'root'@'localhost' ...

  2. 这五件事,二次SaaS创业的老炮儿都在做(转)

    在我看来,我们现在正处在SaaS公司发展过程中的第三代.第一代SaaS公司有Salesforce.Netsuite和Webex等.紧接着兴起的第二代SaaS公司大多都是利用Salesforce或其他网 ...

  3. replace 将逗号替换~

    var reg = new RegExp(",","g"); //"g"表示全局替换var aa="qq,ww";aa= ...

  4. CTF-逆向工程实验吧Just Click

    题目链接:http://www.shiyanbar.com/ctf/1889 步骤一:PEID解析:如下图所示 步骤二:打开exe,这种类型的东西用OD打不开,想了一下,这种东西应该是C#做的,用.n ...

  5. 动态规划之139 Word Break

    题目链接:https://leetcode-cn.com/problems/word-break/ 参考链接:https://blog.csdn.net/c_flybird/article/detai ...

  6. InstallShield安装包在Win7下权限问题的解决方案 (转载)

    转载:http://blog.csdn.net/wuzhengqing1/article/details/6570149 转载:http://blog.csdn.net/brikoff/article ...

  7. Error: php71w-common conflicts with php-common-5.4.16-46.el7.x86_64

    Centos7.3 阿里云主机 安装zabbix报错 问题: [root@lvhanzhi code]# yum install -y zabbix-web-mysql zabbix-agent ma ...

  8. windows下如何安装vundle?

    参考: http://blog.csdn.net/zhuxiaoyang2000/article/details/8636472 vundle是gmarik 受 ruby的 bunler的启发开发的. ...

  9. What are the differences between Flyweight and Object Pool patterns?

    What are the differences between Flyweight and Object Pool patterns? They differ in the way they are ...

  10. Awesome Torch

    Awesome Torch This blog from: A curated list of awesome Torch tutorials, projects and communities. T ...