欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語言 >

亞馬遜經(jīng)典面試題實(shí)例詳解

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

亞馬遜面試題:

如下所示的Map中,0代表海水,1代表島嶼,其中每一個(gè)島嶼與其八領(lǐng)域的區(qū)間的小島能相連組成島嶼群。寫代碼,統(tǒng)計(jì)Map中島嶼個(gè)數(shù)。

/* 
Q1. 
Map 
[ 
0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 
0 0 0 0 0 0 1 0 
0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 
] 
*/

實(shí)現(xiàn)代碼:

#include<iostream>
#include<queue>
using namespace std;

typedef struct {
  int i;
  int j;
}position;

void search(int a[][], int n, int i, int j, int cnt) {

  queue<position> qu = new queue<position>();

  position p;
  p.i = i;
  p.j = j;

  qu.push(p);
  a[i][j] = cnt;

  while (!qu.empty()) {
    p = qu.pop();

    for (int ii = p.i - 1; ii <= p.i + 1; ii++) {
      for (int jj = p.j - 1; jj <= p.j + 1; jj++) {
        if (ii >= 0 && ii < n && jj >= 0 && jj < n && a[ii][jj] == 1 && (ii != i || jj != j)) {
          a[ii][jj] = cnt;
          p.i = ii;
          p.j = jj;
          qu.push(p);
        }
      }
    }
  }
}

int count(int a[][], int n) {
  int cnt = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i][j] == 1) {
        cnt++; // 發(fā)現(xiàn)一個(gè)新陸地
        search(a, n, i, j, cnt);
      }
    }
  }
  return cnt;
}


int main() {

  int n;
  cin >> n;

  int a[][] = new int[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }

  int cnt = count(a, n);

  cout << cnt - 1 << endl;


  return 0;
}

如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

上一篇:C經(jīng)典算法之二分查找法

欄    目:C語言

下一篇:c++難以發(fā)現(xiàn)的bug(有趣)

本文標(biāo)題:亞馬遜經(jīng)典面試題實(shí)例詳解

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1120.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有