#include<bits/stdc++.h>
using namespace std;
const int N=1010;
char g[N][N];
bool st[N][N];
int n,m;
int cnt;
int dist[N][N];
int dx[]={0,0,-1,1,1,1,-1,-1},dy[]={-1,1,0,0,1,-1,-1,1};
void bfs(int x,int y){
queue<pair<int,int>>q;
q.push({x,y});
st[x][y]=true;
while(q.size()){
auto t=q.front();
q.pop();
for(int i=0;i<8;i++){
int xx=t.first+dx[i],yy=t.second+dy[i];
if(xx>n||xx<1||yy>m||y<1) continue;
if(st[xx][yy]||g[xx][yy]!='W') continue;
st[xx][yy]=true;
q.push({xx,yy});
}
}
}
int main(){
cin >> n >> m ;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> g[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!st[i][j]&&g[i][j]=='W'){
bfs(i,j);
cnt++;
}
}
}
cout << cnt;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=55;
int g[N][N];
bool st[N][N];
int n,m;
int ans;
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int bfs(int x,int y){
int num=1;
st[x][y]=true;
queue<pair<int,int>>q;
q.push({x,y});
while(q.size()){
auto t=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=t.first+dx[i],yy=t.second+dy[i];
if((g[t.first][t.second]>>i) & 1 ) continue;
if(st[xx][yy]) continue;
st[xx][yy]=true;
q.push({xx,yy});
num++;
}
}
return num;
}
int main(){
int cnt=0;
cin >> n >> m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> g[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!st[i][j]){
cnt=max(cnt,bfs(i,j));
ans++;
}
}
}
cout << ans << endl <<cnt;
return 0;
}
参考题解
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int g[N][N];
bool st[N][N];
int n;
bool higher,lower;
int peak;
int valley;
int dx[]={-1,1,0,0,1,1,-1,-1},dy[]={0,0,-1,1,1,-1,1,-1};
void bfs(int x,int y,bool& higher, bool& lower){
queue<pair<int,int>>q;
q.push({x,y});
while(q.size()){
auto t=q.front();
q.pop();
for(int i=0;i<8;i++){
int xx=t.first+dx[i],yy=t.second+dy[i];
if(xx>n||xx<1||yy>n||yy<1) continue;
if(g[x][y]<g[xx][yy]) higher=true;
if(g[x][y]>g[xx][yy]) lower=true;
if(g[x][y]==g[xx][yy]&&!st[xx][yy]){
q.push({xx,yy});
st[xx][yy]=true;
}
}
}
}
int main(){
cin >> n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin >> g[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(!st[i][j]){
higher=lower=false;
st[i][j]=true;
bfs(i,j,higher,lower);
if(higher==true&&lower==true){
continue;
}
if(higher==false&&lower==false){
peak++,valley++;
continue;
}
if(higher==true){
valley++;
continue;
}
if(lower==true){
peak++;
}
}
}
}
cout << peak << " " << valley;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int g[N][N];
bool st[N][N];
pair<int,int> pre[N][N];
int dist[N][N];
int dx[]={0,0,-1,1},dy[]={1,-1,0,0};
int n;
void bfs(int x,int y){
st[x][y]=true;
queue<pair<int,int>>q;
q.push({x,y});
while(q.size()){
auto t=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=t.first+dx[i],yy=t.second+dy[i];
if(xx>n||xx<1||yy>n||yy<1||st[xx][yy]||g[xx][yy]==1) continue;
dist[xx][yy]=dist[t.first][t.second]+1;
st[xx][yy]=true;
pre[xx][yy]={t.first,t.second};
q.push({xx,yy});
}
}
}
int main(){
cin >> n ;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin >> g[i][j];
}
}
bfs(n,n);
auto t=pre[1][1];
cout << 0 << " " << 0 <<endl;
while(1){
cout << t.first-1 << " " << t.second-1 << endl;
if(t.first==n&&t.second==n){
break;
}
t=pre[t.first][t.second];
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
bool st[N*2];
int dist[N*2];
int sta,ed;
void bfs(){
queue<int>q;
q.push(sta);
st[sta]=true;
while(q.size()){
auto t=q.front();
q.pop();
if(t==ed){
break;
}
if(t-1>=0&&!st[t-1]){
st[t-1]=true;
q.push(t-1);
dist[t-1]=dist[t]+1;
}
if(t+1<=ed&&!st[t+1]){
st[t+1]=true;
q.push(t+1);
dist[t+1]=dist[t]+1;
}
if(t*2<=2*ed&&!st[t*2]){
st[t*2]=true;
q.push(t*2);
dist[t*2]=dist[t]+1;
}
}
}
int main(){
cin >> sta >> ed;
bfs();
cout << dist[ed];
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=155;
char g[N][N];
bool st[N][N];
int dist[N][N];
int n,m;
int stx,sty;
int endx,endy;
int ans=0;
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
void bfs(){
queue<pair<int,int>>q;
q.push({stx,sty});
st[stx][sty]=true;
while(q.size()){
auto t=q.front();
if(g[t.first][t.second]=='H') return;
q.pop();
for(int i=0;i<8;i++){
int x=t.first+dx[i],y=t.second+dy[i];
if(st[x][y]||g[x][y]=='*') continue;
if(x<1||x>n||y<1||y>m) continue;
st[x][y]=true;
dist[x][y]=dist[t.first][t.second]+1;
q.push({x,y});
}
}
}
int main(){
cin >> m >> n;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> g[i][j];
if(g[i][j]=='K'){
stx=i;
sty=j;
}
if(g[i][j]=='H'){
endx=i;
endy=j;
}
}
}
bfs();
cout <<dist[endx][endy];
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
char g[N][N];
bool st[N][N];
int n,m;
int dist[N][N];
queue<pair<int,int>>q;
int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
void bfs(){
while(q.size()){
auto t=q.front();
q.pop();
for(int i=0;i<4;i++){
int x=t.first+dx[i],y=t.second+dy[i];
if(x>n||x<1||y>m||y<1) continue;
if(st[x][y]) continue;
dist[x][y]=dist[t.first][t.second]+1;
st[x][y]=true;
q.push({x,y});
}
}
}
int main(){
cin >> n >> m;
for(int i=1;i<=n;i++){
string a;
cin >> a;
for(int j=0;j<a.size();j++){
if(a[j]=='1'){
q.push({i,j+1});
st[i][j+1]=true;
}
}
}
bfs();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout << dist[i][j] << " ";
}
cout << endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
unordered_map<string,int> dist;
unordered_map<string,pair<char,string>>pre;
string st="",ed="";
string move1(string t){
for(int i=0;i<4;i++) swap(t[i],t[7-i]);
return t;
}
string move2(string t){
for(int i=0;i<3;i++) swap(t[3],t[i]);
for(int i=4;i<7;i++) swap(t[i],t[i+1]);
return t;
}
string move3(string t){
swap(t[1],t[2]),swap(t[5],t[6]),swap(t[1],t[5]);
return t;
}
void bfs(){
queue<string>q;
q.push(st);
while(q.size()){
auto t=q.front();
q.pop();
if(t==ed){
break;
}
string m[3];
m[0]=move1(t);
m[1]=move2(t);
m[2]=move3(t);
for(int i=0;i<3;i++){
if(!dist.count(m[i])){
q.push(m[i]);
dist[m[i]]=dist[t]+1;
pre[m[i]]={'A'+i,t};
}
}
}
}
int main(){
for(int i=1;i<=8;i++){
char b=(i+'0');
st=st+b;
char a;
cin >> a;
ed=ed+a;
}
bfs();
cout << dist[ed] << endl;
string res="";
if(dist[ed])
{
while(ed!=st)
{
res+=pre[ed].first;
ed=pre[ed].second;
}
reverse(res.begin(),res.end());
cout<< res <<endl;
}
return 0;
}