xuanhongye @ 2024-10-30 22:27:39
谁能帮我看看在哪里错了 我看样例全对 可测试点一个没过
#include <bits/stdc++.h>
using namespace std;
char a[1005][1005];
bool vis[1005][1005];
int ans,n,m,k;
void dfs(int x,int y,int d,int k){
if (vis[y][x] == 0){
vis[y][x] = 1;
ans++;
}
if (k == 0) return ;
if (d == 0){
if (x + 1 > m || a[y][x + 1] == 'x') {dfs(x,y,(d + 1)%4,k - 1);}
else{
dfs(x + 1,y,d,k - 1);
}
}
if (d == 1){
if (y + 1 > n || a[y + 1][x] == 'x') {dfs(x,y,(d + 1)%4,k - 1);}
else{
dfs(x,y + 1,d,k - 1);
}
}
if (d == 2){
if (x - 1 <= 0 || a[y][x - 1] == 'x') {dfs(x,y,(d + 1)%4,k - 1);}
else {
dfs(x - 1,y,d,k - 1);
}
}
if (d == 3){
if (y - 1 <= 0 || a[y - 1][x] == 'x') {dfs(x,y,(d + 1)%4,k - 1);}
else {
dfs(x,y - 1,d,k - 1);
}
}
}
int main(){
int t;
cin >> t;
while(t--){
memset(vis,0,sizeof(vis));
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++){
cin >> a[i][j];
}
}
dfs(x,y,d,k);
cout << ans << '\n';
ans = 0;
}
return 0;
}
by dpvis2025liu @ 2024-10-31 16:58:45
嘿嘿和我在考场上一样的错误,不能DFS,直接逆天模拟就行。```
using namespace std;
bool vis[1010][1010]; char s[1010][1010]; int T,n,m,k,x,y,d,ans; signed main(){ cin>>T; while(T--){ cin>>n>>m>>k>>x>>y>>d; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>s[i][j]; vis[i][j]=0; } } ans=1; vis[x][y]=1; for(int i=1;i<=k;i++){ if(d==0){ if(x>=1&&x<=n&&y+1>=1&&y+1<=m&&s[x][y+1]=='.'){ y+=1; if(vis[x][y]==0){ vis[x][y]=1; ans++; } } else d=(d+1)%4; } else if(d==1){ if(x+1>=1&&x+1<=n&&y>=1&&y<=m&&s[x+1][y]=='.'){ x+=1; if(vis[x][y]==0){ vis[x][y]=1; ans++; } } else d=(d+1)%4; } else if(d==2){ if(x>=1&&x<=n&&y-1>=1&&y-1<=m&&s[x][y-1]=='.'){ y-=1; if(vis[x][y]==0){ vis[x][y]=1; ans++; } } else d=(d+1)%4; } else if(d==3){ if(x-1>=1&&x-1<=n&&y>=1&&y<=m&&s[x-1][y]=='.'){ x-=1; if(vis[x][y]==0){ vis[x][y]=1; ans++; } } else d=(d+1)%4; } } cout<<ans<<"\n"; } return 0; }
by mayisang @ 2024-10-31 22:31:28
这题不是dfs题,看清题目再问。你那个d对应的x,y好像写得也不太对