1 条题解
-
0
C++ :
#include<iostream> #include<queue> #include<cstdio> #define maxn 701 using namespace std; int n,sum=0; int a[maxn][maxn]; int x[8]={0,0,1,1,1,-1,-1,-1}, y[8]={1,-1,-1,0,1,-1,0,1}; void work(int,int); string s; int main() { cin>>n; for(int i=1;i<=n;i++) { cin>>s; for(int j=0;j<n;j++) a[i][j+1]=s[j]-48; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(a[i][j]==1) { work(i,j); sum++; } } } cout<<sum; return 0; } void work(int i,int j) { queue<int> qi,qj; qi.push(i); qj.push(j); a[i][j]=0; while(!qi.empty()) { int ki=qi.front(),kj=qj.front(); for(int i=0;i<8;i++) { int kx=ki+x[i],ky=kj+y[i]; if(kx>=1&&kx<=n&&ky>=1&&ky<=n) { if(a[kx][ky]==1) { a[kx][ky]=0; qi.push(kx); qj.push(ky); } } } qi.pop(); qj.pop(); } }
Pascal :
program pd; const dx:array[1..8] of integer=(-1,0,0,1,1,1,-1,-1); dy:array[1..8] of integer=(0,1,-1,0,1,-1,-1,1); var n,m,i,j,num:longint; s:char; a:array[0..1000,0..1000]of boolean; b:array[0..1000,0..1000]of longint; d:array[1..500000,1..2]of longint; procedure chuli(x,y:integer); var t,w,p,q,k:longint; begin a[x,y]:=false; d[1,1]:=x; d[1,2]:=y; t:=1; w:=1; repeat for k:= 1 to 8 do begin p:=d[t,1]+dx[k]; q:=d[t,2]+dy[k]; if (p>0) and (p<=n) and (q>0) and (q<=n) and a[p,q] then begin w:=w+1; d[w,1]:=p; d[w,2]:=q; a[p,q]:=false; end; end; t:=t+1; until t>w; num:=num+1; end; begin readln(n); fillchar(a,sizeof(a),true); for i:= 1 to n do begin for j:= 1 to n do begin read(s); b[i,j]:=ord(s)-ord('0'); if b[i,j]=0 then a[i,j]:=false; end; readln; end; for i:= 1 to n do for j:= 1 to n do begin if a[i,j]=true then chuli(i,j); end; writeln(num); end.
- 1
信息
- ID
- 2123
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者