본문 바로가기

C/C++

Cheese




#include 

int N,M,data[100][100],cnt,d;
void input(){
	int i,j;
	FILE *in=fopen("input2.txt","r");
	fscanf(in,"%d %d",&N,&M);
	for (i=1;i<=N;i++){
		for (j=1;j<=M;j++){
			fscanf(in,"%d",&data[i][j]);
		}
	}
}

void back(int x,int y,int pass,int change){
	data[x][y]=change;
	if (data[x+1][y]==pass && x<=N)
		back(x+1,y,pass,change);
	if (data[x][y+1]==pass && y<=M) 
		back(x,y+1,pass,change);
	if (data[x-1][y]==pass && x-1>=0) 
		back(x-1,y,pass,change);
	if (data[x][y-1]==pass && y-1>=0)
		back(x,y-1,pass,change);
}

void process(){
	int i,j,c;
	back(0,0,0,2);
	while(1){
		c=0;
		for (i=1;i<=N;i++){
			for (j=1;j<=M;j++){
				if(data[i][j]==1 && (data[i+1][j]==2 || data[i][j+1]==2 || data[i-1][j]==2 || data[i][j-1]==2)){
					data[i][j]=3;
					c=1;
				}
			}
		}
		if (!c) break;
		d=0;
		for (i=1;i<=N;i++){
			for (j=1;j<=M;j++){
				if (data[i][j]==3){
					d++;
					back(i,j,0,2);
				}
			}
		}
		cnt++;
	}
}

void output(){
	FILE *out=fopen("output.txt","w");
	fprintf(out,"%d\n%d\n",cnt,d);
}

void main(){
	input();
	process();
	output();
}