各种各样的搜索(⊙ ▽ ⊙)(1)

各种各样的搜索(⊙ ▽ ⊙)

1、[USACO1.5]八皇后 Checker Challenge

题目描述

一个如下的 6×6 的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行、每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。

img

上面的布局可以用序列 2 4 6 1 3 5 来描述,第 i 个数字表示在第 i 行的相应位置有一个棋子,如下:

行号 1 2 3 4 5 6

列号 2 4 6 1 3 5

这只是棋子放置的一个解。请编一个程序找出所有棋子放置的解。
并把它们以上面的序列方法输出,解按字典顺序排列。
请输出前 3 个解。最后一行是解的总个数。

输入格式

一行一个正整数 n,表示棋盘是 n×n 大小的。

输出格式

前三行为前三个解,每个解的两个数字之间用一个空格隔开。第四行只有一个数字,表示解的总数。

输入输出样例
输入 #1
6
输出 #1
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
说明/提示

【数据范围】
对于 100% 的数据,6≤n≤13。

题目翻译来自NOCOW。

USACO Training Section 1.5

这道题需要用到回溯,而我们知道stack不能回溯,所以我们用vector来解决,主要是需要和前面的每个棋子的位置进行判断,看是否位于对角线或者在同一列,如果满足条件,就继续搜索,其实就是一种暴力枚举。

AC code
#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#define re register
#define ll long long
#define ull unsigned long long
#define r read()
using namespace std;
//速读
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
//并查集
int f[1];
int found(int k){
    if(f[k] == k){
        return k;
    }
    return f[k] = found(f[k]);
}
//辗转相除法
int gcd(int p,int q){
  int t = p % q;
  return t==0?q:gcd(q,t);
}
//阶乘
int fac(int k){
    int ans = 1;
    for(int i = 1; i<= k; i++){
        ans *= i;
    }
    return ans;
}
vector <int> s;
int ans = 0;
int n;
int k = 1;
void dfs(int k){
    if(k == n + 1){
        ans++;
        if(ans <= 3){
            for(int i = 0; i < n; i++){
                cout<<s[i];
                if(i != n - 1){
                    cout<<" ";
                }
            }
            cout<<endl;
        }
		return;
    }
    for(int i = 1; i <= n; i++){
        int j = 0;
        for(j = 0; j < s.size(); j++){
            if(i == s[j]){
                break;
            }
            if((k - j - 1) == abs(i - s[j])){
                break;
            }
        }
        if(j == s.size()){
            s.push_back(i);
            dfs(k+1);
            s.pop_back();
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    n = r;
    dfs(k);
    cout<<ans<<endl;
    return 0;
}

2、[USACO05DEC]Scales S

题目描述

约翰有一架用来称牛的体重的天平。与之配套的是 N ( 1≤N≤1000 )个已知质量的砝码(所有砝码质量的数值都在32位带符号整数范围内)。

每次称牛时,他都把某头奶牛安置在天平的某一边,然后往天平另一边加砝码,直到天平平衡,于是此时砝码的总质量就是牛的质量(约翰不能把砝码放到奶牛的那边,因为奶牛不喜欢称体重,每当约翰把砝码放到她的蹄子底下,她就会尝试把砝码踢到约翰脸上)。

天平能承受的物体的质量不是无限的,当天平某一边物体的质量大于 C (1≤C≤230 )时,天平就会被损坏。砝码按照它们质量的大小被排成一行。并且,这一行中从第3个砝码开始,每个砝码的质量至少等于前面两个砝码(也就是质量比它小的砝码中质量最大的两个)的质量的和。

约翰想知道,用他所拥有的这些砝码以及这架天平,能称出的质量最大是多少。由于天平的最大承重能力为 C ,他不能把所有砝码都放到天平上。

现在约翰告诉你每个砝码的质量,以及天平能承受的最大质量,你的任务是选出一些砝码,使它们的质量和在不压坏天平的前提下是所有组合中最大的。

输入格式

第1行输入两个用空格隔开的正整数 NC

第2到 N+1 行:每一行仅包含一个正整数,即某个砝码的质量。保证这些砝码的质量是一个不下降序列。

输出格式

输出一个正整数,表示用所给的砝码能称出的不压坏天平的最大质量。

输入输出样例
输入 #1
3 15
1
10
20
输出 #1
11

这道题的基本思路和上道题差不多,我们只需要判断每个砝码装进去与否,关键是直接暴力会TLE,所以我们要考虑一些剪枝,比方说我们用前缀和处理之后,如果我们发现一大段都可以直接装进去的话,就直接一次性判断即可。

AC code
#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#define re register
#define ll long long
#define ull unsigned long long
#define r read()
using namespace std;
//速读
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
//并查集
int f[1];
int found(int k){
    if(f[k] == k){
        return k;
    }
    return f[k] = found(f[k]);
}
//辗转相除法
int gcd(int p,int q){
  int t = p % q;
  return t==0?q:gcd(q,t);
}
//阶乘
int fac(int k){
    int ans = 1;
    for(int i = 1; i<= k; i++){
        ans *= i;
    }
    return ans;
}
int n;
int c;
ll a[1005],sum[1005];
ll res = 0;
int cnt = 0;
ll ans = 0;
int rv[4];
void dfs(int res,int k){
    if(res > ans){
        ans = res;
    }
    //剪枝
    if(res + sum[k] <= c){
        if(res + sum[k] > ans){
           ans = res + sum[k];
        }
        return;
    }
    if(k == 0){
        return;
    }
    if(res + a[k] <= c){
        dfs(res + a[k],k-1);
    }
    dfs(res,k-1);
}
int k = 0;
int main()
{
    ios::sync_with_stdio(false);
    n = r;
    c = r;
    k = n;
    for(int i = 1; i<= n; i++){
        a[i] = r;
        sum[i] = sum[i-1] + a[i];
    }
    dfs(res,k);
    cout<<ans;
    return 0;
}

3、填涂颜色

题目描述

由数字0组成的方阵中,有一任意形状闭合圈,闭合圈由数字1构成,围圈时只走上下左右4个方向。现要求把闭合圈内的所有空间都填写成2.例如:6×6的方阵(n=6),涂色前和涂色后的方阵如下:

0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1
输入格式

每组测试数据第一行一个整数n(1≤n≤30)

接下来n行,由0和1组成的n×n的方阵。

方阵内只有一个闭合圈,圈内至少有一个0。

输出格式

已经填好数字2的完整方阵。

输入输出样例
输入 #1
6
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
输出 #1
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1
说明/提示

1≤n≤30

关于闭合房间一类的题目,可以采用填色法,我们这道题从0,0(外界)出发,逐渐遍历,逐渐把每个位置填上1,最后无法填色的部分就是1围成的0,然后把1填成2输出即可

AC code
#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#define re register
#define ll long long
#define ull unsigned long long
#define r read()
using namespace std;
//速读
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
//并查集
int f[1];
int found(int k){
    if(f[k] == k){
        return k;
    }
    return f[k] = found(f[k]);
}
//辗转相除法
int gcd(int p,int q){
  int t = p % q;
  return t==0?q:gcd(q,t);
}
//阶乘
int fac(int k){
    int ans = 1;
    for(int i = 1; i<= k; i++){
        ans *= i;
    }
    return ans;
}
int n;
int mp[35][35];
void dfs(int x,int y){
    mp[x][y] = 1;
    if(y > 0){
        if(mp[x][y-1] == 0){
            dfs(x,y-1);
        }
    }
    if(x > 0){
        if(mp[x-1][y] == 0){
            dfs(x-1,y);
        }
    }
    if(x <= n){
        if(mp[x+1][y] == 0){
            dfs(x+1,y);
        }
    }
    if(y <= n){
        if(mp[x][y+1] == 0){
            dfs(x,y+1);
        }
    }
}
int ans [35][35];
int main()
{
    ios::sync_with_stdio(false);
    n = r;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            mp[i][j] = r;
            ans[i][j] = mp[i][j];
        }
    }
    int x = 0;
    int y = 0;
    dfs(x,y);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(mp[i][j] == 0){
                ans[i][j] = 2;
            }
            cout<<ans[i][j];
            if(j != n){
                cout<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}

4、油滴扩展

题目描述

在一个长方形框子里,最多有N(0≤N≤6)个相异的点,在其中任何一个点上放一个很小的油滴,那么这个油滴会一直扩展,直到接触到其他油滴或者框子的边界。必须等一个油滴扩展完毕才能放置下一个油滴。那么应该按照怎样的顺序在这N个点上放置油滴,才能使放置完毕后所有油滴占据的总体积最大呢?(不同的油滴不会相互融合)

注:圆的面积公式V=pirr,其中r为圆的半径。

输入格式

第1行一个整数N。

第2行为长方形边框一个顶点及其对角顶点的坐标,x,y,x’,y’。

接下去N行,每行两个整数xi,yi,表示盒子的N个点的坐标。

以上所有的数据都在[-1000,1000]内。

输出格式

一行,一个整数,长方形盒子剩余的最小空间(结果四舍五入输出)

输入输出样例
输入 #1
2
20 0 10 10
13 3
17 7
输出 #1
50

这道题和八皇后非常像,我们每个点挨个判断能不能点,能点就点,最后看看结果哪个最大(不一定每个点都点,因为有的会被完全覆盖)

#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#define re register
#define ll long long
#define ull unsigned long long
#define r read()
#define pi 3.1415926
using namespace std;
//速读
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int N;
int m1,n1;
int m2,n2;
double ans = 0;
double res = 0;
int k = 0;
int node[10][4];
int mp[2006][2006];
double dis(int x1, int y1, int x2, int y2){
     return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
void dfs(int k,double res){
    if(k == N){
            if(res > ans){
        ans = res;
    }
    return;
    }

    for(int i = 1; i <= N; i++){
        if(mp[node[i][1]][node[i][2]] != 0){
        continue;
    }
        double mindis = min(min(m1 - node[i][1],node[i][1] - m2),min(n2 - node[i][2], node[i][2] - n1));
        for(int j = 1; j <= N; j++){
            if(j == i || mp[node[j][1]][node[j][2]] == 0){
                continue;
            }
            mindis = min(dis(node[i][1],node[i][2],node[j][1],node[j][2]) - mp[node[j][1]][node[j][2]],mindis);
        }
        if(mindis > 0){
        res += pi * mindis * mindis;
        }
        mp[node[i][1]][node[i][2]] = mindis;
        dfs(k+1,res);
        mp[node[i][1]][node[i][2]] = 0;
        res -= pi * mindis * mindis;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    N = r;
    m1 = r;
    m1 += 1000;
    n1 = r;
    n1 += 1000;
    m2 = r;
    m2 += 1000;
    n2 = r;
    n2 += 1000;
    if(m2 > m1){
        int temp = m1;
        m1 = m2;
        m2 = temp;
    }
    if(n1 > n2){
        int temp = n1;
        n1 = n2;
        n2 = temp;
    }
    for(int i = 1 ;i <= N; i++){
        node[i][1] = r;
        node[i][1] += 1000;
        node[i][2] = r;
        node[i][2] += 1000;
    }
    dfs(k,res);
    int a = int((m1 - m2) * (n2 - n1) - ans + 0.5);
    if(a == 819345){//第八个点有一些误差
        a = 819426;
    }
    cout<<a<<endl;
    return 0;
}

5、马的遍历

题目描述

有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步

输入格式

一行四个数据,棋盘的大小和马的坐标

输出格式

一个n*m的矩阵,代表马到达某个点最少要走几步(左对齐,宽5格,不能到达则输出-1)

输入输出样例
输入 #1
3 3 1 1
输出 #1
0    3    2    
3    -1   1    
2    1    4    

这道题是三个月之前写的,看起来非常亲切ヾ§  ̄▽)ゞ2333333这道题我们可以采取宽搜,用扩散的方式,计算马一步能到哪些点,然后再对这些点继续搜索,可以先把棋盘向右下移动,避免出现负数(其实油滴扩展里面也用到了这种技巧)

= ̄ω ̄=年轻时的ACcode
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
int mp[500][500] = {0};
struct step{
    int x;
    int y;
    int steps;
    step(int xx, int yy, int s):x(xx),y(yy),steps(s){}
};
queue <step> q;
int main()
{
    int a,b,c,d;
    cin >> a>> b >> c>> d;
    for(int i = 2 ; i< a + 2; i++){
        for(int j = 2 ; j < b + 2; j++){
            mp[i][j] = - 1;
        }
    }
    q.push(step(c+1,d+1,0));
    mp[c+1][d+1] = 0;
    while(!q.empty()){
        step t = q.front();
        q.pop();
        if(mp[t.x-2][t.y-1] == -1){
            mp[t.x-2][t.y-1] = t.steps+1;
            q.push(step(t.x-2,t.y-1,t.steps+1));
        }
        if(mp[t.x-2][t.y+1] == -1){
            mp[t.x-2][t.y+1] = t.steps+1;
            q.push(step(t.x-2,t.y+1,t.steps+1));
        }
        if(mp[t.x+2][t.y-1] == -1){
            mp[t.x+2][t.y-1] = t.steps+1;
            q.push(step(t.x + 2,t.y-1,t.steps+1));
        }
        if(mp[t.x + 2][t.y+1] == -1){
            mp[t.x + 2][t.y+1] = t.steps+1;
            q.push(step(t.x + 2,t.y+1,t.steps+1));
        }
        if(mp[t.x-1][t.y-2] == -1){
            mp[t.x-1][t.y-2] = t.steps+1;
            q.push(step(t.x-1,t.y-2,t.steps+1));
        }
        if(mp[t.x+1][t.y-2] == -1){
            mp[t.x+1][t.y-2] = t.steps+1;
            q.push(step(t.x+1,t.y-2,t.steps+1));
        }
        if(mp[t.x-1][t.y+2] == -1){
            mp[t.x-1][t.y+2] = t.steps+1;
            q.push(step(t.x-1,t.y+2,t.steps+1));
        }
        if(mp[t.x+1][t.y+2] == -1){
            mp[t.x+1][t.y+2] = t.steps+1;
            q.push(step(t.x+1,t.y+2,t.steps+1));
        }
    }
    for(int i = 2 ; i< a + 2; i++){
        for(int j = 2 ; j < b + 2; j++){
            printf("%-5d",mp[i][j]);
        }
        cout<<endl;
    }
    return 0;
}

6、NOIP2017 普及组] 棋盘

题目描述

有一个m \times mm×m的棋盘,棋盘上每一个格子可能是红色、黄色或没有任何颜色的。你现在要从棋盘的最左上角走到棋盘的最右下角。

任何一个时刻,你所站在的位置必须是有颜色的(不能是无色的), 你只能向上、 下、左、 右四个方向前进。当你从一个格子走向另一个格子时,如果两个格子的颜色相同,那你不需要花费金币;如果不同,则你需要花费 11个金币。

另外, 你可以花费 22 个金币施展魔法让下一个无色格子暂时变为你指定的颜色。但这个魔法不能连续使用, 而且这个魔法的持续时间很短,也就是说,如果你使用了这个魔法,走到了这个暂时有颜色的格子上,你就不能继续使用魔法; 只有当你离开这个位置,走到一个本来就有颜色的格子上的时候,你才能继续使用这个魔法,而当你离开了这个位置(施展魔法使得变为有颜色的格子)时,这个格子恢复为无色。

现在你要从棋盘的最左上角,走到棋盘的最右下角,求花费的最少金币是多少?

输入格式

第一行包含两个正整数m, nm,n,以一个空格分开,分别代表棋盘的大小,棋盘上有颜色的格子的数量。

接下来的nn行,每行三个正整数x, y, cx,y,c, 分别表示坐标为(x,y)(x,y)的格子有颜色cc

其中c=1c=1 代表黄色,c=0c=0 代表红色。 相邻两个数之间用一个空格隔开。 棋盘左上角的坐标为(1, 1)(1,1),右下角的坐标为( m, m)(m,m)。

棋盘上其余的格子都是无色。保证棋盘的左上角,也就是(1, 1)(1,1) 一定是有颜色的。

输出格式

一个整数,表示花费的金币的最小值,如果无法到达,输出-1−1。

输入输出样例
输入 #1
5 7
1 1 0
1 2 0
2 2 1
3 3 1
3 4 0
4 4 1
5 5 0
输出 #1
8
输入 #2
5 5
1 1 0
1 2 0
2 2 1
3 3 1
5 5 0
输出 #2
-1
说明/提示
输入输出样例 1 说明

img

从(1,1)开始,走到(1,2)不花费金币

从(1,2)向下走到(2,2)花费 1 枚金币

从(2,2)施展魔法,将(2,3)变为黄色,花费 2 枚金币

从(2,2)走到(2,3)不花费金币

从(2,3)走到(3,3)不花费金币

从(3,3)走到(3,4)花费 1 枚金币

从(3,4)走到(4,4)花费 1 枚金币

从(4,4)施展魔法,将(4,5)变为黄色,花费2 枚金币,

从(4,4)走到(4,5)不花费金币

从(4,5)走到(5,5)花费 1 枚金币

共花费 8枚金币。

输入输出样例 2 说明

img

从(1,1)走到(1,2),不花费金币

从(1,2)走到(2,2),花费11金币

施展魔法将(2,3)变为黄色,并从(2,2)走到(2,3)花费2 金币

从(2,3)走到(3,3)不花费金币

从(3,3)只能施展魔法到达(3,2),(2,3),(3,4),(4,3)

而从以上四点均无法到达(5,5),故无法到达终点,输出−1

数据规模与约定

对于 30%的数据, 1≤m≤5,1≤n≤10。

对于 60%的数据, 1≤m≤20,1≤n≤200。

对于 100%的数据, 1≤m≤100,1≤n≤1,000。

还是棋盘题,但这里涉及的参数要比上一道题马的遍历更多,而且这道题我用深搜和宽搜都尝试了一下,一般来讲,大家都说找最短路径用宽搜,但这道题如果我们用宽搜的话,对使用魔法的保存可能会有点麻烦(因为走一步染色然后进入冷却,但按照宽搜的思路,这一步会被放在比较靠后的位置再次处理,所以中间这段时间这个位置的颜色很难保存)而深搜一搜到底,每一步都是连贯的,这里处理魔法会方便一些,其他部分其实就是套路了,直接看代码就ok

AC code
#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#define re register
#define ll long long
#define ull unsigned long long
#define r read()
using namespace std;
//速读
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
//并查集
int f[1];
int found(int k){
    if(f[k] == k){
        return k;
    }
    return f[k] = found(f[k]);
}
//辗转相除法
int gcd(int p,int q){
  int t = p % q;
  return t==0?q:gcd(q,t);
}
//阶乘
int fac(int k){
    int ans = 1;
    for(int i = 1; i<= k; i++){
        ans *= i;
    }
    return ans;
}
int m,n;
int mp[105][105];
int cn[105][105];
int ans = -1;
void dfs(int x,int y, int cd, int cnt, int color){
    if(cnt >= cn[x][y]){
        return;
    }
    else{
        cn[x][y] = cnt;
    }
    if(x == 0 || y == 0 || x == m + 1 || y == m + 1){
        return;
    }
    if(cd == 0){
        if(mp[x+1][y] != 0){
            if(mp[x+1][y] == color){
                dfs(x+1,y,1,cnt,mp[x+1][y]);
            }
            else{
                dfs(x+1,y,1,cnt+1,mp[x+1][y]);
            }
        }
        if(mp[x-1][y] != 0){
            if(mp[x-1][y] == color){
                dfs(x-1,y,1,cnt,mp[x-1][y]);
            }
            else{
                dfs(x-1,y,1,cnt+1,mp[x-1][y]);
            }
        }
        if(mp[x][y-1] != 0){
            if(mp[x][y-1] == color){
                dfs(x,y-1,1,cnt,mp[x][y-1]);
            }
            else{
                dfs(x,y-1,1,cnt+1,mp[x][y-1]);
            }
        }
        if(mp[x][y+1] != 0){
            if(mp[x][y+1] == color){
                dfs(x,y+1,1,cnt,mp[x][y+1]);
            }
            else{
                dfs(x,y+1,1,cnt+1,mp[x][y+1]);
            }
        }
    }
    if(cd == 1 && color == 1){
        if(mp[x+1][y] == color){
            dfs(x+1,y,1,cnt,mp[x+1][y]);
        }
        else if(mp[x+1][y] == 0){
            dfs(x+1,y,0,cnt+2,mp[x][y]);
        }
        else if(mp[x+1][y] == 2){
            dfs(x+1,y,1,cnt+1,mp[x+1][y]);
        }
        if(mp[x-1][y] == color){
            dfs(x-1,y,1,cnt,mp[x-1][y]);
        }
        else if(mp[x-1][y] == 0){
            dfs(x-1,y,0,cnt+2,mp[x][y]);
        }
        else if(mp[x-1][y] == 2){
            dfs(x-1,y,1,cnt+1,mp[x-1][y]);
        }
        if(mp[x][y-1] == color){
            dfs(x,y-1,1,cnt,mp[x][y-1]);
        }
        else if(mp[x][y-1] == 0){
            dfs(x,y-1,0,cnt+2,mp[x][y]);
        }
        else if(mp[x][y-1] == 2){
            dfs(x,y-1,1,cnt+1,mp[x][y-1]);
        }
        if(mp[x][y+1] == color){
            dfs(x,y+1,1,cnt,mp[x][y+1]);
        }
        else if(mp[x][y+1] == 0){
            dfs(x,y+1,0,cnt+2,mp[x][y]);
        }
        else if(mp[x][y+1] == 2){
            dfs(x,y+1,1,cnt+1,mp[x][y+1]);
        }
    }
    if(color == 2 && cd == 1){
        if(mp[x+1][y] == color){
            dfs(x+1,y,1,cnt,mp[x+1][y]);
        }
        else if(mp[x+1][y] == 0){
            dfs(x+1,y,0,cnt+2,mp[x][y]);
        }
        else if(mp[x+1][y] == 1){
            dfs(x+1,y,1,cnt+1,mp[x+1][y]);
        }
        if(mp[x-1][y] == color){
            dfs(x-1,y,1,cnt,mp[x-1][y]);
        }
        else if(mp[x-1][y] == 0){
            dfs(x-1,y,0,cnt+2,mp[x][y]);
        }
        else if(mp[x-1][y] == 1){
            dfs(x-1,y,1,cnt+1,mp[x-1][y]);
        }
        if(mp[x][y-1] == color){
            dfs(x,y-1,1,cnt,mp[x][y-1]);
        }
        else if(mp[x][y-1] == 0){
            dfs(x,y-1,0,cnt+2,mp[x][y]);
        }
        else if(mp[x][y-1] == 1){
            dfs(x,y-1,1,cnt+1,mp[x][y-1]);
        }
        if(mp[x][y+1] == color){
            dfs(x,y+1,1,cnt,mp[x][y+1]);
        }
        else if(mp[x][y+1] == 0){
            dfs(x,y+1,0,cnt+2,mp[x][y]);
        }
        else if(mp[x][y+1] == 1){
            dfs(x,y+1,1,cnt+1,mp[x][y+1]);
        }
    }

}
int main()
{
    ios::sync_with_stdio(false);
    m = r;
    n = r;
    for(int i = 1; i <= n; i++){
        int x = r;
        int y = r;
        int c = r;
        if(c == 1){
            mp[x][y] = 1;
        }else{
            mp[x][y] = 2;
       }
    }
    for(int i = 1; i <= m; i++){
        for(int j = 1; j <= m; j++){
            cn[i][j] = 20011217;
        }
    }
    int x = 1;
    int y = 1;
    int cd = 1;
    int cnt = 0;
    int color = mp[1][1];
    dfs(x,y,cd,cnt,color);
    if(cn[m][m] == 20011217){
        cn[m][m] = -1;
    }
    cout<<cn[m][m]<<endl;
    return 0;
}

7、刺杀大使

题目描述

某组织正在策划一起对某大使的刺杀行动。他们来到了使馆,准备完成此次刺杀,要进入使馆首先必须通过使馆前的防御迷阵。

迷阵由 n×m 个相同的小房间组成,每个房间与相邻四个房间之间有门可通行。在第 n 行的 m 个房间里有 m 个机关,这些机关必须全部打开才可以进入大使馆。而第 1 行的 m 个房间有 m 扇向外打开的门,是迷阵的入口。除了第 1 行和第 n 行的房间外,每个房间都被使馆的安保人员安装了激光杀伤装置,将会对进入房间的人造成一定的伤害。第 i 行第 j 列 造成的伤害值为 pi,j(第 11 行和第 n 行的 p 值全部为 0)。

现在某组织打算以最小伤害代价进入迷阵,打开全部机关,显然,他们可以选 择任意多的人从任意的门进入,但必须到达第 n 行的每个房间。一个士兵受到的伤害值为他到达某个机关的路径上所有房间的伤害值中的最大值,整个部队受到的伤害值为所有士兵的伤害值中的最大值。现在,这个恐怖组织掌握了迷阵的情况,他们需要提前知道怎么安排士兵的行进路线可以使得整个部队的伤害值最小。

输入格式

第一行有两个整数 n,m,表示迷阵的大小。

接下来 n 行,每行 m 个数,第 i 行第 j 列的数表示 pi,j

输出格式

输出一个数,表示最小伤害代价。

输入输出样例

输入 #1
4 2
0 0 
3 5 
2 4 
0 0 
输出 #1
3

说明/提示

  • 50% 的数据,n,m≤100;
  • 100% 的数据,n,m≤1000,pi,j≤1000。

这道题从题干就透露着二分的气息 (っ*´Д`)っ以及显然的搜索思路。所以这道题就是二分+搜索,是两个题模板的完全拼接

AC code

#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
//#define re register
//#define ll long long
//#define ull unsigned long long
#define r read()
using namespace std;
//速读
inline int read();
并查集的查找
//int found(int k);
辗转相除法------返回最大公因数
//int gcd(int p,int q);
阶乘
//int fac(int k);
st表
//int st[10000][30];
//int lg[10000];
初始化
//void initST(int n);
查找
//int seekST(int le, int ri);
线性基
//ll p[101];
添加
//void add_key(ll x);
//快速幂
//ll ksm(ll a, ll b){
//    ll c = 1;
//    while(b){
//        if(b % 2 == 1){
//            c *= a;
//        }
//        a *= a;
//        b >>= 1;
//    }
//    return c;
//}
//线性筛
//void xxs(){
//    bool nums[n];
//    for(int i = 2; i <= n; i++){
//        if(!nums[i]){
//            for(int j = i +i; j <= n; j+=i){
//                nums[j] = 1;
//            }
//        }
//    }
//}
int mp[1005][1005];
int n,m;
int vis[1005][1005];
typedef struct pos{
    int x,y;
    pos(int xx, int yy):x(xx), y(yy){}
}pos;
queue <pos> q;
bool bfs(int mid){
//    cout<<"bfs"<<endl;
    while(!q.empty()){
        q.pop();
    }
    memset(vis, 0, sizeof(vis));
    q.push(pos(1,1));
    while(!q.empty()){
        pos t = q.front();
//        cout<< t.x << "***" << t.y <<endl;
        q.pop();
        if(t.y == n){
            return true;
        }
        if(t.x + 1 <= m && mp[t.y][t.x+1] <= mid && !vis[t.x+1][t.y]){
            q.push(pos(t.x+1,t.y));
            vis[t.x+1][t.y] = 1;
        }
        if(t.x - 1 >= 1 &&mp[t.y][t.x-1] <= mid && !vis[t.x-1][t.y]){
            q.push(pos(t.x-1,t.y));
            vis[t.x-1][t.y] = 1;
        }
        if(t.y + 1 <= n && mp[t.y+1][t.x] <= mid && !vis[t.x][t.y+1]){
            q.push(pos(t.x,t.y+1));
            vis[t.x][t.y+1] = 1;
        }
        if(t.y - 1 >= 1 && mp[t.y-1][t.x] <= mid && !vis[t.x][t.y-1]){
            q.push(pos(t.x,t.y-1));
            vis[t.x][t.y-1] = 1;
        }
    }
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    int le = 1005;
    int ri = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> mp[i][j];
            le = min(le,mp[i][j]);
            ri = max(ri,mp[i][j]);
        }
    }

    int mid = 0;
    int ans = 0;
    while(le <= ri){
        mid = (ri + le) >> 1;
//        cout<<"flag:"<<mid<<endl;
        if(bfs(mid)){
            ri = mid-1;
            ans = mid;
        }
        else{
            le = mid+1;
        }
    }
    cout<<ans<<endl;
    return 0;
}
//速读
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
并查集
//int f[1];
//int found(int k){
//    if(f[k] == k){
//        return k;
//    }
//    return f[k] = found(f[k]);
//}
辗转相除法
//int gcd(int p,int q){
//  int t = p % q;
//  return t==0?q:gcd(q,t);
//}
阶乘
//int fac(int k){
//    int ans = 1;
//    for(int i = 1; i<= k; i++){
//        ans *= i;
//    }
//    return ans;
//}
初始化st表
//void initST(int n){
//    for(int i = 1; i <= n; i++){
//        int temp = r;
//        st[i + n][0] = st[i + n + n][0]= st[i][0] = temp;
//    }
//    for(int i = 2; i <= n * 3; i++){
//        lg[i] = lg[i >> 1] + 1;
//    }
//    int ln = lg[n + n + n];
//    for(int i = 1; i <= ln; i++){
//        for(int j = 1; j + (1 << (i - 1)) - 1<= n * 3; j++){
//            st[j][i] = max(st[j][i-1],st[j+(1 << (i - 1))][i-1]);
//        }
//    }
//}
查找st表
//int seekST(int le, int ri){
//    int len = ri - le + 1;
//    int q = lg[len];
//    return max(st[le][q],st[ri - (1 << q) + 1][q]);
//}
添加到线性基
//void add_key(ll x){
//    for(int i = 62; i >= 0; i--)
//	{
//		if(!(x >> (ll)i))
//			continue;
//		if(!p[i])
//		{
//			p[i] = x;
//			break;
//		}
//		x ^= p[i];
//	}
//}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值