1 条题解
-
1
//导入 #include<bits/stdc++.h> using namespace std; //定义 const int N = 35; int a[N][N]; int vis[N][N]; int n; int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; //4方向的移动(上下左右) //定义函数 void dfs(int x, int y){ for(int i = 0; i < 4; i++){ int xx = x + dx[i], yy = y + dy[i]; if(xx < 0 || xx > n + 1 || yy < 0 || yy > n + 1 || a[xx][yy] || vis[xx][yy]) continue; vis[xx][yy] = 1; dfs(xx,yy); } } //主函数 int main(){ //输入 cin >> n; //循环 for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ cin >> a[i][j]; } } vis[0][0] = 1;//标记起点 dfs(0,0);//搜索 //循环 for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ //判断 if(a[i][j]){ cout << 1 << " "; }else if(vis[i][j]){ cout << 0 << ' '; }else{ cout << 2 << ' '; } } cout << endl;//输出 } return 0; }
- 1
信息
- ID
- 356
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 71
- 已通过
- 30
- 上传者