AOJ 0118: Property Distribution (简单DFS)
生活随笔
收集整理的這篇文章主要介紹了
AOJ 0118: Property Distribution (简单DFS)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0118
題意:給定一個矩陣,同類字符相連的為一個塊,問總共有幾個塊。
輸入:h,w(行和列)0 <= h <= 100,0 <= w <= 100
矩陣
輸入包含多組用例,以0,0結束。
輸出:塊數。
代碼:
#include <iostream> using namespace std; typedef long long ll; #define INF 2147483647int w,h; char a[102][102]; int dir[4][2] = {-1,0,1,0,0,-1,0,1}; int ans = 0;void dfs(int x,int y,char s){if(x < 0 || x >= h || y < 0 || y >= w || a[x][y] != s) return;char t = a[x][y];a[x][y] = 'o';for(int i = 0;i < 4; i++){dfs(x+dir[i][0], y+dir[i][1], t);} }int main(){while(cin >> h >> w){if(w == 0 && h == 0) break;ans = 0;int sx,sy;for(int i = 0;i < h; i++){for(int j = 0;j < w; j++){cin >> a[i][j];}}for(int i = 0;i < h; i++){for(int j = 0;j < w; j++){if(a[i][j] != 'o'){ans++;dfs(i,j,a[i][j]);}}}cout << ans << endl;}return 0; } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的AOJ 0118: Property Distribution (简单DFS)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ 1979 Red and Bla
- 下一篇: POJ 1064 Cable maste