ACM - 第6章 数据结构基础(2)

子明
• 阅读 1987

6.4.2 走迷宫

利用bfs寻找最短路。通过对u/m得到x,u%m得到y。u = x * m + y
将方向保存在dx[], dy[]中。

c
int q[maxn*maxn]; void bfs(int x, int y) { int front = 0, rear = 0, d, u; u = x*m + y; vis[x][y] = 1; fa[x][y] = u; dist[x][y] = 0; q[rear++] = u; while(front < rear) { u = q[front++]; x = u/m; y = u %m; for(d = 0; d < 4; d++) { int nx = x + dx[d], ny = y + dy[d]; if (nx >= 0 && nx < n && ny >= 0 && ny <m && maze[nx][ny] && !vis[nx][ny]) { int v= nx * m + ny; q[rear++] = v; vis[nx][ny] = 1; fa[nx][ny] = u; dist[nx][ny] = dist[x][y]+1; last_dir[nx][ny] = d; } } } }

6.4.3 拓扑排序,DAG(Directed Acyclic Graph)有向无环图

c#include <stdio.h>
#include <string.h>

const int maxn = 1000;
const int maxm = 1000;

// 用于判断是否存在环
int c[maxn];
int topo[maxn], t;
int n, m;
int G[maxm][maxm];

bool dfs(int u)
{
    c[u] = -1;
    for(int v= 0; v < n; v++) if(G[u][v])
    {
        if(c[v] < 0) return false;
        else if(!c[v] && !dfs(v)) return false;
    }
    c[u] = 1; topo[--t] = u;
    return true;
}

bool toposort()
{
    t = n;
    memset(c, 0, sizeof(c));
    for(int u = 0; u < n; u++) if(!c[u])
        if(!dfs(u)) return false;
    return true;
}


int main()
{
    int u, v;
    while(~scanf("%d %d", &n, &m))
    {
        memset(G, 0, sizeof(G));
        memset(topo, 0, sizeof(topo));

        for(int i = 0; i < m; i++)
        {
            scanf("%d %d", &u, &v);
            G[u][v] = 1;
        }

        if(toposort())
        {
            for(int i = 0; i < n; i++)
                printf("%d ", topo[i]);
            printf("\n");
        } else 
        {
            printf("sort error\n");
        }
    }
    return 0;
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Stella981 Stella981
3年前
AndroidStudio封装SDK的那些事
<divclass"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,2.55,5z"id"raphael
Wesley13 Wesley13
3年前
VSCode配置FiraCode和更纱黑体字体
!(https://oscimg.oschina.net/oscnet/c7bb62d935ceb01d3b7fe176322e84ae00d.png)Fira Code下载到FiraCode字体的GitHub(https://www.oschina.net/action/GoToLink?urlhttps%
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
mysql timestamp
 select from\_unixtime(m.createdAt, '%Y%m%d %H:%i:%s') from kfrobotaidlog m;select m.customeruid, from\_unixtime(m.createtime, '%Y%m%d %H:%i:%s') as \datetime\, m.kfui
Stella981 Stella981
3年前
Python time模块 返回格式化时间
常用命令  strftimetime.strftime("%Y%m%d%H:%M:%S",formattime)第二个参数为可选参数,不填第二个参数则返回格式化后的当前时间日期201812112:00:00time.strftime('%H:%M:%S')返回当前时间的时分秒time.strftim
Stella981 Stella981
3年前
JVM 字节码指令表
字节码助记符指令含义0x00nop什么都不做0x01aconst\_null将null推送至栈顶0x02iconst\_m1将int型1推送至栈顶0x03iconst\_0将int型0推送至栈顶0x04iconst\_1将int型1推送至栈顶0x05ic
Stella981 Stella981
3年前
Forrester机器学习报告发布,腾讯云跃居第一阵营
  !(https://nimg.ws.126.net/?urlhttp%3A%2F%2Fdingyue.ws.126.net%2F2020%2F1016%2Fecdc1f59j00qi98j7000od200u000fpg00it009u.jpg&thumbnail650x2147483647&quality80&typejpg)  A