题意:黑白图像的路径表示法


代码:(Accepted,0.120s)

//UVa806 - Spatial Structures
//Accepted 0.120s
//#define _XIENAOBAN_
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std; int N, T(0);
bool Img[66][66];
vector<string> Sqns; const unsigned C5_10(const string& five) {//五进制转十进制
unsigned ans(0), i(1);
for (auto p(five.rbegin());p != five.rend();++p, i *= 5)
ans += (*p - 48) * i;
return ans;
} const string C10_5(unsigned ten) {//十进制转五进制(倒序)
string ans;
do ans += char(ten % 5 + 48);
while (ten /= 5);
return ans;
} bool DFS(int x, int y, string f) {
int len = N / (int)pow(2, f.length()) / 2;
if (!len) {
if (Img[x][y]) {
Sqns.push_back(f);
//cerr << "Debug: Sqns(" << x << ", " << y << ") " << f << endl;//Debug
}
return Img[x][y];
}
bool NW(DFS(x, y, '1' + f));
bool NE(DFS(x, y + len, '2' + f));
bool SW(DFS(x + len, y, '3' + f));
bool SE(DFS(x + len, y + len, '4' + f));
bool flag(NW&&NE&&SW&&SE);
if (flag) {
Sqns.pop_back(), Sqns.pop_back(), Sqns.pop_back(), Sqns.pop_back();
Sqns.push_back(f);
}
return flag;
} void Initialize() {
Sqns.clear();
for (int i(0);i < N;++i) for (int j(0);j <= N;++j)
Img[i][j] = false;
} void SolvePlus() {
Initialize(); //Input
char n;
for (int i(0);i < N;++i) for (int j(0);j < N;++j)
cin >> n, Img[i][j] = n - 48; //Solve
DFS(0, 0, ""); //Output
if (Sqns.size()) {
sort(Sqns.begin(), Sqns.end(), [](string& a, string& b)->bool {
if (a.length() != b.length()) return a.length() < b.length();
return a < b;
});
auto p(Sqns.begin());
int cnt(0);
while (p != Sqns.end()) {
if (cnt == 12) cnt = 0, cout << '\n';
if (cnt++) cout << ' ' << C5_10(*p);
else cout << C5_10(*p);
++p;
}
cout << '\n';
}
cout << "Total number of black nodes = " << Sqns.size() << '\n';
} void SolveMinus() {
N = -N;
Initialize(); //Input
unsigned n;
while (cin >> n && n != -1)
Sqns.push_back(C10_5(n)); //Solve
if (Sqns.empty());
else if (Sqns[0] == "0") {
for (int i(0);i < N;++i) for (int j(0);j < N;++j)
Img[i][j] = true;
}
else {
for (const auto& t : Sqns) {
//cerr <<"Debug1: "<< t << endl;//
int x(0), y(0), len(N);
for (const auto& c : t) {
len /= 2;
switch (c)
{
case '1': break;
case '2': y += len;break;
case '3': x += len;break;
case '4': x += len;y += len;break;
default:break;
}
}
//cerr << "Debug2: ("<<x << ", " << y <<") "<<len<< endl;//
for (int i(0);i < len;++i)
for (int j(0);j < len;++j)
Img[x + i][y + j] = true;
}
} //Output
for (int i(0);i < N;++i) {
for (int j(0);j < N;++j)
cout << (Img[i][j] ? '*' : '.');
cout << '\n';
}
} int main()
{
#ifdef _XIENAOBAN_
#define gets(T) gets_s(T, 129)
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif std::ios::sync_with_stdio(false);
while (cin >> N && N) {
if (T) cout << '\n';
cout << "Image " << ++T << '\n';
if (N > 0) SolvePlus();
else SolveMinus();
}
return 0;
}

分析:用的DFS来递归来着,题目倒是不难,直接跟着题意霸王硬上弓就行。但是大神们用10、20ms,我用了120ms。。。刚刚网上看了看好像思路也差不多。啊不管了,其实好久之前写的了,只是忘记发博客里了,我也有点忘了我怎么写的(记得当时写的时候有点昏昏沉沉)。

唉感觉自己越来越懒了,分析也不高兴写了。

[刷题]算法竞赛入门经典(第2版) 6-8/UVa806 - Spatial Structures的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...

  2. [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci

    题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...

  3. [刷题]算法竞赛入门经典(第2版) 5-13/UVa822 - Queue and A

    题意:模拟客服MM,一共有N种话题,每个客服MM支持处理其中的i个(i < N),处理的话题还有优先级.为了简化流程方便出题,设每个话题都是每隔m分钟来咨询一次.现知道每个话题前来咨询的时间.间 ...

  4. [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...

  5. [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation

    题意:模拟Petri网的执行.虽然没听说过Petri网,但是题目描述的很清晰. 代码:(Accepted,0.210s) //UVa804 - Petri Net Simulation //Accep ...

  6. [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile

    题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...

  7. [刷题]算法竞赛入门经典(第2版) 6-1/UVa673 6-2/UVa712 6-3/UVa536

    这三题比较简单,只放代码了. 题目:6-1 UVa673 - Parentheses Balance //UVa673 - Parentheses Balance //Accepted 0.000s ...

  8. [刷题]算法竞赛入门经典(第2版) 5-16/UVa212 - Use of Hospital Facilities

    题意:模拟患者做手术. 其条件为:医院有Nop个手术室.准备手术室要Mop分钟,另有Nre个恢复用的床.准备每张床要Mre分钟,早上Ts点整医院开张,从手术室手术完毕转移到回复床要Mtr分钟.现在医院 ...

  9. [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary

    题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...

  10. [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web

    题意:不难理解,照搬题意的解法. 代码:(Accepted,0.190s) //UVa1597 - Searching the Web //#define _XIENAOBAN_ #include&l ...

随机推荐

  1. spring(一) IOC讲解

    spring基本就两个核心内容,IOC和AOP.把这两个学会了基本上就会用了. --WH 一.什么是IOC? IOC:控制反转,通俗点讲,将对象的创建权交给spring,我们需要new对象,则由spr ...

  2. ubuntu 软件安装配置使用总结(由xmind:Depends:java8-runtime but is not installed引出)

    ubuntu 软件安装总结(由xmind:Depends:java8-runtime but is not installed引出) 不知道抽什么风,这几天PC上又用起了linux操作系统.其实之前断 ...

  3. Linux shell指令运行的原理

    shell是一种命令行解释器 对于一般用户,我们不能直接使用操作系统(kernel).而是通过 kernel的"外壳"程序,也就是所谓的shell,来与kernel沟通.    为 ...

  4. 第5章Zabbix自动化监控

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; t ...

  5. if与switch的性能比较

    前言 之前学习Java时,遇到了个问题,有点纠结.当if与switch都实现相同的功能时,该改采用哪种方法实现?我并不懂得如何准确测量两者之间的性能区别,便在OlineJudge上找条该类型的题,来测 ...

  6. [SinGuLaRiTy] 树形存储结构阶段性测试

    [SinGuLaRiTy-1011] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. G2019级信息奥赛专项训练 题目 程序名 时间 内存 ...

  7. es6编写reactjs事件处理函数绑定this三种方式

    第一种:官方推荐的: class LoginControl extends React.Component { constructor(props) { super(props); this.hand ...

  8. 老李分享:Web Services 组件 2

    WSDL 是一种基于 XML 的语言,它用来对 web service 及其如何访问进行描述. WSDL 表示 web service 描述语言(Web Services Description La ...

  9. [android] 手机卫士黑名单功能(ListView结合SQLite增删改)

    修改界面,在顶部横条上增加一个添加按钮,点击打开一个自定义对话框,输入电话号码和拦截模式保存到数据库 自定义对话框看这篇http://www.cnblogs.com/taoshihan/p/53703 ...

  10. 解析新浪微博表情包的一套js代码

    本文出自本人原创,转载请注明出处 /** * Created by Lemon on 2017/4/6. *//** * return 解析后的值 * analysis 参数 * obj.value: ...