Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int a[20][20][2];
  6. void isl(int i, int j, int k, int n, int m) {
  7.     a[i][j][0]=k;
  8.     a[i][j][1]=1;
  9.     if (i>0&&a[i-1][j][0]!=0&&a[i-1][j][1]==0) isl(i-1,j,k,n,m);
  10.     if (i<n-1&&a[i+1][j][0]!=0&&a[i+1][j][1]==0) isl(i+1,j,k,n,m);
  11.     if (j>0&&a[i][j-1][0]!=0&&a[i][j-1][1]==0) isl(i,j-1,k,n,m);
  12.     if (j<m-1&&a[i][j+1][0]!=0&&a[i][j+1][1]==0) isl(i,j+1,k,n,m);
  13. }
  14. void show (int n, int m) {
  15.     for (int i=0; i<n;++i)
  16.         {
  17.             for (int j=0;j<m;++j)
  18.                 cout<<a[i][j][0];
  19.             cout<<endl;
  20.         }
  21. }
  22. int main () {
  23.     int n,m;
  24.     cout<<"Input the scale of the sea(NxM)"<<endl;
  25.     cin>>n>>m;
  26.     for (int i=0;i<n;++i)
  27.         for (int j=0;j<m;++j)
  28.         {
  29.             cin>>a[i][j][0];
  30.             a[i][j][1]=0;
  31.         }
  32.     int is=0;
  33.     for (int i=0;i<n;++i)
  34.         for (int j=0;j<m;++j)
  35.         {
  36.             if (a[i][j][0]!=0&&a[i][j][1]==0) {
  37.                 is++;
  38.                 isl(i,j,is,n,m);
  39.             }
  40.         }
  41.     show(n,m);
  42.     int* b=new int[is];
  43.     for (int i=0;i<is;++i) b[i]=0;
  44.     for (int i=0;i<n-1;++i)
  45.         for (int j=0;j<m-1;++j) {
  46.             if (a[i][j][0]>0&&a[i+1][j+1][0]>0&&a[i][j+1][0]==0&&a[i+1][j][0]==0&&a[i][j][0]!=a[i+1][j+1][0]) {
  47.                 b[a[i][j][0]-1]++;
  48.                 b[a[i+1][j+1][0]-1]++;
  49.             }
  50.             if (a[i][j][0]==0&&a[i+1][j+1][0]==0&&a[i][j+1][0]>0&&a[i+1][j][0]>0&&a[i+1][j][0]!=a[i][j+1][0]) {
  51.                 b[a[i+1][j][0]-1]++;
  52.                 b[a[i][j+1][0]-1]++;
  53.             }
  54.         }
  55.     bool f=1;
  56.     for (int i=0;i<is;++i) if (b[i]==0) {f=0; break;}
  57.     if (is==0) f=0;
  58.     if (is==1) f=1;
  59.     if (f) cout<<"Yes";
  60.     else cout<<"No";
  61.     return 0;                                                                                                                        
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement