5 solutions
-
5
#include <bits/stdc++.h> //仅供参考 using namespace std; bool jz[105][105] = {false}; char jz1[105][105]; int dfs(int x,int y,int tarx,int tary,int n){ int rt = 0; jz[x][y] = true; if(x == tarx and y == tary){ return 1; } if(x-1>=0 and jz1[x-1][y]!='#' and jz[x-1][y] == false){ rt += dfs(x-1,y,tarx,tary,n); } if(x+1<n and jz1[x+1][y]!='#' and jz[x+1][y] == false){ rt += dfs(x+1,y,tarx,tary,n); } if(y-1>=0 and jz1[x][y-1]!='#' and jz[x][y-1] == false){ rt += dfs(x,y-1,tarx,tary,n); } if(y+1<n and jz1[x][y+1]!='#' and jz[x][y+1] == false){ rt += dfs(x,y+1,tarx,tary,n); } return rt; } int main(){ int k,n,x1,y1,x2,y2; cin >> k; int a[k]; for(int i = 0;i < k;i++){ cin >> n; for(int j = 0;j <= n;j++){ for(int l = 0;l <= n;l++){ if(j < n and l < n){ cin >> jz1[j][l]; }else{ jz1[j][l] = '#'; } jz[j][l] = false; } } cin >> x1 >> y1 >> x2 >> y2; if(jz1[x1][y1] == '#' or jz1[x2][y2] == '#'){ a[i] = -1; continue; } a[i] = dfs(x1,y1,x2,y2,n); } for(int i = 0;i < k;i++){ if(a[i] > 0){ cout << "YES" << endl; }else{ cout << "NO" << endl; } } return 0; }
-
3
A1215. 迷宫 AC 代码(注释中文版)
#include <bits/stdc++.h> using namespace std; const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; int k, n, ha, la, hb, lb, flag; char g[105][105]; bool vis[105][105]; void dfs(int x,int y) { if (x == hb && y == lb) // 递归出口,return时记得将flag变为1(即true的状态) { flag = 1; return; } vis[x][y] = 1; // 将vis[x][y]标记为1,表示来过vis[x][y]了,不然会重复来到同一个点 for (int i = 0; i < 4; i++) { int xx = x + dx[i], yy = y + dy[i]; // 将xx、yy加上dx[]和dy[] if(!flag && xx > 0 && xx <= n && yy > 0 && yy <= n && g[xx][yy] == '.' && !vis[xx][yy]) dfs(xx, yy); // 剪枝和进行深搜 } } int main() { cin >> k; while (k--) { flag = 0; cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> g[i][j]; // 输入g[i][j] vis[i][j] = 0; // 将vis[i][j]标记为0(即false的状态) } } cin >> ha >> la >> hb >> lb; // 输入4个整数ha, la, hb, lb ha++, la++, hb++, lb++; // 先全部加1 if (g[ha][la] == '#' || g[hb][lb] == '#') // 如果是‘#’那就直接输出“NO” { cout << "NO" << endl; continue; } dfs(ha, la); // 进行dfs if(flag) cout<< "YES" << endl; // 当flag为true时,输出“YES” else cout << "NO" << endl; // 当flag为false时,输出“NO” } return 0; // 完结撒花^_^ }
-
0
#include<iostream> #include<cstring> using namespace std; int n,k; int ha,la,hb,lb; bool flag; const int dx[4]={-1,1,0,0}; const int dy[4]={0,0,-1,1}; char s[101][101]; bool check(int a,int b,int ma){ if(a>0&&a<=ma&&b>0&&b<=ma&&s[a][b]!='#') return 1; return 0; } void dfs(int x,int y){ // if(check(x,y,n)) return; // cout<<x<<' '<<y<<endl; s[x][y]='#'; if(x==hb&&y==lb){ cout<<"YES\n"; flag=1; memset(s,'#',sizeof(s)); return; } for(int i=0;i<4;i++){ int nx=x+dx[i]; int ny=y+dy[i]; if(check(nx,ny,n)) dfs(nx,ny); } } int main(){ cin>>k; for(int t=1;t<=k;t++){ cin>>n; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin>>s[i][j]; } } cin>>ha>>la>>hb>>lb; ha++,la++,hb++,lb++; if(s[ha][la]=='#'||s[hb][lb]=='#'){ cout<<"NO"<<endl; continue; } dfs(ha,la); if(!flag){ cout<<"NO"<<endl; } else{ flag=0; } } return 0; }
-
0
#include <iostream> #include <queue> #include <vector> using namespace std; // 定义四个方向:上、下、左、右 const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; // 广度优先搜索函数,判断是否能从起点到达终点 bool bfs(vector<vector<char>>& maze, int startX, int startY, int endX, int endY, int n) { // 用于标记节点是否已访问 vector<vector<bool>> visited(n, vector<bool>(n, false)); // 队列用于 BFS queue<pair<int, int>> q; // 将起点加入队列并标记为已访问 q.push({startX, startY}); visited[startX][startY] = true; while (!q.empty()) { // 取出队首节点 pair<int, int> current = q.front(); q.pop(); int x = current.first; int y = current.second; // 若到达终点,返回 true if (x == endX && y == endY) { return true; } // 遍历四个方向 for (int i = 0; i < 4; ++i) { int newX = x + dx[i]; int newY = y + dy[i]; // 检查新节点是否在迷宫范围内、是否可通行且未访问过 if (newX >= 0 && newX < n && newY >= 0 && newY < n && maze[newX][newY] == '.' &&!visited[newX][newY]) { // 将新节点加入队列并标记为已访问 q.push({newX, newY}); visited[newX][newY] = true; } } } // 队列为空仍未到达终点,返回 false return false; } int main() { int k; cin >> k; while (k--) { int n; cin >> n; // 读取迷宫矩阵 vector<vector<char>> maze(n, vector<char>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> maze[i][j]; } } int ha, la, hb, lb; cin >> ha >> la >> hb >> lb; // 检查起点和终点是否可通行 if (maze[ha][la] == '#' || maze[hb][lb] == '#') { cout << "NO" << endl; } else { // 调用 BFS 函数判断是否能到达 if (bfs(maze, ha, la, hb, lb, n)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } return 0; }
-
-1
#include<iostream> using namespace std; int k,n; int sx,sy,ex,ey; int xl[4]={0,0,1,-1}; int yl[4]={1,-1,0,0}; char g[105][105]; bool f=0,vis[105][105]; void dfs(int x,int y) { if(x==ex && y==ey) { f=1; return; } vis[x][y]=1; for(int i=0;i<4;i++) { int xs=x+xl[i]; int ys=y+yl[i]; if(!f && xs>0 && xs<=n && ys>0 && ys<=n && g[xs][ys]=='.' && !vis[xs][ys]) dfs(xs,ys); } } int main() { cin>>k; while(k--) { f=0; cin>>n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { cin>>g[i][j]; vis[i][j]=0; } cin>>sx>>sy>>ex>>ey; sx++,sy++,ex++,ey++; if(g[sx][sy]=='#' || g[ex][ey]=='#') { cout<<"NO\n"; continue; } dfs(sx,sy); if(f) cout<<"YES\n"; else cout<<"NO\n"; } return 0; }
- 1
Information
- ID
- 701
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 8
- Tags
- # Submissions
- 216
- Accepted
- 37
- Uploaded By