2 条题解
-
0
#include<bits/stdc++.h> using namespace std; int n,m; int sx,sy,ex,ey; char g[115][115]; int dx[4] = {0,0,1,-1}; int dy[4] = {1,-1,0,0}; //用深搜的方法把 (x,y) 相连的'.'都变成'X' void dfs(int x,int y){ g[x][y]='X'; for(int i=0;i<4;i++) { int nx=x+dx[i]; int ny=y+dy[i]; if(g[nx][ny]=='.') dfs(nx,ny); } } int main() { int k; cin>>k; while(k--) { cin>>n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>g[i][j]; for(int i=0;i<=n+1;i++) g[i][0]=g[i][n+1]=g[0][i]=g[n+1][i]='#'; cin>>sx>>sy>>ex>>ey; sx++,sy++,ex++,ey++; if(g[sx][sy]=='#'||g[ex][ey]=='#') { cout<<"NO\n"; continue; } dfs(sx,sy); if(g[ex][ey]=='X') cout<<"YES\n"; else cout<<"NO\n"; } return 0; }
-
0
#include<bits/stdc++.h> using namespace std; int T,n; char g[105][105]; int sx, sy, ex, ey; bool vis[105][105]; int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; void dfs(int x,int y){ if(vis[ex][ey]){ return; } for(int i=0;i<4;i++){ int nx = x+dx[i]; int ny = y+dy[i]; if(g[nx][ny]!='#'&&!vis[nx][ny]){ vis[nx][ny] = true; dfs(nx,ny); } } } int main() { cin>>T; while(T--){ cin>>n; for(int i=0;i<=n+1;i++) g[i][0]=g[i][n+1]=g[0][i]=g[n+1][i]='#'; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { cin>>g[i][j]; vis[i][j]=false; } cin>>sx>>sy>>ex>>ey; sx++,sy++,ex++,ey++; if(g[sx][sy]=='#'||g[ex][ey]=='#') { cout<<"NO\n"; continue; } vis[sx][sy] = true; dfs(sx,sy); if(vis[ex][ey]) cout<<"YES\n"; else cout<<"NO\n"; } return 0; }
- 1
信息
- ID
- 435
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 7
- 标签
- (无)
- 递交数
- 190
- 已通过
- 51
- 上传者