【习题 6-8 UVA

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

写两个dfs模拟就好。 注意每12个数字输出一个换行。。

【代码】

/*
  	1.Shoud it use long long ?
  	2.Have you ever test several sample(at least therr) yourself?
  	3.Can you promise that the solution is right? At least,the main ideal
  	4.use the puts("") or putchar() or printf and such things?
  	5.init the used array or any value?
  	6.use error MAX_VALUE?
  	7.use scanf instead of cin/cout?
*/
#include <bits/stdc++.h>
using namespace std;

const int N = 100;

int n;
char s[N][N];
vector <long long> v;

int numberblack(int x1,int y1,int x2,int y2){
	int cnt = 0;
    for (int i = x1;i <= x2;i++)
    	for (int j = y1;j<= y2;j++)
    		if (s[i][j]=='1')
    			cnt++;
    return cnt;
}	

void dfs(int x1,int y1,int x2,int y2,int now,int xishu){
	int temp1 = numberblack(x1,y1,x2,y2);
	if (temp1==0) return;
	int should = (x2-x1+1)*(y2-y1+1);
	if (temp1==should){
	 	v.push_back(now);
	 	return;
	}
	int midx = (x1+x2)/2,midy = (y1+y2)/2;
	dfs(x1,y1,midx,midy,now + 1*xishu,xishu*5);
	dfs(x1,midy+1,midx,y2,now + 2*xishu,xishu*5);
	dfs(midx+1,y1,x2,midy,now + 3*xishu,xishu*5);
	dfs(midx+1,midy+1,x2,y2,now + 4*xishu,xishu*5);
}

void fugai(int x1,int y1,int x2,int y2){
 	for (int i = x1;i <= x2;i++)
 		for (int j = y1;j <= y2;j++)
 			s[i][j] = '*';
}

void dfs2(int x1,int y1,int x2,int y2,int rest){
 	if (rest==0){
		fugai(x1,y1,x2,y2);
		return; 	 	
 	}
 	int ope = rest%5;

 	int midx = (x1+x2)/2,midy = (y1+y2)/2;
	switch (ope){
	 	case 1: dfs2(x1,y1,midx,midy,rest/5);break;
	 	case 2: dfs2(x1,midy+1,midx,y2,rest/5);break;
	 	case 3: dfs2(midx+1,y1,x2,midy,rest/5);break;
	 	case 4: dfs2(midx+1,midy+1,x2,y2,rest/5);break;
	}
}

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("F:\c++source\rush_in.txt", "r", stdin);
	    freopen("F:\c++source\rush_out.txt", "w", stdout);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
	int kase = 0;
	while (cin >>n && n!=0){
		if (kase>0) cout << endl;
		kase++;
		cout <<"Image "<<kase<<endl;
		if (n>0){
			v.clear(); 
			for (int i = 1;i <= n;i++) cin >> (s[i]+1);
			dfs(1,1,n,n,0,1); 	
			sort(v.begin(),v.end());
			for (int i = 0;i < (int) v.size();i++){
			 	cout << v[i];
			 	if ((i+1)%12==0 || i==(int) v.size()-1) cout << endl;else cout <<' ';
			}
			cout <<"Total number of black nodes = "<<(int) v.size()<<endl;
		}else{
			n = -n;
		 	for (int i = 1;i <= n;i++)
		 		for (int j =1 ;j <= n;j++) s[i][j] = '.';
		 	int x;
		 	for (cin >>x;x!=-1;cin>>x){
		 	 	dfs2(1,1,n,n,x);
		 	}

		 	for (int i = 1;i <= n;i++){
		 		for (int j =1 ;j <= n;j++) cout <<s[i][j];
		 		cout << endl;
			}
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7894749.html