|
沙发
楼主 |
发表于 2021-5-17 16:36:57
|
只看该作者
和1451几乎一样,改动8个方向就是。一次AC。
- #include<iostream>
- using namespace std;
- const int mm=110;
- int m,n,x;
- int a[mm][mm]; ///原始棋盘
- int v[mm][mm]; ///是否被访问
- int dr[]= {0,+1,+1,+0,-1,-1,-1,+0,+1};
- int dc[]= {0,+0,+1,+1,+1,+0,-1,-1,-1};
- void check() ///检查a数组和v数组
- {
- for (int r=1; r<=m; r++,cout<<endl)
- for (int c=1; c<=n; c++) cout<<a[r][c];
- for (int r=1; r<=m; r++,cout<<endl)
- for (int c=1; c<=n; c++) cout<<v[r][c];
- }
- void dfs(int r ,int c,int x) ///从rc处开始给区域染色为x
- {
- a[r][c]=x,v[r][c]=1;
- ///cout<<r<<' '<<c<<endl;check();int y;cin>>y;
- for (int i=1; i<=8; i++)
- {
- int tr=r+dr[i],tc=c+dc[i];
- if (a[tr][tc]!=0 && v[tr][tc]==0)dfs(tr,tc,x);
- }
- }
- int main()
- {
- cin>>m>>n;
- for (int r=1; r<=m; r++)
- for (int c=1; c<=n; c++)
- {
- char ch;cin>>ch;
- a[r][c]=(ch=='W');
- }
- for (int r=1; r<=m; r++)
- for (int c=1; c<=n; c++)
- if (a[r][c]!=0 && v[r][c]==0) dfs(r,c,++x);
- //check();
- cout<<x;
- return 0;
- }
- /*
- 10 12
- W........WW.
- .WWW.....WWW
- ....WW...WW.
- .........WW.
- .........W..
- ..W......W..
- .W.W.....WW.
- W.W.W.....W.
- .W.W......W.
- ..W.......W.
- */
复制代码 |
|