Uva10067 playing with wheels [drawing + Shortest Path]

Source: Internet
Author: User

Question: uva10067 playing with wheels


Question: Give a machine with four cycles. See the figure below. Then give an initial number and a target number, and then a number that cannot appear during the period. You can change the number every minute, ask you the minimum required time.



Analysis: This question can be converted into the Shortest Path of the graph.

Because there are eight States that can be converted to the current state, we can convert each state to a vertex, and then the State is connected to an edge with a length of 1, find the shortest path from the initial state to the target State.

At the beginning, we created a graph for each set of data for 0.9 s, and optimized it for a moment, that is, we restored it after deleting the edge that cannot be reached for each graph creation, in this way, you don't need to create a graph every time. It takes 0.19 s and the time is quite fast. It's just a spfa time complexity O (m ),


AC code:

#include <cstdio>#include <iostream>#include <queue>#include <cstring>#include <algorithm>#include <stack>#include <vector>#include <utility>#include <cmath>using namespace std;const int N = 10005;const int M = 10000;const int inf = 0x3f3f3f3f;struct Node{    int x,len;};vector<Node> v[N];void add_Node(int x,int y,int len){    v[x].push_back((Node){y,len});    v[y].push_back((Node){x,len});}int count(int i,int j,int k,int f){    return ((i+10)%10)*1000+((j+10)%10)*100+((k+10)%10)*10+(f+10)%10;}int dir[10][5]={    {0,0,0,1},{0,0,0,-1},    {0,0,1,0},{0,0,-1,0},    {0,1,0,0},{0,-1,0,0},    {1,0,0,0},{-1,0,0,0}};void build(int i,int j,int k,int f){    int tmp=count(i,j,k,f);    for(int p=0; p<8; p++)    {        int tmp1=count(i+dir[p][0],j+dir[p][1],k+dir[p][2],f+dir[p][3]);        add_Node(tmp,tmp1,1);    }}void isit(){    for(int i=0; i<10; i++)        for(int j=0; j<10; j++)            for(int k=0; k<10; k++)                for(int f=0; f<10; f++)                    build(i,j,k,f);}int dis[N];void spfa(int s)  {    int i;    queue<int> q;    for(i=0; i<N; i++)        dis[i]=inf;    dis[s]=0;    q.push(s);    while(!q.empty())    {        int u=q.front();        q.pop();        for(i=0; i<v[u].size(); i++)        {            Node p=v[u][i];            if(dis[p.x]>dis[u]+p.len)            {                dis[p.x]=dis[u]+p.len;                q.push(p.x);            }        }    }}int dx[N],dy[N],dz[N],dk[N];int main(){    int T;    isit();    scanf("%d",&T);    while(T--)    {        int x,y,z,k;        scanf("%d%d%d%d",&x,&y,&z,&k);        int st=x*1000+y*100+z*10+k;        scanf("%d%d%d%d",&x,&y,&z,&k);        int en=x*1000+y*100+z*10+k;        int cc;        scanf("%d",&cc);        for(int i=0;i<cc;i++)        {            scanf("%d%d%d%d",&dx[i],&dy[i],&dz[i],&dk[i]);            int tmp=count(dx[i],dy[i],dz[i],dk[i]);            v[tmp].clear();        }        spfa(st);        if(dis[en]>=inf)            puts("-1");        else            printf("%d\n",dis[en]);        for(int i=0;i<cc;i++)        {            build(dx[i],dy[i],dz[i],dk[i]);        }    }}


Uva10067 playing with wheels [drawing + Shortest Path]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.