#include <bits/stdc++.h> using namespace std; int a[100][100],k,n; int ha,hb,la,lb; int dir1[4]= {0,0,1,-1}; int dir2[4]= {1,-1,0,0}; char maze[100][100]; bool flag; void search(int x,int y) { if(x==hb&&y==lb) { flag=true; return; } for(int i=0; i<4; i++) { int sx=x+dir1[i]; int sy=y+dir2[i]; if(sx<0||sx>=n||sy<0||sy>=n) continue; if(a[sx][sy]) continue; if(maze[sx][sy]=='#') continue; a[sx][sy]=1; search(sx,sy); } return; } int main() { cin>>k; for(int i=0; i<k; i++) { cin>>n; for(int j=0; j<n; j++) for(int l=0; l<n; l++) cin>>maze[j][l]; cin>>ha>>la>>hb>>lb; if(maze[ha][la]=='#'||maze[hb][lb]=='#') { cout<<"NO\n"; continue; } a[ha][la]=1; memset(a,0,sizeof(a)); flag=false; search(ha,la); cout<<(flag?"YES":"NO")<<endl; } return 0; }
时间: 2024-03-19 07:42:29 浏览: 12
这段代码实现了一个迷宫问题的解法。
首先,输入一个整数 $k$,表示测试数据的数量。
每组测试数据包括一个整数 $n$ 和一个 $n \times n$ 的字符矩阵,表示迷宫的大小和迷宫的布局。字符矩阵中 "." 表示可以通过的路,"#" 表示障碍物。
接下来的四个整数 $ha$、$la$、$hb$、$lb$ 表示起点和终点的位置。
如果起点或终点是障碍物,输出 "NO"。
否则,从起点开始搜索,用数组 $a$ 记录已经访问过的位置,用递归实现深度优先搜索,一旦到达终点就将标志位 $flag$ 设为 $true$ 并返回。搜索过程中,如果下一个位置已经超出了边界或者已经访问过或者是障碍物,则跳过不再搜索。最后输出结果,如果 $flag$ 为 $true$ 则输出 "YES",否则输出 "NO"。
代码中,$dir1$ 和 $dir2$ 数组分别表示四个方向的行和列偏移量,$search$ 函数实现了深度优先搜索,$main$ 函数实现了输入数据和调用搜索函数的逻辑。
相关问题
#include <bits/stdc++.h> using namespace std;
这个头文件是C++11标准引入的,它包含了所有标准库中的头文件。使用这个头文件可以方便地在一个地方包含所有需要的头文件,而不需要一个一个地包含。这个头文件通常只在竞赛中使用,因为它不是标准C++头文件,不保证在所有编译器中都能正常工作。
以下是一个使用这个头文件的示例,实现输入4个整数a、b、c、d,将它们倒序输出:
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << d << ' ' << c << ' ' << b << ' ' << a << endl;
return 0;
}
```
#include <bits/stdc++.h> using namespace std
这段代码是一个C++的头文件引用和命名空间的使用示例。具体来说,`#include <bits/stdc++.h>`是一个常用的头文件引用方式,它包含了C++标准库中的所有头文件。而`using namespace std`则是为了使用`std`命名空间中的标准库函数和对象,这样就可以直接使用`cout`、`cin`等标准输入输出流对象,而不需要写`std::cout`、`std::cin`。
这种写法虽然方便,但也存在一些问题。首先,包含了所有的标准库头文件可能会导致编译时间变长。其次,使用了`using namespace std`会将整个`std`命名空间中的所有标识符引入当前作用域,可能会导致命名冲突。因此,在实际开发中,建议根据需要只包含需要的头文件,并使用具体的命名空间来避免潜在的问题。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![h](https://img-home.csdnimg.cn/images/20210720083646.png)
![h](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)