离开中山路
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e3+5;
int stx,sty,edx,edy;
int dx[N] = {0,0,1,-1};
int dy[N] = {1,-1,0,0};
int dist[N][N];
bool vis[N][N];
int n;
char va[N][N];
void bfs()
{
queue<pair<int,int> > q;
vis[stx][sty] = 1;
dist[stx][sty] = 0;
q.push({stx,sty});
while(q.size()>=1)
{
auto now = q.front();
q.pop();
int x = now.fi,y = now.se;
for(int i =0;i<=3;i++)
{
int xx = x+dx[i],yy = y+dy[i];
if(xx>=1&&xx<=n&&y>=1&&y<=n&&vis[xx][yy]==0&&va[xx][yy]=='0')
{
vis[xx][yy]= 1;
dist[xx][yy] = dist[x][y]+1;
q.push({xx,yy});
}
}
}
}
int main()
{
IOS;
cin>>n;
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=n;j++)
{
cin>>va[i][j];
}
}
cin>>stx>>sty>>edx>>edy;
bfs();
cout<<dist[edx][edy];
return 0;
}
奇怪的电梯
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e3+5;
int n,sta,edb;
int va[N];
int dist[N];
int vis[N];
void bfs()
{
queue<int> q;
dist[sta] = 0;
vis[sta] = 1;
q.push(sta);
while(q.size()>=1)
{
auto now = q.front();
q.pop();
int up = now+va[now];
int down = now-va[now];
if(up>=1&&up<=n&&vis[up] ==0)
{
vis[up] = 1;
dist[up] = dist[now]+1;
q.push(up);
}
if(down>=1&&down<=n&&vis[down] ==0)
{
vis[down] = 1;
dist[down] = dist[now]+1;
q.push(down);
}
}
}
int main()
{
IOS;
cin>>n>>sta>>edb;
for(int i = 1;i<=n;i++) cin>>va[i];
bfs();
if(vis[edb] !=0)cout<<dist[edb];
else cout<<-1;
}
马的遍历
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e3+5;
int n,m,stx,sty;
bool vis[N][N];
int dist[N][N];
int dx[N] ={-2,-1,1,2,-2,-1,1,2};
int dy[N] ={1,2,2,1,-1,-2,-2,-1};
void bfs()
{
queue<pair<int,int> > q;
vis[stx][sty] = 1;
dist[stx][sty] = 0;
q.push({stx,sty});
while(q.size())
{
auto now = q.front();
int x = now.fi,y = now.se;
q.pop();
for(int i=0;i<8;i++)
{
int xx = x+dx[i],yy=y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy] == 0)
{
vis[xx][yy] = 1;
dist[xx][yy] = dist[x][y]+1;
q.push({xx,yy});
}
}
}
}
int main()
{
cin>>n>>m>>stx>>sty;
bfs();
for(int i = 1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(vis[i][j]==0) printf("%-5d",-1);
else printf("%-5d",dist[i][j]);
}
cout<<"\n";
}
}
Lake Counting S数水坑
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e4+5;
int n,m;
int cnt = 0;
char va[N][N];
int dx[N] ={0,0,1,-1,-1,1,1,-1};
int dy[N] ={1,-1,1,-1,1,-1,0,0};
void bfs(int a,int b)
{
queue< pair<int,int> > q;
q.push({a,b});
while(q.size())
{
auto now = q.front();
int x = now.fi,y = now.se;
q.pop();
for(int k = 0;k<=7;k++)
{
int xx = x+dx[k],yy=y+dy[k];
if(va[xx][yy] =='W')
{
va[xx][yy]='.';
q.push({xx,yy});
}
}
}
}
int main()
{
IOS;
cin>>n>>m;
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=m;j++)
{
cin>>va[i][j];
}
}
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=m;j++)
{
if(va[i][j]=='W')
{
cnt++;
bfs(i,j);
}
}
}
cout<<cnt;
}
观星
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1505;
int n,m;
int maxn = 0;
map<int,int> mp;
bool vis[N];
char va[N][N];
int dx[N] ={0,0,1,-1,-1,1,1,-1};
int dy[N] ={1,-1,1,-1,1,-1,0,0};
void bfs(int a,int b)
{
queue< pair<int,int> > q;
q.push({a,b});
va[a][b] = '.';
int sum = 0;
while(q.size())
{
auto now = q.front();
int x = now.fi,y = now.se;
q.pop();
sum++;
for(int k = 0;k<=7;k++)
{
int xx = x+dx[k],yy=y+dy[k];
if(va[xx][yy] =='*')
{
va[xx][yy]='.';
q.push({xx,yy});
}
}
}
mp[sum]++;
}
int main()
{
IOS;
cin>>n>>m;
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=m;j++)
{
cin>>va[i][j];
}
}
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=m;j++)
{
if(va[i][j]=='*')
{
bfs(i,j);
}
}
}
for(auto t:mp) maxn = max(maxn,t.se*t.fi);
cout<<mp.size()<<" "<<maxn;
}
填涂颜色
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e3+5;
int va[N][N];
bool vis[N][N];
int n;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0} ;
void bfs()
{
queue<pair<int,int> > q;
vis[0][0] = 1;
q.push({0,0});
while(q.size())
{
auto now = q.front();
int x = now.fi,y = now.se;
q.pop();
for(int i = 0;i<=3;i++)
{
int xx = x+dx[i],yy = y+dy[i];
if(xx>=0&&xx<=n+1&&yy>=0&&yy<=n+1&&vis[xx][yy]==0&&va[xx][yy]==0)
{
vis[xx][yy]=1;
q.push({xx,yy});
}
}
}
}
int main()
{
IOS;
cin>>n;
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=n;j++)
{
cin>>va[i][j];
}
}
bfs();
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=n;j++)
{
if(va[i][j]==0&&vis[i][j]==0)
{
cout<<2<<" ";
}
else cout<<va[i][j]<<' ';
}
cout<<"\n";
}
}