广度优先搜索(BFS),即优先扩展浅层节点,逐渐深入的搜索算法。
广度优先搜索一般使用队列来实现。
填涂颜色
题目描述
由数字 0 0 0 组成的方阵中,有一任意形状的由数字 1 1 1 构成的闭合圈。现要求把闭合圈内的所有空间都填写成 2 2 2。例如: 6 × 6 6\times 6 6×6 的方阵( n = 6 n=6 n=6),涂色前和涂色后的方阵如下:
如果从某个 0 0 0 出发,只向上下左右 4 4 4 个方向移动且仅经过其他 0 0 0 的情况下,无法到达方阵的边界,就认为这个 0 0 0 在闭合圈内。闭合圈不一定是环形的,可以是任意形状,但保证闭合圈内的 0 0 0 是连通的(两两之间可以相互到达)。
0 0 0 0 0 0
0 0 0 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 1 0 1
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 1 2 1
1 1 1 1 1 1
输入格式
每组测试数据第一行一个整数 n ( 1 ≤ n ≤ 30 ) n(1 \le n \le 30) n(1≤n≤30)。
接下来 n n n 行,由 0 0 0 和 1 1 1 组成的 n × n n \times n n×n 的方阵。
方阵内只有一个闭合圈,圈内至少有一个 0 0 0。
输出格式
已经填好数字 2 2 2 的完整方阵。
样例 #1
样例输入 #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
提示
对于 100 % 100\% 100% 的数据, 1 ≤ n ≤ 30 1 \le n \le 30 1≤n≤30。
代码实现
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define MAX_N 30
typedef struct Data{
int x,y;
};
int cnt=0;
int arr[MAX_N+5][MAX_N+5],vis[MAX_N+5][MAX_N+5];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>arr[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(vis[i][j]||arr[i][j])continue;
int flag=0;
queue<Data>q;
q.push({i,j});
vis[i][j]=1;
vector<Data>v;
while(!q.empty())
{
int x=q.front().x,y=q.front().y;
Data pos={x,y};
q.pop();
if(x==0||x==n||y==0||y==n)
{
flag=1;
continue;
}
v.push_back(pos);
if(!vis[x+1][y]&&!arr[x+1][y])
q.push({x+1,y}),vis[x+1][y]=1,cnt++;
if(!vis[x][y+1]&&!arr[x][y+1])
q.push({x,y+1}),vis[x][y+1]=1,cnt++;
if(!vis[x-1][y]&&!arr[x-1][y])
q.push({x-1,y}),vis[x-1][y]=1,cnt++;
if(!vis[x][y-1]&&!arr[x][y-1])
q.push({x,y-1}),vis[x][y-1]=1,cnt++;
}
if(flag)continue;
for(auto k:v)arr[k.x][k.y]=2;
break;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(j!=1)cout<<" ";
cout<<arr[i][j];
}
cout<<endl;
}
return 0;
}
马的遍历
题目描述
有一个 n × m n \times m n×m 的棋盘,在某个点 ( x , y ) (x, y) (x,y) 上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步。
输入格式
输入只有一行四个整数,分别为 n , m , x , y n, m, x, y n,m,x,y。
输出格式
一个 n × m n \times m n×m 的矩阵,代表马到达某个点最少要走几步(不能到达则输出 − 1 -1 −1)。
样例 #1
样例输入 #1
3 3 1 1
样例输出 #1
0 3 2
3 -1 1
2 1 4
提示
数据规模与约定
对于全部的测试点,保证 1 ≤ x ≤ n ≤ 400 1 \leq x \leq n \leq 400 1≤x≤n≤400, 1 ≤ y ≤ m ≤ 400 1 \leq y \leq m \leq 400 1≤y≤m≤400。
#include<iostream>
#include<queue>
using namespace std;
#define MAXSIZE 400
int dis[MAXSIZE+5][MAXSIZE+5];
int pos[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
struct Data
{
Data(int x,int y,int t):x(x),y(y),t(t){};
int x,y,t;
};
int x,y,n,m;
void bfs(int x,int y,int t)
{
queue<Data>q;
q.push(Data(x,y,t));
dis[x][y]=0;
while(!q.empty())
{
Data item=q.front();
q.pop();
for(int i=0;i<8;i++)
{
int xx=item.x+pos[i][0];
int yy=item.y+pos[i][1];
int tt=item.t;
if(xx>n||xx<1)continue;
if(yy>m||yy<1)continue;
if(dis[xx][yy]!=-1)continue;
q.push(Data(xx,yy,tt+1));
dis[xx][yy]=tt+1;
}
}
return ;
}
int main()
{
cin>>n>>m>>x>>y;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
dis[i][j]=-1;
}
}
bfs(x,y,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout<<dis[i][j];
if(j!=m)cout<<" ";
}
cout<<endl;
}
return 0;
}
[NOIP2017 普及组] 棋盘
题目背景
NOIP2017 普及组 T3
题目描述
有一个 m × m m \times m m×m 的棋盘,棋盘上每一个格子可能是红色、黄色或没有任何颜色的。你现在要从棋盘的最左上角走到棋盘的最右下角。
任何一个时刻,你所站在的位置必须是有颜色的(不能是无色的), 你只能向上、下、左、右四个方向前进。当你从一个格子走向另一个格子时,如果两个格子的颜色相同,那你不需要花费金币;如果不同,则你需要花费 $1 $ 个金币。
另外, 你可以花费 2 2 2 个金币施展魔法让下一个无色格子暂时变为你指定的颜色。但这个魔法不能连续使用, 而且这个魔法的持续时间很短,也就是说,如果你使用了这个魔法,走到了这个暂时有颜色的格子上,你就不能继续使用魔法; 只有当你离开这个位置,走到一个本来就有颜色的格子上的时候,你才能继续使用这个魔法,而当你离开了这个位置(施展魔法使得变为有颜色的格子)时,这个格子恢复为无色。
现在你要从棋盘的最左上角,走到棋盘的最右下角,求花费的最少金币是多少?
输入格式
第一行包含两个正整数 $ m, n$,以一个空格分开,分别代表棋盘的大小,棋盘上有颜色的格子的数量。
接下来的 $ n $ 行,每行三个正整数 $ x, y, c$, 分别表示坐标为 ( x , y ) (x,y) (x,y) 的格子有颜色 $ c$。
其中 $ c=1$ 代表黄色,$ c=0$ 代表红色。 相邻两个数之间用一个空格隔开。 棋盘左上角的坐标为 ( 1 , 1 ) (1, 1) (1,1),右下角的坐标为 ( m , m ) ( m, m) (m,m)。
棋盘上其余的格子都是无色。保证棋盘的左上角,也就是 ( 1 , 1 ) (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
样例输入 #2
5 5
1 1 0
1 2 0
2 2 1
3 3 1
5 5 0
样例输出 #2
-1
提示
样例 1 说明
棋盘的颜色如下表格所示,其中空白的部分表示无色。
红 \color{red}\text{红} 红 | 红 \color{red}\text{红} 红 | |||
---|---|---|---|---|
黄 \color{yellow}\text{黄} 黄 | ||||
黄 \color{yellow}\text{黄} 黄 | 红 \color{red}\text{红} 红 | |||
黄 \color{yellow}\text{黄} 黄 | ||||
红 \color{red}\text{红} 红 |
从 ( 1 , 1 ) (1,1) (1,1) 开始,走到 ( 1 , 2 ) (1,2) (1,2) 不花费金币。
从 ( 1 , 2 ) (1,2) (1,2) 向下走到 ( 2 , 2 ) (2,2) (2,2) 花费 1 1 1 枚金币。
从 ( 2 , 2 ) (2,2) (2,2) 施展魔法,将 ( 2 , 3 ) (2,3) (2,3) 变为黄色,花费 2 2 2 枚金币。
从 ( 2 , 2 ) (2,2) (2,2) 走到 ( 2 , 3 ) (2,3) (2,3) 不花费金币。
从 ( 2 , 3 ) (2,3) (2,3) 走到 ( 3 , 3 ) (3,3) (3,3) 不花费金币。
从 ( 3 , 3 ) (3,3) (3,3) 走到 ( 3 , 4 ) (3,4) (3,4) 花费 1 1 1 枚金币。
从 ( 3 , 4 ) (3,4) (3,4) 走到 ( 4 , 4 ) (4,4) (4,4) 花费 1 1 1 枚金币。
从 ( 4 , 4 ) (4,4) (4,4) 施展魔法,将 ( 4 , 5 ) (4,5) (4,5) 变为黄色,花费 $ 2$ 枚金币。
从 ( 4 , 4 ) (4,4) (4,4) 走到 ( 4 , 5 ) (4,5) (4,5) 不花费金币。
从 ( 4 , 5 ) (4,5) (4,5) 走到 ( 5 , 5 ) (5,5) (5,5) 花费 1 1 1 枚金币。
共花费 $8 $ 枚金币。
样例 2 说明
棋盘的颜色如下表格所示,其中空白的部分表示无色。
红 \color{red}\text{红} 红 | 红 \color{red}\text{红} 红 | |||
---|---|---|---|---|
黄 \color{yellow}\text{黄} 黄 | ||||
黄 \color{yellow}\text{黄} 黄 | ||||
\color{white}\text{ } | ||||
红 \color{red}\text{红} 红 |
从 ( 1 , 1 ) ( 1, 1) (1,1) 走到 ( 1 , 2 ) ( 1, 2) (1,2),不花费金币。
从 ( 1 , 2 ) ( 1, 2) (1,2) 走到 ( 2 , 2 ) ( 2, 2) (2,2),花费 $ 1 $ 金币。
施展魔法将 ( 2 , 3 ) ( 2, 3) (2,3) 变为黄色,并从 ( 2 , 2 ) ( 2, 2) (2,2) 走到 ( 2 , 3 ) ( 2, 3) (2,3) 花费 $ 2$ 金币。
从 ( 2 , 3 ) ( 2, 3) (2,3) 走到 ( 3 , 3 ) ( 3, 3) (3,3) 不花费金币。
从 ( 3 , 3 ) ( 3, 3) (3,3) 只能施展魔法到达 ( 3 , 2 ) , ( 2 , 3 ) , ( 3 , 4 ) , ( 4 , 3 ) ( 3, 2),( 2, 3),( 3, 4),( 4, 3) (3,2),(2,3),(3,4),(4,3)。
而从以上四点均无法到达 ( 5 , 5 ) ( 5, 5) (5,5),故无法到达终点,输出 − 1 -1 −1。
数据规模与约定
对于 30 % 30\% 30% 的数据, 1 ≤ m ≤ 5 , 1 ≤ n ≤ 10 1 ≤ m ≤ 5, 1 ≤ n ≤ 10 1≤m≤5,1≤n≤10。
对于 60 % 60\% 60% 的数据, 1 ≤ m ≤ 20 , 1 ≤ n ≤ 200 1 ≤ m ≤ 20, 1 ≤ n ≤ 200 1≤m≤20,1≤n≤200。
对于 100 % 100\% 100% 的数据, 1 ≤ m ≤ 100 , 1 ≤ n ≤ 1 , 000 1 ≤ m ≤ 100, 1 ≤ n ≤ 1,000 1≤m≤100,1≤n≤1,000。
代码实现
#include<iostream>
#include<cstring>
using namespace std;
#define MAX_M 100
int arr[MAX_M+5][MAX_M+5],num[MAX_M+5][MAX_M+5];
int fx[5]={0,-1,0,1,0},fy[5]={0,0,-1,0,1};
int m,n;
void dfs(int x,int y,int c,int color)
{
if(x==m&&y==m)return ;
for(int i=1;i<=4;i++)
{
int xx=x+fx[i],yy=y+fy[i];
if(xx<1||xx>m||yy<0||yy>m)continue;
if(arr[xx][yy]==arr[x][y])
{
if(arr[xx][yy]!=0)
{
if(num[xx][yy]<=c)continue;
num[xx][yy]=c;
dfs(xx,yy,c,arr[xx][yy]);
}
}
else if(arr[xx][yy]!=arr[x][y])
{
if(arr[xx][yy]&&arr[x][y])
{
if(num[xx][yy]<=c+1)continue;
num[xx][yy]=c+1;
dfs(xx,yy,c+1,arr[xx][yy]);
}
else if(arr[xx][yy]==0)
{
if(num[xx][yy]<=c+2)continue;
num[xx][yy]=c+2;
dfs(xx,yy,c+2,color);
}
else{
if(color==arr[xx][yy])
{
if(num[xx][yy]<=c)continue;
num[xx][yy]=c;
dfs(xx,yy,c,arr[xx][yy]);
}
else{
if(num[xx][yy]<=c+1)continue;
num[xx][yy]=c+1;
dfs(xx,yy,c+1,arr[xx][yy]);
}
}
}
}
return ;
}
int main()
{
cin>>m>>n;
for(int i=0;i<=m;i++)
for(int j=0;j<=m;j++)
{
arr[i][j]=0;
num[i][j]=10000000;
}
for(int i=1,x,y,c;i<=n;i++)
{
cin>>x>>y>>c;
arr[x][y]=c+1;
}
if(m==1)
{
cout<<0;
return 0;
}
dfs(1,1,0,arr[1][1]);
if(num[m][m]==10000000)cout<<-1;
else cout<<num[m][m];
return 0;
}
[NOIP2002 提高组] 字串变换
题目背景
本题不保证存在靠谱的多项式复杂度的做法。测试数据非常的水,各种做法都可以通过,不代表算法正确。因此本题题目和数据仅供参考。
本题为搜索题,本题不接受 hack 数据。关于此类题目的详细内容
题目描述
已知有两个字串 A , B A,B A,B 及一组字串变换的规则(至多 6 6 6 个规则),形如:
- A 1 → B 1 A_1\to B_1 A1→B1。
- A 2 → B 2 A_2\to B_2 A2→B2。
规则的含义为:在 A A A 中的子串 A 1 A_1 A1 可以变换为 $ B_1 , , ,A_2$ 可以变换为 B 2 ⋯ B_2\cdots B2⋯。
例如: A = abcd A=\texttt{abcd} A=abcd, B = xyz B=\texttt{xyz} B=xyz,
变换规则为:
- abc → xu \texttt{abc}\rightarrow\texttt{xu} abc→xu, ud → y \texttt{ud}\rightarrow\texttt{y} ud→y, y → yz \texttt{y}\rightarrow\texttt{yz} y→yz。
则此时, A A A 可以经过一系列的变换变为 B B B,其变换的过程为:
- abcd → xud → xy → xyz \texttt{abcd}\rightarrow\texttt{xud}\rightarrow\texttt{xy}\rightarrow\texttt{xyz} abcd→xud→xy→xyz。
共进行了 3 3 3 次变换,使得 A A A 变换为 B B B。
输入格式
第一行有两个字符串 A , B A,B A,B。
接下来若干行,每行有两个字符串 A i , B i A_i,B_i Ai,Bi,表示一条变换规则。
输出格式
若在
10
10
10 步(包含
10
10
10 步)以内能将
A
A
A 变换为
B
B
B,则输出最少的变换步数;否则输出 NO ANSWER!
。
样例 #1
样例输入 #1
abcd xyz
abc xu
ud y
y yz
样例输出 #1
3
提示
对于 100 % 100\% 100% 数据,保证所有字符串长度的上限为 20 20 20。
【题目来源】
NOIP 2002 提高组第二题
代码实现
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define endl '\n'
struct Data1{
string from,to;
}to_from[10];
typedef struct Node{
Node(string s,int step):s(s),step(step){}
string s;
int step;
};
string st,ed;
int main()
{
map<string,int>jilu;
cin>>st>>ed;
int k=1;
while(cin>>to_from[k].from>>to_from[k].to)++k;
k--;
queue<Node>q;
q.push(Node(st,0));
while(!q.empty())
{
string now_string=q.front().s;
int now_step=q.front().step;
q.pop();
if(now_step==11)
{
cout<<"NO ANSWER!"<<endl;
return 0;
}
if(now_string==ed)
{
cout<<now_step<<endl;
return 0;
}
for(int i=1;i<=k;i++)
{
int ind=-1;
while((ind=(now_string.find(to_from[i].from,ind+1)))!=-1)
{
string after_string=now_string;
after_string.replace(ind,to_from[i].from.size(),to_from[i].to);
if(jilu[after_string]!=0)continue;
jilu[after_string]+=1;
q.push(Node(after_string,now_step+1));
}
}
}
cout<<"NO ANSWER!"<<endl;
return 0;
}