題意
原題是這樣的:
原題呢就是上面這個,我還是來簡單翻譯一下吧,
AOJ 0118 Property Distribution {廣度優(yōu)先搜索}
。<喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPr+0tb3PwsPmtcTNvMHLw7Sjv7TzuMXT0DPW1s28sLi1xLHq1r6jrM/gzay1xL/J0tTGtL3Ttb3Su8bwo6zE49Do0qrV0rP21+6689K7ubLT0Lbgydm/6aGjscjI59XiwO+1xL7NysfT0DEwv+mhozwvcD4NCjxwPjxpbWcgYWx0PQ=="這里寫圖片描述" src="http://www.2cto.com/uploadfile/Collfiles/20151211/20151211082910159.jpg" title="\" />它的輸入是這樣的:
<code class="hljs" mel="">10 10####*****@@#@@@@#*#*@##***@@@*#****#*@**##@*#@@*##*@@@@*@@@#***#@*@##**@@@*@@##@*@*#*@##**@****#@@#@0 0</code>
兩個0表示結(jié)束輸入,輸出塊的個數(shù)即可,上面的輸入對應(yīng)的輸出就是33。
分析
我還是用的這個給代碼定的規(guī)定,方向什么的。
走過的點,全部都賦值為
代碼
<code class="hljs" cpp="">#include<iostream>using namespace std;#define MAX_W 100#define MAX_H 100char room[MAX_W][MAX_H];int W,H;const int direc[4][2] = { {0, -1}, {1, 0}, {0, 1}, {-1, 0},};void dfs(const int& row, const int& col, const char c);int main() { while(cin>>H>>W, W > 0) { int step = 0; int col, row; for (row = 0; row < H; ++row) { for (col = 0; col < W; ++col) { cin >> room[row][col]; } } for (row = 0; row < H; ++ row) { for(col = 0; col < W; ++ col) { if(room[row][col] != '!') { dfs(row, col, room[row][col]); ++ step; } } } cout<<step<<endl; char="" const="" curcol="col" currow="" d="0;" int="" return="" void="">= 0 && curRow < H && curCol >= 0 && curCol < W && room[curRow][curCol] == c) { dfs(curRow, curCol, c); } }}</step<<endl;></iostream></code>