UVA
来源:互联网 发布:加内特职业生涯总数据 编辑:程序博客网 时间:2024/06/01 09:40
紫书数据结构那章的题,今天个人赛开了出来,纸上模拟实现了一遍,因为格式问题 PE 了好几次,中间还有两次 忘记删除 文件测试那两行
写完了 看一下感觉有点丑,但好歹思路还算清晰,贴一哈
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<set>using namespace std;const int maxn = 100 + 7;int n, a[maxn][maxn]; // shuzu ji daxiaoset<int> st, stans;int cnt;char s[1000];void init() { st.clear(); stans.clear(); cnt = 0; memset(a, 0, sizeof a); if(n > 0) { for(int i = 0; i < n; ++i) { scanf("%s", s); for(int j = 0; j < n; ++j) { a[i][j] = s[j] - '0'; } //scanf("%d", &a[i][j]);// >> a[i][j] } } else { //n = -1*n; int x; while(scanf("%d", &x) && x != -1) { st.insert(x); // mingling jihe } }}void dfs(int x, int y, int d, int id) { int num = 0; for(int i = x; i < x+d; ++i) { for(int j = y; j < y+d; ++j) { if(a[i][j] == 1) num++; // heidian geshu } } if(num == d*d) { cnt++; // hei jiedian geshu st.insert(id); } else if(num == 0) { return; } else { dfs(x, y, d/2, id*10+1); // NO. x hang, y lie dfs(x, y+d/2, d/2, id*10+2); dfs(x+d/2, y, d/2, id*10+3); dfs(x+d/2, y+d/2, d/2, id*10+4); }}void dfs2(int x, int y, int d, int id) { if(id == 0) { for(int i = x; i < x+d; ++i) { for(int j = y; j < y+d; ++j) { a[i][j] = 1; } } } else { int t = id % 5; if(t == 1) dfs2(x, y, d/2, id/5); // NO. x hang, y lie if(t == 2) dfs2(x, y+d/2, d/2, id/5); if(t == 3) dfs2(x+d/2, y, d/2, id/5); if(t == 4) dfs2(x+d/2, y+d/2, d/2, id/5); }}void solve() { if(n > 0) { dfs(0, 0, n, 0); if(cnt == 0) { printf("Total number of black nodes = 0\n"); // ans1 } else { set<int>::iterator it = st.begin(); for( ; it != st.end(); ++it) { int t = *it; int sum = 0; while(t) { sum = sum*5 + t%10; t /= 10; } stans.insert(sum); } int tt = 0; for(it = stans.begin(); it != stans.end(); ++it) { if(tt) cout << ' '; cout << *it; tt++; if(tt >= 12) { tt = 0; cout << endl; } } if(tt != 0) cout << endl; printf("Total number of black nodes = %d\n", cnt); // nas2 } } else { n = -1*n; set<int>::iterator it = st.begin(); for( ; it != st.end(); ++it) { int t = *it; if(t == 0) { for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { //if(j) cout << " "; cout << '*' ; } cout << endl; } //cout << endl; return; } else { dfs2(0, 0, n, t); } } for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) if(a[i][j] == 0) cout << '.'; else if(a[i][j] == 1) cout << '*'; cout << endl; } //cout << endl; }}int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int kase = 1; while(scanf("%d", &n) != EOF && n) { init(); if(kase != 1) cout << endl; printf("Image %d\n", kase++); solve(); } return 0;}
阅读全文
1 0
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- 上传图片时,Input file图片预览的实现
- Hadoop HA 高可用集群搭建
- 移动端meta整合
- The First Ryu Application
- 在Ubuntu16.04上安装QQ
- UVA
- 退了实验室之后的生活
- 图片在块级元素中的对齐问题(取消图片在垂直和水平方向上的空隙)
- HDU 6043 KazaQ's Socks【规律】
- Mac下的解压命令
- 使用wsimport生成客户端java类
- 动态顺序表的实现
- 嵌入式Linux驱动笔记(七)------浅析tty与uart框架
- 【算法设计与数据结构】为何程序员喜欢将INF设置为0x3f3f3f3f?