离开中山路
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define int long long
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1e6+10,M = 2000;
int n,m;
int stx,sty,edx,edy;
char va[M][M];
int vis[M][M];
int dist[M][M];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
void bfs()
{
queue<pair<int,int> > q;
vis[stx][sty] = 1;
dist[stx][sty] = 0;
q.push({stx,sty});
while(q.size()>=1)
{
pair<int,int> now = q.front();
q.pop();
int x = now.first,y = now.second;
for(int i=0;i<=3;i++)
{
int xx = x+dx[i],yy = y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&vis[xx][yy]==0&&va[xx][yy]=='0')
{
vis[xx][yy] = 1;
dist[xx][yy] = dist[x][y]+1;
q.push({xx,yy});
}
}
}
}
signed 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 PII pair<int,int >
#define int long long
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1e6+10,M = 2000;
int n,A,B;
int va[N];
int vis[N];
int dist[N];
void bfs()
{
queue<int > q;
dist[A] = 0;
vis[A] = 1;
q.push(A);
while(q.size())
{
auto now = q.front();
q.pop();
int up = now+va[now],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);
}
}
}
signed main()
{
IOS;
cin>>n>>A>>B;
for(int i=1;i<=n;i++) cin>>va[i];
bfs();
if(vis[B]==0) cout<<-1;
else cout<<dist[B];
return 0;
}
马的遍历
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define int long long
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1e6+10,M = 2000;
int n,m,A,B;
int va[N];
int vis[M][M];
int dist[M][M];
int dx[8] = {-2,-1,1,2,2,1,-1,-2};
int dy[8] = {1,2,2,1,-1,-2,-2,-1};
void bfs()
{
queue<pair<int,int> > q;
dist[A][B] = 0;
vis[A][B] = 1;
q.push({A,B});
while(q.size())
{
auto now = q.front();
q.pop();
int x = now.first,y = now.se;
for(int i=0;i<=7;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});
}
}
}
}
signed main()
{
IOS;
cin>>n>>m>>A>>B;
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]);
}
printf("\n");
}
return 0;
}
Lake Counting S
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define int long long
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1e6+10,M = 2000;
int n,m,A,B;
char va[M][M];
int dx[8] = {0,1,1,1,0,-1,-1,-1};
int dy[8] = {1,1,0,-1,-1,-1,0,1};
void bfs(int A,int B)
{
queue<pair<int,int> > q;
va[A][B] = '.';
q.push({A,B});
while(q.size())
{
auto now = q.front();
q.pop();
int x = now.first,y = now.se;
for(int i=0;i<=7;i++)
{
int xx = x+dx[i],yy = y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&va[xx][yy]=='W')
{
va[xx][yy] = '.';
q.push({xx,yy});
}
}
}
}
signed main()
{
IOS;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>va[i][j];
}
}
int cnt = 0;
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;
return 0;
}
观星
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define int long long
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1e6+10,M = 2000;
int n,m,A,B;
char va[M][M];
int dx[8] = {0,1,1,1,0,-1,-1,-1};
int dy[8] = {1,1,0,-1,-1,-1,0,1};
map<int,int > mp;
void bfs(int A,int B)
{
queue<pair<int,int> > q;
va[A][B] = '.';
q.push({A,B});
int sum = 0;
while(q.size())
{
auto now = q.front();
q.pop();
sum++;
int x = now.first,y = now.se;
for(int i=0;i<=7;i++)
{
int xx = x+dx[i],yy = y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&va[xx][yy]=='*')
{
va[xx][yy] = '.';
q.push({xx,yy});
}
}
}
mp[sum]++;
}
signed 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);
}
}
}
int maxn = 0;
for(auto t:mp) maxn = max(maxn,t.fi*t.se);
cout<<mp.size()<<" "<<maxn;
return 0;
}
填涂颜色
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define int long long
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1e6+10,M = 2000;
int n,m;
int va[M][M];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
void bfs(int A,int B)
{
queue<pair<int,int> > q;
if(A==0&&B==0) va[A][B] = -1;
else va[A][B] = 2;
q.push({A,B});
while(q.size())
{
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>=0&&xx<=n+1&&yy>=0&&yy<=n+1&&va[xx][yy]==0)
{
if(A==0&&B==0) va[xx][yy] = -1;
else va[xx][yy] = 2;
q.push({xx,yy});
}
}
}
}
signed main()
{
IOS;
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cin>>va[i][j];
}
bfs(0,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(va[i][j]==0)
{
bfs(i,j);
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(va[i][j]==-1) va[i][j] = 0;
cout<<va[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}