NYOJ 27 水池數目

來源:互聯網
上載者:User
#include<stdio.h>int n;int map[101][101];int row, col;int der[4][2]={{0, 1},{1, 0},{0 , -1},{-1, 0}};//用二維數組記錄四個方向void count(int x, int y){int i;int x1,y1 ;map[x][y] = 0;//可以走到即置零//else//{for(i = 0; i < 4; i ++){x1 = x;//可以不改變當前座標,進入下一個點y1 = y;x1 += der[i][0];//妙。。。。。y1 += der[i][1];if(x1 < 0 || y1 < 0 || x1 == row || y1 == col)continue;else{if(map[x1][y1] == 1)count(x1, y1);}}//}}int main (){int i, j;int N;scanf("%d", &N);getchar();while(N --){scanf("%d%d",&row,&col);getchar();for(i = 0 ; i < row; i ++)for(j = 0 ; j < col; j ++)//這能這樣初始化,不然會第一行 100 列 全部輸入完再走下一行{scanf("%d",&map[i][j]);//初始化地圖getchar();}for(i = 0 ; i < row; i ++)for(j = 0 ; j < col; j ++)//判斷不為0 的點{if(map[i][j] == 1)//一條路徑走完,那一片都被標記為 0,下次不會被找到{n++;count(i, j);}}printf("%d\n",n);n = 0;}return 0;}

思路:以一個點為基準,這個點所能走到的所有的點  構成的一片都當成1個水池 , 即一個點所能走過的路徑就是 一片

      我們只需要記錄我走了一次路 就好

       為了便於尋找點,和判斷點,我們採用 遇到 水池 ,水池數目加一,而走過的路  標記為0 的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.