fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int arr[105][105];
  4. int x,y,ans,n,m;
  5. void dfs(int x,int y){
  6. if(x<=n+1 && y<=m+1 && x>=0 && y>=0 && arr[x][y]!=1){
  7. arr[x][y]=9;
  8. dfs(x-1,y);
  9. dfs(x+1,y);
  10. dfs(x,y-1);
  11. dfs(x,y+1);
  12. }else{
  13. return;
  14. }
  15. }
  16. int main(){
  17. cin>>n>>m;
  18. for(int i=2;i<=n;i++){
  19. for(int j=2;j<=m;j++){
  20. cin>>arr[i][j];
  21. }
  22. }
  23. for(int i=1;i<=n+1;i++){
  24. for(int j=1;j<=m+1;j++){
  25. if(i==1||i==n+1||j==1||j==m+1){
  26. dfs(i,j);
  27. }
  28. }
  29. }
  30. for(int i=2;i<=n;i++){
  31. for(int j=2;j<=m;j++){
  32. if(arr[i][j]==9){
  33. cout<<0<<" ";
  34. }else if(arr[i][j]==0){
  35. cout<<2<<" ";
  36. }else{
  37. cout<<1<<" ";
  38. }
  39. }
  40. cout<<"\n";
  41. }
  42. }
Runtime error #stdin #stdout 1.69s 2096008KB
stdin
6
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
stdout
Standard output is empty