栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3221 Diamond Puzzle

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3221 Diamond Puzzle

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;struct Edge{    int v, next;}edge[50];struct Node{    int f[7];    int s;}q[1000000], node;int head[10];int ecount = 0;bool vis[1000000];int step[1000000];void addedge(int a, int b){    edge[ecount].v = b;    edge[ecount].next = head[a];    head[a] = ecount++;}int hash(Node &a){    int x = 1;    int ret = 0;    for (int i = 0; i < 7; i++)    {        ret += a.f[i] * x;        x *= 7;    }    return ret;}void bfs(){    int front = 0;    int rear = 1;    memset(vis, 0, sizeof(vis));    for (int i = 0; i < 7; i++)        q[0].f[i] = i;    int h = hash(q[0]);    step[h] = q[0].s = 0;    vis[h] = true;    while (front < rear)    {        int a;        for (int i = 0; i < 7; i++) if (q[front].f[i] == 0) {     a = i;     break; }        for (int i = head[a]; i != -1; i = edge[i].next)        { int b = edge[i].v; swap(q[front].f[a], q[front].f[b]); h = hash(q[front]); if (!vis[h]) {     vis[h] = true;     q[rear] = q[front];     step[h] = q[rear++].s = q[front].s + 1; } swap(q[front].f[a], q[front].f[b]);        }        front++;    }}int main(){    memset(head, -1, sizeof(head));    memset(step, -1, sizeof(step));    addedge(0, 2);    addedge(0, 4);    addedge(0, 6);    addedge(1, 6);    addedge(1, 2);    addedge(2, 1);    addedge(2, 0);    addedge(2, 3);    addedge(3, 2);    addedge(3, 4);    addedge(4, 3);    addedge(4, 0);    addedge(4, 5);    addedge(5, 4);    addedge(5, 6);    addedge(6, 1);    addedge(6, 0);    addedge(6, 5);    bfs();    int t;    scanf("%d", &t);    getchar();    while (t--)    {        for (int i = 0; i < 7; i++)        { char ch = getchar(); node.f[i] = ch - '0';        }        getchar();        printf("%dn", step[hash(node)]);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/370983.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号