#include <stdio.h> int land[10][10]= { 0,0,0,1,0,0,0,0,0,0, 0,0,1,1,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,1,0,0, 0,0,0,0,0,0,0,1,0,0, 0,0,0,1,1,0,1,1,0,0, 0,0,0,1,0,0,0,0,0,0, 0,0,1,1,0,0,0,0,0,0, 0,0,0,0,0,0,0,1,0,0, 0,0,0,0,0,0,0,0,0,0 }; int cnt[100]={0}; int count; void pr(void); int search(void); int size_land(int x, int y); int main(void) { int result, i; pr(); result=search(); pr(); printf("총 %d개\n\n", result); for(i=0; i<result; i++) { printf("%d번째 땅 크기: %d \n", i+1, cnt[i]); } return 0; } void pr(void) { int i, j; for(i=0; i<10; i++) { for(j=0; j<10; j++) printf("%d ", land[i][j]); printf("\n"); } printf("\n"); } int search(void) { int i, j, c=0; for(i=0; i<10; i++) { for(j=0; j<10; j++) { if(land[i][j]==1) { count=0; size_land(i, j); cnt[c]=count; c++; //return result; } } } return c; } int size_land(int x, int y) { if(x>=0 && y>=0 && x<10 && y<10) { if(land[x][y]==1) { land[x][y]++; count++; size_land(x, y+1); size_land(x, y-1); size_land(x+1, y); size_land(x-1, y); } } else { return 0; } }
2018.03.24 11:34
졸업문제 - 연속된 땅 개수와 땅 크기 찾기
조회 수 186 추천 수 0 댓글 0