QAQ_liu @ 2024-08-21 16:23:10
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,sum,d[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
char a[1005][1005];
void dfs(int x,int y,int s){
int nx,ny;
char h;
if(x==n&&y==m) sum=max(sum,s);
if(a[x][y]=='1') h='1';
else h='0';
for(int i=0;i<4;i++){
nx=x+d[i][0];
ny=y+d[i][1];
if(nx<1||ny<1||nx>n||ny>n) continue;
if(a[nx][ny]=='1'&&h!=a[nx][ny]) dfs(nx,ny,s+1);
else continue;
}
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) cin>>a[i][j];
}
while(m--){
sum=0;
int x,y;
cin>>x>>y;
dfs(x,y,0);
cout<<sum<<"\n";
}
return 0;
}
by xionghao2013 @ 2024-08-21 16:56:02
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,sum,d[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
char a[1005][1005];
bool vis[1005][1005];
void dfs(int x,int y,int s){
if (vis[x][y]) return;
vis[x][y]=1;
int nx,ny;
char h;
sum=max(sum,s);
if(a[x][y]=='1') h='1';
else h='0';
for(int i=0;i<4;i++){
nx=x+d[i][0];
ny=y+d[i][1];
if(nx<1||ny<1||nx>n||ny>n) continue;
if(h!=a[nx][ny]) dfs(nx,ny,s+1);
else continue;
}
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) cin>>a[i][j];
}
while(m--){
memset(vis,0,sizeof(vis));
sum=0;
int x,y;
cin>>x>>y;
dfs(x,y,1);
cout<<sum<<"\n";
}
return 0;
}
改好之后的代码
by xionghao2013 @ 2024-08-21 16:58:03
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,sum,d[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
char a[1005][1005];
bool vis[1005][1005];
void dfs(int x,int y,int s){
if (vis[x][y]) return;
vis[x][y]=1;
int nx,ny;
char h;
sum=max(sum,s);
if(a[x][y]=='1') h='1';
else h='0';
for(int i=0;i<4;i++){
nx=x+d[i][0];
ny=y+d[i][1];
if(nx<1||ny<1||nx>n||ny>n) continue;
if(h!=a[nx][ny]) dfs(nx,ny,s+1);
else continue;
}
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) cin>>a[i][j];
}
while(m--){
memset(vis,0,sizeof(vis));
sum=0;
int x,y;
cin>>x>>y;
dfs(x,y,1);
cout<<sum<<"\n";
}
return 0;
}
by xionghao2013 @ 2024-08-21 17:00:45
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,sum,d[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
char a[1005][1005];
bool vis[1005][1005];
void dfs(int x,int y,int s){
if (vis[x][y]) return;
vis[x][y]=1;
int nx,ny;
char h;
sum=max(sum,s);
if(a[x][y]=='1') h='1';
else h='0';
for(int i=0;i<4;i++){
nx=x+d[i][0];
ny=y+d[i][1];
if(nx<1||ny<1||nx>n||ny>n) continue;
if(h!=a[nx][ny]) dfs(nx,ny,s+1);
else continue;
}
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) cin>>a[i][j];
}
while(m--){
memset(vis,0,sizeof(vis));
sum=0;
int x,y;
cin>>x>>y;
dfs(x,y,1);
cout<<sum<<"\n";
}
return 0;
}
by xionghao2013 @ 2024-08-21 17:02:05
后面不要at我了,我后面有事(一直到7:30)
by QAQ_liu @ 2024-10-02 20:41:46
@xionghao2013 也是错的0分
by xionghao2013 @ 2024-10-03 10:45:27
https://www.luogu.com.cn/record/179609484
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int a[1005][1005],vis[1005][1005],ans[1005][1005],s[1000005];
int dx[10]={0,-1,0,1,0};
int dy[10]={0,0,-1,0,1};
int n;
void dfs(int nx,int ny,int col)
{
ans[nx][ny]=col;
s[col]++;
vis[nx][ny]=true;
for (int i=1;i<=4;i++)
{
int tx,ty;
tx=nx+dx[i];
ty=ny+dy[i];
if (tx<=n&&ty<=n&&tx>=1&&ty>=1&&!vis[tx][ty]&&a[tx][ty]!=a[nx][ny])
{
dfs(tx,ty,col);
}
}
}
int main(){
int m;
cin>>n>>m;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
char c;
cin>>c;
a[i][j]=c-'0';
}
}
int cur=0;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if (!vis[i][j])
{
dfs(i,j,++cur);
}
}
}
for (int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
cout<<s[ans[x][y]]<<endl;
}
return 0;
}