池塘计数
题目:池塘计数
方法一:BFS
#include <bits/stdc++.h>
using namespace std;
const int N=1010,M=N*N;
#define x first
#define y second
typedef pair<int,int> PII;
char g[N][N];
int n,m;
PII q[M];
bool st[N][N];
void bfs(int sx,int sy)
{
int hh=0,tt=0;
q[0]={sx,sy};
st[sx][sy]=true;
while(hh<=tt)
{
PII t=q[hh++];
for(int i=t.x-1;i<=t.x+1;i++)
for(int j=t.y-1;j<=t.y+1;j++)
{
if(i==t.x&&j==t.y) continue;
if(i<0||i>=n||j<0||j>=m) continue;
if(g[i][j]=='.'||st[i][j]) continue;
q[++tt]={i,j};
st[i][j]=true;
}
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>g[i][j];
int res=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(g[i][j]=='W'&&!st[i][j])
{
bfs(i,j);
res++;
}
cout<<res;
}
方法二:DFS
#include <bits/stdc++.h>
using namespace std;
const int N=1009;
int n,m;
char a[N][N];
void dfs(int x,int y)
{
int dx[8]={1,1,1,0,0,-1,-1,-1};
int dy[8]={-1,0,1,-1,1,-1,0,1};
a[x][y]='.';
for(int i=0;i<8;i++)
{
if(a[x+dx[i]][y+dy[i]]=='W')
{
dfs(x+dx[i],y+dy[i]);
}
}
}
int main()
{
int ans=0;
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]=='W')
{
dfs(i,j);
ans++;
}
}
}
cout<<ans;
}
城堡问题
题目:城堡问题
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
const int N=50;
int g[N][N];
int st[N][N];
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int n,m;
int bfs(int i,int j)
{
queue<PII> q;
q.push({i,j});
st[i][j]=true;
int cnt=0;
while(!q.empty())
{
PII t=q.front();
q.pop();
cnt++;
for(int k=0;k<4;k++)
{
if(g[t.first][t.second]>>k&1) continue;
int x=t.first+dx[k];
int y=t.second+dy[k];
if(st[x][y]) continue;
q.push({x,y});
st[x][y]=true;
}
}
return cnt;
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>g[i][j];
int ans=0,maxx=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(!st[i][j])
{
ans++;
maxx=max(maxx,bfs(i,j));
}
cout<<ans<<endl;
cout<<maxx<<endl;
}
山峰和山谷
题目:山峰和山谷
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
const int N=1020;
typedef pair<int,int> PII;
int g[N][N];
bool st[N][N];
PII q[N*N];
int n;
void bfs(int x,int y,bool& hig,bool& dow)
{
int hh=0,tt=0;
q[0]={x,y};
st[x][y]=true;
while(hh<=tt)
{
PII t=q[hh++];
for(int i=t.x-1;i<=t.x+1;i++)
for(int j=t.y-1;j<=t.y+1;j++)
{
if(i<0||i>=n||j<0||j>=n) continue;
if(g[i][j]>g[t.x][t.y])
hig=true;
else if(g[i][j]<g[t.x][t.y])
dow=true;
else if(!st[i][j])
{
q[++tt]={i,j};
st[i][j]=true;
}
}
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&g[i][j]);
int h=0,d=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(!st[i][j])
{ bool hig=false,dow=false;
bfs(i,j,hig,dow);
if(!hig)
h++;
if(!dow)
d++;
}
}
printf("%d %d\n",h,d);
}
迷宫问题
题目:迷宫问题
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
const int N=1000;
PII pre[N][N];
int g[N][N];
bool st[N][N];
int n;
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
void bfs()
{
queue<PII> q;
q.push({n-1,n-1});
st[n-1][n-1]=true;
while(q.size())
{
PII t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int x=t.x+dx[i];
int y=t.y+dy[i];
if(x<0||x>=n||y<0||y>=n) continue;
if(g[x][y]||st[x][y]) continue;
st[x][y]=true;
q.push({x,y});
pre[x][y]=t;
}
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>g[i][j];
bfs();
PII end(0,0);
while(true)
{
cout<<end.x<<' '<<end.y<<endl;
if(end.x==n-1&&end.y==n-1)
break;
end=pre[end.x][end.y];
}
}