P1135 奇怪的电梯
#include<bits/stdc++.h>
using namespace std;
const int N = 700;
const int INF = 0x3f3f3f3f;
int dir[10] = {1,-1};
int n,a,b,ans = 1000000,flag = 0;
int Floor[N];
struct node{
int x;
int step;
};
int check(node tmp){
if(tmp.x < 1 || tmp.x > n)return 0;
return 1;
}
void BFS(int x){
//int mul = 1,sum = 0;
bool vis[N];
vis[x] = true;
queue<node>q;
node cur,tmp;
cur.x = x;
cur.step = 0;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
for(int i = 0;i < 2;i++){
tmp.x = cur.x + dir[i] * Floor[cur.x];
if(check(tmp) && vis[tmp.x] == false){
tmp.step = cur.step + 1;
q.push(tmp);
vis[tmp.x] = true;
//cout<<tmp.step<<" ";
if(tmp.x == b){
flag = 1;
ans = min(ans,tmp.step);
}
}
}
}
}
int main(){
cin>>n>>a>>b;
for(int i = 1;i <= n;i++)cin>>Floor[i];
BFS(a);
if(a == b)cout<<"0"<<endl;
else if(!flag)cout<<"-1"<<endl;
else cout<<ans<<endl;
return 0;
}
P1141 01迷宫
#include<bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof(a))
#define ll long long
//这题记忆化BFS-AC
//其他做法: DFS tarjan-强连通分量
using namespace std;
const int N = 1010;
int res[N][N];
char ans[N][N];
bool vis[N][N];
int n,m,X,Y;
int dir[][2] = {{0,1},{1,0},{0,-1},{-1,0}};
struct node{
int x;
int y;
};
queue<node>q,p;
int is_valid(node tmp){
if(tmp.x < 1 || tmp.x > n)return 0;
if(tmp.y < 1 || tmp.y > n)return 0;
return 1;
}
void BFS(int x,int y){
int cnt = 1;
vis[x][y] = true;
node cur,tmp;
cur.x = x;
cur.y = y;
q.push(cur);
p.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
for(int i = 0;i < 4;i++){
tmp.x = cur.x + dir[i][0];
tmp.y = cur.y + dir[i][1];
if(ans[cur.x][cur.y] == '1')if(ans[tmp.x][tmp.y] == '1')continue;
if(ans[cur.x][cur.y] == '0')if(ans[tmp.x][tmp.y] == '0')continue;
if(is_valid(tmp) && !vis[tmp.x][tmp.y]){
q.push(tmp);
p.push(tmp);
vis[tmp.x][tmp.y] = true;
cnt++;
}
}
}
while(!p.empty()){
cur = p.front();
p.pop();
res[cur.x][cur.y] = cnt;
}
}
int main(){
cin>>n>>m;
for(int i = 1;i <= n;i++)for(int j = 1;j <= n;j++)cin>>ans[i][j];
for(int i = 1;i <= m;i++){
cin>>X>>Y;
if(res[X][Y] == 0)BFS(X,Y);//学的教训,vis数组该初始化的时候不初始化 不该的时候手贱....
cout<<res[X][Y]<<endl;
}
return 0;
}
P1162 填涂颜色
#include<bits/stdc++.h>
using namespace std;
const int N = 200;
//一定要注意把 N 开的大一点 血的教训 起码3h浪费这上面
//方法1 找到圈圈里面的一个点 然后就可以染色圈内所有点 AC
//方法2 先全部变成2 然后搜索外围变成0 前提是要多围出一圈0 方便搜索
struct Node{
int x;
int y;
};
int dir[][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int n,x,y,flag = 1;
int table[N][N];
int check(Node tmp){
if(tmp.x < 1 || tmp.x > n)return 0;
if(tmp.y < 1 || tmp.y > n)return 0;
return 1;
}
void BFS(int x,int y){
//int mul = 1,sum = 0;
bool vis[N][N];
table[x][y] = 2;
vis[x][y] = true;
queue<Node>q;
Node cur,tmp;
cur.x = x;
cur.y = y;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
for(int i = 0;i < 4;i++){
tmp.x = cur.x + dir[i][0];
tmp.y = cur.y + dir[i][1];
if(table[tmp.x][tmp.y] == 1)continue;
if(check(tmp) && vis[tmp.x][tmp.y] == false){
q.push(tmp);
vis[tmp.x][tmp.y] = true;
table[tmp.x][tmp.y] = 2;
}
}
}
}
int main(){
cin>>n;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
cin>>table[i][j];
if(i > 1 && j > 1 && table[i - 1][j] == 1 && table[i][j - 1] == 1 && table[i][j] == 0 && flag){
x = i;
y = j;
flag = 0;
}
}
}
BFS(x,y);
cout<<x<<y<<endl;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++)cout<<table[i][j]<<" ";
cout<<endl;
}
//cout<<table[1][2];
return 0;
}
P1331 海战
#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
const int N = 1500;
char ans[N][N];
bool vis[N][N];
int n,m;
int cnt;
int dir[][2] = {{0,1},{1,0},{0,-1},{-1,0}};
struct node{
int x;
int y;
};
int is_valid(node tmp){
if(tmp.x < 1 || tmp.x > n)return 0;
if(tmp.y < 1 || tmp.y > m)return 0;
return 1;
}
int check(int x,int y,int xx,int yy){
for(int i = x;i <= xx;i++)for(int j = y;j <= yy;j++)if(ans[i][j] == '.')return 0;
return 1;
}
int BFS(int x,int y){
vis[x][y] = true;
int fx,fy,sx,sy;
fx = x,fy = y;
sx = x,sy = y;
queue<node>q;
node cur,tmp;
cur.x = x;
cur.y = y;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
for(int i = 0;i < 4;i++){
tmp.x = cur.x + dir[i][0];
tmp.y = cur.y + dir[i][1];
if(ans[tmp.x][tmp.y] == '.')continue;
fx = min(fx,tmp.x);
sx = max(sx,tmp.x);
fy = min(fy,tmp.y);
sy = max(sy,tmp.y);
if(is_valid(tmp) && !vis[tmp.x][tmp.y]){
q.push(tmp);
vis[tmp.x][tmp.y] = true;
}
}
}
cnt++;
if(!check(fx,fy,sx,sy)){
cout<<"Bad placement."<<endl;
return 0;
}
return 1;
}
int main(){
FAST;
cin>>n>>m;
for(int i = 1;i <= n;i++)for(int j = 1;j <= m;j++)cin>>ans[i][j];
for(int i = 1;i <= n;i++)for(int j = 1;j <= m;j++)if(ans[i][j] == '#' && !vis[i][j])if(!BFS(i,j))return 0;
cout<<"There are "<<cnt<<" ships."<<endl;
return 0;
}
P1332 血色先锋队
#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a))
#define MP make_pair
#define rush() int T;cin>>T;while(T--)
#define ll long long
using namespace std;
const int N = 1e5 + 100;
int ans[510][510],res[510][510];
bool vis[510][510];
int n,m,a,b;
int dir[][2] = {{0,1},{1,0},{0,-1},{-1,0}};
inline int readint()//快读
{
int res = 0;
char c = 0;
while(!isdigit(c))
c = getchar();
while(isdigit(c))
res = res * 10 + c - '0', c = getchar();
return res;
}
struct node{
int x;
int y;
int step;
};
struct ank{
int x,y;
}source[N],lord[N];
queue<node>q;
int is_valid(node tmp){
if(tmp.x < 1 || tmp.x > n)return 0;
if(tmp.y < 1 || tmp.y > n)return 0;
return 1;
}
void BFS(int x,int y){
ms(vis,false);
vis[x][y] = true;
node cur,tmp;
cur.x = x;
cur.y = y;
cur.step = 0;
q.push(cur);
if(res[cur.x][cur.y] == 1)res[cur.x][cur.y] = 2;
while(!q.empty()){
cur = q.front();
q.pop();
for(int i = 0;i < 4;i++){
tmp.x = cur.x + dir[i][0];
tmp.y = cur.y + dir[i][1];
if(is_valid(tmp) && !vis[tmp.x][tmp.y]){
tmp.step = cur.step + 1;
if(ans[tmp.x][tmp.y] == 0)ans[tmp.x][tmp.y] = tmp.step;
else ans[tmp.x][tmp.y] = min(tmp.step,ans[tmp.x][tmp.y]);
q.push(tmp);
vis[tmp.x][tmp.y] = true;
}
}
}
}
int main(){
cin>>n>>m>>a>>b;
for(int i = 1;i <= a;i++){
source[i].x = readint();
source[i].y = readint();
}//cin>>>>source[i].y;
for(int i = 1;i <= b;i++){
lord[i].x = readint();
lord[i].y = readint();
//cin>>lord[i].x>>lord[i].y;
res[lord[i].x][lord[i].y] = 1;
}
for(int i = 1;i <= a;i++)BFS(source[i].x,source[i].y);
for(int i = 1;i <= b;i++){
if(res[lord[i].x][lord[i].y] == 2)cout<<"0"<<endl;
else cout<<ans[lord[i].x][lord[i].y]<<endl;
}
return 0;
}
P1443 马的遍历(第一个真正意义上的BFS)
注意点:方向数组初始化不能用小括号!!!
#include<bits/stdc++.h>
using namespace std;
const int N = 500;
int ans[N][N];
int n,m,x,y;
int dir[][2] = {{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};//奇怪!!!!!
//为什么一定要用中括号括起来
struct node{
int x;
int y;
};
int is_valid(node tmp){
if(tmp.x < 1 || tmp.x > n)return 0;
if(tmp.y < 1 || tmp.y > m)return 0;
return 1;
}
void BFS(int x,int y,int step){
bool vis[N][N];
ans[x][y] = step;
vis[x][y] = true;
queue<node>q;
node cur,tmp;
cur.x = x;
cur.y = y;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
for(int i = 0;i < 8;i++){
tmp.x = cur.x + dir[i][0];
tmp.y = cur.y + dir[i][1];
if(is_valid(tmp) && !vis[tmp.x][tmp.y]){
q.push(tmp);
vis[tmp.x][tmp.y] = true;
ans[tmp.x][tmp.y] = ans[cur.x][cur.y] + 1;
}
}
}
}
int main(){
memset(ans,-1,sizeof(ans));
cin>>n>>m>>x>>y;
BFS(x,y,0);
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++)printf("%-5d",ans[i][j]);
cout<<endl;
}
return 0;
}
P1506 拯救oibh总部
#include<bits/stdc++.h>
using namespace std;
const int N = 1000;
//提前读取 * 的个数 在最外围建立额外 0 圈 方便BFS
struct Node{
int x;
int y;
};
int dir[][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int n,m,cnt = 0,num_star = 0,ans;
int x,y;
char table[N][N];
int check(Node tmp){
if(tmp.x < 0 || tmp.x > n + 1)return 0;
if(tmp.y < 0 || tmp.y > m + 1)return 0;
return 1;
}
void BFS(int x,int y){
//int mul = 1,sum = 0;
bool vis[N][N];
cnt++;
vis[x][y] = true;
queue<Node>q;
Node cur,tmp;
cur.x = x;
cur.y = y;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
for(int i = 0;i < 4;i++){
tmp.x = cur.x + dir[i][0];
tmp.y = cur.y + dir[i][1];
if(table[tmp.x][tmp.y] == '*')continue;
if(check(tmp) && vis[tmp.x][tmp.y] == false){
q.push(tmp);
vis[tmp.x][tmp.y] = true;
cnt++;
}
}
}
}
int main(){
cin>>n>>m;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++){
cin>>table[i][j];
if(table[i][j] == '*')num_star++;
}
BFS(0,0);
ans = (n + 2) * (m + 2) - num_star - cnt;
cout<<ans<<endl;
return 0;
}