2020蓝桥杯B 组省赛计蒜客模拟赛(一)题解

试题 A:有趣的数字(结果填空)

题意点此进入
我们称一个数是质数,而且数位中出现了 5 的数字是有趣的。例如 5, 59, 457 都是有趣的,而 15, 7不是。求 1到 100000 中有趣的数的个数。

做法:用筛法把素数筛出来再判断数位中含不含5。

代码

#include  <algorithm>
#include  <iostream>
#include  <cstring>
#include  <vector>
#include  <cstdio>
#include  <queue>
#include  <cmath>
#include  <set>
#include  <map>
#include<bitset>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-9
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<LL, int> mli;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 2e5+10;
const LL Mod = 1e9+7;
const int M = 3e5+10;
int a[N];
int check(int x) {
    
    
  while(x) {
    
    
    if(x % 10 == 5) return 1;
    x /= 10;
  }
  return 0;
}
int main() {
    
    
  for(int i = 2; i*i < N; ++i) {
    
    
    if(!a[i]) {
    
    
      for(int j = i+i; j < N; j += i) a[j] = 1;
    }
  }
  int ans = 0;
  for(int i = 5; i <= 100000; ++i) {
    
    
    if(!a[i] && check(i)) cout << i << " ", ++ans;
  }
  cout << endl << ans;
  return 0;
}

答案:3282


试题 B:爬楼梯(结果填空)

题意点此进入
蒜头君要爬楼梯。楼梯一共有 10 层台阶。因为腿长的限制,每次最多能上 4 层台阶。但是第 5,7 层楼梯坏掉了不能踩。求上楼梯的方案数。

做法:从i-4~i-1状态转移过来。

代码

#include  <algorithm>
#include  <iostream>
#include  <cstring>
#include  <vector>
#include  <cstdio>
#include  <queue>
#include  <cmath>
#include  <set>
#include  <map>
#include<bitset>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-9
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<LL, int> mli;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 2e5+10;
const LL Mod = 1e9+7;
const int M = 3e5+10;
int dp[20];
int main() {
    
    
  dp[0] = 1;
  _rep(1, 10, i) {
    
    
    if(i == 5 || i == 7) continue;
    _rep(max(i-4, 0), i-1, j) {
    
    
      dp[i] += dp[j];
    }
  }
  cout << dp[10];
  return 0;
}

答案:72


试题 C:七巧板(结果填空)

题意点此进入
在这里插入图片描述

做法:会发现加一根直线是多了6个区域,加两根直线是多了7个区域,所以就是 7(+6+7+8+9+10)。

答案:47


试题 D:苹果(结果填空)

题意点此进入
有 30 个篮子,每个篮子里有若干个苹果,篮子里的苹果数序列已经给出。
现在要把苹果分给小朋友们,每个小朋友要么从一个篮子里拿三个苹果,要么从相邻的三个篮子里各拿一个苹果。
苹果可以剩余,而且不同的拿法会导致不同的答案。比如对于序列3 1 3 ,可以分给两个小朋友变成0 1 0;也可以分给一个小朋友变成2 0 2,此时不能继续再分了。所以答案是 2 。
求问对于以下序列,最多分给几个小朋友?

7 2 12 5 9 9 8 10 7 10 5 4 5 8 4 4 10 11 3 8 7 8 3 2 1 6 3 9 7 1

做法:这题一开始WA了很多次,我一开始试过先优先相邻三个取完,再来单个单个的取,发现这样是59,WA,然后换了一种方式,先单个单个取,再相邻三个取,这样57,WA,正解是对于每个单个单个取完就考虑三个相邻的,这样结果是62,三者取最大,但我也想不到这最后一种啊 ,更正确的做法应该是dp把,可惜我不会

代码

#include  <algorithm>
#include  <iostream>
#include  <cstring>
#include  <vector>
#include  <cstdio>
#include  <queue>
#include  <cmath>
#include  <set>
#include  <map>
#include<bitset>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-9
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<LL, int> mli;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 2e5+10;
const LL Mod = 1e9+7;
const int M = 3e5+10;
int a[32] = {
    
    0, 7, 2, 12, 5, 9, 9, 8, 10, 7, 10, 5, 4, 5, 8, 4, 4, 10, 11, 3, 8, 7,8,3,2,1,6,3,9,7,1};
int main() {
    
    
  int ans = 0;
  _rep(1, 30, i) {
    
    
    ans += a[i] / 3, a[i] %= 3;
    if(a[i] && a[i+1] && a[i+2]) {
    
    
      int mid = min(a[i], min(a[i+1], a[i+2]));
      ans += mid; a[i+2] -= mid; a[i+1] -= mid; a[i] -= mid;
    }
  }
  cout << ans << endl;
  return 0;
}

答案:62

试题 E:方阵(结果填空)

题意点此进入
在这里插入图片描述

做法这题也是做不出来系列 ,想过找规律,但是没找出来,如果两点能够看见一定是基于横坐标之差与纵坐标之差的gcd是1,设横坐标之差是x,纵坐标之差是y:

  • x为0或者y为0,是2 * n * (n-1)对,这就是k=1的时候啦。
  • x,y不为0且最大公约数是1,是2 * (n - x) * ( n - y) 对

代码

#include  <algorithm>
#include  <iostream>
#include  <cstring>
#include  <vector>
#include  <cstdio>
#include  <queue>
#include  <cmath>
#include  <set>
#include  <map>
#include<bitset>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-9
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<LL, int> mli;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 2e5+10;
const LL Mod = 1e9+7;
const int M = 3e5+10;
LL f[N];
int main() {
    
    
  int n = 1000, k = 500;
  LL ans = 2 * n * (n-1) % Mod;
  for(int i = 1; i < n; ++i) {
    
    
    for(int j = 1; j < n; ++j) {
    
    
      if(__gcd(i, j) == 1 && i*i+j*j <= k*k) {
    
    
        ans = (ans + 2*(n-i)%Mod*(n-j)%Mod) % Mod;
      }
    }
  }
  cout << ans % Mod << endl;
  return 0;
}

答案:916585708


试题 F:寻找重复项(程序设计)

题意点此进入
在这里插入图片描述在这里插入图片描述

做法:用map记录,访问到就值+1

代码

#include  <algorithm>
#include  <iostream>
#include  <cstring>
#include  <vector>
#include  <cstdio>
#include  <queue>
#include  <cmath>
#include  <set>
#include  <map>
#include<bitset>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-9
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<LL, int> mli;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 2e6+10;
const LL Mod = 1e9+7;
const int M = 3e5+10;
LL f[N], a, b, c;
map<LL, int> mp;
int main() {
    
    
  f[0] = 1;
  ++mp[1];
  scanf("%lld%lld%lld", &a, &b, &c);
  int flag = 0;
  _rep(1, 2000000, i) {
    
    
    f[i] = (a*f[i-1]%c + f[i-1]%b)%c;
    if(mp[f[i]]) {
    
     flag = i; break; }
    ++mp[f[i]];
  }
  if(!flag) puts("-1");
  else printf("%d\n", flag);
  return 0;
}


试题 G:被袭击的村庄(程序设计)

题意点此进入
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

做法:这就是一道卡了我一下午的模拟题,水逆的一天,这题不难,就是题目错了…输出的是道路、房屋、田地耐久度降为0的个数。直接模拟就好了,我是少考虑的一个边界情况以及老眼昏花没看见自己判断结果的循环放到while循环里了。

代码

#include  <algorithm>
#include  <iostream>
#include  <cstring>
#include  <vector>
#include  <cstdio>
#include  <queue>
#include  <cmath>
#include  <set>
#include  <map>
#include<bitset>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-9
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<LL, int> mli;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 310;
const LL Mod = 1e9+7;
const int M = 3e5+10;
LL n, m;
LL a, b, c; //三种耐久度
LL k; //炮弹攻击范围k*k
LL w; //溅射伤害
LL q; //q枚炮弹
LL id, x, y;  //炮弹编号,及中心位置
LL sh[N][N]; //炮弹伤害
LL ans;
LL num1, num2, num3;
LL num[N][N];
LL bx, by, ex, ey;
LL hp[N][N];
void caljs(int x, int y) {
    
      //溅射伤害
  int tx, ty;
  for(int i = 0; i < 8; ++i) {
    
    
    tx = x + ne[i][0]; ty = y + ne[i][1];
    if(tx < 1 || ty < 1 || tx > n || ty > m) continue;
    hp[tx][ty] = max(0ll, hp[tx][ty] - w);
  }
}
int main() {
    
    
  scanf("%lld%lld%lld%lld%lld%lld%lld", &n, &m, &a, &b, &c, &k, &w);
  _rep(1, k, i) _rep(1, k, j) scanf("%lld", &sh[i][j]);
  _rep(1, n, i) _rep(1, m, j) {
    
    
    scanf("%lld", &num[i][j]);
    if(num[i][j] == 1) hp[i][j] = a;
    else if(num[i][j] == 2) hp[i][j] = b;
    else hp[i][j] = c;
  }
  scanf("%lld", &q);
  while(q--) {
    
    
    scanf("%lld%lld%lld", &id, &x, &y);
    bx = x - k/2, by = y - k/2;  //炸弹范围的起始坐标
    ex = x + k/2, ey = y + k/2;  //炸弹范围的终止坐标

    for(int i = bx, ii = 1; i <= ex, ii <= k; ++i, ++ii)
      for(int j = by, jj = 1; j <= ey, jj <= k; ++j, ++jj) {
    
    
        if(i < 1 || j < 1 || i > n || j > m) continue;
        if(!id) caljs(i, j); hp[i][j] = max(0ll, hp[i][j]-sh[ii][jj]);
      }
  }
  _rep(1, n, i) _rep(1, m, j) {
    
    
    if(num[i][j] == 1) {
    
    
      if(!hp[i][j]) ++num1;
      ans += a-hp[i][j];
    } else if(num[i][j] == 2) {
    
    
      if(!hp[i][j]) ++num2;
      ans += b-hp[i][j];
    } else {
    
    
      if(!hp[i][j]) ++num3;
      ans += c-hp[i][j];
    }
  }
  printf("%lld %lld %lld\n%lld", num1, num2, num3, ans);
  return 0;
}


试题 H:字符串(程序设计)

题意点此进入在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

做法:这一题明显比上一题简单…直接暴力判断就好了,注意下处理的技巧以防超时。

代码

#include  <algorithm>
#include  <iostream>
#include  <cstring>
#include  <vector>
#include  <cstdio>
#include  <queue>
#include  <cmath>
#include  <set>
#include  <map>
#include<bitset>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-9
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<LL, int> mli;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 2010;
const LL Mod = 1e9+7;
const int M = 3e5+10;
string s, res;
LL a[N];
int main() {
    
    
  int m;
  cin >> s >> m;
  int n = s.size();
  LL mid = 0, mid1, mid2;
  for(int i = 0; i < n; ++i) mid = (mid * 26 + s[i]-'A') % m;
  a[n-1] = 1;
  for(int i = n-2; i >= 0; --i) a[i] = a[i+1] * 26 % m;
  if(!mid) {
    
     puts("0 0"); return 0; }
  for(int i = 0; i < n; ++i) {
    
    
    for(int j = i+1; j < n; ++j) {
    
    
      mid1 = (s[i]-s[j]) * a[j];
      mid2 = (s[j]-s[i]) * a[i];
      if((mid+mid1+mid2+m)%m == 0) {
    
    
        printf("%d %d\n", i+1, j+1); return 0;
      }
    }
  }
  puts("-1 -1");
  return 0;
}


试题 I:最短路(程序设计)

题意点此进入
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
做法:正向建图跑一遍再反向建图跑一遍最短路就好了,最后把两次dis的和全加起来。

代码

#include  <algorithm>
#include  <iostream>
#include  <cstring>
#include  <vector>
#include  <cstdio>
#include  <queue>
#include  <cmath>
#include  <set>
#include  <map>
#include<bitset>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-9
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<LL, int> mli;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const LL INF = 1e18;
const int N = 2e4+10;
const LL Mod = 1e9+7;
const int M = 2e5+10;
struct edge {
    
    
  int u, v; LL w;
}ed[M];
struct xx {
    
    
  int nxt, to; LL w;
  xx() {
    
    }
  xx(int a, int b, LL c) {
    
    
    nxt = a; to = b; w = c;
  }
}e[M];
int tot, head[N];
void Add(int u, int v, LL w) {
    
    
  e[++tot] = xx(head[u], v, w);
  head[u] = tot;
}
LL dis[N]; int vis[N];
priority_queue<pli, vector<pli>, greater<pli> >pq;
int n, m;
void dij() {
    
    
  _rep(1, n, i) dis[i] = INF, vis[i] = 0;
  dis[1] = 0;
  while(!pq.empty()) pq.pop();
  pq.push(make_pair(0ll, 1));
  int u, v;
  while(!pq.empty()) {
    
    
    u = pq.top().second; pq.pop();
    if(vis[u]) continue;
    vis[u] = 1;
    for(int i = head[u]; i; i = e[i].nxt) {
    
    
      v = e[i].to;
      if(dis[v] > dis[u] + e[i].w) {
    
    
        dis[v] = dis[u] + e[i].w;
        pq.push(make_pair(dis[v], v));
      }
    }
  }
}
int main() {
    
    
  int T; scanf("%d", &T);
  while(T--) {
    
    
    memset(head, 0, sizeof head); tot = 0;
    scanf("%d%d", &n, &m);
    _for(0, m, i) {
    
    
      scanf("%d%d%lld", &ed[i].u, &ed[i].v, &ed[i].w);
      Add(ed[i].u, ed[i].v, ed[i].w);
    }

    LL ans = 0;
    dij(); _rep(2, n, i) ans += dis[i];

    memset(head, 0, sizeof head); tot = 0;
    _for(0, m, i) Add(ed[i].v, ed[i].u, ed[i].w);
    dij(); _rep(2, n, i) ans += dis[i];

    printf("%lld\n", ans);
  }
  return 0;
}


试题 J:迷宫(程序设计)

题意点此进入
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

做法:bfs搜索题,乍看一眼很简单,实际上有点坑。

  • 循环跳传送门
    • 循环跳过程中有障碍
    • 循环跳过程中经过了终点
    • 循环跳过程中经过了起点
  • 起点就是终点
  • 起点或终点就是障碍物

代码

#include  <algorithm>
#include  <iostream>
#include  <cstring>
#include  <vector>
#include  <cstdio>
#include  <queue>
#include  <cmath>
#include  <set>
#include  <map>
#include  <bitset>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-9
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<LL, int> mli;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const LL INF = 1e18;
const int N = 1010;
const LL Mod = 1e9+7;
const int M = 110;
int n, m, Q, mid;
char tu[N][N];
int door[N][N], vis[N][N];
pii d[M];
int ex, ey;
struct xx {
    
    
  int x, y, step;
}u;
queue<xx> q;
int check(int tx, int ty) {
    
    
  if(tx < 1 || ty < 1 || tx > n || ty > m ) return 0;
  if(vis[tx][ty] || tu[tx][ty] != '.') return 0;
  return 1;
}
int bfs() {
    
    

  int tx = 1, ty = 1;
  while(door[tx][ty] && !vis[tx][ty] && tu[tx][ty] == '.') {
    
    
    if(tx == ex && ty == ey) return 0;
    mid = door[tx][ty];
    vis[tx][ty] = 1; tx = d[mid].first, ty = d[mid].second;
    if(tx == ex && ty == ey) return 0;
  }

  if(!check(tx, ty)) return -1;
  q.push(xx{
    
    tx, ty, 0}); vis[tx][ty] = 1;

  while(!q.empty()) {
    
    
    u = q.front(); q.pop();
    if(u.x == ex && u.y == ey) return u.step;
    for(int i = 0; i < 4; ++i) {
    
    
      tx = u.x + ne[i][0], ty = u.y + ne[i][1];
      if(!check(tx, ty)) continue;
      while(door[tx][ty] && !vis[tx][ty] && tu[tx][ty] == '.') {
    
    
        if(tx == ex && ty == ey) return u.step+1;
        mid = door[tx][ty];
        vis[tx][ty] = 1; tx = d[mid].first, ty = d[mid].second;

        if(tx == ex && ty == ey) return u.step+1;
      }
      if(!check(tx, ty)) continue;
      vis[tx][ty] = 1; q.push(xx{
    
    tx, ty, u.step+1});
    }
  }
  return -1;
}
int main() {
    
    
  scanf("%d %d", &n, &m);
  _rep(1, n, i) scanf("%s", tu[i]+1);
  scanf("%d", &Q);
  int x, y;
  _rep(1, Q, i) {
    
    
    scanf("%d%d%d%d", &x, &y, &d[i].first, &d[i].second);
    door[x][y] = i;
  }
  scanf("%d%d", &ex, &ey);
  int res = bfs();
  if(res == -1) puts("No solution");
  else printf("%d\n", res);
  return 0;
}
/*
3 4
..*.
..*.
..*.
3
2 2 2 4
2 4 3 1
3 1 1 4
1 4
*/


猜你喜欢

转载自blog.csdn.net/qq_43408978/article/details/108932389