你的任務是寫一個程序,分辨出給定圖像中一共被分爲多少個區域。
2 3 3 0 1 1 1 0 1 0 0 1 0 3 4 1 10 11 12 13 9 8 7 6 2 3 4 5
3 1
2014機考C題 php
#include<iostream> #include<cstdio> #include<algorithm> #include<string> using namespace std; int donser[101][101]; bool lable[101][101]; bool chack(int m,int n) { for(int i=0;i<m;i++) for(int j=0;j<n;j++) if(lable[i][j]==0) return 0; return 1; } void counts(int x,int y,int m,int n,int d) { if(lable[x][y]==1) return; lable[x][y]=1; int color=donser[x][y]; if(x-1>=0&&y-1>=0&&abs(donser[x-1][y-1]-color)<=d) counts(x-1,y-1,m,n,d); if(x-1>=0&&y>=0&&abs(donser[x-1][y]-color)<=d) counts(x-1,y,m,n,d); if(x-1>=0&&y+1<n&&abs(donser[x-1][y+1]-color)<=d) counts(x-1,y+1,m,n,d); if(x>=0&&y-1>=0&&abs(donser[x][y-1]-color)<=d) counts(x,y-1,m,n,d); if(x>=0&&y+1<n&&abs(donser[x][y+1]-color)<=d) counts(x,y+1,m,n,d); if(x+1<m&&y-1>=0&&abs(donser[x+1][y-1]-color)<=d) counts(x+1,y-1,m,n,d); if(x+1<m&&y>=0&&abs(donser[x+1][y]-color)<=d) counts(x+1,y,m,n,d); if(x+1<m&&y+1<n&&abs(donser[x+1][y+1]-color)<=d) counts(x+1,y+1,m,n,d); return; } int main() { int T; cin>>T; while(T--) { int m,n,d,num=0; cin>>m>>n>>d; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { cin>>donser[i][j]; lable[i][j]=0; } } while(!chack(m,n)) { for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(lable[i][j]==0) { num++; counts(i,j,m,n,d); } } } } cout<<num<<endl; } return 0; }