只有10分,4个超时,5个错误。路过的大神帮忙看一下

P1189 SEARCH

```c #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=60; char arr[maxn][maxn]; int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1} }; int vis[maxn][maxn][1010]; int arr2[1010]; int n,m,k; bool check(int x,int y){ if(x>=1&&x<=n&&y>=1&&y<=m&&arr[x][y]!='X')return true; return false; } void dfs(int x,int y,int index){ if(vis[x][y][index])return ; if(index==k+1){ arr[x][y]='*'; return ; } vis[x][y][index]=1; int newx=x+dir[arr2[index]][0]; int newy=y+dir[arr2[index]][1]; while(check(newx,newy)){ dfs(newx,newy,index+1); newx+=dir[arr2[index]][0]; newy+=dir[arr2[index]][1]; } } int main(){ cin>>n>>m; int x,y; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>arr[i][j]; if(arr[i][j]=='*'){ x=i; y=j; } } } cin>>k; for(int i=1;i<=k;i++){ string str; cin>>str; if(str[0]=='N'){ arr2[i]=0; }else if(str[0]=='W'){ arr2[i]=2; }else if(str[0]=='S'){ arr2[i]=1; }else{ arr2[i]=3; } } arr[x][y]='.'; dfs(x,y,1); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cout<<arr[i][j]; } cout<<endl; } return 0; } ```
by 123huchenghao @ 2024-06-28 17:13:07


|