• COJ1150(食用油)


    题目大意:这题跟HDOJ"非常可乐"那题很像,用状态空间搜索即可。

    View Code
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <queue>
     4 #define N 101
     5 #define MIN(a,b) ((a)<(b)?(a):(b))
     6 using namespace std;
     7 typedef struct node
     8 {
     9   int v[2],t;
    10 }node;
    11 node cur,next;
    12 queue<node> Q;
    13 int a[2],c;
    14 char vis[N][N];
    15 node st_tran(node tmp,int i)
    16 {
    17   int d;
    18   switch(i)
    19   {
    20     case 0: tmp.v[0]=a[0];  break;
    21     case 1: tmp.v[1]=a[1];  break;
    22     case 2: tmp.v[0]=0; break;
    23     case 3: tmp.v[1]=0; break;
    24     case 4: d=MIN(tmp.v[0],a[1]-tmp.v[1]),tmp.v[0]-=d,tmp.v[1]+=d;  break;
    25     case 5: d=MIN(tmp.v[1],a[0]-tmp.v[0]),tmp.v[1]-=d,tmp.v[0]+=d;  break;
    26   }
    27   tmp.t++;
    28   return tmp;
    29 }
    30 void bfs()
    31 {
    32   bool success=false;
    33   int ans;
    34   memset(vis,0,sizeof(vis));
    35   while(!Q.empty()) Q.pop();
    36   cur.v[0]=cur.v[1]=0;
    37   cur.t=0;
    38   vis[0][0]=1;
    39   Q.push(cur);
    40   while(!Q.empty() && !success)
    41   {
    42     cur=Q.front(),Q.pop();
    43     if(cur.v[0]==c || cur.v[1]==c)  success=true,ans=cur.t;
    44     for(int i=0;!success && i<6;i++)
    45     {
    46       next=st_tran(cur,i);
    47       if(vis[next.v[0]][next.v[1]]) continue;
    48       if(next.v[0]==c || next.v[1]==c)  success=true,ans=next.t;
    49       else  vis[next.v[0]][next.v[1]]=1,Q.push(next);
    50     }
    51   }
    52   if(success) printf("%d\n",ans);
    53   else  puts("-1");
    54 }
    55 int main()
    56 {
    57   while(~scanf("%d%d%d",&a[0],&a[1],&c))
    58   {
    59     bfs();
    60   }
    61   return 0;
    62 }
  • 相关阅读:
    py 中反射的基本应用和总结
    py内置函数
    py 中 函数基础
    tomcat用户管理权限(即访问到tomcat时可进行配置管理)
    py 中对接口数据的简单分析处理
    py中的 字典‘排序 ' 方法-lambda
    py 中 dict字典常用操作
    py 中元组tuple 常用操作
    py 中 list列表常用操作
    py中 字符串常用操作
  • 原文地址:https://www.cnblogs.com/algorithms/p/2506653.html
Copyright © 2020-2023  润新知