这个没数组的bfs表示看不懂
by hater @ 2019-07-25 21:09:11
我的意思不是用STL看不懂
by hater @ 2019-07-25 21:09:28
这个题要从先从最外面搜,把外面的0填成其他数,之后再搜里面的0
by tythen @ 2019-07-25 21:12:14
@[KING__Arthur](/space/show?uid=29384) 我做这题是先把0全部变2,再把矩形的一圈都搜一遍变0。
by 赫敏·东方延绪 @ 2019-07-25 21:21:05
修改后还不对!!!
看这个怎么错了!!!
```cpp
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
queue< pair<int,int> > q;
int map[31][31],n,ans[31][31];
int bfs(int x,int y)
{
q.push(make_pair(x,y));
ans[x][y]=1;
while(q.empty()!=0)
{
int x=q.front().first;
int y=q.front().second;
q.pop();
ans[x][y]=1;
/* if(map[x+1][y]==0&&x+1>=1&&x+1<=n&&y<=n&&y>=1)
{q.push(make_pair(x+1,y));ans[x+1][y]=1;}
if(map[x-1][y]==0&&x-1>=1&&x-1<=n&&y<=n&&y>=1)
{q.push(make_pair(x-1,y));ans[x-1][y]=1;}
if(map[x][y+1]==0&&x>=1&&x<=n&&y+1<=n&&y+1>=1)
{q.push(make_pair(x,y+1));ans[x][y+1]=1;}
if(map[x][y-1]==0&&x>=1&&x<=n&&y-1<=n&&y-1>=1)
{q.push(make_pair(x,y-1));ans[x][y-1]=1;}
*/
if(map[x-1][y]==0&&ans[x-1][y]!=1)
{q.push(make_pair(x-1,y));ans[x-1][y]=1;}
if(map[x+1][y]==0&&ans[x+1][y]!=1)
{q.push(make_pair(x+1,y));ans[x+1][y]=1;}
if(map[x][y+1]==0&&ans[x][y+1]!=1)
{q.push(make_pair(x,y+1));ans[x][y+1]=1;}
if(map[x][y-1]==0&&ans[x][y-1]!=1)
{q.push(make_pair(x,y-1));ans[x][y-1]=1;}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>map[i][j];
if(map[i][j]==1) ans[i][j]=1;
}
for(int i=1;i<=n;i++) //
for(int j=1;j<=n;j++)
{
if(map[i][j]==0)
{
bfs(i,j);
for(int i=1;i<=n;i++) //
for(int j=1;j<=n;j++)
if(ans[i][j]==0)
{
cout<<2<<" ";
if(j==n) cout<<endl;
}
else
{
cout<<map[i][j]<<" ";
if(j==n) cout<<endl;
}
return 0;
}
}
}
```
by KING__Arthur @ 2019-07-25 21:21:14
还有一种做法就是先把0全部变2,再在矩形外面加一圈2,然后从(0,0)开始搜
by 赫敏·东方延绪 @ 2019-07-25 21:23:27
我看不懂这STL诶
by 赫敏·东方延绪 @ 2019-07-25 21:25:55
我的第一种做法
```cpp
int main()
{
int i,j;
scanf("%d",&n);
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==0)
map[i][j] = 2;//把0全部变2
}
for(i=1; i<n; i++)//搜到是2的就变0
{
if(map[i][1]==2) dfs(i,1);
if(map[1][i]==2) dfs(1,i);
if(map[i][n]==2) dfs(i,n);
if(map[n][i]==2) dfs(n,i);
}
//然后直接输出
return 0;
}
```
第二种
```cpp
int main()
{
int i,j;
scanf("%d",&n);
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==0)//把0全部变2
map[i][j] = 2;
}
for(i=0; i<=n; i++)//在外面加一圈2
map[i][0] = map[0][i] = map[i][n+1] = map[n+1][i] =2;
dfs(0,0);//搜到的都变0
//然后直接输出
return 0;
}
```
希望能帮到你
by 赫敏·东方延绪 @ 2019-07-25 21:35:08
看不懂STL
by 赫敏·东方延绪 @ 2019-07-25 21:35:56