图像常用一棵四分树表示。黑白图像有两种表示法:点阵表示和路径表示。你的任务是在这两种表示方法中转换。
题不难,理解题意按照要求做就行。再注意一些细节就行。
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int dx[4]={0,0,1,1}; const int dy[4]={0,1,0,1}; const int powf[7]={1,5,25,125,625,3125,15625}; int N; char mp [65][65]; vector<int> ans,num; bool check(int x, int y, int len){ for(int i = x; i<x+len; i++) for(int j = y; j<y+len; j++) if(mp[i][j] == '0')return false; return true; } bool checkw(int x, int y, int len){ for(int i = x; i<x+len; i++) for(int j = y; j<y+len; j++) if(mp[i][j] == '1')return false; return true; } void dfs(int x, int y, int len, int pt, int ly){ for(int i = 0; i<4; ++i){ int blen = len/2; int bx = x+dx[i]*blen, by = y+dy[i]*blen; int bpt = (i+1)*powf[ly]+pt; if(check(bx, by, blen)) {ans.push_back(bpt);continue;} if(checkw(bx,by, blen)) continue; dfs(bx, by, blen, bpt, ly+1); } } void pton(){ if(checkw(0, 0, N)) return; if(check(0,0,N)){ans.push_back(0);return;} dfs(0,0, N, 0, 0); } void draw(int x, int y, int len){ for(int i = x; i<x+len; i++) for(int j = y; j<y+len; j++) mp[i][j] = '*'; } void trans(int x){ while(x){ ans.push_back(x%5); x/=5; } } void ntop(){ if(!num.size())return; if(num[0] == 0){draw(0, 0, N); return;} for(int i = 0, k = num.size(); i<k; ++i){ ans.clear(); trans(num[i]); int x = 0, y = 0, len = N; for(int j=0, kj=ans.size(); j<kj; ++j){ len/=2; x = x+dx[ans[j]-1]*len; y = y+dy[ans[j]-1]*len; } draw(x, y, len); } } int main(){ freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); int kase = 0; while(scanf("%d", &N)!=EOF && N){ if(kase) printf("\n"); if(N>0){ memset(mp, 0, sizeof(mp)); ans.clear(); for(int i = 0; i<N; ++i) scanf("%s", mp[i]); pton(); sort(ans.begin(), ans.end()); printf("Image %d", ++kase); if(ans.size()){ for(int i = 0 , k = ans.size(); i<k; i++){ if(i%12 == 0)printf("\n%d", ans[i]); else printf(" %d", ans[i]); } printf("\n"); } else printf("\n"); printf("Total number of black nodes = %d\n", ans.size()); } else{ N = -N; memset(mp, '.', sizeof(mp)); num.clear(); int buf; while(scanf("%d", &buf) && buf != -1) num.push_back(buf); ntop(); printf("Image %d\n", ++kase); for(int i = 0; i<N; i++){ for(int j = 0; j<N; j++) printf("%c", mp[i][j]); printf("\n"); } } } return 0; }