- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为闭源程序创建一个工具。调试后,我有了要访问的结构的基地址。经过仔细检查,我发现它是一个多链表,其中前 3 个整数是指针,所有其他值都是数据。
我已经成功地从我的分析中重建了这个结构:
struct UnitsInfo
{
//pointers to UnitsInfo structures, null when no structure available at that location
DWORD * a;//0x00
DWORD * b;//0x04
DWORD * c;//0x08
//data
int unkown_1; //0x0C
unsigned int Type; //0x10
unsigned int Amount;//ox14
};
UnitsInfo * Base_Node = ((UnitsInfo*)( (*(DWORD*)( (*(DWORD*)(0x00400000+0x008E98EC)) + 0x38)) + 0x0C);
现在,我对链接结构真的很陌生,更不用说多链接结构了。
我在想的是制作一张 map 并进行暴力破解,直到我获得所有地址。 (可能无限循环)?
但是我知道这不是遍历这个链表的方法。我如何在不知道它是如何连接的情况下有效地遍历这个多链表中的所有节点(并避免我已经拥有的节点)?
编辑:多亏了我终于成功的答案!
这是我的代码,可能对其他人有用?
#define MAX_PLAYERS (6)
#define POINTER(type,addr) (*(type*)(addr))
struct UnitsInfo
{
//pointers to UnitsInfo structures, null when no structure available at that location
DWORD * nodes[3];//0x00
//data
int unkown_1; //0x0C
unsigned int Type; //0x10
unsigned int Amount;//ox14
};
struct pinfo
{
bool in;
enum Controller {Ctrl_UNKNOWN, Ctrl_HUMAN, Ctrl_AI };
enum Nation {N_UNKNOWN, N_ALLIES, N_SOVIET, N_JAPAN };
std::string Name;
Nation Side;
short Team;
Controller Who;
int *Money;
int *Power;
int *Usage;
unsigned int *Color;
bool GotUnits;
DWORD *unit_start_node;
};
std::map<DWORD*,UnitsInfo*> UnitList[MAX_PLAYERS];
void GenerateUnitList(unsigned short slot)
{
std::set<DWORD*> todo;
unsigned int inserted = 1;
while(inserted)
{
inserted = 0;
for(auto it = UnitList[slot].begin(); it != UnitList[slot].end(); ++it)
{
for(short i = 0; i < 3; ++i)
{
if(it->second->nodes[i] != NULL)
{
if(UnitList[slot].find(it->second->nodes[i]) == UnitList[slot].end())
{
todo.insert(it->second->nodes[i]);
++inserted;
}
}
}
}
while(!todo.empty())
{
UnitList[slot][*todo.begin()] = &POINTER(UnitsInfo,*todo.begin());
todo.erase(todo.begin());
}
}
}
pinfo Player[MAX_PLAYERS];
//adding the first node
unsigned int CurrentSlot = (0xD8+(0x4*slot));
if(POINTER(DWORD,POINTER(DWORD,0x00400000+0x008E98EC)+CurrentSlot) != NULL)
{
Player[slot].unit_start_node = &POINTER(DWORD,POINTER(DWORD,POINTER(DWORD,POINTER(DWORD,0x00400000+0x008E98EC)+CurrentSlot) + 0x38) + 0x0C);
}
//adding first unit if available, if yes continue generating list
if(!Player[i].GotUnits)
{
if(POINTER(DWORD,*Player[i].unit_start_node+0x10) != NULL)
{
Player[i].GotUnits = true;
UnitList[i][(Player[i].unit_start_node)] = &POINTER(UnitsInfo,*Player[i].unit_start_node);
}
}
else
{
GenerateUnitList(i);
}
最棒的是,它就像一个魅力,不会滞后 :)
最佳答案
首先想到的是 BFS 和 DFS:
它们都常用于遍历图。链接结构中的循环在大多数情况下都会被检测到,因此如果实现得当,就不会有无限循环的风险。
基本上来说,这两种方法的工作原理都是(至少在概念层面上)标记任何已检查的节点,同时将到目前为止看到的任何未访问的节点保留在某种列表中。
虽然广度优先遍历会将节点保留在队列中以某种 FIFO 方式处理节点,但深度优先遍历会将新节点插入堆栈,从而首先访问新发现的节点。
实现深度优先遍历的最简单方法之一甚至无需实现堆栈,因为通过递归调用自身,它只是(未命中)使用程序的调用堆栈来跟踪仍要访问的位置。
假设你要遍历的图的节点有类似下面的结构:
struct Node {
int visited;
struct Node **edges;
void *some_data;
}
边是指向另一个节点的以 NULL 结尾的链接数组。从您选择的任何节点开始。您基本上只需调用一个可能称为 visit 的函数就可以了:
void visit (struct Node *node) {
struct Node *n;
node->visited = 1;
for (n=node->edges; *n!=NULL; ++n)
if (!(*n)->visited)
visit (*n, depth+1);
}
当然,您仍然需要对每个访问过的节点做一些有用的事情。但是为了访问从您的起始节点可到达的所有节点而无需绕圈运行,这已经是必须完成的一切。
这里试图为您提供一个完整的工作示例。经过一些努力尝试后,我想我想出了一个相对不错的图表,显示了所选择的不同路径。
我试着在下面的评论行中把它画下来:
#include <stdio.h>
struct Node {
int visited;
int id;
struct Node **edges;
};
/* recursively visit this node and all unvisited nodes
reachable from here */
void visit (struct Node *node, int depth) {
struct Node **n;
node->visited = 1;
/* show the node id surrounded with '[' ']' characters
to indicate this node has been visited in the output */
printf ("[%d]\n", node->id);
for (n=node->edges; *n!=NULL; ++n) {
/* indent this line according the depth that has
been reached in recursion and show the id
of the reachable node currently inspected */
printf ("%*s %d ", 5*depth, "", (*n)->id);
if (!(*n)->visited) {
/* indicate the search goes down one recursion level
by drawing a '=>' next to the node id */
printf ("=>");
visit (*n, depth+1);
}
else
printf ("\n");
}
}
int main (int argc, char *argv[]) {
/* This is the graph that will be modeled and traversed
+----+
v \
+ -- 0 -+ \
/ ^ \ |
| / v /
v / 2 -
1- ^ \
\ / v
+---> 3 4
*/
struct Node v[5]; /* These will form the vertices of the graph */
/ * These will be the edges */
struct Node *e[][5] = {
{v+1, v+2, NULL},
{v+0, v+3, NULL},
{v+0, v+4, NULL},
{v+2, NULL},
{ NULL}
};
/* initialize graph */
int i;
for (i=0; i<5; ++i) {
v[i].id = i;
v[i].visited = 0;
v[i].edges=e[i];
}
/* starting with each vertex in turn traverse the graph */
int j;
for (j=0; j<5; ++j) {
visit (v+j, 0);
printf ("---\n");
/* reset the visited flags before the
next round starts */
for (i=0; i<5; ++i)
v[i].visited = 0;
}
return 0;
}
输出将以某种或多或少的神秘方式显示算法在递归过程中来回跳转。
根据起始节点,您会得到不同的结果。例如,从节点 4
开始,不会找到任何其他节点,因为没有从那里引出的路径。
此处输出的起始节点范围从 0
到 4
。被访问的节点将被 [
]
字符包围,那些因为已经被检查而未被访问的节点将被绘制为没有边框。
我希望该示例有助于了解该算法的工作原理和原因。
[0]
1 =>[1]
0
3 =>[3]
2 =>[2]
0
4 =>[4]
2
---
[1]
0 =>[0]
1
2 =>[2]
0
4 =>[4]
3 =>[3]
2
---
[2]
0 =>[0]
1 =>[1]
0
3 =>[3]
2
2
4 =>[4]
---
[3]
2 =>[2]
0 =>[0]
1 =>[1]
0
3
2
4 =>[4]
---
[4]
---
关于c++ - 遍历多链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15703948/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!