Red and Black POJ - 1979

    xiaoxiao2021-03-25  53

    普通dfs,求从起点开始,'.'一共连通的有多少个,行走范围上下左右

    把从起点开始走过的变为'#'

    #include <cstdio> #include <algorithm> using namespace std; int w,h,sx,sy,sum; char mp[25][25]; void dfs(int x,int y) { if (x<0 || y<0 || x>=h || y>=w) return; if (mp[x][y]=='#') return; sum++; mp[x][y]='#'; dfs(x-1,y); dfs(x+1,y); dfs(x,y-1); dfs(x,y+1); return; } int main() { while (~scanf ("%d%d",&w,&h) && (w || h)) { for (int i=0;i<h;i++) { scanf ("%s",mp[i]); for (int j=0;j<w;j++) if (mp[i][j]=='@') { sx=i;sy=j; } } sum=0; dfs(sx,sy); printf ("%d\n",sum); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-26095.html

    最新回复(0)