NFOT @ 2024-10-29 14:01:32
#include<bits/stdc++.h>
using namespace std;
int n,m,k,res=0;
int tx[5]={0,1,0,-1,0},ty[5]={1,0,-1,0,0};
bool gt[1100][1100];
bool mp[1100][1100];
void DFS(int x,int y,int d,int k){
//cout<<x<<' '<<y<<' '<<d<<' '<<k<<endl;
if(k==0) return;
int dx=x+tx[d];
int dy=y+ty[d];
//cout<<dx<<' '<<dy<<endl;
if(dx>=1&&dy>=1&&dx<=n&&dy<=m&&mp[dx][dy]==1){
if(gt[dx][dy]==0) gt[dx][dy]=1,res++;
DFS(dx,dy,d,k-1);
}else{
DFS(x,y,(d+1)%4,k-1);
}
return;
}
int main(){
int T;cin>>T;
for(int ttt=1;ttt<=T;ttt++){
cin>>n>>m>>k;
int x,y,d;cin>>x>>y>>d;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char c;cin>>c;
if(c=='x') mp[i][j]=0;
else mp[i][j]=1;
}
}
DFS(x,y,d,k);
cout<<res+1<<endl;
memset(gt,0,sizeof(gt));
res=0;
}
return 0;
}
by Ff472130 @ 2024-10-29 14:35:56
没有标记起点导致的
代码
#include<bits/stdc++.h>
using namespace std;
int n,m,k,res=0;
int tx[5]={0,1,0,-1,0},ty[5]={1,0,-1,0,0};
bool gt[1100][1100];
bool mp[1100][1100];
void DFS(int x,int y,int d,int k){
//cout<<x<<' '<<y<<' '<<d<<' '<<k<<endl;
if(k==0) return;
int dx=x+tx[d];
int dy=y+ty[d];
//cout<<dx<<' '<<dy<<endl;
if(dx>=1&&dy>=1&&dx<=n&&dy<=m&&mp[dx][dy]==1){
if(gt[dx][dy]==0) gt[dx][dy]=1,res++;
DFS(dx,dy,d,k-1);
}else{
DFS(x,y,(d+1)%4,k-1);
}
return;
}
int main(){
int T;cin>>T;
for(int ttt=1;ttt<=T;ttt++){
cin>>n>>m>>k;
int x,y,d;cin>>x>>y>>d;
gt[x][y]=1;//标记起点
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char c;cin>>c;
if(c=='x') mp[i][j]=0;
else mp[i][j]=1;
}
}
DFS(x,y,d,k);
cout<<res+1<<endl;
memset(gt,0,sizeof(gt));
res=0;
}
return 0;
}
求关
by NFOT @ 2024-10-30 13:26:51
@Ff472130 谢谢好人