POJ 1128 拓扑排序 + 深搜
/* (⊙v⊙)嗯 貌似是一个建图 拓扑+深搜的过程。至于为什么要深搜嘛。。一个月前敲得题现在全部推了重敲,于是明白了。因为题意要求如果有多个可能的解的话。
* 就要输出字典序最小的那个。所以可以对26个英文字母从小到大尝试能否排出结果。于是出现了 深搜回溯。先选定入度为0的边框。标记为已用。将所有与它连通的
* 边框入度减一。然后递归搜索下一个。此时开始回溯。当前边框标记为未用,所有与它连通的边框入度加1.
* 建图的过程则是。对每一个字母的边框测量出来。然后对A里的其它字母B。(B 覆盖 A)。有关系 map[A][B] = *1。in[B]++;
*/
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 27
int map[maxn+5][maxn+5]; //建的图
char m[35][35]; //输入
int h, w; // 输入size
int tot; //有多少种字母边框
int in[maxn]; //每种字母对应的入度。
char ans[maxn]; // 存储答案
bool flag;
struct Node // 存储每个字母对应的边框边界
{
int lo, hi;
int le, ri;
}node[maxn+5];
void buildmap()
{
for (int i=0; i<h; ++i) // 测量边界
{
for (int j=0; j<w; ++j)
{
if (m[i][j] == '.')
continue;
int temp = m[i][j] - 'A';
if (in[temp] == -1)
{
in[temp] = 0;
tot++;
}
if (i > node[temp].hi) node[temp].hi = i;
if (i < node[temp].lo) node[temp].lo = i;
if (j < node[temp].le) node[temp].le = j;
if (j > node[temp].ri) node[temp].ri = j;
}
}
for (int k=0; k<maxn; ++k) //建图
{
if (in[k] == -1)
continue;
for (int i=node[k].lo; i<=node[k].hi; ++i)
{
for (int j=node[k].le; j<=node[k].ri; ++j)
{
if (i>node[k].lo && i<node[k].hi && j>node[k].le && j<node[k].ri)
continue;
int temp = m[i][j] - 'A';
if (k != temp && !map[k][temp])
{
map[k][temp] = 1;
in[temp] += 1;
}
}
}
}
}
int hh = 0;
void dfs(int num)
{
if (num == tot)
{
ans[num] = '\0';
cout << ans << endl;
return;
}
//觉得应该会有字典序不同的答案。所以在输出加上判断。然而。wa了。应该是递归没有理解清楚。
// if (num == tot && flag)
// {
// ans[num] = '\0';
// puts(ans);
// flag = false;
// return;
// }
for (int i=0; i<maxn; ++i)
{
if (in[i] == 0)
{
ans[num] = i + 'A';
in[i] = -1;
for (int j=0; j<maxn; j++)
{
if (map[i][j])
in[j] -= 1;
}
dfs(num+1);
in[i] = 0;
for (int j=0; j<maxn; ++j)
{
if (map[i][j])
in[j] += 1;
}
}
}
return;
}
int main()
{
while (scanf("%d%d", &h, &w) == 2)
{
tot = 0;
memset(in, -1, sizeof(in));
memset(map, 0, sizeof(map));
flag = true;
for (int i=0; i<maxn+5; ++i)
{
node[i].lo = 100;
node[i].le = 100;
node[i].hi = -1;
node[i].ri = -1;
}
for (int i=0; i<h; ++i)
{
scanf("%s", m[i]);
}
buildmap();
dfs(0);
}
return 0;
}
POJ 1128 拓扑排序 + 深搜的更多相关文章
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- POJ 3249 拓扑排序+DP
貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...
- poj 3249 拓扑排序 and 动态规划
思路:我们首先来一遍拓扑排序,将点按先后顺序排列于一维数组中,然后扫描一遍数组,将每个点的出边所连接的点进行更新,即可得到最优解. #include<iostream> #include& ...
- poj 2585 拓扑排序
这题主要在于建图.对9个2*2的小块,第i块如果出现了不等于i的数字,那么一定是在i之后被brought的.可以从i到该数字建一条边. 图建好后,进行一次拓扑排序,判段是否存在环.若存在环,那么就是B ...
- Sorting It All Out POJ - 1094 拓扑排序
题意:给N个字母,和M个偏序关系 求一个可确定的全序,可确定是指没有其他的可能例如A>B D>B 那么有ADB DAB两种,这就是不可确定的其中,M个偏序关系可以看做是一个一个按时间给出的 ...
- poj 3984 -- 迷宫问题 深搜
迷宫问题 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
- nyoj 349 (poj 1094) (拓扑排序)
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
- POJ 1128 Frame Stacking 拓扑排序+暴搜
这道题输出特别坑.... 题目的意思也不太好理解.. 就解释一下输出吧.. 它让你 从下往上输出. 如果有多种情况,按照字典序从小往大输出... 就是这个多种情况是怎么产生的呢. 下面给一组样例. 很 ...
- Poj(3687),拓扑排序,
题目链接:http://poj.org/problem?id=3687 题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号 ...
随机推荐
- 20145225 《网络对抗》逆向及Bof基础实践
实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShe ...
- exp9《网络对抗》web安全基础实践201453331魏澍琛
201453331魏澍琛web安全基础实践 一.实验过程 1.webgoat开启 2.Injection Flaws练习 Command Injection 原网页中没有注入的地方,那就用burpsu ...
- 小测(noip2005的两道题) 2017.3.3
过河 题目描述 Description 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把 ...
- libcurl开源库在Win32程序中使用下载文件显示进度条实例
一.配置工程引用libcurl库 #define CURL_STATICLIB #include "curl/curl.h" #ifdef _DEBUG #pragma comme ...
- 1-20 RHEL7的启动原理和服务控制
大纲: RHEL7启动原理 RHEL7服务启动配置 网络概述 发布内网服务器 ############################################################ ...
- HDU 6072 Logical Chain(Kosaraju+bitset)
http://acm.hdu.edu.cn/showproblem.php?pid=6072 题意: 给你$n*n$的矩阵,每次修改k条边,让你计算其中能相互到达的点对有多少. 思路: 其实就是求强连 ...
- java23种设计模式之一: 策略模式
由于最近在研究学习设计模式,我会用自己的理解方式来表述对设计模式的学习和认识,通过最常用.好记的案例来记住和使用设计模式,希望对设计代码方面有所提高和改进. 一.应用背景 在软件开发中常常遇到 ...
- 引用类(RC)
R 中还有一种具有引用语义的类系统,它更像其他面向对象编程语言中的类系统.首先,为了定义一个引用类( reference class , RC ),我们要给 setRefClass( )一个类定义.不 ...
- 日志_测试代码_Qt532
1. int LogFile(QString &_str) { QDateTime datetime = QDateTime::currentDateTime();//获取系统现在的时间 QS ...
- Error updating database. Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'as3'
执行更新时的出错信息 Whitelabel Error Page This application has no explicit mapping for /error, so you are see ...